-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathps4_module.py
1948 lines (1719 loc) · 71.3 KB
/
ps4_module.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
#!/usr/bin/env python
'''
PS4 Module Loader by SocraticBliss (R)
Major Thanks to...
# aerosoul
# balika011
# Znullptr
# Pablo (kozarovv)
# ChendoChap
# xyz
# CelesteBlue
# kiwidogg
# motoharu
# noname120
# flatz
# Team Reswitched
Extra Special Thanks for telling me my program sucks...
# zecoxao
ps4_module.py: IDA loader for reading Sony PlayStation(R) 4 Module files
'''
from idaapi import *
from idc import *
import csv
import idaapi
import idc
import shutil
import struct
class Binary:
__slots__ = ('EI_MAGIC', 'EI_CLASS', 'EI_DATA', 'EI_VERSION',
'EI_OSABI', 'EI_PADDING', 'EI_ABIVERSION', 'EI_SIZE',
'E_TYPE', 'E_MACHINE', 'E_VERSION', 'E_START_ADDR',
'E_PHT_OFFSET', 'E_SHT_OFFSET', 'E_FLAGS', 'E_SIZE',
'E_PHT_SIZE', 'E_PHT_COUNT', 'E_SHT_SIZE', 'E_SHT_COUNT',
'E_SHT_INDEX', 'E_SEGMENTS', 'E_SECTIONS')
# Elf Types
ET_NONE = 0x0
ET_REL = 0x1
ET_EXEC = 0x2
ET_DYN = 0x3
ET_CORE = 0x4
ET_SCE_EXEC = 0xFE00
ET_SCE_REPLAY_EXEC = 0xFE01
ET_SCE_RELEXEC = 0xFE04
ET_SCE_STUBLIB = 0xFE0C
ET_SCE_DYNEXEC = 0xFE10
ET_SCE_DYNAMIC = 0xFE18
ET_LOPROC = 0xFF00
ET_HIPROC = 0xFFFF
# Elf Architecture
EM_X86_64 = 0x3E
def __init__(self, f):
f.seek(0)
self.EI_MAGIC = struct.unpack('4s', f.read(4))[0]
self.EI_CLASS = struct.unpack('<B', f.read(1))[0]
self.EI_DATA = struct.unpack('<B', f.read(1))[0]
self.EI_VERSION = struct.unpack('<B', f.read(1))[0]
self.EI_OSABI = struct.unpack('<B', f.read(1))[0]
self.EI_ABIVERSION = struct.unpack('<B', f.read(1))[0]
self.EI_PADDING = struct.unpack('6x', f.read(6))
self.EI_SIZE = struct.unpack('<B', f.read(1))[0]
# Elf Properties
self.E_TYPE = struct.unpack('<H', f.read(2))[0]
self.E_MACHINE = struct.unpack('<H', f.read(2))[0]
self.E_VERSION = struct.unpack('<I', f.read(4))[0]
self.E_START_ADDR = struct.unpack('<Q', f.read(8))[0]
self.E_PHT_OFFSET = struct.unpack('<Q', f.read(8))[0]
self.E_SHT_OFFSET = struct.unpack('<Q', f.read(8))[0]
self.E_FLAGS = struct.unpack('<I', f.read(4))[0]
self.E_SIZE = struct.unpack('<H', f.read(2))[0]
self.E_PHT_SIZE = struct.unpack('<H', f.read(2))[0]
self.E_PHT_COUNT = struct.unpack('<H', f.read(2))[0]
self.E_SHT_SIZE = struct.unpack('<H', f.read(2))[0]
self.E_SHT_COUNT = struct.unpack('<H', f.read(2))[0]
self.E_SHT_INDEX = struct.unpack('<H', f.read(2))[0]
# Prevent Other Binaries
if self.E_MACHINE != Binary.EM_X86_64:
return None
f.seek(self.E_PHT_OFFSET)
# Elf Program Header Table
Binary.E_SEGMENTS = [Segment(f) for entry in range(self.E_PHT_COUNT)]
f.seek(self.E_SHT_OFFSET)
# Elf Section Header Table
Binary.E_SECTIONS = [Section(f) for entry in range(self.E_SHT_COUNT)]
def type(self):
return {
Binary.ET_NONE : 'None',
Binary.ET_REL : 'Relocatable',
Binary.ET_EXEC : 'Executable',
Binary.ET_DYN : 'Shared Object',
Binary.ET_CORE : 'Core Dump',
Binary.ET_SCE_EXEC : 'Main Module',
Binary.ET_SCE_REPLAY_EXEC : 'Replay Module',
Binary.ET_SCE_RELEXEC : 'Relocatable PRX',
Binary.ET_SCE_STUBLIB : 'Stub Library',
Binary.ET_SCE_DYNEXEC : 'Main Module - ASLR',
Binary.ET_SCE_DYNAMIC : 'Shared Object PRX',
}.get(self.E_TYPE, 'Missing Program Type!!!')
def procomp(self, processor, pointer, til):
# Processor Type
idc.set_processor_type(processor, SETPROC_LOADER)
# Compiler Attributes
idc.set_inf_attr(INF_COMPILER, COMP_GNU)
idc.set_inf_attr(INF_MODEL, pointer)
idc.set_inf_attr(INF_SIZEOF_BOOL, 0x1)
idc.set_inf_attr(INF_SIZEOF_LONG, 0x8)
idc.set_inf_attr(INF_SIZEOF_LDBL, 0x10)
# Type Library
idc.add_default_til(til)
# Loader Flags
#idc.set_inf_attr(INF_LFLAGS, LFLG_64BIT)
# Assume GCC3 names
idc.set_inf_attr(INF_DEMNAMES, DEMNAM_GCC3 | DEMNAM_NAME)
# Analysis Flags
# (unchecked) Delete instructions with no xrefs
# (unchecked) Coagulate data segments in the final pass
idc.set_inf_attr(INF_AF, 0xDFFFFFDF)
# Return Bitsize
return self.EI_CLASS
class Segment:
__slots__ = ('TYPE', 'FLAGS', 'OFFSET', 'MEM_ADDR',
'FILE_ADDR', 'FILE_SIZE', 'MEM_SIZE', 'ALIGNMENT')
# Segment Types
PT_NULL = 0x0
PT_LOAD = 0x1
PT_DYNAMIC = 0x2
PT_INTERP = 0x3
PT_NOTE = 0x4
PT_SHLIB = 0x5
PT_PHDR = 0x6
PT_TLS = 0x7
PT_NUM = 0x8
PT_SCE_DYNLIBDATA = 0x61000000
PT_SCE_PROCPARAM = 0x61000001
PT_SCE_MODULEPARAM = 0x61000002
PT_SCE_RELRO = 0x61000010
PT_GNU_EH_FRAME = 0x6474E550
PT_GNU_STACK = 0x6474E551
PT_SCE_COMMENT = 0x6FFFFF00
PT_SCE_LIBVERSION = 0x6FFFFF01
PT_HIOS = 0x6FFFFFFF
PT_LOPROC = 0x70000000
PT_SCE_SEGSYM = 0x700000A8
PT_HIPROC = 0x7FFFFFFF
# Segment Alignments
AL_NONE = 0x0
AL_BYTE = 0x1
AL_WORD = 0x2
AL_DWORD = 0x4
AL_QWORD = 0x8
AL_PARA = 0x10
AL_4K = 0x4000
def __init__(self, f):
self.TYPE = struct.unpack('<I', f.read(4))[0]
self.FLAGS = struct.unpack('<I', f.read(4))[0]
self.OFFSET = struct.unpack('<Q', f.read(8))[0]
self.MEM_ADDR = struct.unpack('<Q', f.read(8))[0]
self.FILE_ADDR = struct.unpack('<Q', f.read(8))[0]
self.FILE_SIZE = struct.unpack('<Q', f.read(8))[0]
self.MEM_SIZE = struct.unpack('<Q', f.read(8))[0]
self.ALIGNMENT = struct.unpack('<Q', f.read(8))[0]
def alignment(self):
return {
Segment.AL_NONE : saAbs,
Segment.AL_BYTE : saRelByte,
Segment.AL_WORD : saRelWord,
Segment.AL_DWORD : saRelDble,
Segment.AL_QWORD : saRelQword,
Segment.AL_PARA : saRelPara,
Segment.AL_4K : saRel4K,
}.get(self.ALIGNMENT, saRel_MAX_ALIGN_CODE)
def flags(self):
return self.FLAGS & 0xF
def name(self):
return {
Segment.PT_NULL : 'NULL',
Segment.PT_LOAD : 'CODE' if self.flags() == (SEGPERM_EXEC | SEGPERM_READ) else 'DATA',
Segment.PT_DYNAMIC : 'DYNAMIC',
Segment.PT_INTERP : 'INTERP',
Segment.PT_NOTE : 'NOTE',
Segment.PT_SHLIB : 'SHLIB',
Segment.PT_PHDR : 'PHDR',
Segment.PT_TLS : 'TLS',
Segment.PT_NUM : 'NUM',
Segment.PT_SCE_DYNLIBDATA : 'SCE_DYNLIBDATA',
Segment.PT_SCE_PROCPARAM : 'SCE_PROCPARAM',
Segment.PT_SCE_MODULEPARAM : 'SCE_MODULEPARAM',
Segment.PT_SCE_RELRO : 'SCE_RELRO',
Segment.PT_GNU_EH_FRAME : 'GNU_EH_FRAME',
Segment.PT_GNU_STACK : 'GNU_STACK',
Segment.PT_SCE_COMMENT : 'SCE_COMMENT',
Segment.PT_SCE_LIBVERSION : 'SCE_LIBVERSION',
}.get(self.TYPE, 'UNK')
def struct(self, name, members, location = 0x0):
if self.FLAGS > 7:
return idc.get_struc_id(name)
entry = idc.add_struc(BADADDR, name, False)
for (member, comment, size) in members:
flags = idaapi.get_flags_by_size(size)
if member in ['addend', 'offset']:
idc.add_struc_member(entry, member, location, flags + FF_0OFF, BADADDR, size, BADADDR, 0, REF_OFF64)
else:
idc.add_struc_member(entry, member, location, flags, BADADDR, size)
idc.set_member_cmt(entry, location, comment, False)
location += size
return entry
def type(self):
return {
Segment.PT_LOAD : 'CODE' if self.flags() == (SEGPERM_EXEC | SEGPERM_READ) else 'DATA',
Segment.PT_DYNAMIC : 'DATA',
Segment.PT_INTERP : 'CONST',
Segment.PT_NOTE : 'CONST',
Segment.PT_PHDR : 'CODE',
Segment.PT_TLS : 'BSS',
Segment.PT_SCE_DYNLIBDATA : 'CONST',
Segment.PT_SCE_PROCPARAM : 'CONST',
Segment.PT_SCE_MODULEPARAM : 'CONST',
Segment.PT_SCE_RELRO : 'DATA',
Segment.PT_GNU_EH_FRAME : 'CONST',
Segment.PT_GNU_STACK : 'DATA',
}.get(self.TYPE, 'UNK')
class Section:
__slots__ = ('NAME', 'TYPE', 'FLAGS', 'MEM_ADDR',
'OFFSET', 'FILE_SIZE', 'LINK', 'INFO',
'ALIGNMENT', 'FSE_SIZE')
def __init__(self, f):
self.NAME = struct.unpack('<I', f.read(4))[0]
self.TYPE = struct.unpack('<I', f.read(4))[0]
self.FLAGS = struct.unpack('<Q', f.read(8))[0]
self.MEM_ADDR = struct.unpack('<Q', f.read(8))[0]
self.OFFSET = struct.unpack('<Q', f.read(8))[0]
self.FILE_SIZE = struct.unpack('<Q', f.read(8))[0]
self.LINK = struct.unpack('<I', f.read(4))[0]
self.INFO = struct.unpack('<I', f.read(4))[0]
self.ALIGNMENT = struct.unpack('<Q', f.read(8))[0]
self.FSE_SIZE = struct.unpack('<Q', f.read(8))[0]
class Dynamic:
__slots__ = ('TAG', 'VALUE', 'ID', 'VERSION_MAJOR', 'VERSION_MINOR', 'INDEX')
# Dynamic Tags
(DT_NULL, DT_NEEDED, DT_PLTRELSZ, DT_PLTGOT, DT_HASH, DT_STRTAB, DT_SYMTAB,
DT_RELA, DT_RELASZ, DT_RELAENT, DT_STRSZ, DT_SYMENT, DT_INIT, DT_FINI,
DT_SONAME, DT_RPATH, DT_SYMBOLIC, DT_REL, DT_RELSZ, DT_RELENT, DT_PLTREL,
DT_DEBUG, DT_TEXTREL, DT_JMPREL, DT_BIND_NOW, DT_INIT_ARRAY, DT_FINI_ARRAY,
DT_INIT_ARRAYSZ, DT_FINI_ARRAYSZ, DT_RUNPATH, DT_FLAGS, DT_ENCODING, DT_PREINIT_ARRAY,
DT_PREINIT_ARRAYSZ) = range(0x22)
DT_SCE_IDTABENTSZ = 0x61000005
DT_SCE_FINGERPRINT = 0x61000007
DT_SCE_ORIGINAL_FILENAME = 0x61000009
DT_SCE_MODULE_INFO = 0x6100000D
DT_SCE_NEEDED_MODULE = 0x6100000F
DT_SCE_MODULE_ATTR = 0x61000011
DT_SCE_EXPORT_LIB = 0x61000013
DT_SCE_IMPORT_LIB = 0x61000015
DT_SCE_EXPORT_LIB_ATTR = 0x61000017
DT_SCE_IMPORT_LIB_ATTR = 0x61000019
DT_SCE_STUB_MODULE_NAME = 0x6100001D
DT_SCE_STUB_MODULE_VERSION = 0x6100001F
DT_SCE_STUB_LIBRARY_NAME = 0x61000021
DT_SCE_STUB_LIBRARY_VERSION = 0x61000023
DT_SCE_HASH = 0x61000025
DT_SCE_PLTGOT = 0x61000027
DT_SCE_JMPREL = 0x61000029
DT_SCE_PLTREL = 0x6100002B
DT_SCE_PLTRELSZ = 0x6100002D
DT_SCE_RELA = 0x6100002F
DT_SCE_RELASZ = 0x61000031
DT_SCE_RELAENT = 0x61000033
DT_SCE_STRTAB = 0x61000035
DT_SCE_STRSZ = 0x61000037
DT_SCE_SYMTAB = 0x61000039
DT_SCE_SYMENT = 0x6100003B
DT_SCE_HASHSZ = 0x6100003D
DT_SCE_SYMTABSZ = 0x6100003F
DT_SCE_HIOS = 0x6FFFF000
DT_GNU_HASH = 0x6FFFFEF5
DT_VERSYM = 0x6FFFFFF0
DT_RELACOUNT = 0x6FFFFFF9
DT_RELCOUNT = 0x6FFFFFFA
DT_FLAGS_1 = 0x6FFFFFFB
DT_VERDEF = 0x6FFFFFFC
DT_VERDEFNUM = 0x6FFFFFFD
def __init__(self, f):
self.TAG = struct.unpack('<Q', f.read(8))[0]
self.VALUE = struct.unpack('<Q', f.read(8))[0]
def tag(self):
return {
Dynamic.DT_NULL : 'DT_NULL',
Dynamic.DT_NEEDED : 'DT_NEEDED',
Dynamic.DT_PLTRELSZ : 'DT_PLTRELSZ',
Dynamic.DT_PLTGOT : 'DT_PLTGOT',
Dynamic.DT_HASH : 'DT_HASH',
Dynamic.DT_STRTAB : 'DT_STRTAB',
Dynamic.DT_SYMTAB : 'DT_SYMTAB',
Dynamic.DT_RELA : 'DT_RELA',
Dynamic.DT_RELASZ : 'DT_RELASZ',
Dynamic.DT_RELAENT : 'DT_RELAENT',
Dynamic.DT_STRSZ : 'DT_STRSZ',
Dynamic.DT_SYMENT : 'DT_SYMENT',
Dynamic.DT_INIT : 'DT_INIT',
Dynamic.DT_FINI : 'DT_FINI',
Dynamic.DT_SONAME : 'DT_SONAME',
Dynamic.DT_RPATH : 'DT_RPATH',
Dynamic.DT_SYMBOLIC : 'DT_SYMBOLIC',
Dynamic.DT_REL : 'DT_REL',
Dynamic.DT_RELSZ : 'DT_RELSZ',
Dynamic.DT_RELENT : 'DT_RELENT',
Dynamic.DT_PLTREL : 'DT_PLTREL',
Dynamic.DT_DEBUG : 'DT_DEBUG',
Dynamic.DT_TEXTREL : 'DT_TEXTREL',
Dynamic.DT_JMPREL : 'DT_JMPREL',
Dynamic.DT_BIND_NOW : 'DT_BIND_NOW',
Dynamic.DT_INIT_ARRAY : 'DT_INIT_ARRAY',
Dynamic.DT_FINI_ARRAY : 'DT_FINI_ARRAY',
Dynamic.DT_INIT_ARRAYSZ : 'DT_INIT_ARRAYSZ',
Dynamic.DT_FINI_ARRAYSZ : 'DT_FINI_ARRAYSZ',
Dynamic.DT_RUNPATH : 'DT_RUN_PATH',
Dynamic.DT_FLAGS : 'DT_FLAGS',
Dynamic.DT_ENCODING : 'DT_ENCODING',
Dynamic.DT_PREINIT_ARRAY : 'DT_PREINIT_ARRAY',
Dynamic.DT_PREINIT_ARRAYSZ : 'DT_PREINIT_ARRAYSZ',
Dynamic.DT_SCE_IDTABENTSZ : 'DT_SCE_IDTABENTSZ',
Dynamic.DT_SCE_FINGERPRINT : 'DT_SCE_FINGERPRINT',
Dynamic.DT_SCE_ORIGINAL_FILENAME : 'DT_SCE_ORIGINAL_FILENAME',
Dynamic.DT_SCE_MODULE_INFO : 'DT_SCE_MODULE_INFO',
Dynamic.DT_SCE_NEEDED_MODULE : 'DT_SCE_NEEDED_MODULE',
Dynamic.DT_SCE_MODULE_ATTR : 'DT_SCE_MODULE_ATTR',
Dynamic.DT_SCE_EXPORT_LIB : 'DT_SCE_EXPORT_LIB',
Dynamic.DT_SCE_IMPORT_LIB : 'DT_SCE_IMPORT_LIB',
Dynamic.DT_SCE_EXPORT_LIB_ATTR : 'DT_SCE_EXPORT_LIB_ATTR',
Dynamic.DT_SCE_IMPORT_LIB_ATTR : 'DT_SCE_IMPORT_LIB_ATTR',
Dynamic.DT_SCE_STUB_MODULE_NAME : 'DT_SCE_STUB_MODULE_NAME',
Dynamic.DT_SCE_STUB_MODULE_VERSION : 'DT_SCE_STUB_MODULE_VERSION',
Dynamic.DT_SCE_STUB_LIBRARY_NAME : 'DT_SCE_STUB_LIBRARY_NAME',
Dynamic.DT_SCE_STUB_LIBRARY_VERSION : 'DT_SCE_STUB_LIBRARY_VERSION',
Dynamic.DT_SCE_HASH : 'DT_SCE_HASH',
Dynamic.DT_SCE_PLTGOT : 'DT_SCE_PLTGOT',
Dynamic.DT_SCE_JMPREL : 'DT_SCE_JMPREL',
Dynamic.DT_SCE_PLTREL : 'DT_SCE_PLTREL',
Dynamic.DT_SCE_PLTRELSZ : 'DT_SCE_PLTRELSZ',
Dynamic.DT_SCE_RELA : 'DT_SCE_RELA',
Dynamic.DT_SCE_RELASZ : 'DT_SCE_RELASZ',
Dynamic.DT_SCE_RELAENT : 'DT_SCE_RELAENT',
Dynamic.DT_SCE_STRTAB : 'DT_SCE_STRTAB',
Dynamic.DT_SCE_STRSZ : 'DT_SCE_STRSZ',
Dynamic.DT_SCE_SYMTAB : 'DT_SCE_SYMTAB',
Dynamic.DT_SCE_SYMENT : 'DT_SCE_SYMENT',
Dynamic.DT_SCE_HASHSZ : 'DT_SCE_HASHSZ',
Dynamic.DT_SCE_SYMTABSZ : 'DT_SCE_SYMTABSZ',
Dynamic.DT_SCE_HIOS : 'DT_SCE_HIOS',
Dynamic.DT_GNU_HASH : 'DT_GNU_HASH',
Dynamic.DT_VERSYM : 'DT_VERSYM',
Dynamic.DT_RELACOUNT : 'DT_RELACOUNT',
Dynamic.DT_RELCOUNT : 'DT_RELCOUNT',
Dynamic.DT_FLAGS_1 : 'DT_FLAGS_1',
Dynamic.DT_VERDEF : 'DT_VERDEF',
Dynamic.DT_VERDEFNUM : 'DT_VERDEFNUM',
}.get(self.TAG, 'Missing Dynamic Tag!!!')
def lib_attribute(self):
return {
0x1 : 'AUTO_EXPORT',
0x2 : 'WEAK_EXPORT',
0x8 : 'LOOSE_IMPORT',
0x9 : 'AUTO_EXPORT|LOOSE_IMPORT',
0xA : 'WEAK_EXPORT|LOOSE_IMPORT',
}.get(self.INDEX, 'Missing Library Attribute!!!')
def mod_attribute(self):
return {
0x0 : 'NONE',
0x1 : 'SCE_CANT_STOP',
0x2 : 'SCE_EXCLUSIVE_LOAD',
0x4 : 'SCE_EXCLUSIVE_START',
0x8 : 'SCE_CAN_RESTART',
0x10 : 'SCE_CAN_RELOCATE',
0x20 : 'SCE_CANT_SHARE',
}.get(self.INDEX, 'Missing Module Attribute!!!')
def comment(self, address, stubs, modules, libraries):
if self.TAG in [Dynamic.DT_NEEDED, Dynamic.DT_SONAME]:
return '%s | %s' % (self.tag(), str(stubs[self.VALUE]))
elif self.TAG == Dynamic.DT_SCE_HASH:
address += Dynamic.HASHTAB
idc.add_entry(address, address, '.hash', False)
return '%s | %#x' % (self.tag(), address)
elif self.TAG == Dynamic.DT_SCE_STRTAB:
address += Dynamic.STRTAB
idc.add_entry(address, address, '.dynstr', False)
return '%s | %#x' % (self.tag(), address)
elif self.TAG == Dynamic.DT_SCE_SYMTAB:
address += Dynamic.SYMTAB
idc.add_entry(address, address, '.dynsym', False)
return '%s | %#x' % (self.tag(), address)
elif self.TAG == Dynamic.DT_SCE_JMPREL:
return '%s | %#x' % (self.tag(), address + Dynamic.JMPTAB)
elif self.TAG == Dynamic.DT_SCE_RELA:
return '%s | %#x' % (self.tag(), address + Dynamic.RELATAB)
elif self.TAG in [Dynamic.DT_SCE_NEEDED_MODULE, Dynamic.DT_SCE_IMPORT_LIB,
Dynamic.DT_SCE_IMPORT_LIB_ATTR, Dynamic.DT_SCE_EXPORT_LIB,
Dynamic.DT_SCE_EXPORT_LIB_ATTR, Dynamic.DT_SCE_MODULE_INFO,
Dynamic.DT_SCE_MODULE_ATTR, Dynamic.DT_SCE_FINGERPRINT,
Dynamic.DT_SCE_ORIGINAL_FILENAME]:
self.ID = self.VALUE >> 48
self.VERSION_MINOR = (self.VALUE >> 40) & 0xF
self.VERSION_MAJOR = (self.VALUE >> 32) & 0xF
self.INDEX = self.VALUE & 0xFFF
if self.TAG in [Dynamic.DT_SCE_NEEDED_MODULE, Dynamic.DT_SCE_MODULE_INFO]:
return '%s | MID:%#x Version:%i.%i Name:%s' % \
(self.tag(), self.ID, self.VERSION_MAJOR, self.VERSION_MINOR, str(modules[self.INDEX]))
elif self.TAG in [Dynamic.DT_SCE_IMPORT_LIB, Dynamic.DT_SCE_EXPORT_LIB]:
return '%s | LID:%#x Version:%i Name:%s' % \
(self.tag(), self.ID, self.VERSION_MAJOR, str(libraries[self.INDEX]))
elif self.TAG == Dynamic.DT_SCE_MODULE_ATTR:
return '%s | %s' % (self.tag(), self.mod_attribute())
elif self.TAG in [Dynamic.DT_SCE_IMPORT_LIB_ATTR, Dynamic.DT_SCE_EXPORT_LIB_ATTR]:
return '%s | LID:%#x Attributes:%s' % \
(self.tag(), self.ID, self.lib_attribute())
elif self.TAG == Dynamic.DT_SCE_FINGERPRINT:
return '%s | %s' % (self.tag(), Dynamic.FINGERPRINT)
elif self.TAG == Dynamic.DT_SCE_ORIGINAL_FILENAME:
return '%s | %s' % (self.tag(), str(stubs[self.VALUE]))
return '%s | %#x' % (self.tag(), self.VALUE)
def process(self, stubs, modules, libraries):
if self.TAG == Dynamic.DT_INIT:
Dynamic.INIT = self.VALUE
idc.add_entry(Dynamic.INIT, Dynamic.INIT, '.init', True)
elif self.TAG == Dynamic.DT_FINI:
Dynamic.FINI = self.VALUE
idc.add_entry(Dynamic.FINI, Dynamic.FINI, '.fini', True)
elif self.TAG in [Dynamic.DT_NEEDED, Dynamic.DT_SONAME]:
stubs[self.VALUE] = 0
elif self.TAG == Dynamic.DT_SCE_STRTAB:
Dynamic.STRTAB = self.VALUE
elif self.TAG == Dynamic.DT_SCE_STRSZ:
Dynamic.STRTABSZ = self.VALUE
elif self.TAG == Dynamic.DT_SCE_SYMTAB:
Dynamic.SYMTAB = self.VALUE
elif self.TAG == Dynamic.DT_SCE_SYMTABSZ:
Dynamic.SYMTABSZ = self.VALUE
elif self.TAG == Dynamic.DT_SCE_JMPREL:
Dynamic.JMPTAB = self.VALUE
elif self.TAG == Dynamic.DT_SCE_PLTRELSZ:
Dynamic.JMPTABSZ = self.VALUE
elif self.TAG == Dynamic.DT_SCE_PLTREL:
if self.VALUE == 0x7:
return '%s | %#x | DT_RELA' % (self.tag(), self.VALUE)
elif self.TAG == Dynamic.DT_SCE_RELA:
Dynamic.RELATAB = self.VALUE
elif self.TAG == Dynamic.DT_SCE_RELASZ:
Dynamic.RELATABSZ = self.VALUE
elif self.TAG == Dynamic.DT_SCE_HASH:
Dynamic.HASHTAB = self.VALUE
elif self.TAG == Dynamic.DT_SCE_HASHSZ:
Dynamic.HASHTABSZ = self.VALUE
elif self.TAG == Dynamic.DT_SCE_PLTGOT:
Dynamic.GOT = self.VALUE
idc.add_entry(Dynamic.GOT, Dynamic.GOT, '.got.plt', False)
elif self.TAG in [Dynamic.DT_SCE_NEEDED_MODULE, Dynamic.DT_SCE_IMPORT_LIB,
Dynamic.DT_SCE_IMPORT_LIB_ATTR, Dynamic.DT_SCE_EXPORT_LIB,
Dynamic.DT_SCE_EXPORT_LIB_ATTR, Dynamic.DT_SCE_MODULE_INFO,
Dynamic.DT_SCE_MODULE_ATTR, Dynamic.DT_SCE_FINGERPRINT,
Dynamic.DT_SCE_ORIGINAL_FILENAME]:
self.ID = self.VALUE >> 48
self.VERSION_MINOR = (self.VALUE >> 40) & 0xF
self.VERSION_MAJOR = (self.VALUE >> 32) & 0xF
self.INDEX = self.VALUE & 0xFFF
if self.TAG in [Dynamic.DT_SCE_NEEDED_MODULE, Dynamic.DT_SCE_MODULE_INFO]:
modules[self.INDEX] = 0
return '%s | MID:%#x Version:%i.%i | %#x' % \
(self.tag(), self.ID, self.VERSION_MAJOR, self.VERSION_MINOR, self.INDEX)
elif self.TAG in [Dynamic.DT_SCE_IMPORT_LIB, Dynamic.DT_SCE_EXPORT_LIB]:
libraries[self.INDEX] = self.ID
return '%s | LID:%#x Version:%i | %#x' % \
(self.tag(), self.ID, self.VERSION_MAJOR, self.INDEX)
elif self.TAG == Dynamic.DT_SCE_MODULE_ATTR:
return '%s | %s' % (self.tag(), self.mod_attribute())
elif self.TAG in [Dynamic.DT_SCE_IMPORT_LIB_ATTR, Dynamic.DT_SCE_EXPORT_LIB_ATTR]:
return '%s | LID:%#x Attributes:%s' % \
(self.tag(), self.ID, self.lib_attribute())
elif self.TAG == Dynamic.DT_SCE_FINGERPRINT:
Dynamic.FINGERPRINT = self.VALUE
elif self.TAG == Dynamic.DT_SCE_ORIGINAL_FILENAME:
stubs[self.INDEX] = 0
return '%s | %#x' % (self.tag(), self.VALUE)
class Relocation:
__slots__ = ('OFFSET', 'INDEX', 'INFO', 'ADDEND')
# PS4 (X86_64) Relocation Codes (40)
(R_X86_64_NONE, R_X86_64_64, R_X86_64_PC32, R_X86_64_GOT32,
R_X86_64_PLT32, R_X86_64_COPY, R_X86_64_GLOB_DAT, R_X86_64_JUMP_SLOT,
R_X86_64_RELATIVE, R_X86_64_GOTPCREL, R_X86_64_32, R_X86_64_32S,
R_X86_64_16, R_X86_64_PC16, R_X86_64_8, R_X86_64_PC8, R_X86_64_DTPMOD64,
R_X86_64_DTPOFF64, R_X86_64_TPOFF64, R_X86_64_TLSGD, R_X86_64_TLSLD,
R_X86_64_DTPOFF32, R_X86_64_GOTTPOFF, R_X86_64_TPOFF32, R_X86_64_PC64,
R_X86_64_GOTOFF64, R_X86_64_GOTPC32, R_X86_64_GOT64, R_X86_64_GOTPCREL64,
R_X86_64_GOTPC64, R_X86_64_GOTPLT64, R_X86_64_PLTOFF64, R_X86_64_SIZE32,
R_X86_64_SIZE64, R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC,
R_X86_64_IRELATIVE, R_X86_64_RELATIVE64) = range(0x27)
R_X86_64_ORBIS_GOTPCREL_LOAD = 0x28
def __init__(self, f):
self.OFFSET = struct.unpack('<Q', f.read(8))[0]
self.INFO = struct.unpack('<Q', f.read(8))[0]
self.ADDEND = struct.unpack('<Q', f.read(8))[0]
def type(self):
return {
Relocation.R_X86_64_NONE : 'R_X86_64_NONE',
Relocation.R_X86_64_64 : 'R_X86_64_64',
Relocation.R_X86_64_PC32 : 'R_X86_64_PC32',
Relocation.R_X86_64_GOT32 : 'R_X86_64_GOT32',
Relocation.R_X86_64_PLT32 : 'R_X86_64_PLT32',
Relocation.R_X86_64_COPY : 'R_X86_64_COPY',
Relocation.R_X86_64_GLOB_DAT : 'R_X86_64_GLOB_DAT',
Relocation.R_X86_64_JUMP_SLOT : 'R_X86_64_JUMP_SLOT',
Relocation.R_X86_64_RELATIVE : 'R_X86_64_RELATIVE',
Relocation.R_X86_64_GOTPCREL : 'R_X86_64_GOTPCREL',
Relocation.R_X86_64_32 : 'R_X86_64_32',
Relocation.R_X86_64_32S : 'R_X86_64_32S',
Relocation.R_X86_64_16 : 'R_X86_64_16',
Relocation.R_X86_64_PC16 : 'R_X86_64_PC16',
Relocation.R_X86_64_8 : 'R_X86_64_8',
Relocation.R_X86_64_PC8 : 'R_X86_64_PC8',
Relocation.R_X86_64_DTPMOD64 : 'R_X86_64_DTPMOD64',
Relocation.R_X86_64_DTPOFF64 : 'R_X86_64_DTPOFF64',
Relocation.R_X86_64_TPOFF64 : 'R_X86_64_TPOFF64',
Relocation.R_X86_64_TLSGD : 'R_X86_64_TLSGD',
Relocation.R_X86_64_TLSLD : 'R_X86_64_TLSLD',
Relocation.R_X86_64_DTPOFF32 : 'R_X86_64_DTPOFF32',
Relocation.R_X86_64_GOTTPOFF : 'R_X86_64_GOTTPOFF',
Relocation.R_X86_64_TPOFF32 : 'R_X86_64_TPOFF32',
Relocation.R_X86_64_PC64 : 'R_X86_64_PC64',
Relocation.R_X86_64_GOTOFF64 : 'R_X86_64_GOTOFF64',
Relocation.R_X86_64_GOTPC32 : 'R_X86_64_GOTPC32',
Relocation.R_X86_64_GOT64 : 'R_X86_64_GOT64',
Relocation.R_X86_64_GOTPCREL64 : 'R_X86_64_GOTPCREL64',
Relocation.R_X86_64_GOTPC64 : 'R_X86_64_GOTPC64',
Relocation.R_X86_64_GOTPLT64 : 'R_X86_64_GOTPLT64',
Relocation.R_X86_64_PLTOFF64 : 'R_X86_64_PLTOFF64',
Relocation.R_X86_64_SIZE32 : 'R_X86_64_SIZE32',
Relocation.R_X86_64_SIZE64 : 'R_X86_64_SIZE64',
Relocation.R_X86_64_GOTPC32_TLSDESC : 'R_X86_64_GOTPC32_TLSDESC',
Relocation.R_X86_64_TLSDESC_CALL : 'R_X86_64_TLSDESC_CALL',
Relocation.R_X86_64_TLSDESC : 'R_X86_64_TLSDESC',
Relocation.R_X86_64_IRELATIVE : 'R_X86_64_IRELATIVE',
Relocation.R_X86_64_RELATIVE64 : 'R_X86_64_RELATIVE64',
Relocation.R_X86_64_ORBIS_GOTPCREL_LOAD : 'R_X86_64_ORBIS_GOTPCREL_LOAD',
}.get(self.INFO, 'Missing PS4 Relocation Type!!!')
def process(self, nids, symbols):
if self.INFO > Relocation.R_X86_64_ORBIS_GOTPCREL_LOAD:
self.INDEX = self.INFO >> 32
self.INFO &= 0xFF
# Symbol Value + AddEnd (S + A)
if self.type() == 'R_X86_64_64':
self.INDEX += self.ADDEND
if self.type() != 'R_X86_64_DTPMOD64':
symbol = next(value for key, value in enumerate(symbols) if key + 2 == self.INDEX)[1]
# String (Offset) == Base + AddEnd (B + A)
if self.type() == 'R_X86_64_RELATIVE':
idaapi.put_qword(self.OFFSET, self.ADDEND)
idaapi.create_data(self.OFFSET, FF_QWORD, 0x8, BADNODE)
# TLS Object
elif self.type() in ['R_X86_64_DTPMOD64', 'R_X86_64_DTPOFF64']:
idc.set_name(self.OFFSET, 'tls_access_struct', SN_NOCHECK | SN_NOWARN | SN_FORCE)
# Object
else:
# Resolve the NID...
try:
idc.set_cmt(self.OFFSET, 'NID: ' + symbol, False)
except:
pass
object = nids.get(symbol[:11], symbol)
# Rename the Object...
idc.set_name(self.OFFSET, object, SN_NOCHECK | SN_NOWARN | SN_FORCE)
idaapi.create_data(self.OFFSET, FF_QWORD, 0x8, BADNODE)
return self.type()
def resolve(self, alphabet, nids, symbols, libraries):
if self.INFO > Relocation.R_X86_64_ORBIS_GOTPCREL_LOAD:
self.INDEX = self.INFO >> 32
self.INFO &= 0xFF
symbol = next(value for key, value in enumerate(symbols) if key + 2 == self.INDEX)[1]
# Library
try:
lid1 = alphabet[symbol[12:13]]
# [base64]#
if symbol[13:14] == '#':
library = libraries[lid1]
# [base64][base64]#
elif symbol[14:15] == '#':
lid2 = alphabet[symbol[13:14]]
library = libraries[lid1 + lid2]
else:
raise
# Not a NID
except:
library = ''
# Function Name (Offset) == Symbol Value + AddEnd (S + A)
# Library Name (Offset) == Symbol Value (S)
real = idc.get_qword(self.OFFSET)
idc.add_func(real)
# Hacky way to determine if this is the real function...
real -= 0x6 if idc.print_insn_mnem(real) == 'push' else 0x0
# Resolve the NID...
try:
idc.set_cmt(real, 'NID: ' + symbol, False)
except:
pass
function = str(nids.get(symbol[:11], symbol))
# Rename the Jump Function...
idc.set_name(self.OFFSET, '__imp_' + function, SN_NOCHECK | SN_NOWARN | SN_FORCE)
# Rename the Real Function...
idc.set_name(real, function, SN_NOCHECK | SN_NOWARN | SN_FORCE)
try:
import_node = idaapi.netnode(str(library), 0, True)
import_node.supset(ea2node(real), function)
if hasattr(idaapi, 'import_module'):
# Requires customized loader.i / ida_loader.py(d)
idaapi.import_module(str(library), None, import_node.index(), None, 'linux')
else:
# Requires https://github.com/janisslsm/ida-ps4-helper
idaapi.ext.import_module(str(library), '', import_node.index(), 'linux')
except:
pass
return self.type()
class Symbol:
__slots__ = ('NAME', 'INFO', 'OTHER', 'SHINDEX', 'VALUE', 'SIZE')
# Symbol Information
ST_LOCAL_NONE = 0x0
ST_LOCAL_OBJECT = 0x1
ST_LOCAL_FUNCTION = 0x2
ST_LOCAL_SECTION = 0x3
ST_LOCAL_FILE = 0x4
ST_LOCAL_COMMON = 0x5
ST_LOCAL_TLS = 0x6
ST_GLOBAL_NONE = 0x10
ST_GLOBAL_OBJECT = 0x11
ST_GLOBAL_FUNCTION = 0x12
ST_GLOBAL_SECTION = 0x13
ST_GLOBAL_FILE = 0x14
ST_GLOBAL_COMMON = 0x15
ST_GLOBAL_TLS = 0x16
ST_WEAK_NONE = 0x20
ST_WEAK_OBJECT = 0x21
ST_WEAK_FUNCTION = 0x22
ST_WEAK_SECTION = 0x23
ST_WEAK_FILE = 0x24
ST_WEAK_COMMON = 0x25
ST_WEAK_TLS = 0x26
def __init__(self, f):
self.NAME = struct.unpack('<I', f.read(4))[0]
self.INFO = struct.unpack('<B', f.read(1))[0]
self.OTHER = struct.unpack('<B', f.read(1))[0]
self.SHINDEX = struct.unpack('<H', f.read(2))[0]
self.VALUE = struct.unpack('<Q', f.read(8))[0]
self.SIZE = struct.unpack('<Q', f.read(8))[0]
def info(self):
return {
Symbol.ST_LOCAL_NONE : 'Local : None',
Symbol.ST_LOCAL_OBJECT : 'Local : Object',
Symbol.ST_LOCAL_FUNCTION : 'Local : Function',
Symbol.ST_LOCAL_SECTION : 'Local : Section',
Symbol.ST_LOCAL_FILE : 'Local : File',
Symbol.ST_LOCAL_COMMON : 'Local : Common',
Symbol.ST_LOCAL_TLS : 'Local : TLS',
Symbol.ST_GLOBAL_NONE : 'Global : None',
Symbol.ST_GLOBAL_OBJECT : 'Global : Object',
Symbol.ST_GLOBAL_FUNCTION : 'Global : Function',
Symbol.ST_GLOBAL_SECTION : 'Global : Section',
Symbol.ST_GLOBAL_FILE : 'Global : File',
Symbol.ST_GLOBAL_COMMON : 'Global : Common',
Symbol.ST_GLOBAL_TLS : 'Global : TLS',
Symbol.ST_WEAK_NONE : 'Weak : None',
Symbol.ST_WEAK_OBJECT : 'Weak : Object',
Symbol.ST_WEAK_FUNCTION : 'Weak : Function',
Symbol.ST_WEAK_SECTION : 'Weak : Section',
Symbol.ST_WEAK_FILE : 'Weak : File',
Symbol.ST_WEAK_COMMON : 'Weak : Common',
Symbol.ST_WEAK_TLS : 'Weak : TLS',
}.get(self.INFO, 'Missing Symbol Information!!!')
def process(self, symbols):
if self.NAME != 0:
symbols[self.NAME] = 0
return self.info()
def resolve(self, address, nids, symbol):
# Resolve the NID...
try:
idc.set_cmt(self.VALUE, 'NID: ' + symbol, False)
except:
pass
function = nids.get(symbol[:11], symbol)
#print('Function: %s | number: %s' % (function, idaapi.get_func_num(self.VALUE)))
if idaapi.get_func_num(self.VALUE) > 0:
idc.del_func(self.VALUE)
if self.VALUE > 0:
idc.add_func(self.VALUE)
idc.add_entry(self.VALUE, self.VALUE, function, True)
idc.set_name(self.VALUE, function, SN_NOCHECK | SN_NOWARN | SN_FORCE)
idc.set_cmt(address, '%s | %s' % (function, self.info()), False)
# PROGRAM START
# Open File Dialog...
def accept_file(f, filename):
ps4 = Binary(f)
# No Kernels
if ps4.E_MACHINE == Binary.EM_X86_64 and ps4.E_START_ADDR < 0xFFFFFFFF82200000:
return { 'format': 'PS4 - ' + ps4.type(),
'options': ACCEPT_FIRST }
return 0
# Since IDA cannot create a compatibility layer to save its life...
def find_binary(address, end, search, format, flags):
# Is this really so hard Ilfak?
# Not only do you break it between the beta and the release candidate...
# You then have the audacity to write in your release notes that you added find_binary, but it is nowhere to be seen
# Feel free to take this version and modify it though to fit all edge cases, and in the spirit of your latest release, for free!
if idaapi.IDA_SDK_VERSION > 760:
binpat = idaapi.compiled_binpat_vec_t()
idaapi.parse_binpat_str(binpat, address, search, format)
# 9.0 RC1
try:
address, _ = idaapi.bin_search(address, end, binpat, flags)
# 9.0 Beta
except:
address, _ = idaapi.bin_search3(address, end, binpat, flags)
else:
address = idaapi.find_binary(address, end, search, format, flags)
return address
# Load NID Library...
def load_nids(location, nids = {}):
try:
with open(location) as database:
nids = dict(row for row in csv.reader(database, delimiter=' '))
except IOError:
retry = idaapi.ask_file(0, 'aerolib.csv|*.csv|All files (*.*)|*.*', 'Please gimme your aerolib.csv file')
if retry != None:
try:
with open(retry) as database:
nids = dict(row for row in csv.reader(database, delimiter=' '))
shutil.copy2(retry, location)
except:
print('Ok, no NIDs for you!')
else:
print('Ok, no NIDs for you!')
return nids
# Pablo's Scripts
def pablo(mode, address, end, search):
while address < end:
address = find_binary(address, end, search, 0x10, SEARCH_DOWN)
if address > idaapi.get_segm_by_name('CODE').end_ea:
offset = address - 0x3
if idaapi.is_unknown(idaapi.get_flags(offset)):
if idaapi.get_qword(offset) <= end:
idaapi.create_data(offset, FF_QWORD, 0x8, BADNODE)
address = offset + 0x4
else:
address += mode
idaapi.del_items(address, 0)
idaapi.create_insn(address)
idaapi.add_func(address, BADADDR)
address += 0x1
# Load Input Binary...
def load_file(f, neflags, format):
print('# PS4 Module Loader')
ps4 = Binary(f)
# PS4 Processor, Compiler, Library
bitness = ps4.procomp('metapc', CM_N64 | CM_M_NN | CM_CC_FASTCALL, 'ps4_errno_700')
# Load Aerolib...
nids = load_nids(idc.idadir() + '/loaders/aerolib.csv')
# Segment Loading...
for segm in ps4.E_SEGMENTS:
# Process Loadable Segments...
if segm.name() in ['CODE', 'DATA', 'SCE_RELRO', 'DYNAMIC', 'GNU_EH_FRAME', 'SCE_DYNLIBDATA']:
address = segm.MEM_ADDR if segm.name() not in ['DYNAMIC', 'SCE_DYNLIBDATA'] else segm.OFFSET + 0x1000000
size = segm.MEM_SIZE if segm.name() not in ['DYNAMIC', 'SCE_DYNLIBDATA'] else segm.FILE_SIZE
print('# Processing %s Segment...' % segm.name())
f.file2base(segm.OFFSET, address, address + segm.FILE_SIZE, FILEREG_PATCHABLE)
if segm.name() not in ['DYNAMIC', 'GNU_EH_FRAME']:
idaapi.add_segm(0, address, address + size, segm.name(), segm.type(), ADDSEG_NOTRUNC | ADDSEG_FILLGAP)
# Processor Specific Segment Details
idc.set_segm_addressing(address, bitness)
idc.set_segm_alignment(address, segm.alignment())
idc.set_segm_attr(address, SEGATTR_PERM, segm.flags())
# Process Dynamic Segment....
elif segm.name() == 'DYNAMIC':
stubs = {}
modules = {}
libraries = {}
f.seek(segm.OFFSET)
offset = segm.OFFSET
dynamic = address
dynamicsize = size
for entry in range(int(dynamicsize / 0x10)):
idc.set_cmt(address + (entry * 0x10), Dynamic(f).process(stubs, modules, libraries), False)
'''
# Process Exception Handling Segment...
elif segm.name() == 'GNU_EH_FRAME':
# Exception Handling Frame Header Structure
members = [('version', 'Version', 0x1),
('eh_frame_ptr_enc', 'Encoding of Exception Handling Frame Pointer', 0x1),
('fde_count_enc', 'Encoding of Frame Description Entry Count', 0x1),
('table_enc', 'Encoding of Table Entries', 0x1)]
struct = segm.struct('EHFrame', members)
idaapi.create_struct(address, 0x4, struct)
# Exception Handling Structure
members = [('exception', 'value', 0x8)]
struct = segm.struct('Exception', members)
for entry in range(int(size / 0x8)):
idaapi.create_struct(address + (entry * 0x8), 0x8, struct)
'''
# Process SCE 'Special' Shared Object Segment...
if segm.name() == 'SCE_DYNLIBDATA':
# SCE Fingerprint
idc.make_array(address, 0x14)
idc.set_name(address, 'SCE_FINGERPRINT', SN_NOCHECK | SN_NOWARN | SN_FORCE)
#idc.set_cmt(address, ' '.join(x.encode('hex') for x in idc.get_bytes(address, 0x14)).upper(), False)
# Dynamic Symbol Table
try:
# --------------------------------------------------------------------------------------------------------
# Dynamic Symbol Entry Structure
members = [('name', 'Name (String Index)', 0x4),
('info', 'Info (Binding : Type)', 0x1),
('other', 'Other', 0x1),
('shtndx', 'Section Index', 0x2),
('value', 'Value', 0x8),
('size', 'Size', 0x8)]
struct = segm.struct('Symbol', members)
# Dynamic Symbol Table
location = address + Dynamic.SYMTAB
f.seek(segm.OFFSET + Dynamic.SYMTAB)
symbols = {}
for entry in range(int(Dynamic.SYMTABSZ / 0x18)):
idaapi.create_struct(location + (entry * 0x18), 0x18, struct)
idc.set_cmt(location + (entry * 0x18), Symbol(f).process(symbols), False)
except:
pass