This repository was archived by the owner on Jun 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbdecrypt.py
58 lines (52 loc) · 2.03 KB
/
dbdecrypt.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from Crypto.Cipher import AES
import base64
import sqlite3
import json, re, sys
def decrypt_record(key, encrypted):
unpad = lambda s : s[0:-s[-1]]
key = base64.b64decode(key)
encrypted = base64.b64decode(encrypted)
iv = encrypted[:16]
content = encrypted[16:]
mode = AES.MODE_CBC
encryptor = AES.new(key, mode, IV=iv)
data = encryptor.decrypt(content)
data = unpad(data)
load = json.loads(data.decode())
return load
def decrypt(filename, keys):
conn = sqlite3.connect(filename)
conn.row_factory = sqlite3.Row
cur = conn.cursor()
upd = conn.cursor()
keypairs = {}
for i in keys: keypairs[i['id']] = i['key']
cur.execute("SELECT * FROM sqlite_master WHERE type='table'")
tables = cur.fetchall()
notfound = []
count = 0
for i in tables:
if 'release_tag' in i['sql']:
print('Found Encrypted Table', i['tbl_name'])
cur.execute("SELECT _rowid_ as __rowid, * FROM " + i['tbl_name'] + " WHERE release_tag IS NOT NULL", )
data = cur.fetchall()
for item in data:
keyid = item['_encryption_release_id']
content = item['release_tag']
__rowid = item['__rowid']
if keyid in keypairs:
dec = decrypt_record(keypairs[keyid], content)
for col in dec:
val = dec[col] if dec[col] is not False else None
count += 1
upd.execute("UPDATE " + i['tbl_name'] + " SET " + col + " = ? WHERE _rowid_ = ?", (val, __rowid))
upd.execute("UPDATE " + i['tbl_name'] + " SET release_tag = ?, _encryption_release_id = ? WHERE _rowid_ = ?", (None, None, __rowid))
else:
if keyid not in notfound:
notfound.append(keyid)
conn.commit()
if len(notfound):
print('Failed to decrypt key', notfound)
print('Successfully Decypted %d Records' % (count))