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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/godbus/dbus/v5 v5.2.4
github.com/inconshreveable/mousetrap v1.1.0
github.com/moond4rk/binarycookies v1.0.3
github.com/moond4rk/binarycookies/v2 v2.0.0
github.com/moond4rk/keychainbreaker v0.2.6
github.com/moond4rk/plist v1.2.2
github.com/spf13/cobra v1.10.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/moond4rk/binarycookies v1.0.3 h1:MRi8GkMKjIoyxdCSCgm4k+gBYA2fYxl1GZc04kQi3G0=
github.com/moond4rk/binarycookies v1.0.3/go.mod h1:iAJr8L7ZgzTKaZhzEZmzjuOFnAoIHaqHFzvn58h1b7c=
github.com/moond4rk/binarycookies/v2 v2.0.0 h1:RKTD8em9ynn0jVIWQX01xx8ZUeKuApA0NMtR31khnSY=
github.com/moond4rk/binarycookies/v2 v2.0.0/go.mod h1:e97QORQKJnov02snqA1dK2XDSy8WKwT2uyXqCsjKcec=
github.com/moond4rk/keychainbreaker v0.2.6 h1:pji+mNKwNndR3wcq5Wu4fJO7lSro3fHpycLbohoOeOc=
github.com/moond4rk/keychainbreaker v0.2.6/go.mod h1:VVx2VXwL2EGhuU2WBD67w66JCKKqLFXGJg91y3FY4f0=
github.com/moond4rk/plist v1.2.2 h1:o1yyy2GTkh/+zSslWqK+/M7Z4dMRzTBCBSXnUgwLLw4=
Expand Down
119 changes: 25 additions & 94 deletions internal/browser/browsertest/safari.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"
"unicode/utf16"

"github.com/moond4rk/binarycookies/v2"
"github.com/moond4rk/plist"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -63,43 +64,43 @@ func WriteSafariLibrary(t *testing.T, library string) {
)

