Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions cmd/hack-browser-data/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

import (
"bytes"
"path/filepath"
"strings"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/moond4rk/hackbrowserdata/internal/golden"
"github.com/moond4rk/hackbrowserdata/internal/masterkey"
)

// browsersPlaceholder stands for this OS's browser names, so one golden serves every OS.
const browsersPlaceholder = "<browsers>"

func TestHelpGolden(t *testing.T) {
var screens strings.Builder
for _, args := range [][]string{
{"-h"}, {"dump", "-h"}, {"dumpkeys", "-h"}, {"archive", "-h"}, {"restore", "-h"}, {"list", "-h"}, {"version", "-h"},
} {
_, screen, err := runRoot(args...)
require.NoError(t, err, args)
screens.WriteString(commandLine(args) + screen)
}
golden.Assert(t, "help.golden", screens.String())
}

// TestUsageErrorsGolden puts a marker where a run printed its usage: help.golden pins the wording, this pins when it shows.
func TestUsageErrorsGolden(t *testing.T) {
setHome(t, t.TempDir())
var screens strings.Builder
for _, args := range [][]string{
{"-b", "nosuch"},
{"dump", "-b", "nosuch"},
{"dump", "-c", "nosuch"},
{"dump", "-f", "xml"},
{"dump", "--max-storage-value", "-1"},
{"dump", "--keychain-pw", "secret", "--keychain-pw-stdin"},
{"dumpkeys", "-b", "nosuch"},
{"archive", "-c", "nosuch"},
{"restore"},
{"restore", "--keys", "keys.json"},
{"restore", "--keys", "keys.json", "--data-dir", "data", "--data-zip", "data.zip"},
{"list", "--nosuch"},
{"nosuch"},
} {
root, screen, err := runRoot(args...)
require.Error(t, err, args)
for _, command := range sortedCommands(root) {
usage := strings.ReplaceAll(command.UsageString(), browserNames(), browsersPlaceholder)
screen = strings.Replace(screen, usage, "<usage: "+command.CommandPath()+">\n", 1)
}
screens.WriteString(commandLine(args) + screen)
}
golden.Assert(t, "errors.golden", screens.String())
}

func TestNothingFound(t *testing.T) {
setHome(t, t.TempDir())

t.Run("dump creates no output dir", func(t *testing.T) {
outputDir := filepath.Join(t.TempDir(), "results")
_, _, err := runRoot("dump", "-d", outputDir)
require.NoError(t, err)
assert.NoDirExists(t, outputDir)
})

t.Run("archive creates no archive", func(t *testing.T) {
archivePath := filepath.Join(t.TempDir(), "browser-data.zip")
_, _, err := runRoot("archive", "-o", archivePath)
require.NoError(t, err)
assert.NoFileExists(t, archivePath)
})

t.Run("list says so", func(t *testing.T) {
_, screen, err := runRoot("list")
require.NoError(t, err)
assert.Equal(t, "No browsers found.\n", screen)
})

t.Run("dumpkeys prints a key file without vaults", func(t *testing.T) {
_, screen, err := runRoot("dumpkeys")
require.NoError(t, err)
keyFile, err := masterkey.ReadKeyFile(strings.NewReader(screen))
require.NoError(t, err)
assert.Empty(t, keyFile.Vaults)
})
}

// runRoot returns the root it ran, for its commands' usage texts, and everything the run printed.
func runRoot(args ...string) (*cobra.Command, string, error) {
var screen bytes.Buffer
root := newRootCommand()
root.SetOut(&screen)
root.SetErr(&screen)
root.SetArgs(args)
err := root.Execute()
return root, strings.ReplaceAll(screen.String(), browserNames(), browsersPlaceholder), err
}

