mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-05-19 18:58:03 +02:00
feat: cli migrate to cobra with subcommands (#550)
* feat: migrate CLI to cobra with dump/list/version subcommands (#546) * fix: remove residual duckduckgo references and add README/LICENSE to release archives * fix: address PR review feedback from Copilot
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/moond4rk/hackbrowserdata/browser"
|
||||
"github.com/moond4rk/hackbrowserdata/log"
|
||||
"github.com/moond4rk/hackbrowserdata/output"
|
||||
"github.com/moond4rk/hackbrowserdata/types"
|
||||
"github.com/moond4rk/hackbrowserdata/utils/fileutil"
|
||||
)
|
||||
|
||||
func dumpCmd() *cobra.Command {
|
||||
var (
|
||||
browserName string
|
||||
category string
|
||||
outputFormat string
|
||||
outputDir string
|
||||
profilePath string
|
||||
keychainPw string
|
||||
compress bool
|
||||
)
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "dump",
|
||||
Short: "Extract and decrypt browser data (default command)",
|
||||
Example: ` hack-browser-data dump
|
||||
hack-browser-data dump -b chrome -c password,cookie
|
||||
hack-browser-data dump -b chrome -f json -d output
|
||||
hack-browser-data dump -f cookie-editor
|
||||
hack-browser-data dump --zip`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
browsers, err := browser.PickBrowsers(browser.PickOptions{
|
||||
Name: browserName,
|
||||
ProfilePath: profilePath,
|
||||
KeychainPassword: keychainPw,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(browsers) == 0 {
|
||||
log.Warnf("no browsers found")
|
||||
return nil
|
||||
}
|
||||
|
||||
categories, err := parseCategories(category)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w, err := output.NewWriter(outputDir, outputFormat)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, b := range browsers {
|
||||
data, extractErr := b.Extract(categories)
|
||||
if extractErr != nil {
|
||||
log.Errorf("extract %s/%s: %v", b.BrowserName(), b.ProfileName(), extractErr)
|
||||
}
|
||||
w.Add(b.BrowserName(), b.ProfileName(), data)
|
||||
}
|
||||
|
||||
if err := w.Write(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if compress {
|
||||
if err := fileutil.CompressDir(outputDir); err != nil {
|
||||
return fmt.Errorf("compress: %w", err)
|
||||
}
|
||||
log.Warnf("compressed: %s/%s.zip", outputDir, filepath.Base(outputDir))
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().StringVarP(&browserName, "browser", "b", "all", "target browser: all|"+browser.Names())
|
||||
cmd.Flags().StringVarP(&category, "category", "c", "all", "data categories (comma-separated): all|"+categoryNames())
|
||||
cmd.Flags().StringVarP(&outputFormat, "format", "f", "csv", "output format: csv|json|cookie-editor")
|
||||
cmd.Flags().StringVarP(&outputDir, "dir", "d", "results", "output directory")
|
||||
cmd.Flags().StringVarP(&profilePath, "profile-path", "p", "", "custom profile dir path, get with chrome://version")
|
||||
cmd.Flags().StringVar(&keychainPw, "keychain-pw", "", "macOS keychain password")
|
||||
cmd.Flags().BoolVar(&compress, "zip", false, "compress output to zip")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
// parseCategories converts a comma-separated string into a Category slice.
|
||||
// "all" returns all categories.
|
||||
func parseCategories(s string) ([]types.Category, error) {
|
||||
s = strings.TrimSpace(s)
|
||||
if strings.EqualFold(s, "all") {
|
||||
return types.AllCategories, nil
|
||||
}
|
||||
|
||||
categoryMap := make(map[string]types.Category, len(types.AllCategories))
|
||||
for _, c := range types.AllCategories {
|
||||
categoryMap[c.String()] = c
|
||||
}
|
||||
|
||||
var categories []types.Category
|
||||
for _, name := range strings.Split(s, ",") {
|
||||
name = strings.TrimSpace(strings.ToLower(name))
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
c, ok := categoryMap[name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown category: %q, available: all|%s", name, categoryNames())
|
||||
}
|
||||
categories = append(categories, c)
|
||||
}
|
||||
if len(categories) == 0 {
|
||||
return nil, fmt.Errorf("no categories specified")
|
||||
}
|
||||
return categories, nil
|
||||
}
|
||||
|
||||
func categoryNames() string {
|
||||
names := make([]string, len(types.AllCategories))
|
||||
for i, c := range types.AllCategories {
|
||||
names[i] = c.String()
|
||||
}
|
||||
return strings.Join(names, ",")
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/moond4rk/hackbrowserdata/browser"
|
||||
"github.com/moond4rk/hackbrowserdata/types"
|
||||
)
|
||||
|
||||
func listCmd() *cobra.Command {
|
||||
var detail bool
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List detected browsers and profiles",
|
||||
Example: ` hack-browser-data list
|
||||
hack-browser-data list --detail`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
browsers, err := browser.PickBrowsers(browser.PickOptions{Name: "all"})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(browsers) == 0 {
|
||||
cmd.Println("No browsers found.")
|
||||
return nil
|
||||
}
|
||||
if detail {
|
||||
return printDetail(cmd.OutOrStdout(), browsers)
|
||||
}
|
||||
return printBasic(cmd.OutOrStdout(), browsers)
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().BoolVar(&detail, "detail", false, "show per-category entry counts")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func printBasic(out io.Writer, browsers []browser.Browser) error {
|
||||
w := tabwriter.NewWriter(out, 0, 0, 3, ' ', 0)
|
||||
fmt.Fprintln(w, "Browser\tProfile\tPath")
|
||||
for _, b := range browsers {
|
||||
fmt.Fprintf(w, "%s\t%s\t%s\n", b.BrowserName(), b.ProfileName(), b.ProfileDir())
|
||||
}
|
||||
return w.Flush()
|
||||
}
|
||||
|
||||
func printDetail(out io.Writer, browsers []browser.Browser) error {
|
||||
// Build header: Browser Profile Password Cookie ...
|
||||
w := tabwriter.NewWriter(out, 0, 0, 3, ' ', 0)
|
||||
fmt.Fprint(w, "Browser\tProfile")
|
||||
for _, c := range types.AllCategories {
|
||||
fmt.Fprintf(w, "\t%s", c.String())
|
||||
}
|
||||
fmt.Fprintln(w)
|
||||
|
||||
for _, b := range browsers {
|
||||
data, _ := b.Extract(types.AllCategories)
|
||||
fmt.Fprintf(w, "%s\t%s", b.BrowserName(), b.ProfileName())
|
||||
for _, c := range types.AllCategories {
|
||||
fmt.Fprintf(w, "\t%d", countEntries(data, c))
|
||||
}
|
||||
fmt.Fprintln(w)
|
||||
}
|
||||
return w.Flush()
|
||||
}
|
||||
|
||||
func countEntries(data *types.BrowserData, c types.Category) int {
|
||||
if data == nil {
|
||||
return 0
|
||||
}
|
||||
switch c {
|
||||
case types.Password:
|
||||
return len(data.Passwords)
|
||||
case types.Cookie:
|
||||
return len(data.Cookies)
|
||||
case types.Bookmark:
|
||||
return len(data.Bookmarks)
|
||||
case types.History:
|
||||
return len(data.Histories)
|
||||
case types.Download:
|
||||
return len(data.Downloads)
|
||||
case types.CreditCard:
|
||||
return len(data.CreditCards)
|
||||
case types.Extension:
|
||||
return len(data.Extensions)
|
||||
case types.LocalStorage:
|
||||
return len(data.LocalStorage)
|
||||
case types.SessionStorage:
|
||||
return len(data.SessionStorage)
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
@@ -3,95 +3,53 @@ package main
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
|
||||
"github.com/moond4rk/hackbrowserdata/browser"
|
||||
"github.com/moond4rk/hackbrowserdata/log"
|
||||
"github.com/moond4rk/hackbrowserdata/output"
|
||||
"github.com/moond4rk/hackbrowserdata/types"
|
||||
"github.com/moond4rk/hackbrowserdata/utils/fileutil"
|
||||
)
|
||||
|
||||
var (
|
||||
browserName string
|
||||
outputDir string
|
||||
outputFormat string
|
||||
verbose bool
|
||||
compress bool
|
||||
profilePath string
|
||||
isFullExport bool
|
||||
)
|
||||
var verbose bool
|
||||
|
||||
func main() {
|
||||
Execute()
|
||||
}
|
||||
func rootCmd() *cobra.Command {
|
||||
root := &cobra.Command{
|
||||
Use: "hack-browser-data",
|
||||
Short: "A CLI tool for decrypting and exporting browser data",
|
||||
Long: `hack-browser-data decrypts and exports browser data from Chromium-based
|
||||
browsers and Firefox on Windows, macOS, and Linux.
|
||||
|
||||
func Execute() {
|
||||
app := &cli.App{
|
||||
Name: "hack-browser-data",
|
||||
Usage: "Export passwords|bookmarks|cookies|history|credit cards|download history|localStorage|extensions from browser",
|
||||
UsageText: "[hack-browser-data -b chrome -f json --dir results --zip]\nExport all browsing data (passwords/cookies/history/bookmarks) from browser\nGithub Link: https://github.com/moonD4rk/HackBrowserData",
|
||||
Version: "0.5.0",
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{Name: "verbose", Aliases: []string{"vv"}, Destination: &verbose, Value: false, Usage: "verbose"},
|
||||
&cli.BoolFlag{Name: "compress", Aliases: []string{"zip"}, Destination: &compress, Value: false, Usage: "compress result to zip"},
|
||||
&cli.StringFlag{Name: "browser", Aliases: []string{"b"}, Destination: &browserName, Value: "all", Usage: "available browsers: all|" + browser.Names()},
|
||||
&cli.StringFlag{Name: "results-dir", Aliases: []string{"dir"}, Destination: &outputDir, Value: "results", Usage: "export dir"},
|
||||
&cli.StringFlag{Name: "format", Aliases: []string{"f"}, Destination: &outputFormat, Value: "csv", Usage: "output format: csv|json"},
|
||||
&cli.StringFlag{Name: "profile-path", Aliases: []string{"p"}, Destination: &profilePath, Value: "", Usage: "custom profile dir path, get with chrome://version"},
|
||||
&cli.BoolFlag{Name: "full-export", Aliases: []string{"full"}, Destination: &isFullExport, Value: true, Usage: "is export full browsing data"},
|
||||
},
|
||||
HideHelpCommand: true,
|
||||
Action: func(c *cli.Context) error {
|
||||
GitHub: https://github.com/moonD4rk/HackBrowserData`,
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
if verbose {
|
||||
log.SetVerbose()
|
||||
}
|
||||
|
||||
browsers, err := browser.PickBrowsers(browserName, profilePath)
|
||||
if err != nil {
|
||||
log.Errorf("pick browsers: %v", err)
|
||||
return err
|
||||
}
|
||||
if len(browsers) == 0 {
|
||||
log.Warnf("no browsers found")
|
||||
return nil
|
||||
}
|
||||
|
||||
categories := types.AllCategories
|
||||
if !isFullExport {
|
||||
categories = types.NonSensitiveCategories()
|
||||
}
|
||||
|
||||
w, err := output.NewWriter(outputDir, outputFormat)
|
||||
if err != nil {
|
||||
log.Errorf("create output writer: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
for _, b := range browsers {
|
||||
data, err := b.Extract(categories)
|
||||
if err != nil {
|
||||
log.Errorf("extract %s/%s: %v", b.BrowserName(), b.ProfileName(), err)
|
||||
}
|
||||
w.Add(b.BrowserName(), b.ProfileName(), data)
|
||||
}
|
||||
|
||||
if err := w.Write(); err != nil {
|
||||
log.Errorf("write output: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if compress {
|
||||
if err = fileutil.CompressDir(outputDir); err != nil {
|
||||
log.Errorf("compress error %v", err)
|
||||
}
|
||||
log.Debug("compress success")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
err := app.Run(os.Args)
|
||||
if err != nil {
|
||||
log.Fatalf("run app error %v", err)
|
||||
|
||||
root.CompletionOptions.HiddenDefaultCmd = true
|
||||
|
||||
root.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "enable debug logging")
|
||||
|
||||
dump := dumpCmd()
|
||||
root.AddCommand(dump, listCmd(), versionCmd())
|
||||
|
||||
// Default to dump when no subcommand is given.
|
||||
// Copy dump flags to root so that `hack-browser-data -b chrome`
|
||||
// works the same as `hack-browser-data dump -b chrome`.
|
||||
root.RunE = func(cmd *cobra.Command, args []string) error {
|
||||
return dump.RunE(dump, args)
|
||||
}
|
||||
dump.Flags().VisitAll(func(f *pflag.Flag) {
|
||||
if root.Flags().Lookup(f.Name) == nil {
|
||||
root.Flags().AddFlag(f)
|
||||
}
|
||||
})
|
||||
|
||||
return root
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := rootCmd().Execute(); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
version = "dev"
|
||||
commit = "none"
|
||||
buildDate = "unknown"
|
||||
)
|
||||
|
||||
func versionCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Print version information",
|
||||
Run: func(cmd *cobra.Command, _ []string) {
|
||||
resolveVersionFromBuildInfo()
|
||||
fmt.Fprintf(cmd.OutOrStdout(), "hack-browser-data %s\n commit: %s\n built: %s\n",
|
||||
version, commit, buildDate)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func resolveVersionFromBuildInfo() {
|
||||
if version != "dev" {
|
||||
return
|
||||
}
|
||||
info, ok := debug.ReadBuildInfo()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if info.Main.Version != "" && info.Main.Version != "(devel)" {
|
||||
version = info.Main.Version
|
||||
}
|
||||
for _, s := range info.Settings {
|
||||
switch s.Key {
|
||||
case "vcs.revision":
|
||||
if len(s.Value) > 8 {
|
||||
commit = s.Value[:8]
|
||||
} else if s.Value != "" {
|
||||
commit = s.Value
|
||||
}
|
||||
case "vcs.time":
|
||||
if s.Value != "" {
|
||||
buildDate = s.Value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user