Skip to content

Instantly share code, notes, and snippets.

@microo8
Created September 21, 2016 09:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save microo8/d0ecb52ec592971a466a3189287631c7 to your computer and use it in GitHub Desktop.
Save microo8/d0ecb52ec592971a466a3189287631c7 to your computer and use it in GitHub Desktop.
Gets and decrypts chrome cookies on linux
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/sha1"
"database/sql"
"log"
"net/http"
"os/user"
"path"
_ "github.com/mattn/go-sqlite3"
"golang.org/x/crypto/pbkdf2"
)
//GetCookies returns decrypted chrome cookies on linux
func GetCookies(hostname string) (ret []*http.Cookie) {
usr, err := user.Current()
if err != nil {
log.Panic(err)
}
cookiesDBPath := path.Join(usr.HomeDir, ".config", "chromium", "Default", "Cookies")
db, err := sql.Open("sqlite3", cookiesDBPath)
if err != nil {
log.Panic(err)
}
defer db.Close()
rows, err := db.Query("select name, encrypted_value from cookies where host_key=?", hostname)
if err != nil {
log.Panic(err)
}
defer rows.Close()
for rows.Next() {
var name, encryptedValue string
rows.Scan(&name, &encryptedValue)
//Decrypt cookie value
//On Mac, replace MY_PASS with your password from Keychain
//On Linux, replace MY_PASS with 'peanuts'
myPass := []byte("peanuts")
//Default values used by both Chrome and Chromium in OSX and Linux
salt := []byte("saltysalt")
iv := []byte(" ")
length := 16
//1003 on Mac, 1 on Linux
iterations := 1
key := pbkdf2.Key(myPass, salt, iterations, length, sha1.New)
block, err := aes.NewCipher(key)
if err != nil {
log.Panic(err)
}
mode := cipher.NewCBCDecrypter(block, iv)
valueBytes := make([]byte, 1024)
mode.CryptBlocks(valueBytes, []byte(encryptedValue[3:]))
value := string(valueBytes)
ret = append(ret, &http.Cookie{Name: name, Value: value})
}
return
}
func main() {
for _, c := range GetCookies("google.com") {
log.Println(c)
}
}
@tim-tx
Copy link

tim-tx commented Apr 12, 2022

Probably out of date. I doubt “peanuts” is correct, putting in keychain password (usually asked on launch in Linux) fails also. Result is a bunch of invalid byte … in Cookie.Value

@microo8
Copy link
Author

microo8 commented Apr 12, 2022

I've written it a long time ago from a python script. It could be that they changed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment