-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPdfInspector.py
661 lines (645 loc) · 23.8 KB
/
PdfInspector.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
#!/usr/bin/python3
############################################################################
#
# Pdf Inspector [ Main Program ]
# © 2020 ABDULKADİR GÜNGÖR All Rights Reserved
# Contact email address: [email protected]
#
# Developper: Abdulkadir GÜNGÖR ([email protected])
# Date: 08/2020
# All Rights Reserved (Tüm Hakları Saklıdır)
#
############################################################################
from lib.pdf.PdfFile import PdfFile as pdfFile
from lib.pdf.pdfshow.PdfShow import PdfShow as pdfShow
from lib.pdf.pdfanalyze.CheckObjBetik import checkObjBetik as pdfBetik
from lib.hexlib.ShowHex import show2HexBytes as s2hex
import os
# Ekranı Temizlemek için
def screenClear():
print("\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n") # Konsol dışı kullanımda ekranı temizlemek için
# clear screen
# ********************
# Windows
if os.name=='nt':
os.system('cls')
# Linux or MAC
elif os.name=='posix':
os.system('clear')
# ********************
print("")
# Giriş Menüsü
def menu0() -> str:
while True:
screenClear()
print()
print('\t#####################################################')
print('\t#/*************************************************\#')
print('\t#**||~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~||**#')
print('\t#**|| ||**#')
print('\t#**|| Pdf Inspector V2.3 ||**#')
print('\t#**|| 08/2020 ||**#')
print('\t#**|| ||**#')
print('\t#**|| Developer: Abdulkadir GÜNGÖR ||**#')
print('\t#**|| ([email protected]) ||**#')
print('\t#**|| ||**#')
print('\t#**||~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~||**#')
print('\t#\*************************************************/#')
print('\t#####################################################')
print()
print()
print( '\tÇıkış (Exit) <<e>>' )
tmp=input('\tFile Name : ')
if len(tmp.strip()) > 3:
return tmp.strip()
elif tmp.strip().lower() == 'e' :
exit(0)
# Ön yükleme menüsü
def menu1(Filename:str) -> pdfFile:
screenClear()
pdffile = pdfFile(filename=Filename)
pdffile.process()
print()
if pdffile.c1_checkNormal:
return pdffile
else:
print()
print()
input('\tGeri (Back) <<enter>> : ')
return pdffile
#
def menu2() -> int:
while True:
#
screenClear()
txt1_format = '\t{:<25}'
txt2_format = '\t{:^25}'
print('')
print( txt2_format.format('MENU') )
print( txt1_format.format( '-'*25 ) )
print( txt1_format.format('1) Pdf Summary') )
print( txt1_format.format('2) Check Pdf Tags'))
print( txt1_format.format('3) * Show Object (Normal - use Obj Header)') )
print( txt1_format.format('4) Show Object (Stream Summary)'))
print(txt1_format.format( '5) Show Object (Normal)'))
print( txt1_format.format('6) Show Object (Hex)') )
print( txt1_format.format('7) Show Stream (Normal)') )
print( txt1_format.format('8) Show Stream (Hex)') )
print( txt1_format.format('9) Write Stream '))
print( txt1_format.format('10) Show Xref (Hex)' ) )
print( txt1_format.format('11) Show Trailer (Hex)') )
print( txt1_format.format('12) Show StartXref (Hex)'))
print()
print()
print('\tÇıkış (Exit) <<e>> , Geri (Back) <<b>>')
selection = input('\tSeçim (Selection) : ')
if selection.strip().lower() == 'e' :
exit(0)
elif selection.strip().lower() == 'b' :
return -1
elif selection.strip().isdigit():
result = int(selection.strip())
if 0 < result < 13:
return result
#
#
def menu3( file:pdfFile ) :
screenClear()
file.show()
print()
print()
input('\tDevam (Continue) <<enter>> : ')
def menu4( fileshow: pdfShow):
objs_length = len(fileshow.o1_pdffile.p4_objs)
selection = ''
while True:
screenClear()
if objs_length == 0:
print()
print('\t<<obj-endobj>> bulunamadı.')
print()
print()
input('\tGeri (Back) <<enter>> : ')
break
elif objs_length == 1:
print()
print('\t<<obj-endobj>> 1 adet bulundu.')
print('\t No : 0 ')
print()
print()
print( '\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>' )
selection = input('\t(Seçim) Selection No : ')
elif objs_length > 1:
print()
print('\t<<obj-endobj>> {} adet bulundu.'.format(objs_length))
print('\t No : 0 - {}'.format(objs_length-1) )
print()
print()
print( '\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>' )
selection = input('\t(Seçim) Selection No : ')
#
if selection.strip().lower() == 'e':
exit(0)
elif selection.strip().lower() == 'b':
break
elif selection.strip().isdigit():
slc = int(selection.strip())
if -1 < slc < objs_length:
screenClear()
fileshow.objShow(index=slc)
print()
print()
input('\tDevam (Continue) <<enter>> : ')
#
def menu5( fileshow: pdfShow):
objs_length = len(fileshow.o1_pdffile.p4_objs)
selection = ''
while True:
screenClear()
if objs_length == 0:
print()
print('\t<<obj-endobj>> bulunamadı.')
print()
print()
input('\tGeri (Back) <<enter>> : ')
break
elif objs_length == 1:
print()
print('\t<<obj-endobj>> 1 adet bulundu.')
print('\t No : 0 ')
print()
print()
print( '\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>' )
selection = input('\t(Seçim) Selection No : ')
elif objs_length > 1:
print()
print('\t<<obj-endobj>> {} adet bulundu.'.format(objs_length))
print('\t No : 0 - {}'.format(objs_length-1) )
print()
print()
print( '\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>' )
selection = input('\t(Seçim) Selection No : ')
#
if selection.strip().lower() == 'e':
exit(0)
elif selection.strip().lower() == 'b':
break
elif selection.strip().isdigit():
slc = int(selection.strip())
if -1 < slc < objs_length:
screenClear()
s2hex(
dataBytes= fileshow.o1_pdffile.p2_pdf_bytes[fileshow.o1_pdffile.p4_objs[slc].i1_out_start:fileshow.o1_pdffile.p4_objs[slc].i1_out_end]
, dataName= 'Obj (index:'+str(slc)+')',
columnSize= 20,
index=fileshow.o1_pdffile.p4_objs[slc].i1_out_start
)
print()
print()
input('\tDevam (Continue) <<enter>> : ')
def menu6( fileshow: pdfShow):
streams_length = len(fileshow.o1_pdffile.p5_streams)
selection = ''
while True:
screenClear()
if streams_length == 0:
print()
print('\t<<stream-endstream>> bulunamadı.')
print()
print()
input('\tGeri (Back) <<enter>> : ')
break
elif streams_length == 1:
print()
print('\t<<stream-endstream>> 1 adet bulundu.')
print('\t No : 0 ')
print()
print()
print( '\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>' )
selection = input('\t(Seçim) Selection No : ')
elif streams_length > 1:
print()
print('\t<<stream-endstream>> {} adet bulundu.'.format(streams_length))
print('\t No : 0 - {}'.format(streams_length-1) )
print()
print()
print( '\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>' )
selection = input('\t(Seçim) Selection No : ')
#
if selection.strip().lower() == 'e':
exit(0)
elif selection.strip().lower() == 'b':
break
elif selection.strip().isdigit():
slc = int(selection.strip())
if -1 < slc < streams_length:
screenClear()
fileshow.streamShow(index=slc)
print()
print()
input('\tDevam (Continue) <<enter>> : ')
def menu7( fileshow: pdfShow):
streams_length = len(fileshow.o1_pdffile.p5_streams)
selection = ''
while True:
screenClear()
if streams_length == 0:
print()
print('\t<<stream-endstream>> bulunamadı.')
print()
print()
input('\tGeri (Back) <<enter>> : ')
break
elif streams_length == 1:
print()
print('\t<<stream-endstream>> 1 adet bulundu.')
print('\t No : 0 ')
print()
print()
print( '\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>' )
selection = input('\t(Seçim) Selection No : ')
elif streams_length > 1:
print()
print('\t<<stream-endstream>> {} adet bulundu.'.format(streams_length))
print('\t No : 0 - {}'.format(streams_length-1) )
print()
print()
print( '\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>' )
selection = input('\t(Seçim) Selection No : ')
#
if selection.strip().lower() == 'e':
exit(0)
elif selection.strip().lower() == 'b':
break
elif selection.strip().isdigit():
slc = int(selection.strip())
if -1 < slc < streams_length:
screenClear()
s2hex(
dataBytes= fileshow.o1_pdffile.p2_pdf_bytes[fileshow.o1_pdffile.p5_streams[slc].i1_out_start:fileshow.o1_pdffile.p5_streams[slc].i1_out_end]
, dataName= 'Stream (index:'+str(slc)+')',
columnSize= 20,
index=fileshow.o1_pdffile.p5_streams[slc].i1_out_start
)
print()
print()
input('\tDevam (Continue) <<enter>> : ')
def menu8( fileshow: pdfShow):
xrefs_length = len(fileshow.o1_pdffile.p6_xrefs)
selection = ''
while True:
screenClear()
if xrefs_length == 0:
print()
print('\t<<xref>> bulunamadı.')
print()
print()
input('\tGeri (Back) <<enter>> : ')
break
elif xrefs_length == 1:
print()
print('\t<<xref>> 1 adet bulundu.')
print('\t No : 0 ')
print()
print()
print( '\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>' )
selection = input('\t(Seçim) Selection No : ')
elif xrefs_length > 1:
print()
print('\t<<xref>> {} adet bulundu.'.format(xrefs_length))
print('\t No : 0 - {}'.format(xrefs_length-1) )
print()
print()
print( '\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>' )
selection = input('\t(Seçim) Selection No : ')
#
if selection.strip().lower() == 'e':
exit(0)
elif selection.strip().lower() == 'b':
break
elif selection.strip().isdigit():
slc = int(selection.strip())
if -1 < slc < xrefs_length:
screenClear()
fileshow.xrefShow(index=slc)
print()
print()
input('\tDevam (Continue) <<enter>> : ')
def menu9( fileshow: pdfShow):
trailers_length = len(fileshow.o1_pdffile.p7_trailers)
selection = ''
while True:
screenClear()
if trailers_length == 0:
print()
print('\t<<trailer>> bulunamadı.')
print()
print()
input('\tGeri (Back) <<enter>> : ')
break
elif trailers_length == 1:
print()
print('\t<<trailer>> 1 adet bulundu.')
print('\t No : 0 ')
print()
print()
print( '\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>' )
selection = input('\t(Seçim) Selection No : ')
elif trailers_length > 1:
print()
print('\t<<trailer>> {} adet bulundu.'.format(trailers_length))
print('\t No : 0 - {}'.format(trailers_length-1) )
print()
print()
print( '\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>' )
selection = input('\t(Seçim) Selection No : ')
#
if selection.strip().lower() == 'e':
exit(0)
elif selection.strip().lower() == 'b':
break
elif selection.strip().isdigit():
slc = int(selection.strip())
if -1 < slc < trailers_length:
screenClear()
fileshow.trailerShow(index=slc)
print()
print()
input('\tDevam (Continue) <<enter>> : ')
def menu10( fileshow: pdfShow):
startxref_length = len(fileshow.o1_pdffile.p8_startxrefs)
selection = ''
while True:
screenClear()
if startxref_length == 0:
print()
print('\t<<startxref>> bulunamadı.')
print()
print()
input('\tGeri (Back) <<enter>> : ')
break
elif startxref_length == 1:
print()
print('\t<<startxref>> 1 adet bulundu.')
print('\t No : 0 ')
print()
print()
print( '\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>' )
selection = input('\t(Seçim) Selection No : ')
elif startxref_length > 1:
print()
print('\t<<startxref>> {} adet bulundu.'.format(startxref_length))
print('\t No : 0 - {}'.format(startxref_length-1) )
print()
print()
print( '\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>' )
selection = input('\t(Seçim) Selection No : ')
#
if selection.strip().lower() == 'e':
exit(0)
elif selection.strip().lower() == 'b':
break
elif selection.strip().isdigit():
slc = int(selection.strip())
if -1 < slc < startxref_length:
screenClear()
fileshow.startXrefShow(index=slc)
print()
print()
input('\tDevam (Continue) <<enter>> : ')
#
def menu11(betik:pdfBetik, fileshow:pdfShow):
screenClear()
print()
print('\tPdf Analiz Ediliyor!...')
betik.findPdfBetik()
betik.findObjBetik()
turn = True
while turn:
screenClear()
print()
betik.show()
print()
print()
print('\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>')
selection = input('\t(Seçim) Selection No : ')
if selection.strip().lower() == 'e':
exit(0)
elif selection.strip().lower() == 'b':
turn = False
break
elif selection.strip().isdigit():
slc = int(selection.strip())
if 0 < slc < 54:
index_list = betik.selection(index=slc)
if len(index_list) > 0:
for ii in index_list:
screenClear()
print()
fileshow.objShow(index=ii)
print()
print()
print('\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>')
selection2 = input('\tDevam (Continue) <<enter>> : ')
if selection2.strip().lower() == 'e':
exit(0)
elif selection2.strip().lower() == 'b':
turn = True
break
else:
screenClear()
print()
print('\tTag bulunamadı. (Tag not found)')
print()
print()
input('\tDevam (Continue) <<enter>> : ')
#
def menu12( pdffile : pdfFile , fileshow : pdfShow):
objs_length = len(fileshow.o1_pdffile.p4_objs)
selection = ''
while True:
screenClear()
if objs_length == 0:
print()
print('\t<<obj-endobj>> bulunamadı.')
print()
print()
input('\tGeri (Back) <<enter>> : ')
break
elif objs_length > 0:
print()
print('\t<<obj-endobj>> {} adet bulundu.'.format(objs_length))
print()
print()
print( '\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>' )
selection = input('\tObj Header (First No) : ')
#
if selection.strip().lower() == 'e':
exit(0)
elif selection.strip().lower() == 'b':
break
elif selection.strip().isdigit():
slc = int(selection.strip())
screenClear()
tmp_obj = pdffile.objFirstNo( index=slc )
if tmp_obj.i0_no != -1 and tmp_obj.i0_check:
fileshow.objShow(index=tmp_obj.i0_no)
print()
print()
input('\tDevam (Continue) <<enter>> : ')
#
#
def menu13( fileshow:pdfShow ):
from lib.hexlib.WriteFile import WriteFile as writefile
#
stream_len = len( fileshow.o1_pdffile.p5_streams )
#
while True:
screenClear()
if stream_len == 0:
print()
print('\tYazılacak stream dosyası yok.')
print()
print()
input('\tDevam (Continue) <<enter>> : ')
return
elif stream_len == 1:
print()
print('\t<<stream-endstream>> 1 adet bulundu.')
print('\tNo: 0')
elif stream_len > 1:
print()
print('\t<<stream-endstream>> {} adet bulundu.'.format(stream_len))
print('\tNo: 0 - {}'.format(stream_len-1))
print()
print()
print('\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>')
selection = input('\t(Seçim) Selection No : ')
if selection.strip().lower() == 'b':
break
elif selection.strip().lower() == 'e':
exit(0)
elif selection.strip().isdigit():
slc = int(selection.strip())
if -1 < slc < stream_len:
res = fileshow.detect_stream(index=slc)
if res['result']:
screenClear()
print()
print()
print("\tDafult file name '{}' <<enter>> ".format( ('stream_'+str(slc)) ))
select_name = input('\tNew File Name : ')
if select_name.strip() == '':
file_name = 'stream_'+str(slc)
else:
file_name = select_name.strip()
try:
wf = writefile(filename=file_name)
wf.writeByte(data=res['bytes'])
screenClear()
print()
print()
print("\t'{}' yazıldı. ('{}' was written)".format(file_name,file_name) )
print()
print()
input('\tDevam (Continue) <<enter>> : ')
except:
screenClear()
print()
print()
print('\tDosya yazılırken hata oluştu.')
print('\t(Dosya adını ya da yazma izinlerini kontrol ediniz!)')
print()
print()
input('\tDevam (Continue) <<enter>> : ')
else:
screenClear()
print()
print()
print('\tStream datası tanımlanamadı.')
print()
print()
input('\tDevam (Continue) <<enter>> : ')
#
#
def menu14( fileshow: pdfShow):
objs_length = len(fileshow.o1_pdffile.p4_objs)
selection = ''
while True:
screenClear()
if objs_length == 0:
print()
print('\t<<obj-endobj>> bulunamadı.')
print()
print()
input('\tGeri (Back) <<enter>> : ')
break
elif objs_length == 1:
print()
print('\t<<obj-endobj>> 1 adet bulundu.')
print('\t No : 0 ')
print()
print()
print( '\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>' )
selection = input('\t(Seçim) Selection No : ')
elif objs_length > 1:
print()
print('\t<<obj-endobj>> {} adet bulundu.'.format(objs_length))
print('\t No : 0 - {}'.format(objs_length-1) )
print()
print()
print( '\tGeri (Back) <<b>>, Çıkıs (Exit) <<e>>' )
selection = input('\t(Seçim) Selection No : ')
#
if selection.strip().lower() == 'e':
exit(0)
elif selection.strip().lower() == 'b':
break
elif selection.strip().isdigit():
slc = int(selection.strip())
if -1 < slc < objs_length:
screenClear()
fileshow.objSummaryShow(index=slc)
print()
print()
input('\tDevam (Continue) <<enter>> : ')
#
def main():
while True:
filename = menu0()
filepdf = menu1(Filename=filename)
fileshow = pdfShow(pdffile=filepdf)
if filepdf.c1_checkNormal:
while True:
selection = menu2()
if selection != -1:
if selection == 1:
menu3(file=filepdf)
elif selection == 2:
betikpdf = pdfBetik(pdffile=filepdf)
menu11(betik=betikpdf, fileshow=fileshow)
elif selection == 3:
menu12(pdffile=filepdf ,fileshow=fileshow)
elif selection == 4:
menu14(fileshow=fileshow)
elif selection == 5:
menu4(fileshow=fileshow)
elif selection == 6:
menu5(fileshow=fileshow)
elif selection == 7:
menu6(fileshow=fileshow)
elif selection == 8:
menu7(fileshow=fileshow)
elif selection == 9:
menu13(fileshow=fileshow)
elif selection == 10:
menu8(fileshow=fileshow)
elif selection == 11:
menu9(fileshow=fileshow)
elif selection == 12:
menu10(fileshow=fileshow)
else:
break
#
# Main Program Start
main()