forked from pradeep1288/ffpasscracker
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ffpassdecrypt.py
executable file
·174 lines (138 loc) · 4.57 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/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.
"""
import sys
import os
try:
# try to use the python-nss lib from mozilla
# import nss
#except ImportError:
# fall back to dlopen of libnss3.so
from ctypes import (
CDLL, Structure,
c_void_p, c_int, c_uint, c_ubyte, c_char_p,
byref, cast, string_at,
)
#### libnss definitions
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)]
(PW_NONE, PW_FROMFILE, PW_PLAINTEXT, PW_EXTERNAL) = (0, 1, 2, 3)
# SECStatus
(SECWouldBlock, SECFailure, SECSuccess) = (-2, -1, 0)
#### end of libnss definitions
#except ImportError as e:
# print 'Failed to find either nss or ctypes library.'
# raise
except ImportError: pass
try:
from sqlite3 import dbapi2 as sqlite
except ImportError:
from pysqlite2 import dbapi2 as sqlite
import base64
import struct
import glob
import re
import time
import getopt
from getpass import getpass
def findpath_userdirs(profiledir='~/.mozilla/firefox'):
usersdir = os.path.expanduser(profiledir)
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(directory, dbname, use_pass, libnss):
profile = os.path.split(directory)[-1]
if libnss.NSS_Init(directory) != 0:
print 'Could not initialize NSS for "%s"' % profile
print "Profile directory: %s" % profile
keySlot = libnss.PK11_GetInternalKeySlot()
libnss.PK11_CheckUserPassword(keySlot, 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
signons_db = directory+os.sep+dbname
conn = sqlite.connect(signons_db)
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, signons_db, libnss)
print "----Username %s" % string_at(dectext.data,dectext.len)
if libnss.PK11SDR_Decrypt(byref(passwd),byref(dectext),byref(pwdata))==-1:
errorlog(row, signons_db, libnss)
print "----Password %s" % string_at(dectext.data,dectext.len)
c.close()
conn.close()
libnss.NSS_Shutdown()
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
# dlopen libnss3
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", skipping' % filename
if __name__ == '__main__':
main()