mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-12-06 05:53:25 +01:00
Def, Request, DirID, and Source said too little, and short variables and doc comments that skip their name hid intent. varnamelen and staticcheck ST1020-22 now keep both from coming back.
180 lines
5.9 KiB
Go
180 lines
5.9 KiB
Go
package chromium
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"database/sql"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
_ "modernc.org/sqlite"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// Ya Passman Data holds meta, active_keys, and logins; Ya Credit Cards holds meta and records.
|
|
|
|
const yandexLoginsSchema = `CREATE TABLE logins (
|
|
origin_url VARCHAR NOT NULL,
|
|
action_url VARCHAR,
|
|
username_element VARCHAR,
|
|
username_value VARCHAR,
|
|
password_element VARCHAR,
|
|
password_value BLOB,
|
|
signon_realm VARCHAR NOT NULL,
|
|
date_created INTEGER NOT NULL DEFAULT 0
|
|
)`
|
|
|
|
const yandexMetaSchema = `CREATE TABLE meta (
|
|
key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY,
|
|
value LONGVARCHAR
|
|
)`
|
|
|
|
const yandexActiveKeysSchema = `CREATE TABLE active_keys (
|
|
key_id TEXT,
|
|
sealed_key TEXT
|
|
)`
|
|
|
|
const yandexRecordsSchema = `CREATE TABLE records (
|
|
guid TEXT PRIMARY KEY,
|
|
public_data TEXT,
|
|
private_data BLOB
|
|
)`
|
|
|
|
// yandexTestNonce is fixed to keep failures reproducible by hand; Yandex itself draws a random one per row.
|
|
var yandexTestNonce = bytes.Repeat([]byte{0x77}, 12)
|
|
|
|
// yandexMasterKeyBlobNonce differs from yandexTestNonce, so mixing the two up fails to decrypt instead of passing.
|
|
var yandexMasterKeyBlobNonce = bytes.Repeat([]byte{0xAB}, 12)
|
|
|
|
// yandexSignatureForFixtures is a protobuf header: field 1 varint 1, field 2 length 32.
|
|
var yandexSignatureForFixtures = []byte{0x08, 0x01, 0x12, 0x20}
|
|
|
|
func yandexSealAESGCM(t *testing.T, key, nonce, plaintext, aad []byte) []byte {
|
|
t.Helper()
|
|
block, err := aes.NewCipher(key)
|
|
require.NoError(t, err)
|
|
aead, err := cipher.NewGCM(block)
|
|
require.NoError(t, err)
|
|
return aead.Seal(nil, nonce, plaintext, aad)
|
|
}
|
|
|
|
// buildYandexLocalEncryptorBlob lays out meta.local_encryptor_data: preamble, "v10", nonce, sealed signature and key.
|
|
func buildYandexLocalEncryptorBlob(t *testing.T, masterKey, dataKey []byte) []byte {
|
|
t.Helper()
|
|
plaintext := append([]byte{}, yandexSignatureForFixtures...)
|
|
plaintext = append(plaintext, dataKey...)
|
|
// Pad to 68B (= 96 blob - 12 nonce - 16 tag) to match the on-disk shape.
|
|
plaintext = append(plaintext, make([]byte, 68-len(plaintext))...)
|
|
|
|
ciphertext := yandexSealAESGCM(t, masterKey, yandexMasterKeyBlobNonce, plaintext, nil)
|
|
blob := []byte{0x12, 0x34, 0x56, 0x78} // arbitrary preamble
|
|
blob = append(blob, "v10"...)
|
|
blob = append(blob, yandexMasterKeyBlobNonce...)
|
|
blob = append(blob, ciphertext...)
|
|
return blob
|
|
}
|
|
|
|
type yandexPassword struct {
|
|
OriginURL, UsernameElem, UsernameVal, PasswordElem, SignonRealm, Password string
|
|
DateCreated int64
|
|
}
|
|
|
|
// setupYandexPasswordDB adds a non-empty sealed_key row when hasMasterPassword, as a master-password profile has.
|
|
func setupYandexPasswordDB(t *testing.T, masterKey, dataKey []byte, hasMasterPassword bool, rows ...yandexPassword) string {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), "Ya Passman Data")
|
|
database, err := sql.Open("sqlite", path)
|
|
require.NoError(t, err)
|
|
defer database.Close()
|
|
|
|
for _, schema := range []string{yandexLoginsSchema, yandexMetaSchema, yandexActiveKeysSchema} {
|
|
_, err = database.Exec(schema)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
blob := buildYandexLocalEncryptorBlob(t, masterKey, dataKey)
|
|
_, err = database.Exec(`INSERT INTO meta (key, value) VALUES ('local_encryptor_data', ?)`, blob)
|
|
require.NoError(t, err)
|
|
|
|
if hasMasterPassword {
|
|
_, err = database.Exec(`INSERT INTO active_keys (key_id, sealed_key) VALUES ('kid', 'sealed-opaque')`)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
for _, row := range rows {
|
|
aad := yandexLoginAAD(row.OriginURL, row.UsernameElem, row.UsernameVal, row.PasswordElem, row.SignonRealm, nil)
|
|
ciphertext := yandexSealAESGCM(t, dataKey, yandexTestNonce, []byte(row.Password), aad)
|
|
passwordBlob := append([]byte{}, yandexTestNonce...)
|
|
passwordBlob = append(passwordBlob, ciphertext...)
|
|
statement := fmt.Sprintf(
|
|
`INSERT INTO logins (origin_url, action_url, username_element, username_value,
|
|
password_element, password_value, signon_realm, date_created)
|
|
VALUES ('%s', '', '%s', '%s', '%s', x'%s', '%s', %d)`,
|
|
row.OriginURL, row.UsernameElem, row.UsernameVal, row.PasswordElem,
|
|
hex.EncodeToString(passwordBlob), row.SignonRealm, row.DateCreated,
|
|
)
|
|
_, err = database.Exec(statement)
|
|
require.NoError(t, err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
type yandexCreditCard struct {
|
|
GUID string
|
|
CardHolder, CardTitle, ExpYear, ExpMonth string
|
|
FullCardNumber, PinCode, SecretComment string
|
|
}
|
|
|
|
func setupYandexCreditCardDB(t *testing.T, masterKey, dataKey []byte, rows ...yandexCreditCard) string {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), "Ya Credit Cards")
|
|
database, err := sql.Open("sqlite", path)
|
|
require.NoError(t, err)
|
|
defer database.Close()
|
|
|
|
for _, schema := range []string{yandexRecordsSchema, yandexMetaSchema} {
|
|
_, err = database.Exec(schema)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
blob := buildYandexLocalEncryptorBlob(t, masterKey, dataKey)
|
|
_, err = database.Exec(`INSERT INTO meta (key, value) VALUES ('local_encryptor_data', ?)`, blob)
|
|
require.NoError(t, err)
|
|
|
|
for _, row := range rows {
|
|
public := yandexPublicData{
|
|
CardHolder: row.CardHolder,
|
|
CardTitle: row.CardTitle,
|
|
ExpireDateYear: row.ExpYear,
|
|
ExpireDateMonth: row.ExpMonth,
|
|
}
|
|
publicJSON, err := json.Marshal(public)
|
|
require.NoError(t, err)
|
|
|
|
private := yandexPrivateData{
|
|
FullCardNumber: row.FullCardNumber,
|
|
PinCode: row.PinCode,
|
|
SecretComment: row.SecretComment,
|
|
}
|
|
privateJSON, err := json.Marshal(private)
|
|
require.NoError(t, err)
|
|
|
|
aad := yandexCardAAD(row.GUID, nil)
|
|
ciphertext := yandexSealAESGCM(t, dataKey, yandexTestNonce, privateJSON, aad)
|
|
privateBlob := append([]byte{}, yandexTestNonce...)
|
|
privateBlob = append(privateBlob, ciphertext...)
|
|
|
|
_, err = database.Exec(
|
|
`INSERT INTO records (guid, public_data, private_data) VALUES (?, ?, ?)`,
|
|
row.GUID, string(publicJSON), privateBlob,
|
|
)
|
|
require.NoError(t, err)
|
|
}
|
|
return path
|
|
}
|