-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadleveldb.py
168 lines (137 loc) · 5.99 KB
/
readleveldb.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
import plyvel
import os
import gzip
# write force str(key,value)
def _writeforcestrkv(str4bytesfile, it):
with open(str4bytesfile, mode='w', encoding='utf-8') as f:
print('=====================Begin====================')
f.write('=====================Begin====================\n')
i = 1
for key, value in it:
num = 'Record ' + str(i) + '\n'
f.write(num)
try:
recordline = 'key: ' + str(key) + '\r\n' + 'value:' + str(value) + '\r\n'
print(i)
print(recordline)
f.writelines(recordline)
except Exception as e:
estr = 'Record ' + str(i) + ', key:' + key.decode('utf-8') + ', decode wrong. ' + str(e)
print(estr)
f.writelines(estr)
try:
undecodelist.append((key, value))
except Exception as e:
print(str(i) + ', key:' + key.decode('utf-8') + ', insert into list wrong. ' + str(e))
# continue
i += 1
f.write('=====================end====================')
print('=====================end====================')
# write faildecoded key,value
def _writefaildecodedkv(undecodedfile, undecodelist):
j = 1
print('Begin print undecoded record, total:{num} \n'.format(num=len(undecodelist)))
with open(undecodedfile, mode='w') as f:
try:
for record in undecodelist:
print('record{j}:\n'.format(j=j))
key = record[0]
value = record[1]
print(key, ':', value, '\n')
keystr = key.decode('utf-8')
valuestr = value.decode('utf-8')
#valuestr = value.decode('ASCII')
f.write('Begin write undecoded record:\n', encoding='utf-8')
f.write('Record' + str(j) + ':\n', encoding='utf-8')
f.write(keystr + ':::' + valuestr)
j += 1
print('End print undecoded record.')
except Exception as e:
estr = str(j) + ', key:' + key.decode('utf-8') + ', write into file wrong. ' + str(e)
print(estr)
# write decoded key,value
def _writedecodedkv(decodedfile, it):
undecodelist = [] # fail decoded list
with open(decodedfile, mode='w') as f:
print('=====================Begin====================')
f.write('=====================Begin====================')
i = 1
for key, value in it:
num = 'Record ' + str(i) + '\n'
f.write(num)
try:
keystr = key.decode('utf-8')
valuestr = value.decode('utf-8')
print(i)
#if (keystr.find('whatsapp') > -1):
recordline = 'key: ' + keystr + '\n' + 'value:' + valuestr + '\n'
print(recordline)
f.write(recordline)
except Exception as e:
estr = str(i) + ', key: ' + keystr + '\n' + 'value:' + str(value) + '\n' ', decode wrong. ' + str(e)
print(estr)
f.writelines(estr)
try:
undecodelist.append((key, value))
except Exception as e:
print(str(i) + ', key:' + key.decode('utf-8') + ', insert into list wrong. ' + str(e))
# continue
i += 1
f.write('=====================end====================')
print('=====================end====================')
return undecodelist
# write decoded whatsapp key,value
def _writedecodedwhatsappkv(it):
undecodelist = [] # fail decoded list
with open(decodedfile, mode='w', encoding='utf-8') as f:
print('=====================Begin====================')
f.write('=====================Begin====================')
i = 1
for key, value in it:
num = 'Record ' + str(i) + '\n'
f.write(num)
try:
# keystr = key.decode('utf-8')
# truevalue = value.split(b'\x01')[0]
# valuestr = truevalue.decode('utf-8')
keystr = bytes.decode(key, encoding='utf-8')
print(i)
print('key: ' + str(key) + '\r\n' + 'value:' + str(value) + '\r\n')
if (keystr.find('whatsapp') > -1):
valuestr = bytes.decode(value, encoding='utf-8')
recordline = 'key: ' + keystr + '\n' + 'value:' + valuestr + '\n'
f.writelines(recordline)
except Exception as e:
estr = str(i) + ', key:' + key.decode('utf-8') + ', decode wrong. ' + str(e)
print(estr)
f.writelines(estr)
try:
undecodelist.append((key, value))
except Exception as e:
print(str(i) + ', key:' + key.decode('utf-8') + ', insert into list wrong. ' + str(e))
# continue
i += 1
f.write('=====================end====================')
print('=====================end====================')
return undecodelist
if __name__ == '__main__':
path0 = os.getcwd() # get current path
path = os.path.split(os.path.realpath(__file__))[0]
dbpath = path + "/Local Storage/leveldb";
str4bytesfile = path + '/str4bytesCookieContent.dat'
decodedfile = path + '/decodedCookieContent.dat'
undecodedfile = path + '/undecodedCookieContent.dat'
#get leveldb object
db = plyvel.DB(dbpath)
#get iterator
it = db.iterator()
#read key,value (type is bytes); decode by utf-8 ; write into file
undecodelist = _writedecodedkv(decodedfile, it)
it.close()
it = db.iterator()
# read key,value (type is bytes); force convert to str ; write into file
_writeforcestrkv(str4bytesfile, it)
it.close()
db.close()
# write fail decoded key,value pair into file. (a lot of hex ,eg. x08xa1xe3...)
_writefaildecodedkv(undecodedfile, undecodelist)