writeFile(t, filepath.Join(container, "Cookies", "Cookies.binarycookies"), string(encodeSafariCookieFile(t,
[]safariCookie{
[]binarycookies.Cookie{
{
domain: ".home.safari.test", name: "SID", path: "/", value: "sid-home.safari.test", flags: 0x5,
expires: timeAt(365 * day), created: timeAt(10*time.Minute + 750*time.Millisecond),
Domain: ".home.safari.test", Name: "SID", Path: "/", Value: "sid-home.safari.test", Flags: binarycookies.FlagSecure | binarycookies.FlagHTTPOnly,
Expires: timeAt(365 * day), Created: timeAt(10 * time.Minute),
},
{
domain: "www.home.safari.test", name: "prefs", path: "/settings", value: "theme=dark",
expires: timeAt(30 * day), created: timeAt(20 * time.Minute),
Domain: "www.home.safari.test", Name: "prefs", Path: "/settings", Value: "theme=dark",
Expires: timeAt(30 * day), Created: timeAt(20 * time.Minute),
},
},
[]safariCookie{
[]binarycookies.Cookie{
{
domain: ".home.safari.test", name: "tracker", path: "/", value: "opt-out", flags: 0x1,
expires: timeAt(730 * day), created: timeAt(30 * time.Minute),
Domain: ".home.safari.test", Name: "tracker", Path: "/", Value: "opt-out", Flags: binarycookies.FlagSecure,
Expires: timeAt(730 * day), Created: timeAt(30 * time.Minute),
},
{
domain: "app.home.safari.test", name: "csrf", path: "/", value: "token-123", flags: 0x4,
expires: timeAt(day), created: timeAt(40 * time.Minute),
Domain: "app.home.safari.test", Name: "csrf", Path: "/", Value: "token-123", Flags: binarycookies.FlagHTTPOnly,
Expires: timeAt(day), Created: timeAt(40 * time.Minute),
},
},
)))
// Since macOS 14 the jar lives in the container, which wins over a leftover ~/Library/Cookies jar.
writeFile(t, filepath.Join(library, "Cookies", "Cookies.binarycookies"), string(encodeSafariCookieFile(t,
[]safariCookie{{
domain: "old.home.safari.test", name: "legacy", path: "/", value: "stale",
expires: timeAt(day), created: timeAt(time.Minute),
[]binarycookies.Cookie{{
Domain: "old.home.safari.test", Name: "legacy", Path: "/", Value: "stale",
Expires: timeAt(day), Created: timeAt(time.Minute),
}},
)))
writeFile(t, filepath.Join(workStore, "Cookies", "Cookies.binarycookies"), string(encodeSafariCookieFile(t,
[]safariCookie{
[]binarycookies.Cookie{
{
domain: ".work.safari.test", name: "SSO", path: "/", value: "sso-work.safari.test", flags: 0x5,
expires: timeAt(7 * day), created: timeAt(6 * time.Hour),
Domain: ".work.safari.test", Name: "SSO", Path: "/", Value: "sso-work.safari.test", Flags: binarycookies.FlagSecure | binarycookies.FlagHTTPOnly,
Expires: timeAt(7 * day), Created: timeAt(6 * time.Hour),
},
{
domain: "wiki.work.safari.test", name: "lang", path: "/", value: "en",
expires: timeAt(90 * day), created: timeAt(7 * time.Hour),
Domain: "wiki.work.safari.test", Name: "lang", Path: "/", Value: "en",
Expires: timeAt(90 * day), Created: timeAt(7 * time.Hour),
},
},
)))
Expand Down Expand Up @@ -145,86 +146,16 @@ func writeSafariPlist(t *testing.T, path, body string) {
writeFile(t, path, string(data))
}

type safariCookie struct {
domain, name, path, value string
flags uint32
expires, created time.Time
}

type safariCookieHeader struct {
Size uint32
Version uint32
Flags uint32
HasPort uint32
DomainOffset uint32
NameOffset uint32
PathOffset uint32
ValueOffset uint32
CommentOffset uint32
EndOfHeader uint32
Expires float64
Created float64
}

// encodeSafariCookieFile takes one page of the jar per argument.
func encodeSafariCookieFile(t *testing.T, pages ...[]safariCookie) []byte {
func encodeSafariCookieFile(t *testing.T, pages ...[]binarycookies.Cookie) []byte {
t.Helper()
encoded := make([][]byte, len(pages))
file := binary.BigEndian.AppendUint32([]byte("cook"), uint32(len(pages)))
jar := &binarycookies.File{Pages: make([]binarycookies.Page, len(pages))}
for i, cookies := range pages {
encoded[i] = encodeSafariCookiePage(t, cookies)
file = binary.BigEndian.AppendUint32(file, uint32(len(encoded[i])))
}
for _, page := range encoded {
file = append(file, page...)
}
return append(file, make([]byte, 8)...) // the checksum, which the decoder reads but never verifies
}

func encodeSafariCookiePage(t *testing.T, cookies []safariCookie) []byte {
t.Helper()
records := make([][]byte, len(cookies))
for i, cookie := range cookies {
records[i] = encodeSafariCookieRecord(t, cookie)
}
page := binary.LittleEndian.AppendUint32([]byte{0x00, 0x00, 0x01, 0x00}, uint32(len(cookies)))
offset := 4 + 4 + 4*len(cookies) + 4
for _, encodedRecord := range records {
page = binary.LittleEndian.AppendUint32(page, uint32(offset))
offset += len(encodedRecord)
}
page = append(page, 0x00, 0x00, 0x00, 0x00)
for _, encodedRecord := range records {
page = append(page, encodedRecord...)
}
return page
}

func encodeSafariCookieRecord(t *testing.T, cookie safariCookie) []byte {
t.Helper()
header := safariCookieHeader{
Flags: cookie.flags,
Expires: safariCoreData(cookie.expires),
Created: safariCoreData(cookie.created),
}
headerSize := uint32(binary.Size(header))
var strs []byte
for _, field := range []struct {
offset *uint32
value string
}{
{&header.DomainOffset, cookie.domain},
{&header.NameOffset, cookie.name},
{&header.PathOffset, cookie.path},
{&header.ValueOffset, cookie.value},
} {
*field.offset = headerSize + uint32(len(strs))
strs = append(append(strs, field.value...), 0x00)
jar.Pages[i].Cookies = cookies
}
header.Size = headerSize + uint32(len(strs))
var buffer bytes.Buffer
require.NoError(t, binary.Write(&buffer, binary.LittleEndian, header))
return append(buffer.Bytes(), strs...)
var buf bytes.Buffer
require.NoError(t, binarycookies.Encode(&buf, jar))
return buf.Bytes()
}

type safariEndpoint struct {
Expand Down
42 changes: 19 additions & 23 deletions internal/browser/safari/extract_cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,46 @@ import (
"fmt"
"os"

"github.com/moond4rk/binarycookies"
"github.com/moond4rk/binarycookies/v2"

"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/record"
)

func extractCookies(path string, out browser.Collector) error {
pages, err := decodeBinaryCookies(path)
jar, err := decodeBinaryCookies(path)
if err != nil {
return err
}

for _, page := range pages {
for _, cookie := range page.Cookies {
hasExpire := !cookie.Expires.IsZero()
// binarycookies returns local times; UTC keeps the JSON in line with Chromium's and Firefox's cookies.
out.Add(&record.Cookie{
Host: string(cookie.Domain),
Path: string(cookie.Path),
Name: string(cookie.Name),
Value: string(cookie.Value),
IsSecure: cookie.Secure,
IsHTTPOnly: cookie.HTTPOnly,
HasExpire: hasExpire,
IsPersistent: hasExpire,
ExpireAt: cookie.Expires.UTC(),
CreatedAt: cookie.Creation.UTC(),
})
}
for _, cookie := range jar.Cookies() {
hasExpire := !cookie.Expires.IsZero()
out.Add(&record.Cookie{
Host: cookie.Domain,
Path: cookie.Path,
Name: cookie.Name,
Value: cookie.Value,
IsSecure: cookie.Secure(),
IsHTTPOnly: cookie.HTTPOnly(),
HasExpire: hasExpire,
IsPersistent: hasExpire,
ExpireAt: cookie.Expires,
CreatedAt: cookie.Created,
})
}
return nil
}

func decodeBinaryCookies(path string) ([]binarycookies.Page, error) {
func decodeBinaryCookies(path string) (*binarycookies.File, error) {
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("open cookies file: %w", err)
}
defer file.Close()

jar := binarycookies.New(file)
pages, err := jar.Decode()
jar, err := binarycookies.Decode(file)
if err != nil {
return nil, fmt.Errorf("decode cookies: %w", err)
}
return pages, nil
return jar, nil
}
Loading