Files
Roger a7741eaab2 refactor: name dirs by what they hold, trim exports (#692)
Bare dir now always means a user data dir, so the snapshot root, the export listing, and the
keys.json kind get their own words; names used only inside their package are no longer exported.
2026-11-10 17:00:43 +08:00

58 lines
1.6 KiB
Go

//go:build windows
package masterkey
import (
"context"
"encoding/base64"
"fmt"
"os"
"github.com/tidwall/gjson"
"github.com/moond4rk/hackbrowserdata/internal/winapi"
)
// platformRetrievers run both: a profile upgraded past Chrome 127 holds v10 and v20 values side by side.
var platformRetrievers = []keyRetriever{dpapiRetriever{}, abeRetriever{}}
type dpapiRetriever struct{}
func (dpapiRetriever) Version() Version { return V10 }
func (dpapiRetriever) Retrieve(_ context.Context, locator Locator) ([]byte, error) {
data, err := os.ReadFile(locator.LocalState)
if err != nil {
return nil, fmt.Errorf("read Local State: %w", err)
}
encryptedKey := gjson.GetBytes(data, "os_crypt.encrypted_key")
if !encryptedKey.Exists() {
return nil, fmt.Errorf("os_crypt.encrypted_key not found in Local State")
}
keyBytes, err := base64.StdEncoding.DecodeString(encryptedKey.String())
if err != nil {
return nil, fmt.Errorf("base64 decode encrypted_key: %w", err)
}
const dpapiPrefix = "DPAPI"
if len(keyBytes) <= len(dpapiPrefix) {
return nil, fmt.Errorf("encrypted_key too short: %d bytes", len(keyBytes))
}
if string(keyBytes[:len(dpapiPrefix)]) != dpapiPrefix {
return nil, fmt.Errorf("encrypted_key unexpected prefix: got %q, want %q", keyBytes[:len(dpapiPrefix)], dpapiPrefix)
}
masterKey, err := winapi.DecryptDPAPI(keyBytes[len(dpapiPrefix):])
if err != nil {
return nil, fmt.Errorf("DPAPI decrypt: %w", err)
}
return masterKey, nil
}
// DecryptDPAPI decrypts a legacy Chromium value that has no version prefix, with the live user's DPAPI context.
func DecryptDPAPI(blob []byte) ([]byte, error) {
return winapi.DecryptDPAPI(blob)
}