mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-05-19 18:58:03 +02:00
fix: firefox password decryption failure, Close #70
This commit is contained in:
+147
-10
@@ -4,18 +4,160 @@ import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/des"
|
||||
"crypto/hmac"
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"encoding/asn1"
|
||||
"errors"
|
||||
|
||||
"hack-browser-data/log"
|
||||
|
||||
"golang.org/x/crypto/pbkdf2"
|
||||
)
|
||||
|
||||
var (
|
||||
errSecurityKeyIsEmpty = errors.New("input [security find-generic-password -wa 'Chrome'] in terminal")
|
||||
errPasswordIsEmpty = errors.New("password is empty")
|
||||
errDecryptFailed = errors.New("decrypt failed, password is empty")
|
||||
errDecodeASN1Failed = errors.New("decode ASN1 data failed")
|
||||
)
|
||||
|
||||
type ASN1PBE interface {
|
||||
Decrypt(globalSalt, masterPwd []byte) (key []byte, err error)
|
||||
}
|
||||
|
||||
func NewASN1PBE(b []byte) (pbe ASN1PBE, err error) {
|
||||
var (
|
||||
n NssPBE
|
||||
m MetaPBE
|
||||
l LoginPBE
|
||||
)
|
||||
if _, err := asn1.Unmarshal(b, &n); err == nil {
|
||||
return n, nil
|
||||
}
|
||||
if _, err := asn1.Unmarshal(b, &m); err == nil {
|
||||
return m, nil
|
||||
}
|
||||
if _, err := asn1.Unmarshal(b, &l); err == nil {
|
||||
return l, nil
|
||||
}
|
||||
return nil, errDecodeASN1Failed
|
||||
}
|
||||
|
||||
/* NSS Struct
|
||||
SEQUENCE (2 elem)
|
||||
SEQUENCE (2 elem)
|
||||
OBJECT IDENTIFIER
|
||||
SEQUENCE (2 elem)
|
||||
OCTET STRING (20 byte)
|
||||
INTEGER 1
|
||||
OCTET STRING (16 byte)
|
||||
*/
|
||||
type NssPBE struct {
|
||||
NssSequenceA
|
||||
Encrypted []byte
|
||||
}
|
||||
|
||||
type NssSequenceA struct {
|
||||
DecryptMethod asn1.ObjectIdentifier
|
||||
NssSequenceB
|
||||
}
|
||||
|
||||
type NssSequenceB struct {
|
||||
EntrySalt []byte
|
||||
Len int
|
||||
}
|
||||
|
||||
func (n NssPBE) Decrypt(globalSalt, masterPwd []byte) (key []byte, err error) {
|
||||
// byte[] GLMP; // GlobalSalt + MasterPassword
|
||||
// byte[] HP; // SHA1(GLMP)
|
||||
// byte[] HPES; // HP + EntrySalt
|
||||
// byte[] CHP; // SHA1(HPES)
|
||||
// byte[] PES; // EntrySalt completed to 20 bytes by zero
|
||||
// byte[] PESES; // PES + EntrySalt
|
||||
// byte[] k1;
|
||||
// byte[] tk;
|
||||
// byte[] k2;
|
||||
// byte[] k; // final value containing key and iv
|
||||
glmp := append(globalSalt, masterPwd...)
|
||||
hp := sha1.Sum(glmp)
|
||||
s := append(hp[:], n.EntrySalt...)
|
||||
chp := sha1.Sum(s)
|
||||
pes := PaddingZero(n.EntrySalt, 20)
|
||||
tk := hmac.New(sha1.New, chp[:])
|
||||
tk.Write(pes)
|
||||
pes = append(pes, n.EntrySalt...)
|
||||
k1 := hmac.New(sha1.New, chp[:])
|
||||
k1.Write(pes)
|
||||
tkPlus := append(tk.Sum(nil), n.EntrySalt...)
|
||||
k2 := hmac.New(sha1.New, chp[:])
|
||||
k2.Write(tkPlus)
|
||||
k := append(k1.Sum(nil), k2.Sum(nil)...)
|
||||
iv := k[len(k)-8:]
|
||||
log.Debug("get firefox pbe key and iv success")
|
||||
return des3Decrypt(k[:24], iv, n.Encrypted)
|
||||
}
|
||||
|
||||
/* META Struct
|
||||
SEQUENCE (2 elem)
|
||||
SEQUENCE (2 elem)
|
||||
OBJECT IDENTIFIER
|
||||
SEQUENCE (2 elem)
|
||||
SEQUENCE (2 elem)
|
||||
OBJECT IDENTIFIER
|
||||
SEQUENCE (4 elem)
|
||||
OCTET STRING (32 byte)
|
||||
INTEGER 1
|
||||
INTEGER 32
|
||||
SEQUENCE (1 elem)
|
||||
OBJECT IDENTIFIER
|
||||
SEQUENCE (2 elem)
|
||||
OBJECT IDENTIFIER
|
||||
OCTET STRING (14 byte)
|
||||
OCTET STRING (16 byte)
|
||||
*/
|
||||
type MetaPBE struct {
|
||||
MetaSequenceA
|
||||
Encrypted []byte
|
||||
}
|
||||
|
||||
type MetaSequenceA struct {
|
||||
PKCS5PBES2 asn1.ObjectIdentifier
|
||||
MetaSequenceB
|
||||
}
|
||||
type MetaSequenceB struct {
|
||||
MetaSequenceC
|
||||
MetaSequenceD
|
||||
}
|
||||
|
||||
type MetaSequenceC struct {
|
||||
PKCS5PBKDF2 asn1.ObjectIdentifier
|
||||
MetaSequenceE
|
||||
}
|
||||
|
||||
type MetaSequenceD struct {
|
||||
AES256CBC asn1.ObjectIdentifier
|
||||
IV []byte
|
||||
}
|
||||
|
||||
type MetaSequenceE struct {
|
||||
EntrySalt []byte
|
||||
IterationCount int
|
||||
KeySize int
|
||||
MetaSequenceF
|
||||
}
|
||||
|
||||
type MetaSequenceF struct {
|
||||
HMACWithSHA256 asn1.ObjectIdentifier
|
||||
}
|
||||
|
||||
func (m MetaPBE) Decrypt(globalSalt, masterPwd []byte) (key2 []byte, err error) {
|
||||
k := sha1.Sum(globalSalt)
|
||||
key := pbkdf2.Key(k[:], m.EntrySalt, m.IterationCount, m.KeySize, sha256.New)
|
||||
iv := append([]byte{4, 14}, m.IV...)
|
||||
return aes128CBCDecrypt(key, iv, m.Encrypted)
|
||||
}
|
||||
|
||||
func aes128CBCDecrypt(key, iv, encryptPass []byte) ([]byte, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
@@ -35,7 +177,7 @@ func PKCS5UnPadding(src []byte) []byte {
|
||||
}
|
||||
|
||||
// Des3Decrypt use for decrypt firefox PBE
|
||||
func Des3Decrypt(key, iv []byte, src []byte) ([]byte, error) {
|
||||
func des3Decrypt(key, iv []byte, src []byte) ([]byte, error) {
|
||||
block, err := des.NewTripleDESCipher(key)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
@@ -69,20 +211,15 @@ SEQUENCE (3 elem)
|
||||
*/
|
||||
type LoginPBE struct {
|
||||
CipherText []byte
|
||||
SequenceLogin
|
||||
LoginSequence
|
||||
Encrypted []byte
|
||||
}
|
||||
|
||||
type SequenceLogin struct {
|
||||
type LoginSequence struct {
|
||||
asn1.ObjectIdentifier
|
||||
Iv []byte
|
||||
}
|
||||
|
||||
func DecodeLogin(decodeItem []byte) (pbe LoginPBE, err error) {
|
||||
_, err = asn1.Unmarshal(decodeItem, &pbe)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
return pbe, nil
|
||||
func (l LoginPBE) Decrypt(globalSalt, masterPwd []byte) (key []byte, err error) {
|
||||
return des3Decrypt(globalSalt, l.Iv, l.Encrypted)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user