-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLiteDBParser.py
executable file
·1807 lines (1596 loc) · 77.2 KB
/
SQLiteDBParser.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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# Name: SQLiteDBParser - Forensic SQLite Database Analyzer
# Purpose: Parses a sqlite database and tries to restore deleted records
# in unused pages, freespace and unallocated space.
#
# Author: GrisoMG
#
# References: n0fate: schema parser example https://github.com/n0fate/walitean
# Jim Hung: cellparser https://github.com/NotionalLabs/SQLiteZer
# Sho Nakatani (laysakura) handling of overflow pages https://github.com/laysakura/SQLiteDbVisualizer
#
# Limitations: sql command parser is not working 100% correctly
# matching of deleted pages to tables only based on column count
#
# output is not really nice
# pointer maps are not handled correctly
#
# Requires: Python 3
#
# Created: 19/08/2015
# License: GPL v2.0
#-------------------------------------------------------------------------------
__author__ = 'grisomg'
from struct import unpack
from optparse import OptionParser, OptionGroup
import sys, tempfile, hashlib
VERSION = '0.9'
BUILD = '20151112'
GPLNOTICE = ' Copyright (C) 2015 GrisoMG\n\
This program comes with ABSOLUTELY NO WARRANTY\n\
This is free software, and you are welcome to redistribute it\n\
under certain conditions'
JPGHEADER = b'\xff\xd8\xff\xe0'
JPGIDENT = b'JFIF\0'
JPGHEADERLEN = 12
EXIFHEADER = b'\xff\xd8\xff\xe1'
EXIFIDENT = b'Exif\0'
EXIFHEADERLEN = 12
MA4HEADER = b'\x10\x00\x00\x00\x1c\x66\x74'
MA4HEADERLEN = 7
MOVHEADER = b'\x00\x00\x00\x1C\x66\x74\x79\x70\x6D\x70\x34\x32'
MOVHEADERLEN = 12
BPLISTHEADER = b'\x62\x70\x6C\x69\x73\x74'
BPLISTHEADERLEN = 6
MP3HEADER = b'\x49\x44\x33' #with ID3v2 container
MP3HEADERLEN = 3
SQLITE_SIGNATURE = b'SQLite format 3\x00'
INTERIOR_INDEX_BTREE_PAGE = 2
INTERIOR_TABLE_BTREE_PAGE = 5
LEAF_INDEX_BTREE_PAGE = 10
LEAF_TABLE_BTREE_PAGE = 13
INTERIOR_OFFSET = 12
LEAF_OFFSET = 8
sql_type = ('table', 'trigger', 'index', 'view')
class CellContent:
LEFT_CHILD_PAGE_NUM = "left child page num"
PAYLOAD_SIZE = "payload size"
RID = "rid"
PAYLOAD = "payload"
OVERFLOW_PAGE_HEAD = "overflow page head"
#######################################################################################
#
# class SQLiteDBParser
#
#######################################################################################
class SQLiteDBParser:
#sqlite database header first 100 bytes
_dbhdrfrmt = '>16sHbbbbbbiiiiiiiiiiii20sii'
_dbhdrkeys = ["signature", "pageSize", "write_version", "read_version", "unused_reserved_space", "maximum_embedded_payload_fraction",
"minimum_embedded_payload_fraction", "leaf_payload_fraction", "file_change_counter", "in_header_database_size",
"first_freelist_trunk_page", "total_num_freelist_pages", "schema_cookie", "schema_format_number", "default_page_cache_size",
"largest_root_b_tree", "database_text_encoding", "user_version", "incremental_vacuum", "application_id", "reserved",
"version_valid_for_number", "sqlite_version_number"]
#header of a btree table
_lbtreefrmt = '>bhhhb' #b-tree header for leaf b-tree pages
_ibtreefrmt = '>I' #additional for interior b-tree pages
_btreehdrkeys = ['pageByte', 'fbOffset', 'cellQty', 'cellOffset', 'freebytes', 'rmpointer']
#header of pointer map
_ptrmapfrmt = '>bI'
_ptrmaphdrkeys = ['pageType', 'pageNr']
def __init__(self, options):
self.opt = {'sqlitedb': '', 'debug':False, 'bin2out':False, 'bin2file':False, 'freespace':False, 'unallocated':False, 'deleted':False}
self.opt['sqlitedb'] = options.infile
self.opt['debug'] = options.debug
self.opt['bin2out'] = options.bin2out
self.opt['bin2file'] = options.bin2file
self.opt['freespace'] = options.freespace
self.opt['unallocated'] = options.unallocated
self.opt['deleted'] = options.deleted
self.opt['verbose'] = False # future use :-)
self.data = b''
self.pageOffsets = []
self.dbInfo = dict()
self.dbHeaderDict = dict()
self.ptrMap = list()
self.dbSchema = {}
self.dbPages = []
self.lPagesWithoutRoot = []
self.overflowpages = []
if self.opt['bin2file']:
self.tmpdir = self._makeTmpDir()
# 1. read db file, parse header, check if valid sqlite database, parse schema, get page offsets
self._readDBFile()
self._parseDBHeader()
if self.isSqliteDB() == False:
return None
# self.dbSchema = self._parseDBSchema(self.data[:self.dbHeaderDict["pageSize"]])
self._getPageOffsets()
# 2. read all pages
self._readallDBPages()
self._markOverflowPages()
self.dbSchema = self._parseDBSchema(1)
self._setSchemaForRootPages()
# 3. are all leaf pages assigned to a root page? if not try to find a mapping root page by mapping schema
self._lPagesWithoutRoot()
def _markOverflowPages(self):
for page in self.dbPages:
if self.dbPages[page]['pageNr'] in self.overflowpages:
self.dbPages[page]['pageType'] = 'Overflow Page'
def isSqliteDB(self):
if self.dbHeaderDict["signature"] == SQLITE_SIGNATURE:
return True
else:
return False
def isRootPage(self, dbpage):
try:
if dbpage["isRootPage"] == True:
return True
except:
return False
def hasFreespace(self, dbpage):
try:
if dbpage["freespace"].__len__() > 0:
return True
except:
return False
def hasUnallocated(self, dbpage):
try:
if dbpage["unallocated"].__len__() > 0:
return True
except:
return False
def hasCelldata(self, dbpage):
try:
if dbpage["celldata"].__len__() > 0:
return True
except:
return False
def hasDeleted(self, dbpage):
try:
if dbpage["deletedpages"].__len__() > 0:
return True
except:
return False
def hasLeafPages(self, dbpage):
try:
if dbpage["leafpages"].__len__() > 0:
return True
except:
return False
def hasPtrMap(self):
ret = False
if self.ptrMap.__len__() > 0:
ret = True
return ret
def _lPagesWithoutRoot(self):
for page in self.dbPages:
schemalist = list()
if self.dbPages[page]["pageHeader"]["pageByte"] == LEAF_TABLE_BTREE_PAGE:
if self._findLPageinRPage(self.dbPages[page]["pageNr"]) == -1:
if self.dbPages[page]["pageHeader"]["cellQty"] > 0:
schemalist = self._findMatchingSchema(self.dbPages[page]["celldata"])
#add page to leafpages for root pages in schemalist
self._addLeafPage2RootPage(self.dbPages[page]["pageNr"], schemalist)
def _addLeafPage2RootPage(self, pageNr, schemalist):
for rootNr in schemalist:
try:
self.dbPages[rootNr]["deletedpages"].append(pageNr)
except:
try:
self.dbPages[rootNr]["deletedpages"] = list()
self.dbPages[rootNr]["deletedpages"].append(pageNr)
except:
pass
def _findMatchingSchema(self, celldata):
#compare number of columns with schema of tables
#col type are not checked at the moment ;-(
num_of_cols = celldata[0].__len__()
possibleschemalist = list()
for schema in self.dbSchema:
if not self.dbSchema[schema]['type'] == 'table':
continue
if num_of_cols == self.dbSchema[schema]["schema"].__len__():
possibleschemalist.append(self.dbSchema[schema]["rootpage"])
return possibleschemalist
def _findLPageinRPage(self, pageNr):
for page in self.dbPages:
if self.dbPages[page]["pageHeader"]["pageByte"] == INTERIOR_TABLE_BTREE_PAGE:
try:
if pageNr in self.dbPages[page]["leafpages"]:
return self.dbPages[page]["pageNr"]
except:
pass
return -1
def _findSQLCmd(self, buf):
open = 0
start = 0
end = 0
i = 0
bindata = False
buf = buf.replace('\n','')
try:
if chr(buf[0]):
bindata = True
except:
bindata = False
while i < buf.__len__():
if not bindata:
if buf[i] == '(':
if start == 0:
start = i
open += 1
if buf[i] == ')':
open -= 1
if open == 0:
end = i
break
else:
if chr(buf[i]) == '(':
if start == 0:
start = i
open += 1
if chr(buf[i]) == ')':
open -= 1
if open == 0:
end = i
break
i += 1
if end <= start:
return "ERROR"
else:
return buf[start+1:end]
def _setSchemaForRootPages(self):
for table in self.dbSchema:
if not isinstance(self.dbSchema[table]["rootpage"], int):
continue
if not self.dbSchema[table]['type'] == 'table':
continue
colheader = list()
tblinfo = dict()
for x in self.dbSchema[table]["schema"]:
colheader.append(x[0])
tblinfo[self.dbSchema[table]["name"]] = colheader
self.dbPages[self.dbSchema[table]["rootpage"]]["schema"] = tblinfo
self.dbPages[self.dbSchema[table]["rootpage"]]["isRootPage"] = True
def _getSchemaForRootPage(self, rootpage):
colheader = list()
tblinfo = dict()
for table in self.dbSchema:
if self.dbSchema[table]["rootpage"] == rootpage:
for x in self.dbSchema[table]["schema"]:
colheader.append(x[0])
tblinfo[self.dbSchema[table]["name"]] = colheader
return tblinfo
def _getPageOffsets(self):
for offset in range(0, len(self.data), self.dbHeaderDict["pageSize"]):
self.pageOffsets.append(offset)
def _lookUpTable(self, tbl_name):
for table in self.dbSchema:
if self.dbSchema[table]["name"] == tbl_name:
return self.dbSchema[table]["rootpage"]
return None
def _readallDBPages(self):
pageSize = self.dbHeaderDict["pageSize"]
pageNr = 0
pageDict = {}
for offset in self.pageOffsets:
dbpage = {}
pageNr += 1
dbpage = self._readDBPage(pageNr, offset)
pageDict[pageNr] = dbpage
self.dbPages = pageDict
offset += pageSize
def _readDBPage(self, pageNr, offset):
pageSize = self.dbHeaderDict["pageSize"]
pageHeader = []
counter = 0
dbpage = {}
page = self.data[offset: offset + pageSize]
if pageNr == 2 and self.dbHeaderDict["incremental_vacuum"] > 0:
#in this case, the page is a pointer map
self._readPointerMap(page)
pageHeader = self._parsePageHeader(page, pageNr)
dbpage["page"] = page
dbpage["pageNr"] = pageNr
dbpage["pageOffset"] = offset
dbpage["pageHeader"] = pageHeader
dbpage["isRootPage"] = False
dbpage["celldata"] = list()
dbpage["deleteddata"] = list()
dbpage["fs_celldata"] = list()
if dbpage["pageHeader"]["pageByte"] == INTERIOR_INDEX_BTREE_PAGE:
dbpage["pageType"] = "interior index b-tree"
if pageNr == 2:
counter += 1
elif dbpage["pageHeader"]["pageByte"] == INTERIOR_TABLE_BTREE_PAGE:
dbpage["pageType"] = "interior table b-tree"
if pageNr == 2:
counter += 1
elif dbpage["pageHeader"]["pageByte"] == LEAF_INDEX_BTREE_PAGE:
dbpage["pageType"] = "leaf index b-tree"
elif dbpage["pageHeader"]["pageByte"] == LEAF_TABLE_BTREE_PAGE:
dbpage["pageType"] = "leaf table b-tree"
elif pageNr == 2 and self.dbHeaderDict["incremental_vacuum"] > 0:
dbpage["pageType"] = "pointer map"
else:
dbpage["pageType"] = "Unknown"
if pageNr == 2 and (dbpage["pageType"] in (3,4)):
counter += 1
dbpage["cellPointer"] = self._readPageCellPointer(page, dbpage["pageHeader"], pageNr)
# if pageNr > 1 and dbpage["pageHeader"]["pageByte"] == INTERIOR_TABLE_BTREE_PAGE:
if dbpage["pageHeader"]["pageByte"] == INTERIOR_TABLE_BTREE_PAGE:
dbpage["leafpages"] = self._readLeafPageList(dbpage, offset)
dbpage["hasLeafPages"] = True
# if (dbpage["pageHeader"]["pageByte"] == LEAF_TABLE_BTREE_PAGE) and (dbpage["pageHeader"]["cellQty"] > 0):
if ((dbpage["pageHeader"]["pageByte"] == LEAF_TABLE_BTREE_PAGE) or (dbpage["pageHeader"]["pageByte"] == LEAF_INDEX_BTREE_PAGE) \
or (dbpage["pageHeader"]["pageByte"] == INTERIOR_TABLE_BTREE_PAGE) or (dbpage["pageHeader"]["pageByte"] == INTERIOR_INDEX_BTREE_PAGE)) and (dbpage["pageHeader"]["cellQty"] > 0):
dbpage["celldata"] = self._readPageCells(dbpage, offset)
dbpage["unallocated"] = self._readPageUnallocated(dbpage)
dbpage["freespace"], dbpage["fs_celldata"] = self._readPageFreeSpace(dbpage)
return dbpage
def _readPageCells(self, dbpage, offset):
celldatalist = list()
for cp in range(0,(dbpage["pageHeader"]["cellQty"]*2),2):
celldata = list()
start = cp
end = cp + 2
cellp = unpack('>H', dbpage["cellPointer"][start:end])[0]
cellstart = offset + cellp
celldata, payloadlen = self._parseCell(self.data, cellstart, dbpage["pageHeader"]["pageByte"])
celldatalist.append(celldata)
return celldatalist
def _readLeafPageList(self, dbpage, offset):
leafpagelist = list()
offset = offset
for cp in range(0,(dbpage["pageHeader"]["cellQty"]*2),2):
start = cp
end = cp + 2
cellp = unpack('>h', dbpage["cellPointer"][start:end])[0]
cellstart = offset + cellp
leftchildpointer = unpack('>L', self.data[cellstart:cellstart+4])[0]
#key = _getVarIntOfs(data, offset+4)
leafpagelist.append(leftchildpointer)
leafpagelist.append(dbpage["pageHeader"]["rmpointer"])
return leafpagelist
def _readPageFreeSpace(self, dbpage):
fbOffset = dbpage["pageHeader"]["fbOffset"]
freeblocklist = list()
fs_data = list()
fs_celldata = list()
#fs_record = ''
rs_offset = 2
while fbOffset > 0:
try:
start, size = unpack('>hh', dbpage["page"][fbOffset: fbOffset + 4])
if size > 0:
freeblock = dbpage["page"][fbOffset: fbOffset + size]
fs_data, payloadlen = self._parseFreeSpaceCell(freeblock, 4, dbpage["pageHeader"]["pageByte"])
else:
freeblock = ''
freeblocklist.append(freeblock)
fs_celldata.append(fs_data)
if (fbOffset != start) and (start > 0):
fbOffset = start
else:
fbOffset = 0
'''
fbOffset = start
'''
except:
fbOffset = 0
return freeblocklist, fs_celldata
def _parseFreeSpaceCell(self, data, offset, cellformat):
'''
Work in progress
:rtype: list
'''
fs_celldata = list()
#fs_record, payloadlen = self._parseCell(data, offset, cellformat)
cellheader, payloadheaderlen, dataoffset, payloadlen, recordnum, payloadsizeincell, overflowpageoffset,overflowpagenum = self._parseLeafTableCellHeader(data, offset, freespace=True)
payload = data[dataoffset:]
if (overflowpagenum > 0) and (overflowpagenum is not None):
payload += self._getoverflowdata(overflowpagenum)
data = payload
dataoffset = 0
for field in cellheader:
dataoffset = int(dataoffset)
if field[0] == "NULL":
fs_celldata.append(recordnum)
elif field[0] == "ST_INT8":
fs_celldata.append(ord(unpack(">c",data[dataoffset:dataoffset+1])[0]))
dataoffset+=field[1]
elif field[0] == "ST_INT16":
fs_celldata.append(unpack(">h",data[dataoffset:dataoffset+2])[0])
dataoffset+=field[1]
elif field[0] == "ST_INT24":
fs_celldata.append("-")
dataoffset+=field[1]
elif field[0] == "ST_INT32":
fs_celldata.append(unpack(">i",data[dataoffset:dataoffset+4])[0])
dataoffset+=field[1]
elif field[0] == "ST_INT48":
fs_celldata.append(unpack(">i",data[dataoffset:dataoffset+6])[0])
dataoffset+=field[1]
#fs_celldata.append("ST_INT48 - NOT IMPLEMENTED!") # NOT IMPLEMENTED YET!
#dataoffset+=field[1]
elif field[0] == "ST_INT64":
fs_celldata.append(unpack(">q",data[dataoffset:dataoffset+8])[0])
dataoffset+=8
elif field[0] == "ST_FLOAT":
fs_celldata.append(unpack(">d",data[dataoffset:dataoffset+8])[0])
dataoffset+=8
elif field[0] == "ST_C0":
fs_celldata.append("-")
elif field[0] == "ST_C1":
fs_celldata.append("-")
elif field[0] == "ST_BLOB":
cell = data[dataoffset:dataoffset+int(field[1])]
fs_celldata.append(cell)
dataoffset+=field[1]
elif field[0] == "ST_TEXT":
try:
fs_celldata.append(data[dataoffset:dataoffset+int(field[1])].decode('UTF-8'))
except:
try:
fs_celldata.append(str(data[dataoffset:dataoffset+int(field[1])]))
except:
pass
dataoffset+=field[1]
else:
pass
#print(field[0])
return fs_celldata, payloadlen
def _readPageUnallocated(self, dbpage):
if dbpage["pageNr"] == 1 and dbpage["pageHeader"]["pageByte"] != INTERIOR_TABLE_BTREE_PAGE:
start = 108 + dbpage["pageHeader"]["cellQty"] * 2
elif dbpage["pageNr"] == 1 and dbpage["pageHeader"]["pageByte"] == INTERIOR_TABLE_BTREE_PAGE:
start = 112 + dbpage["pageHeader"]["cellQty"] * 2
elif dbpage["pageHeader"]["pageByte"] == INTERIOR_TABLE_BTREE_PAGE:
start = 12 + dbpage["pageHeader"]["cellQty"] * 2
else:
start = 8 + dbpage["pageHeader"]["cellQty"] * 2
end = dbpage["pageHeader"]["cellOffset"] - start
return dbpage["page"][start:end]
def _readPageCellPointer(self, page, pageHeader, pageNr):
cellPointer = 0
if pageHeader["cellQty"] > 0:
if pageNr == 1 and pageHeader["pageByte"] != INTERIOR_TABLE_BTREE_PAGE:
start = 108
elif pageNr == 1 and pageHeader["pageByte"] == INTERIOR_TABLE_BTREE_PAGE:
start = 112
elif pageHeader["pageByte"] == INTERIOR_TABLE_BTREE_PAGE:
start = 12
else:
start = 8
end = start + (pageHeader["cellQty"] * 2)
cellPointer = page[start:end]
return cellPointer
def _readPointerMap(self, page):
ptrMapLen = 5
offset = 0
ptrmapdict = dict()
counter = 0
while offset < (self.dbHeaderDict["pageSize"]- ptrMapLen):
hdr = unpack(self._ptrmapfrmt, page[offset:offset+ptrMapLen])
if hdr[0] == 0 and hdr[1] == 0:
break #no more pointers will follow
'''
0x01 0x00 0x00 0x00 0x00
This record relates to a B-tree root page which obviously does not have a parent page, hence the page number being indicated as zero.
0x02 0x00 0x00 0x00 0x00
This record relates to a free page, which also does not have a parent page.
0x03 0xVV 0xVV 0xVV 0xVV (where VV indicates a variable)
This record relates to the first page in an overflow chain. The parent page number is the number of the B-Tree page containing the B-Tree cell to which the overflow chain belongs.
0x04 0xVV 0xVV 0xVV 0xVV (where VV indicates a variable)
This record relates to a page that is part of an overflow chain, but not the first page in that chain. The parent page number is the number of the previous page in the overflow chain linked-list.
0x05 0xVV 0xVV 0xVV 0xVV (where VV indicates a variable)
This record relates to a page that is part of a table or index B-Tree structure, and is not an overflow page or root page. The parent page number is the number of the page containing the parent tree node in the B-Tree structure.
'''
counter += 1
ptrmapdict = dict(zip(self._ptrmaphdrkeys,list(hdr)))
self.ptrMap.append(ptrmapdict)
offset += ptrMapLen
def _readDBFile(self):
try:
f=open(self.opt['sqlitedb'],"rb")
self.data = f.read()
except:
print ("File not Found")
self.data = None
def _parsePageHeader(self, page, pageNr):
pageDict = dict ()
if pageNr == 1:
pageHeader = unpack(self._lbtreefrmt, page[100:108])
else:
pageHeader = unpack(self._lbtreefrmt, page[:8])
pageByte, fbOffset, cellQty, cellOffset, freebytes = pageHeader
if pageByte == INTERIOR_TABLE_BTREE_PAGE:
if pageNr == 1:
rmpointer = unpack(self._ibtreefrmt, page[108:112])[0]
else:
rmpointer = unpack(self._ibtreefrmt, page[8:12])[0]
else:
rmpointer = None
pageHeader = list(pageHeader)
pageHeader.append(rmpointer)
pageDict = dict(zip(self._btreehdrkeys, pageHeader))
return pageDict
def _parseDBHeader(self):
self.dbHeaderDict = dict(zip(self._dbhdrkeys,list(self._unpackDBHeader())))
def _parseDBSchema(self, pageNum):
# based on https://github.com/n0fate/walitean
TABLE = b'table'
CREATETABLE = b'CREATE TABLE'
buf = b''
dbschema = {}
columnsdic = {}
tables = []
dbtable = {}
page = self.dbPages[int(pageNum)]
tables = page["celldata"]
if self.hasLeafPages(page) == True:
for leafpage in page["leafpages"]:
if leafpage < self.dbHeaderDict["in_header_database_size"]:
if self.hasCelldata(self.dbPages[leafpage]):
tables += self.dbPages[leafpage]["celldata"]
for table in tables:
columnlst = []
dbtable = {}
strcolumns = None
if not table[0] in sql_type:
continue
dbtable['type'] = table[0]
dbtable['name'] = table[1]
dbtable['rootpage'] = table[3]
if dbtable['type'] == 'table':
strcolumns = self._findSQLCmd(table[4])
if strcolumns == "ERROR":
continue
l = strcolumns.find('UNIQUE (')
r = strcolumns.find(')')
if l > 0 and r > l:
strcolumns = strcolumns[:l-1] + strcolumns[r+1:]
# if strcolumns[0] == ' ': # remove byte if first byte is space
# strcolumns = strcolumns[1:]
strcolumns = strcolumns.lstrip(' ')
strcolumns = strcolumns.rstrip(' ')
strcolumns = strcolumns.replace(' REFERENCES','')
for column in strcolumns.split(','):
column = column.lstrip(' ')
column = column.replace('"','')
if column == '':
continue
if column[0] == ' ':
column = column[1:]
if str(column).startswith('PRIMARY') or str(column).startswith('UNIQUE'):
continue
try:
column.index('UNIQUE (')
continue
except ValueError:
pass
columninfo = []
columnname = ""
columntype = ""
if len(column.split(' ')) >= 2:
columnname = column.split(' ')[0]
columntype = column.split(' ')[1]
columninfo.append(columnname)
columninfo.append(columntype)
if columninfo.__len__() != 0:
columnlst.append(columninfo)
if len(columnlst):
dbtable['schema'] = columnlst
else:
dbtable['schema'] = []
columnsdic[dbtable['name']] = dbtable
return columnsdic
def _unpackDBHeader(self):
try:
dbheader = unpack(self._dbhdrfrmt, self.data[:100])
except:
dbheader = "No valid SQLite database"
return dbheader
def _remove_ascii_non_printable(self, chunk):
chunk = ' '.join(chunk .split())
return ''.join([ch for ch in chunk if ord(ch) > 31 and ord(ch) < 126 or ord(ch) ==9])
def _remove_non_printable(self, chunk):
ret = ""
for ch in chunk:
if ch > 31 and ch < 126 or ch == 9:
ret = ret + chr(ch)
return ret.strip()
def _makeTmpDir(self):
try:
tmpdir = tempfile.mkdtemp()
except:
tmpdir = ""
return tmpdir
def _writeBinary (self,name, data):
try:
ext = self._filetype(data[0:12])
destname = self.tmpdir + "/" + str(name) + "." + ext
with open(destname, 'wb') as output_file:
output_file.write(data)
output_file.close()
except:
destname = ""
return destname
def _filetype(self, header):
if ((header[:4] == JPGHEADER) and (header[6:] != JPGIDENT)) or ((header[:4] == EXIFHEADER) and (header[6:] != EXIFIDENT)):
return 'jpg'
elif (header[0:MA4HEADERLEN] == MA4HEADER):
return 'ma4'
elif (header[0:MOVHEADERLEN] == MOVHEADER):
return 'mov'
elif (header[0:BPLISTHEADERLEN] == BPLISTHEADER):
return 'bplist'
elif (header[0:MP3HEADERLEN] == MP3HEADER):
return 'mp3'
else:
return 'bin'
def printDBheader(self):
if self.dbHeaderDict["pageSize"] == 1:
print("Page size in bytes:".ljust(35,' ') + "65536")
else:
print("Page size in bytes:".ljust(35,' ') + "%8s" %str(self.dbHeaderDict["pageSize"]))
print("write_version:".ljust(35, ' ') + "%8s" %str(self.dbHeaderDict["write_version"]))
print("read_version:".ljust(35, ' ') + "%8s" %str(self.dbHeaderDict["read_version"]))
print("unused_reservered_space:".ljust(35, ' ') + "%8s" %str(self.dbHeaderDict["unused_reserved_space"]))
print("maximum_embedded_payload_fraction:".ljust(35, ' ') + "%8s" %str(self.dbHeaderDict["maximum_embedded_payload_fraction"]))
print("minimum_embedded_payload_fraction:".ljust(35, ' ') + "%8s" %str(self.dbHeaderDict["minimum_embedded_payload_fraction"]))
print("leaf_payload_fraction:".ljust(35, ' ') + "%8s" %str(self.dbHeaderDict["leaf_payload_fraction"]))
print("file_change_counter:".ljust(35, ' ') + "%8s" %str(self.dbHeaderDict["file_change_counter"]))
print("in_header_database_size:".ljust(35, ' ') + "%8s" %str(self.dbHeaderDict["in_header_database_size"]))
print("first_freelist_trunk_page:".ljust(35, ' ') + "%8s" %str(self.dbHeaderDict["first_freelist_trunk_page"]))
print("total_num_freelist_pages:".ljust(35, ' ') + "%8s" %str(self.dbHeaderDict["total_num_freelist_pages"]))
print("schema_cookie:".ljust(35, ' ') + "%8s" %str(self.dbHeaderDict["schema_cookie"]))
if self.dbHeaderDict["schema_format_number"] == 1:
sf = "SQLite v3.0.0"
elif self.dbHeaderDict["schema_format_number"] == 2:
sf = "SQLite v3.1.3 (2005)"
elif self.dbHeaderDict["schema_format_number"] == 3:
sf = "SQLite v3.1.4 (2005)"
elif self.dbHeaderDict["schema_format_number"] == 4:
sf = "SQLite v3.3.0 (2006) or higher"
else:
sf = "Invalid Schema Format!"
print("schema_format_number:".ljust(35, ' ') + "%8s (%s)" %(str(self.dbHeaderDict["schema_format_number"]),sf))
print("default_page_cache_size:".ljust(35, ' ') + "%8s" %str(self.dbHeaderDict["default_page_cache_size"]))
print("largest_root_b_tree:".ljust(35, ' ') + "%8s" %str(self.dbHeaderDict["largest_root_b_tree"]))
if self.dbHeaderDict["database_text_encoding"] == 1:
te = "UTF-8"
elif self.dbHeaderDict["database_text_encoding"] == 2:
te = "UTF-16le"
elif self.dbHeaderDict["database_text_encoding"] == 3:
te = "UTF-16be"
else:
te = "Invalid Text Encoding!"
print("database_text_encoding:".ljust(35, ' ') + "%8s (%s)" %(str(self.dbHeaderDict["database_text_encoding"]),te))
print("user_version".ljust(35, ' ') + "%8s" %str(self.dbHeaderDict["user_version"]))
print("incremental_vacuum".ljust(35, ' ') + "%8s" %str(self.dbHeaderDict["incremental_vacuum"]))
print("application_id".ljust(35, ' ') + "%8s" %str(self.dbHeaderDict["application_id"]))
#print("reserved".ljust(35, ' ') + "%8s" %str(self.dbHeaderDict["reserved"]))
print("version_valid_for_number".ljust(35, ' ') + "%8s" %str(self.dbHeaderDict["version_valid_for_number"]))
print("sqlite_version_number:".ljust(35, ' ') + "%8s (%s)" %(str(self.dbHeaderDict["sqlite_version_number"]),str(self.dbHeaderDict["sqlite_version_number"]).replace("00","0").replace("0",".")))
if self.opt['debug']:
print('\n##################################################################################\n')
#print("%4i %10s %45s %8s %23s %5s" %(i,str(pageNr), str(tbl_name), str(tbl_type), str(pageType), str(col_count)))
for page in self.dbPages:
print("Page Nr: %3s\tPage offset: %8s\tPage Type: %20s" %(str(self.dbPages[page]['pageNr']), str(self.dbPages[page]['pageOffset']), str(self.dbPages[page]['pageType'])))
if self.dbPages[page]['isRootPage'] == True:
print("Is Root Page: %5s" %(self.dbPages[page]['isRootPage']))
if page == 1:
phOffset = 100
else:
phOffset = 0
if self.dbPages[page]["pageHeader"]["pageByte"] == INTERIOR_TABLE_BTREE_PAGE:
phlen = 12
else:
phlen = 8
print("Page header\tOffset: %8s (%s)\tLen: %4s" %(str(self.dbPages[page]['pageOffset']),str(phOffset), str(phlen)))
print("\t{0:25s} {1:>5s}".format("Flag:", str(self.dbPages[page]["pageHeader"]["pageByte"])))
print("\t{0:25s} {1:>5s}".format("First free block:", str(self.dbPages[page]["pageHeader"]["fbOffset"])))
print("\t{0:25s} {1:>5s}".format("No of cells:", str(self.dbPages[page]["pageHeader"]["cellQty"])))
print("\t{0:25s} {1:>5s}".format("First byte of content:", str(self.dbPages[page]["pageHeader"]["cellOffset"])))
print("\t{0:25s} {1:>5s}".format("Fragmented byte count:", str(self.dbPages[page]["pageHeader"]["freebytes"])))
if self.dbPages[page]["pageHeader"]["rmpointer"] == None:
rmp = ""
else:
rmp = self.dbPages[page]["pageHeader"]["rmpointer"]
print("\t{0:25s} {1:>5s}".format("Right most pointer:", str(rmp)))
if self.dbPages[page]["pageHeader"]["cellQty"] > 0 and self.dbPages[page]['pageType'] != "Overflow Page":
print("\n\t{0:23s} {1:>5s}".format("Cell pointer array:", "")) #str(self.dbPages[page]["pageHeader"]["cellQty"])))
for cp in range(0,(self.dbPages[page]["pageHeader"]["cellQty"]*2),2):
start = cp
end = cp + 2
cellp = unpack('>H', self.dbPages[page]["cellPointer"][start:end])[0]
cellstart = self.dbPages[page]['pageOffset'] + cellp
print("\t\t{0:23s} {1:>5s}".format("Cell pointer:", str(cellp)))
if (self.dbPages[page]["pageHeader"]["pageByte"] == LEAF_TABLE_BTREE_PAGE):
cellheader, payloadheaderlen, dataoffset, payloadlen, recordnum, payloadsizeincell, overflowpageoffset,overflowpagenum = self._parseLeafTableCellHeader(self.data, cellstart, freespace=False)
print("\t\t\t{0:15s} {1:>5s}".format("Payload length:", str(payloadlen)))
print("\t\t\t{0:15s} {1:>5s}".format("Row ID:", str(recordnum)))
print("\t\t\tPayload")
print("\t\t\t\t{0:22s} {1:>8s}".format("Payload header len:", str(payloadheaderlen)))
print("\t\t\t\t{0:22s} {1:>8s}".format("Data offset:", str(dataoffset)))
for cell in cellheader:
#print("\t\t\t\t{0:22s} {1:>8s} {2:12s} {3:>8s}".format("Cell type:", str(cell[0]), "Cell size:", str(cell[1])))
print("\t\t\t\t{0:22s} {1:>8s} ({2:s})".format("Cell type:", str(cell[0]), str(cell[1])))
print("\t\t\t\t{0:22s} {1:>8s}".format("Overflow page offset:", str(overflowpageoffset)))
print("\t\t\t\t{0:22s} {1:>8s}".format("Overflow page num:", str(overflowpagenum)))
elif (self.dbPages[page]["pageHeader"]["pageByte"] == LEAF_INDEX_BTREE_PAGE):
cellheader,payloadheaderlen,dataoffset,payloadlen,overflowpageoffset,overflowpagenum = self._parseLeafIndexCellHeader(self.data, self.dbPages[page]['pageOffset'])
print("\t\t\t{0:15s} {1:>5s}".format("Payload length:", str(payloadlen)))
print("\t\t\tPayload")
print("\t\t\t\t{0:22s} {1:>8s}".format("Payload header len:", str(payloadheaderlen)))
print("\t\t\t\t{0:22s} {1:>8s}".format("Data offset:", str(dataoffset)))
for cell in cellheader:
#print("\t\t\t\t{0:22s} {1:>8s} {2:12s} {3:>8s}".format("Cell type:", str(cell[0]), "Cell size:", str(cell[1])))
print("\t\t\t\t{0:22s} {1:>8s} ({2:s})".format("Cell type:", str(cell[0]), str(cell[1])))
print("\t\t\t\t{0:22s} {1:>8s}".format("Overflow page offset:", str(overflowpageoffset)))
print("\t\t\t\t{0:22s} {1:>8s}".format("Overflow page num:", str(overflowpagenum)))
elif (self.dbPages[page]["pageHeader"]["pageByte"] == INTERIOR_TABLE_BTREE_PAGE):
cellheader, dataoffset, pagechildnum, recordnum = self._parseInteriorTableCellHeader(self.data,cellstart)
print("\t\t\t{0:15s} {1:>8s}".format("Left child:", str(pagechildnum)))
print("\t\t\t{0:15s} {1:>8s}".format("Row ID:", str(recordnum)))
for cell in cellheader:
#print("\t\t\t\t{0:22s} {1:>8s} {2:12s} {3:>8s}".format("Cell type:", str(cell[0]), "Cell size:", str(cell[1])))
print("\t\t\t\t{0:22s} {1:>8s} ({2:s})".format("Cell type:", str(cell[0]), str(cell[1])))
if self.hasLeafPages(self.dbPages[page]) == True:
print("Leaf pages: %s" %(self.dbPages[page]['leafpages']))
if self.hasDeleted(self.dbPages[page]) == True:
print("Deleted pages: %s" %(self.dbPages[page]['deletedpages']))
print('\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n')
def printPtrMap(self):
if self.ptrMap.__len__() > 0:
print("Database has a pointer map...")
pos = 0
for ptrmap in self.ptrMap:
idx = ptrmap['pageNr']
rp = ptrmap['pageType']
print("idx: %s\tpage: %s\troot page: %s" %(str(pos),str(pos+3),str(rp)))
pos += 1
def printDBSchema(self):
print("Parsed database schema...")
i=0
for dbtable in self.dbSchema:
i+=1
print("(%i) Table: %s" %(i,str(dbtable)))
print("\tTable name: %s" %str(self.dbSchema[dbtable]['name']))
print("\tRoot page: %s" %str(self.dbSchema[dbtable]['rootpage']))
if self.dbSchema[dbtable]['type']== 'table':
print("\tSchema:")
for key, value in self.dbSchema[dbtable]['schema']:
print("\t\t %s:\t%s" %(str(key),str(value)))
def printDBData(self):
for ipage in self.dbPages:
if ipage == 1 or self.dbPages[ipage]['pageType'] == "Overflow Page":
continue
self.printTable(number=ipage)
def printTable(self, name=None, number=None):
page = None
schema = {}
tblname = "???"
colheader = "???"
if name == None and number == None:
return
if name is not None:
number = self._lookUpTable(name)
if number is not None:
try:
page = self.dbPages[int(number)]
except:
page = None
if page is None:
return
if self.isRootPage(page) == True:
try:
tblname, colheader = page["schema"].popitem()
schema = self.dbSchema[tblname]['schema']
except:
tblname = "???"
colheader = "???"
print("PageNr: %s\tTable name: %s" %(str(page["pageNr"]),str(tblname)))
hdr = "Page;Type;"
hdr += ";".join(map(str,colheader))
hdr += ";MD5 hash"
print(hdr)
#if the page has leafpages, the page cells contain only the pointer to the leafpages
if self.hasCelldata(page) == True:
rownum = 0
for row in page["celldata"]:
rownum += 1
rowdata = str(page["pageNr"]) + ";C"
md5 = ''
i=0
for cell in row:
fname = ''
try:
if (schema[i][1] == "BLOB"):
if (self.opt['bin2out']):
rowdata += ";'" + str(cell) + "'"
else:
rowdata += ";"
if (self.opt['bin2file']):
fname = self._writeBinary(tblname+"_"+str(page["pageNr"])+"_"+str(rownum)+"_"+str(i), cell)
if (fname != "") and not self.opt['bin2out']:
rowdata += "'" + fname + "'"
else:
rowdata += ";'" + str(cell) + "'"
except:
rowdata += ";'" + str(cell) + "'"
i+=1
md5 = hashlib.md5()
rd = ''.join(str(v) for v in row)
md5.update(rd.encode('utf-8'))
print(rowdata + ";" +str(md5.hexdigest()))
if self.opt['freespace']:
for freespace in page["freespace"]:
if self.opt['debug'] == True:
if self.opt['verbose'] == True:
print(str(page["pageNr"]) + ";F;'';" + "'" + freespace + "'")
else:
print(str(page["pageNr"]) + ";F;'';" + "'" + self._remove_non_printable(freespace) + "'")
for element in page["fs_celldata"]:
rownum += 1
rowdata = str(page["pageNr"]) + ";FC"
i=0
for cell in row:
rowdata += ";"
try:
if (schema[i][1] == "BLOB"):
if (self.opt['bin2out']):
rowdata += "'" + str(cell) + "'"
#else:
# rowdata += ";"
if (self.opt['bin2file']):
fname = self._writeBinary(tblname+"_"+str(page["pageNr"])+"_"+str(rownum)+"_"+str(i), cell)
if (fname != "") and not self.opt['bin2out']:
rowdata += "'" + fname + "'"
else:
rowdata += "'" + str(cell) + "'"
except:
rowdata += "'" + str(cell) + "'"
i+=1
md5 = hashlib.md5()
rd = ''.join(str(v) for v in row)
md5.update(rd.encode('utf-8'))
print(rowdata + ";" +str(md5.hexdigest()))
if self.opt['unallocated']:
if self.opt['verbose'] == True:
print(str(page["pageNr"]) + ";U;'';" + "'" + page["unallocated"] + "'")
else:
data = self._remove_non_printable(page["unallocated"])
if data != "":
print(str(page["pageNr"]) + ";U;'';" + "'" + data + "'")
if self.hasLeafPages(page) == True:
for leafpage in page["leafpages"]:
if self.hasCelldata(self.dbPages[leafpage]) == True:
rownum = 0