forked from nyov/python-ffpassdecrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffpassdecrypt.py
executable file
·156 lines (126 loc) · 4.24 KB
/
ffpassdecrypt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
"""
ffpassdecrypt - Decode the passwords stored using Firefox browser. The script currently works only on Linux.
Author : Pradeep Nayak ([email protected])
usage: ./ffpassdecrypt.py [paths_to_location_of_files]
Run it with no parameters to extract the standard passwords from all profiles of the current logged in user,
or with an optional '-P' argument (before any path) to query the master password for decryption.
Required files:
+ key3.db
+ signons.sqlite
+ cert8.db
are used and needed to collect the passwords.
"""
from ctypes import (
CDLL,
Structure,
c_int, c_uint, c_void_p, c_char_p, c_ubyte,
cast, byref, string_at)
import struct
import sys
import os
import glob
import re
import time
import base64
import getopt
import getpass
# Password structures
class SECItem(Structure):
_fields_ = [('type',c_uint),('data',c_void_p),('len',c_uint)]
class secuPWData(Structure):
_fields_ = [('source',c_ubyte),('data',c_char_p)]
(SECWouldBlock, SECFailure, SECSuccess) = (-2,-1,0)
(PW_NONE, PW_FROMFILE, PW_PLAINTEXT, PW_EXTERNAL) = (0,1,2,3)
def findpath_userdirs():
appdata = os.getenv('HOME')
usersdir = appdata+os.sep+".mozilla"+os.sep+'firefox'
userdir = os.listdir(usersdir)
res=[]
for user in userdir:
if os.path.isdir(usersdir+os.sep+user):
res.append(usersdir+os.sep+user)
return res
def errorlog(row, path, libnss):
print "----[-]Error while Decoding! writting error.log:"
print libnss.PORT_GetError()
try:
f=open('error.log','a')
f.write("-------------------\n")
f.write("#ERROR in: %s at %s\n" %(path,time.ctime()))
f.write("Site: %s\n"%row[1])
f.write("Username: %s\n"%row[6])
f.write("Password: %s\n"%row[7])
f.write("-------------------\n")
f.close()
except IOError:
print "Error while writing logfile - No log created!"
# reads the signons.sqlite which is a sqlite3 Database (>Firefox 3)
def readsignonDB(userpath, dbname, use_pass, libnss):
if libnss.NSS_Init(userpath)!=0:
print """Error Initalizing NSS_Init,\n
propably no usefull results"""
print "Dirname: %s"%os.path.split(userpath)[-1]
keySlot = libnss.PK11_GetInternalKeySlot()
libnss.PK11_CheckUserPassword(keySlot, getpass.getpass() if use_pass else "")
libnss.PK11_Authenticate(keySlot, True, 0)
uname = SECItem()
passwd = SECItem()
dectext = SECItem()
pwdata = secuPWData()
pwdata.source = PW_NONE
pwdata.data = 0
import sqlite3
conn = sqlite3.connect(userpath+os.sep+dbname)
c = conn.cursor()
c.execute("SELECT * FROM moz_logins;")
for row in c:
print "--Site(%s):"%row[1]
uname.data = cast(c_char_p(base64.b64decode(row[6])),c_void_p)
uname.len = len(base64.b64decode(row[6]))
passwd.data = cast(c_char_p(base64.b64decode(row[7])),c_void_p)
passwd.len=len(base64.b64decode(row[7]))
if libnss.PK11SDR_Decrypt(byref(uname),byref(dectext),byref(pwdata))==-1:
errorlog(row, userpath+os.sep+dbname, libnss)
print "----Username %s" % string_at(dectext.data,dectext.len)
if libnss.PK11SDR_Decrypt(byref(passwd),byref(dectext),byref(pwdata))==-1:
errorlog(row, userpath+os.sep+dbname, libnss)
print "----Password %s" % string_at(dectext.data,dectext.len)
c.close()
conn.close()
libnss.NSS_Shutdown()
################# MAIN #################
def main():
try:
optlist, args = getopt.getopt(sys.argv[1:], 'P')
except getopt.GetoptError as err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
if len(args)==0:
ordner = findpath_userdirs()
else:
ordner=args
use_pass = False
for o, a in optlist:
if o == '-P':
use_pass = True
# Load the libnss3 linked file
libnss = CDLL("libnss3.so")
# Set function profiles
libnss.PK11_GetInternalKeySlot.restype = c_void_p
libnss.PK11_CheckUserPassword.argtypes = [c_void_p, c_char_p]
libnss.PK11_Authenticate.argtypes = [c_void_p, c_int, c_void_p]
for user in ordner:
signonfiles = glob.glob(user+os.sep+"signons*.*")
for signonfile in signonfiles:
(filepath,filename) = os.path.split(signonfile)
filetype = re.findall('\.(.*)',filename)[0]
if filetype.lower() == "sqlite":
readsignonDB(filepath, filename, use_pass, libnss)
else:
print "Unhandled Signons File: %s" % filename
print "Skipping"
if __name__ == '__main__':
main()