func commandLine(args []string) string {
return "$ hack-browser-data " + strings.Join(args, " ") + "\n"
}
4 changes: 2 additions & 2 deletions cmd/hack-browser-data/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func runDump(ctx context.Context, stderr io.Writer, flags app.DumpFlags, keychai
return nil
}
summary, err := app.Execute(ctx, discovery, options.Categories, options.Output, options.Keychain, options.FirefoxPassword)
announceExport(stderr, summary.Exported)
printExported(stderr, summary.Exported)
if ctx.Err() == nil {
report(stderr, summary)
printSummary(stderr, summary)
}
return err
}
2 changes: 1 addition & 1 deletion cmd/hack-browser-data/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ func userDataDirOf(t *testing.T, home, browserName string) string {
t.Helper()
namedBrowser, ok := browser.Lookup(browserName)
require.True(t, ok, browserName)
return filepath.Join(home, filepath.FromSlash(namedBrowser.Platform().UserDataDirs[0]))
return filepath.Join(home, filepath.FromSlash(namedBrowser.Platform().UserDataDirPaths[0]))
}
10 changes: 5 additions & 5 deletions cmd/hack-browser-data/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/moond4rk/hackbrowserdata/internal/log"
)

// report prints the run's summary; it is not part of the output, so failing to print it never fails the run.
func report(stderr io.Writer, summary app.Summary) {
// printSummary is not part of the output, so failing to print it never fails the run.
func printSummary(stderr io.Writer, summary app.Summary) {
table := tabwriter.NewWriter(stderr, 0, 0, 2, ' ', 0)
fmt.Fprintln(table, "Browser\tProfile\tCategory\tResult\tRecords\tDetail")
for _, failure := range summary.DirFailures {
Expand Down Expand Up @@ -45,14 +45,14 @@ func warnIfNothingFound(stderr io.Writer, discovery app.Discovery, message strin
return false
}
if len(discovery.DirFailures) > 0 {
report(stderr, app.Summary{DirFailures: discovery.DirFailures})
printSummary(stderr, app.Summary{DirFailures: discovery.DirFailures})
}
log.Warnf("%s", message)
return true
}

// announceExport lists the files a run wrote, even when it stopped early, so the user finds what it left.
func announceExport(stderr io.Writer, exported export.Listing) {
// printExported lists the files a run wrote, even when it stopped early, so the user finds what it left.
func printExported(stderr io.Writer, exported export.Listing) {
if len(exported.Files) == 0 {
return
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/hack-browser-data/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
"github.com/moond4rk/hackbrowserdata/internal/category"
)

func TestReport(t *testing.T) {
func TestPrintSummary(t *testing.T) {
cookie := app.Extraction{UserDataDirID: "chrome", ProfileName: "Default", Category: category.Cookie}
var buffer bytes.Buffer
report(&buffer, app.Summary{
printSummary(&buffer, app.Summary{
Results: []app.Result{
{Extraction: cookie, Outcome: app.OutcomePartial, Records: 224, Failures: 4},
{
Expand Down
4 changes: 2 additions & 2 deletions cmd/hack-browser-data/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ func runRestore(ctx context.Context, stderr io.Writer, flags app.RestoreFlags) e
return nil
}
summary, err := app.ExecuteWithKeys(ctx, discovery, options.Categories, options.Output, keys)
announceExport(stderr, summary.Exported)
printExported(stderr, summary.Exported)
if ctx.Err() == nil {
report(stderr, summary)
printSummary(stderr, summary)
}
return err
}
Expand Down
51 changes: 51 additions & 0 deletions cmd/hack-browser-data/testdata/errors.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
$ hack-browser-data -b nosuch
Error: unknown browser "nosuch", available: all|<browsers>
<usage: hack-browser-data>

$ hack-browser-data dump -b nosuch
Error: unknown browser "nosuch", available: all|<browsers>
<usage: hack-browser-data dump>

$ hack-browser-data dump -c nosuch
Error: unknown category: "nosuch", available: all|password,cookie,bookmark,history,download,creditcard,extension,localstorage,sessionstorage
<usage: hack-browser-data dump>

$ hack-browser-data dump -f xml
Error: unsupported format: xml
<usage: hack-browser-data dump>

$ hack-browser-data dump --max-storage-value -1
Error: --max-storage-value must be >= 0 (0 means unlimited), got -1
<usage: hack-browser-data dump>

$ hack-browser-data dump --keychain-pw secret --keychain-pw-stdin
Error: if any flags in the group [keychain-pw keychain-pw-stdin] are set none of the others can be; [keychain-pw keychain-pw-stdin] were all set
<usage: hack-browser-data dump>

$ hack-browser-data dumpkeys -b nosuch
Error: unknown browser "nosuch", available: all|<browsers>
<usage: hack-browser-data dumpkeys>

$ hack-browser-data archive -c nosuch
Error: unknown category: "nosuch", available: all|password,cookie,bookmark,history,download,creditcard,extension,localstorage,sessionstorage
<usage: hack-browser-data archive>

$ hack-browser-data restore
Error: required flag(s) "keys" not set
<usage: hack-browser-data restore>

$ hack-browser-data restore --keys keys.json
Error: exactly one of --data-dir or --data-zip is required
<usage: hack-browser-data restore>

$ hack-browser-data restore --keys keys.json --data-dir data --data-zip data.zip
Error: if any flags in the group [data-dir data-zip] are set none of the others can be; [data-dir data-zip] were all set
<usage: hack-browser-data restore>

$ hack-browser-data list --nosuch
Error: unknown flag: --nosuch
<usage: hack-browser-data list>

$ hack-browser-data nosuch
Error: unknown command "nosuch" for "hack-browser-data"
Run 'hack-browser-data --help' for usage.
154 changes: 154 additions & 0 deletions cmd/hack-browser-data/testdata/help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
$ hack-browser-data -h
hack-browser-data decrypts and exports browser data from Chromium-based
browsers, Firefox, and Safari on Windows, macOS, and Linux.

GitHub: https://github.com/moonD4rk/HackBrowserData

Usage:
hack-browser-data [flags]
hack-browser-data [command]

Available Commands:
archive Pack decryption-relevant profile files into a zip for cross-host restore
dump Extract and decrypt browser data (default command)
dumpkeys Export Chromium master keys as JSON for cross-host decryption
help Help about any command
list List detected browsers and profiles
restore Decrypt copied profile data using exported master keys
version Print version information

Flags:
-b, --browser string target browser: all|<browsers> (default "all")
-c, --category string data categories (comma-separated): all|password,cookie,bookmark,history,download,creditcard,extension,localstorage,sessionstorage (default "all")
-d, --dir string output directory (default "results")
--firefox-pw string Firefox primary password (visible in ps and shell history)
--firefox-pw-stdin[=true|false] read the Firefox primary password from the first line of stdin
-f, --format string output format: csv|json|cookie-editor (default "json")
-h, --help[=true|false] help for hack-browser-data
--keychain-pw string macOS keychain password (visible in ps and shell history)
--keychain-pw-stdin[=true|false] read the macOS keychain password from the first line of stdin
--max-storage-value int max storage value chars, 0=unlimited (default 2048)
-p, --profile-path string custom profile dir path, get with chrome://version
-v, --verbose[=true|false] enable debug logging
--zip[=true|false] compress output to zip

Use "hack-browser-data [command] --help" for more information about a command.
$ hack-browser-data dump -h
Extract and decrypt browser data (default command)

Usage:
hack-browser-data dump [flags]

Examples:
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

Flags:
-b, --browser string target browser: all|<browsers> (default "all")
-c, --category string data categories (comma-separated): all|password,cookie,bookmark,history,download,creditcard,extension,localstorage,sessionstorage (default "all")
-d, --dir string output directory (default "results")
--firefox-pw string Firefox primary password (visible in ps and shell history)
--firefox-pw-stdin[=true|false] read the Firefox primary password from the first line of stdin
-f, --format string output format: csv|json|cookie-editor (default "json")
-h, --help[=true|false] help for dump
--keychain-pw string macOS keychain password (visible in ps and shell history)
--keychain-pw-stdin[=true|false] read the macOS keychain password from the first line of stdin
--max-storage-value int max storage value chars, 0=unlimited (default 2048)
-p, --profile-path string custom profile dir path, get with chrome://version
--zip[=true|false] compress output to zip

Global Flags:
-v, --verbose[=true|false] enable debug logging
$ hack-browser-data dumpkeys -h
Export Chromium master keys as JSON for cross-host decryption

Usage:
hack-browser-data dumpkeys [flags]

Examples:
hack-browser-data dumpkeys -o keys.json
hack-browser-data dumpkeys -b chrome

Flags:
-b, --browser string target browser: all|<browsers> (default "all")
-h, --help[=true|false] help for dumpkeys
--keychain-pw string macOS keychain password (visible in ps and shell history)
--keychain-pw-stdin[=true|false] read the macOS keychain password from the first line of stdin
-o, --output string output file (default: stdout)

Global Flags:
-v, --verbose[=true|false] enable debug logging
$ hack-browser-data archive -h
Pack decryption-relevant profile files into a zip for cross-host restore

Usage:
hack-browser-data archive [flags]

Examples:
hack-browser-data archive
hack-browser-data archive -b chrome -c cookie -o chrome-cookies.zip

Flags:
-b, --browser string target browser: all|<browsers> (default "all")
-c, --category string data categories (comma-separated): all|password,cookie,bookmark,history,download,creditcard,extension,localstorage,sessionstorage (default "all")
-h, --help[=true|false] help for archive
-o, --output string output archive of decryption-relevant browser files (default "browser-data.zip")

Global Flags:
-v, --verbose[=true|false] enable debug logging
$ hack-browser-data restore -h
Decrypt copied profile data using exported master keys

Usage:
hack-browser-data restore [flags]

Examples:
hack-browser-data restore --keys keys.json --data-zip data.zip
hack-browser-data restore --keys keys.json --data-dir ./data -b chrome -c cookie
hack-browser-data restore --keys keys.json --data-dir ./chrome-userdata -b chrome
ssh origin "hack-browser-data dumpkeys" | hack-browser-data restore --keys - --data-zip data.zip

Flags:
-b, --browser string restore only this browser (optional; must match a vault in --keys)
-c, --category string data categories (comma-separated): all|password,cookie,bookmark,history,download,creditcard,extension,localstorage,sessionstorage (default "all")
--data-dir string copied profile data dir (archive layout, or one browser's User Data with -b)
--data-zip string zip produced by the archive command (alternative to --data-dir)
-d, --dir string output directory (default "results")
-f, --format string output format: csv|json|cookie-editor (default "json")
-h, --help[=true|false] help for restore
--keys string keys file from dumpkeys (use - for stdin)
--max-storage-value int max storage value chars, 0=unlimited (default 2048)
--zip[=true|false] compress output to zip

Global Flags:
-v, --verbose[=true|false] enable debug logging
$ hack-browser-data list -h
List detected browsers and profiles

Usage:
hack-browser-data list [flags]

Examples:
hack-browser-data list
hack-browser-data list --detail

Flags:
--detail[=true|false] show per-category entry counts
-h, --help[=true|false] help for list

Global Flags:
-v, --verbose[=true|false] enable debug logging
$ hack-browser-data version -h
Print version information

Usage:
hack-browser-data version [flags]

Flags:
-h, --help[=true|false] help for version

Global Flags:
-v, --verbose[=true|false] enable debug logging
2 changes: 1 addition & 1 deletion internal/app/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Archive(ctx context.Context, discovery Discovery, categories []category.Cat
}
files := archiveFiles(dirs, categories)

root, err := snapshot.Temp()
root, err := snapshot.NewRoot()
if err != nil {
return 0, err
}
Expand Down
Loading
Loading