forked from tijldeneut/diana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiana-cookieinjector.py
executable file
·96 lines (81 loc) · 6.4 KB
/
diana-cookieinjector.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
#!/usr/bin/python3
## This script creates a SQlite3 cookie database
## Can be used in Firefox (portable) or a custom profile to access websites
import sqlite3, random, string, time, os, optparse, sys
def check_parameters(options):
if not os.path.exists(options.cookiefile):
sys.exit('Please specify a source file containing exported cookies.')
def newConnection(sFilename, boolOldfirefox):
oConn = None
boolNewfile = True
if os.path.exists(sFilename): boolNewfile = False
try: oConn = sqlite3.connect(sFilename)
except sqlite3.Error as e: exit(e)
oCur = oConn.cursor()
if boolNewfile:
## Format for Firefox 51.0 (32-bit) Portable (pre 67)
if boolOldfirefox: oCur.execute('CREATE TABLE moz_cookies (id INTEGER PRIMARY KEY, baseDomain TEXT, originAttributes TEXT NOT NULL DEFAULT "", name TEXT, value TEXT, host TEXT, path TEXT, expiry INTEGER, lastAccessed INTEGER, creationTime INTEGER, isSecure INTEGER, isHttpOnly INTEGER, appId INTEGER DEFAULT 0, inBrowserElement INTEGER DEFAULT 0, CONSTRAINT moz_uniqueid UNIQUE (name, host, path, originAttributes));')
## Format for Firefox 100.0 (64-bit) (post 67)
else: oCur.execute('CREATE TABLE moz_cookies (id INTEGER PRIMARY KEY, originAttributes TEXT NOT NULL DEFAULT "", name TEXT, value TEXT, host TEXT, path TEXT, expiry INTEGER, lastAccessed INTEGER, creationTime INTEGER, isSecure INTEGER, isHttpOnly INTEGER, inBrowserElement INTEGER DEFAULT 0, sameSite INTEGER DEFAULT 0, rawSameSite INTEGER DEFAULT 0, schemeMap INTEGER DEFAULT 0, CONSTRAINT moz_uniqueid UNIQUE (name, host, path, originAttributes));')
return oCur, oConn
def addCookieFromChrome(oCur, boolOldfirefox, iID, sName, sValue, sDomain, sPath, iCreation, iExpiry, iSecure, iHTTPOnly):
## Expiry and creation timestamps for Chrome/Edge: {timeStamp} / 1000000 - 11644473600 in seconds
## Expiry, LastAccessed and Creation timestamps for Firefox (destination): epoch in seconds, epoch micros and epoch in micros
#if not iExpiry: iExpiry = int(time.time() + 31582861) ## Now + 1 Year, in seconds
iCreationTime = int(int(iCreation) / 1000000 - 11644473600)*1000000
iLastAccessed = iCreationTime
iExpiry = int(time.time() + 31582861) ## Now + 1 Year, in seconds
sBaseDomain = sDomain.split('.')[-2] + '.' + sDomain.split('.')[-1]
## Format for Firefox 51.0 (32-bit) Portable (pre 67)
## id, baseDomain, '', name, value, host, path, expiry, lastaccessed, creation, secure, httponly, 0, 0
if boolOldfirefox: oCur.execute('INSERT INTO moz_cookies VALUES({},\'{}\',"",\'{}\',\'{}\',\'{}\',\'{}\',{},{},{},{},{},0,0);'.format(iID, sBaseDomain, sName.replace('\'','"'), sValue.replace('\'','"'), sDomain, sPath, iExpiry, iLastAccessed, iCreationTime, iSecure, iHTTPOnly))
## Format for Firefox 100.0 (64-bit) (post 67)
## id, '', name, value, host, path, expiry, lastaccessed, creation, secure, httponly, 0, 0, 0, 0
else: oCur.execute('INSERT INTO moz_cookies VALUES({},"",\'{}\',\'{}\',\'{}\',\'{}\',{},{},{},{},{},0,0,0,0);'.format(iID, sName.replace('\'','"'), sValue.replace('\'','"'), sDomain, sPath, iExpiry, iLastAccessed, iCreationTime, iSecure, iHTTPOnly))
return
def printCookies(oCur, sDomain = None):
if sDomain: oCur.execute('SELECT * FROM moz_cookies WHERE host=?', (sDomain,))
else: oCur.execute('SELECT * FROM moz_cookies')
print('id, baseDomain, originAttributes, name, value, host, path, expiry, lastAccessed, creationTime, isSecure, isHttpOnly, appId, inBrowserElement')
for sData in oCur.fetchall(): print(sData)
return
if __name__ == '__main__':
usage = (
'This script parses Chrome/Edge/Opera exported cookies and turns them into a Firefox \'cookies.lite\' file\n'
'It suffices to put cookies.lite in its own folder and start \'firefox -new-instance -profile "folder"\'')
parser = optparse.OptionParser(usage=usage)
parser.add_option('--cookiefile', '-c', metavar='FILE', dest='cookiefile', default='cookie-import.txt', help='File containing exported cookies')
parser.add_option('--oldfirefox', dest='oldfirefox', default=False, action='store_true', help='Specify if destination Firefox version predates v67')
parser.add_option('--newfile', '-n', metavar='FILE', dest='newfile', help='Specify new filename, default=random filename')
parser.add_option('--startbrowser', '-s', metavar='BINARY', dest='startbrowser', help='When specified, specified Firefox is started with new cookie file. (Just type \'firefox\' on Unix)')
(options, args) = parser.parse_args()
check_parameters(options)
boolOldfirefox = True if options.oldfirefox else False
if options.newfile: sFilename = options.newfile
else: sFilename = ''.join(random.choice(string.ascii_lowercase) for i in range(8)) + '.sqlite'
print('[+] Creating new SQLite database: {}'.format(sFilename))
oCur, oConn = newConnection(sFilename, boolOldfirefox)
print('[+] Using Chrome/Edge/Opera source file: {}'.format(options.cookiefile))
## print('name|value|host_key|path|is_secure|is_httponly|creation_utc|expires_utc')
iCount = 1
for sLine in open(options.cookiefile).readlines():
if sLine.startswith('name;value'): continue
lstData = sLine.split(';')
## oCur, boolOldfirefox, iID, sName, sValue, sDomain, sPath, iCreation, iExpiry, iSecure, iHTTPOnly
addCookieFromChrome(oCur, boolOldfirefox, iCount, lstData[0], lstData[1], lstData[2], lstData[3], lstData[6], 0, lstData[4], lstData[5])
iCount += 1
print('[+] Converted {} cookies'.format(iCount))
oConn.commit()
oConn.close()
if not options.startbrowser:
print('[+] File {} created, now go forth and close Firefox'.format(sFilename))
print(' place it inside Firefox > Data > profile > \'cookies.sqlite\' and relaunch Firefox')
else:
sNewProfile = ''.join(random.choice(string.ascii_lowercase) for i in range(8)) + '-Profile'
print('[+] Creating profile folder {}'.format(sNewProfile))
os.mkdir(sNewProfile)
## On Linux/Kali the cookies file can be copied before first boot
if os.name =='nt': os.system('copy {} {}\\cookies.sqlite 2> null'.format(sFilename, sNewProfile))
else: os.system('cp {} {}/cookies.sqlite'.format(sFilename, sNewProfile))
print('[+] Launching "{}" -new-instance -profile {}'.format(options.startbrowser, sNewProfile))
os.system('"{}" -new-instance -profile {}'.format(options.startbrowser, sNewProfile))