refactor: move the ABE unit into internal/abe (#670)

crypto/windows and utils/injector were the last public packages, against the no-library-API rule, and
utils/ named nothing. They move as one unit; only import paths and Makefile variables change, and the
built payload is byte-identical.
This commit is contained in:
Roger authored and GitHub committed 2026-10-29 16:48:46 +08:00
1 parent 07b66adc4d
commit eca1f058c6
29 files changed
+36 -35

No files matched your search

+5 -5
View File
@@ -41,22 +41,22 @@ go mod verify
## Chrome ABE Payload (Windows only)
Chrome 127+ cookies (v20) decrypt via a C payload reflectively injected into `chrome.exe`. Sources in `crypto/windows/abe_native/`; design in [RFC-010](rfcs/010-chrome-abe-integration.md).
Chrome 127+ cookies (v20) decrypt via a C payload reflectively injected into `chrome.exe`. Sources in `internal/abe/native/`; design in [RFC-016](rfcs/016-chrome-abe-integration.md).
```bash
make payload # build the DLL (needs zig: brew install zig)
make build-windows # cross-compile hack-browser-data.exe with payload embedded
make gen-layout # regenerate Go layout constants from bootstrap_layout.h
make payload-clean # rm crypto/*.bin
make payload-clean # rm internal/abe/payload/*.bin
```
- Default `go build` links a stub that errors at runtime when ABE is needed — contributors not touching Windows ABE need no zig.
- `-tags abe_embed` embeds the payload via `//go:embed` (see `crypto/abe_embed_windows.go` / `crypto/abe_stub_windows.go`).
- `-tags abe_embed` embeds the payload via `//go:embed` (see `internal/abe/payload/embed_windows.go` / `stub_windows.go`).
## Code Conventions
- **Platform code**: use build tags (`_darwin.go`, `_windows.go`, `_linux.go`)
- **C payload**: all sources in `crypto/windows/abe_native/` are first-party (no vendored C). See RFC-010 §9.2 for why Stephen Fewer's reflective loader was rejected.
- **C payload**: all sources in `internal/abe/native/` are first-party (no vendored C). See RFC-016 §9.2 for why Stephen Fewer's reflective loader was rejected.
- **Error handling**: `fmt.Errorf("context: %w", err)` for wrapping, never `_ =` to ignore errors
- **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.
- **Naming**: follow Go conventions and the settled names in `rfcs/004-naming-decisions.md` — `Def` not `BrowserConfig`, `Extract` not `BrowsingData`
@@ -68,5 +68,5 @@ make payload-clean # rm crypto/*.bin
- `modernc.org/sqlite` pinned at v1.31.1 (v1.32+ requires Go 1.21)
- `tidwall/gjson` < v1.19, `golang.org/x/term` < v0.30, `golang.org/x/sys` at v0.30.0 — later releases raise their `go` directive above 1.20
- New code goes under `internal/`, never `pkg/`; today's top-level packages move there phase by phase (`rfcs/012-migration-roadmap.md`), except `crypto/windows/` and `utils/injector`, which stay where they are
- Code lives under `internal/`, never `pkg/`; `cmd/` is the only entry point
- No library API — `cmd/` is the only consumer of the packages below it
+1 -1
View File
@@ -1,7 +1,7 @@
GO ?= go
GOEXE ?= hack-browser-data
include crypto/windows/abe_native/Makefile.frag
include internal/abe/native/Makefile.frag
.PHONY: build build-windows clean
+1 -1
View File
@@ -98,7 +98,7 @@ brew install zig # macOS
scoop install zig # Windows (scoop)
# or download from https://ziglang.org/download/
# 2. Build the payload (outputs crypto/windows/payload/abe_extractor_amd64.bin)
# 2. Build the payload (outputs internal/abe/payload/abe_extractor_amd64.bin)
make payload
# 3. Build hack-browser-data.exe with the ABE payload embedded
+1 -1
View File
@@ -51,7 +51,7 @@ In scope — vulnerabilities in this repository's code, build, or releases, for
- Crafted input that HackBrowserData reads (browser profile files, a `keys.json`, or an archive passed to `restore`) leading to code execution, writes outside the output or temporary directory, or other compromise of the host running the tool
- Decrypted data or exported master keys leaking beyond their intended destination, such as files created with overly broad permissions, leftover temporary copies, or secrets written to logs
- Memory-safety bugs in the native Windows payload (`crypto/windows/abe_native/`)
- Memory-safety bugs in the native Windows payload (`internal/abe/native/`)
- Weaknesses in the release pipeline (GitHub Actions workflows, release artifacts, the Homebrew tap) that could let someone tamper with what users install
Out of scope:
File renamed without changes.
@@ -5,7 +5,7 @@ package injector
import (
"fmt"
"github.com/moond4rk/hackbrowserdata/crypto/windows/abe_native/bootstrap"
"github.com/moond4rk/hackbrowserdata/internal/abe/native/bootstrap"
)
// abeErrNames maps the payload's ABE_ERR_* category byte (written into
@@ -6,7 +6,7 @@ import (
"strings"
"testing"
"github.com/moond4rk/hackbrowserdata/crypto/windows/abe_native/bootstrap"
"github.com/moond4rk/hackbrowserdata/internal/abe/native/bootstrap"
)
func TestFormatABEError(t *testing.T) {
File renamed without changes.
File renamed without changes.
@@ -13,7 +13,7 @@ import (
"golang.org/x/sys/windows"
"github.com/moond4rk/hackbrowserdata/crypto/windows/abe_native/bootstrap"
"github.com/moond4rk/hackbrowserdata/internal/abe/native/bootstrap"
"github.com/moond4rk/hackbrowserdata/internal/winapi"
)
@@ -6,7 +6,7 @@ import (
"encoding/binary"
"testing"
"github.com/moond4rk/hackbrowserdata/crypto/windows/abe_native/bootstrap"
"github.com/moond4rk/hackbrowserdata/internal/abe/native/bootstrap"
"github.com/moond4rk/hackbrowserdata/internal/winapi"
)
@@ -2,8 +2,8 @@ ZIG ?= zig
ABE_ARCH ?= amd64
ABE_TARGET ?= x86_64-windows-gnu
ABE_SRC_DIR = crypto/windows/abe_native
ABE_BIN_DIR = crypto/windows/payload
ABE_SRC_DIR = internal/abe/native
ABE_BIN_DIR = internal/abe/payload
ABE_BIN = $(ABE_BIN_DIR)/abe_extractor_$(ABE_ARCH).bin
ABE_CFLAGS = -shared -s -O2 \
@@ -33,7 +33,7 @@ else
ABE_REPORT_CMD = printf "built %s (%s bytes)\n" "$@" "$$(wc -c < $@ | tr -d ' ')"
endif
# crypto/windows/payload/ is tracked in git, so no mkdir is required.
# internal/abe/payload/ is tracked in git, so no mkdir is required.
$(ABE_BIN): $(ABE_C_SRCS) $(ABE_HDRS)
$(ZIG) cc -target $(ABE_TARGET) $(ABE_CFLAGS) $(ABE_LDFLAGS) \
$(ABE_C_SRCS) -o $@ $(ABE_LDLIBS)
@@ -55,7 +55,7 @@ payload-clean:
rm -f $(ABE_BIN_DIR)/abe_extractor_*.bin $(ABE_BIN_DIR)/abe_extractor*.lib
# Scratch-layout codegen. The C header bootstrap_layout.h is the single
# source of truth; the Go constants in crypto/windows/abe_native/bootstrap
# source of truth; the Go constants in internal/abe/native/bootstrap
# are derived from it via cgo -godefs. We pin CC to zig for reproducible
# output across macOS / Linux / Windows hosts.
ABE_LAYOUT_PKG = $(ABE_SRC_DIR)/bootstrap
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+1 -1
View File
@@ -99,7 +99,7 @@ func TestTableRows(t *testing.T) {
// The payload looks a browser up by its executable inside the browser process, so its C table is the other half of each
// ABE row. It may list browsers the Go table does not.
func TestABERowsMatchPayloadTable(t *testing.T) {
src, err := os.ReadFile("../../crypto/windows/abe_native/com_iid.c")
src, err := os.ReadFile("../abe/native/com_iid.c")
require.NoError(t, err)
kinds := map[string]masterkey.ABEKind{
+2 -2
View File
@@ -11,10 +11,10 @@ import (
"github.com/tidwall/gjson"
"github.com/moond4rk/hackbrowserdata/crypto/windows/payload"
"github.com/moond4rk/hackbrowserdata/internal/abe/injector"
"github.com/moond4rk/hackbrowserdata/internal/abe/payload"
"github.com/moond4rk/hackbrowserdata/internal/log"
"github.com/moond4rk/hackbrowserdata/internal/winapi"
"github.com/moond4rk/hackbrowserdata/utils/injector"
)
const envEncKeyB64 = "HBD_ABE_ENC_B64"
+1 -1
View File
@@ -5,7 +5,7 @@
// that golang.org/x/sys/windows does not cover, plus shared LazyDLL
// handles and a small error-handling helper.
//
// Callers: utils/injector, filemanager, masterkey.
// Callers: abe/injector, masterkey.
package winapi
import (
+4 -4
View File
@@ -22,14 +22,14 @@ internal/
sqlitex/ thin modernc.org/sqlite helpers (open read-only by path, query)
timeconv/ browser epoch conversions (Chromium/WebKit, Unix, Firefox, Core Data)
winapi/ Windows only: syscall wrappers, DPAPI, locked-file copy, executable lookup
abe/ Windows only: App-Bound Encryption bridge — move DEFERRED, see below
abe/ Windows only: App-Bound Encryption bridge — native/ C sources, payload/, injector/
keychain/ macOS only: the user's login keychain — one handle per run, opened on first use
log/ leveled logging (Debugf/Infof/Warnf)
```
The technique internals (the ABE payload, reflective injection, DPAPI and keychain specifics) sit behind `masterkey`, `keychain`, `winapi`, and the ABE unit by build tag. Their _mechanics_ are documented in the format and technique RFCs (013–021) and are out of scope for the design RFCs — here they are just packages with a contract ([RFC-006](006-key-retrieval-and-crypto.md)).
**The ABE unit stays where it is (DEFERRED).** Today's `crypto/windows/` — the first-party C sources, the generated `bootstrap` layout package, and the `payload` package that embeds the built `.bin` — and `utils/injector` keep their paths in this rewrite, and so do `Makefile.frag`, `make payload`, and `make gen-layout`. Moving them changes the payload build, which needs its own validation on Windows, so only their importers change here. When the unit moves to `internal/abe/`, it moves as one: `//go:embed` only reaches files in the embedding package's directory or below, so the Makefile writes the payload there ([RFC-012](012-migration-roadmap.md)).
**The ABE unit lives in `internal/abe/` and moved as one.** `native/` holds the first-party C sources and `Makefile.frag` and no Go files, since Go rejects C files in a package that does not use cgo; `native/bootstrap/` is the layout package generated from them; `payload/` embeds the built `.bin`, which `make payload` writes there because `//go:embed` only reaches files in the embedding package's directory or below; and `injector/` loads it. `make payload` and `make gen-layout` keep their names, and the built payload is byte-identical to the one built before the move.
## Dependency direction
@@ -49,7 +49,7 @@ Dependencies point downward and are listed exhaustively; anything not listed is
| `sqlitex`, `winapi` | `log` |
| `category`, `crypto`, `timeconv`, `keychain`, `log` | nothing |
`abe` stands for the ABE unit — `utils/injector` and `crypto/windows/payload` — until it moves.
`abe` stands for the ABE unit's Go packages, `abe/injector` and `abe/payload`.
Four rules keep the graph acyclic and its seams honest:
@@ -77,4 +77,4 @@ Trade-off accepted: the module stops being importable as a library. The module p
- A package exists because it owns a _concept_, not because a file got long. `timeconv` and `category` are tiny on purpose. A package with nothing to own is not created — there is no `cli` (flags belong to `cmd`, rules to `app`) and no `textenc` (the CSV BOM is three bytes in `export`).
- A concept with per-OS implementations lives in one package with `_windows.go` / `_darwin.go` / `_linux.go` files; callers import one package and get the right implementation at build time. A concept that exists on only one OS — the locked-file copy, the ABE bridge, the macOS login keychain — lives in its own package whose files are all built for that OS: `winapi`, `abe`, and `keychain`.
- `winapi`, not `platform/windows`: a package named `windows` would collide with `golang.org/x/sys/windows`, which it imports, so every file would need an alias. `log` and `crypto` keep today's names although the standard library has packages of the same names: both are internal, and no file needs both.
- No `utils` grab-bag. The old tree is redistributed to named homes: `utils/sqliteutil` → `sqlitex`; `utils/fileutil` and `filemanager` → `snapshot` (copy, zip transport) and `export` (`--zip`); `utils/{winapi,winutil}` → `winapi`; `utils/injector` and `crypto/windows/` stay in place (the `abe` move is DEFERRED); `types` → `category`, `record`, and `browser`; `output` → `export`; `log` → `internal/log`.
- No `utils` grab-bag. The old tree is redistributed to named homes: `utils/sqliteutil` → `sqlitex`; `utils/fileutil` and `filemanager` → `snapshot` (copy, zip transport) and `export` (`--zip`); `utils/{winapi,winutil}` → `winapi`; `utils/injector` and `crypto/windows/` → `abe`, moved as one unit; `types` → `category`, `record`, and `browser`; `output` → `export`; `log` → `internal/log`.
+1 -1
View File
@@ -92,7 +92,7 @@ The project CLAUDE.md is updated as part of the migration ([RFC-012](012-migrati
- it references `go-sqlmock`, which `go.mod` doesn't have — the real strategy is `t.TempDir()` + fixtures;
- it says HBD supports Chromium-based browsers and Firefox — Safari is missing, as in the root help text;
- it points at `crypto/abe_embed_windows.go` / `crypto/abe_stub_windows.go`, which now live in `crypto/windows/payload/`;
- it points at `crypto/abe_embed_windows.go` / `crypto/abe_stub_windows.go`, which now live in `internal/abe/payload/`;
- its "`golang.org/x/text` will be removed" note is moot (the module is already indirect-only) and its "no `pkg/` + `internal/`" rule is superseded by [RFC-002](002-package-layout.md).
In the code, the `pickTest` / `TestPickFromConfigs` names outlived the function they test (`discoverFromConfigs`) and go away with it. The README says `cookie-editor` skips non-cookie categories, while the code writes them as standard JSON; the contract freezes the code's behavior, so the README is corrected. The format and technique RFCs (013–021) keep today's type names until Phase 8 moves them to the new ones ([RFC-012](012-migration-roadmap.md)). No RFC citations go into source comments — traceability stays in git and these documents.
+1
View File
@@ -66,6 +66,7 @@ Leaves first, bottom-up along the dependency arrows ([RFC-002](002-package-layou
### Phase 8 — Delete & document
- Remove the superseded top-level packages — `browser/`, `crypto/` except the ABE unit's `crypto/windows/`, `filemanager/`, `log/`, `masterkey/`, `output/`, `types/`, and `utils/` except `utils/injector` — once nothing references them.
- Move the ABE unit — `crypto/windows/` and `utils/injector` — into `internal/abe/` as one, once the payload build is validated on Windows ([RFC-002](002-package-layout.md)).
- Move the compatibility tests out of `cmd/hack-browser-data` into the packages whose code they drive — the export goldens into `export`, `keys.json` into `masterkey`, and the `extract`, `archive`, and `restore` goldens, which all run the pipeline, into `app` — each with its goldens in its own `testdata/`. Shared fixture builders move to `internal/browser/browsertest` and the golden helpers to `internal/golden`. `cmd` keeps the flag-surface and `list` tests, the only ones that go through the cobra layer, with their goldens in its own `testdata/`, and the root `testdata/` goes away.
- Update CLAUDE.md (the new paths), the README (Safari, the `cookie-editor` behavior), and the root help text.
- Bring the RFCs to their finished state: the format and technique RFCs (013–021) move from today's type names — `BrowserConfig`, `WindowsABE`, `winutil.Table`, `Hints`, the `Retrievers` struct — to the names in [RFC-004](004-naming-decisions.md); the design RFCs describe the code as it is instead of comparing it with today's; and this roadmap is removed.
+10 -10
View File
@@ -50,7 +50,7 @@ browser/chromium.Extract()
→ ABERetriever.RetrieveKey():
reads Local State → extracts APPB-prefixed blob
resolves browser exe: install locations, then App Paths
→ utils/injector.Reflective.Inject(exePath, payload, env)
→ internal/abe/injector.Reflective.Inject(exePath, payload, env)
```
**Stage 2 — Payload preparation** (still our process)
@@ -99,7 +99,7 @@ DoExtractKey → see §4.2
The returned key flows back up to `crypto.DecryptChromiumGCM` (cross-platform AES-256-GCM; see §5.3) and then to the usual cookie/password extraction pipeline.
## 4. C payload — `crypto/windows/abe_native/`
## 4. C payload — `internal/abe/native/`
Three translation units, ~500 lines of pure C. No C++, no assembly, no direct syscalls, no vendored third-party code (Stephen Fewer's loader was evaluated and rejected — see §8.2). Built with `zig cc -target x86_64-windows-gnu`.
@@ -154,7 +154,7 @@ Static table mapping `exe_basename → { CLSID, IID_v1, IID_v2, kind }`. `kind`
{ "chrome.exe", CHROME_BASE, { CLSID_bytes }, { IID_v1_bytes }, TRUE, { IID_v2_bytes } }
```
Current coverage: Chrome Stable/Beta, Brave, Edge, Avast Secure Browser, CocCoc. Source file `crypto/windows/abe_native/com_iid.c` is the authoritative list — see §10 for how to add a new fork.
Current coverage: Chrome Stable/Beta, Brave, Edge, Avast Secure Browser, CocCoc. Source file `internal/abe/native/com_iid.c` is the authoritative list — see §10 for how to add a new fork.
### 4.4 Bootstrap parameter block (non-obvious design)
@@ -166,7 +166,7 @@ Validity relies on Windows **KnownDlls + session-consistent ASLR** — `kernel32
## 5. Go integration
### 5.1 Injector package — `utils/injector/`
### 5.1 Injector package — `internal/abe/injector/`
Four files collaborate:
@@ -181,7 +181,7 @@ Four files collaborate:
### 5.2 Scratch layout codegen
The C payload and Go injector communicate through a byte-level protocol in a standalone RW scratch allocation inside the target process. The layout is defined **once** as a `BootstrapScratch` struct + `offsetof`-based macros in `crypto/windows/abe_native/bootstrap_layout.h`. `_Static_assert`s in the same header guarantee compile-time detection of layout drift:
The C payload and Go injector communicate through a byte-level protocol in a standalone RW scratch allocation inside the target process. The layout is defined **once** as a `BootstrapScratch` struct + `offsetof`-based macros in `internal/abe/native/bootstrap_layout.h`. `_Static_assert`s in the same header guarantee compile-time detection of layout drift:
```c
_Static_assert(offsetof(struct BootstrapScratch, marker) == 0x28, "marker offset");
@@ -189,7 +189,7 @@ _Static_assert(offsetof(struct BootstrapScratch, hresult) == 0x2C, "hresult offs
_Static_assert(offsetof(struct BootstrapScratch, key) == 0x40, "key offset");
```
Go consumes the same constants via **`go tool cgo -godefs`** (a development-time tool, not a runtime dependency). `make gen-layout` regenerates `crypto/windows/abe_native/bootstrap/layout.go` from `bootstrap_layout.h` using `CC="zig cc"` for bit-identical results across host OSes. `make gen-layout-verify` can be run locally to verify the committed `layout.go` matches the current header.
Go consumes the same constants via **`go tool cgo -godefs`** (a development-time tool, not a runtime dependency). `make gen-layout` regenerates `internal/abe/native/bootstrap/layout.go` from `bootstrap_layout.h` using `CC="zig cc"` for bit-identical results across host OSes. `make gen-layout-verify` can be run locally to verify the committed `layout.go` matches the current header.
**Why `cgo -godefs` rather than runtime `import "C"`**: we only need constants shared, not FFI to C functions. Runtime CGO would force the whole project into `CGO_ENABLED=1`, losing the "non-Windows contributor needs no C toolchain" guarantee. `cgo -godefs` bakes the values into a pure-Go file that commits to git; the project stays `CGO_ENABLED=0`.
@@ -210,7 +210,7 @@ On extraction success, logs at `Info` level (`abe: retrieved <browser> master ke
## 6. Build chain
- **Default build** (any host, no zig): `go build ./cmd/hack-browser-data/` succeeds; ABE is stubbed out. Legacy v10/v11 cookies still decrypt via DPAPI.
- **Windows release with ABE**: `make build-windows` = `make payload` (zig cc → `crypto/windows/payload/abe_extractor_amd64.bin`) + `GOOS=windows go build -tags abe_embed`. The `abe_embed` tag activates `//go:embed` on the compiled binary, so the payload is always built from the C sources in the same run and cannot go stale.
- **Windows release with ABE**: `make build-windows` = `make payload` (zig cc → `internal/abe/payload/abe_extractor_amd64.bin`) + `GOOS=windows go build -tags abe_embed`. The `abe_embed` tag activates `//go:embed` on the compiled binary, so the payload is always built from the C sources in the same run and cannot go stale.
- **Layout regen**: `make gen-layout` after any change to `bootstrap_layout.h`. This also covers the `BootstrapParams` field offsets the Go injector serializes by hand.
- **`go.mod` unchanged** — no new dependencies. `zig` is the only external toolchain, and only when actually rebuilding the payload.
@@ -230,7 +230,7 @@ All ABE-specific Go code is behind `//go:build windows` (plus `&& abe_embed` for
**No payload bytes ever touch disk on the target machine.**
- Payload DLL exists only as:
1. Build artifact on the developer machine (`crypto/windows/payload/abe_extractor_amd64.bin`, git-ignored)
1. Build artifact on the developer machine (`internal/abe/payload/abe_extractor_amd64.bin`, git-ignored)
2. `.rdata` section of `hack-browser-data.exe` (`//go:embed`)
3. Go `[]byte` in our process memory
4. `VirtualAllocEx`'d RX payload region in the target browser during injection; released on `TerminateProcess`
@@ -286,7 +286,7 @@ Comparisons in this section were made against [xaitax/Chrome-App-Bound-Encryptio
| Avast Secure Browser | ABE v20 via `AVAST` slot (13) |
| Opera / OperaGX / Vivaldi / Yandex / Arc / 360 / QQ / Sogou | Not in `com_iid.c`; legacy v10 cookies still decrypt via DPAPI, v20 cookies do not |
Authoritative CLSID/IID table: `crypto/windows/abe_native/com_iid.c`.
Authoritative CLSID/IID table: `internal/abe/native/com_iid.c`.
## 11. Adding support for a new Chromium fork
@@ -296,7 +296,7 @@ Three steps.
2. **Mine IIDs from TypeLib** — the interface IIDs live in the TypeLib resource of `<InstallDir>\Application\<version>\elevation_service.exe`. PowerShell + `ITypeLib.GetTypeInfo` enumerates them. Map `IElevator<Vendor>` → v1 IID, `IElevator2<Vendor>` → v2 IID (absent for older vendors).
3. **Determine vtable slot** — count `IElevator` methods in the TypeLib. Chrome-family has 3 methods (slot 5). Edge prepends 3 placeholders (slot 8). Avast extends the interface further (slot 13).
Edit `crypto/windows/abe_native/com_iid.c` (add the entry), `utils/winutil/browser_meta_windows.go` (add a matching `winutil.Entry` with the right `ABEKind` and install-path fallbacks; `Entry.Key` must equal the `BrowserConfig.Key` of the matching ABE-enabled config), `browser/browser_windows.go` (set `WindowsABE: true` on the new `BrowserConfig` so its `Key` doubles as the ABE / winutil.Table identifier), then `make payload-clean && make build-windows` and redeploy.
Edit `internal/abe/native/com_iid.c` (add the entry), `utils/winutil/browser_meta_windows.go` (add a matching `winutil.Entry` with the right `ABEKind` and install-path fallbacks; `Entry.Key` must equal the `BrowserConfig.Key` of the matching ABE-enabled config), `browser/browser_windows.go` (set `WindowsABE: true` on the new `BrowserConfig` so its `Key` doubles as the ABE / winutil.Table identifier), then `make payload-clean && make build-windows` and redeploy.
## 12. Known issues & future work