-
Notifications
You must be signed in to change notification settings - Fork 0
/
genprog_decomp_ida.py
1208 lines (979 loc) · 45.2 KB
/
genprog_decomp_ida.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
import os
import subprocess
import IPython
import argparse
import tempfile
import re
import shutil
import time
# path to idat binary
IDA_DEFAULT_PATH=os.environ['HOME']+"/seclab_ida/ida/idat"
if os.environ['IDA_BASE_DIR']:
IDA_PATH=os.environ['IDA_BASE_DIR']+"/idat"
else:
IDA_PATH=IDA_DEFAULT_PATH
# path to defs.h
DEFS_PATH=os.path.dirname(os.path.realpath(__file__))+"/refs/defs.h"
# stub markers for processing
IDA_STUB_START = "// Function declarations"
IDA_DATA_START = "// Data declarations"
IDA_SECTION_END = "//-----"
IDA_WEAK_LABEL = "; weak"
TYPEDEF_START = "============================== START =============================="
TYPEDEF_END = "============================== END =============================="
# tags for primitives for replacement
PRIMITIVES = ["int", "long", "short", "char", "void", "double", "float", "long",
"unsigned int", "unsigned long", "unsigned short", "unsigned char", "void", "long double"]
class IDAWrapper:
def __init__(self, typedefScriptPath):
self.typedefScriptPath = typedefScriptPath
# get initial decompiled output of ida hexrays
def decompile(self, binary_path, func_list):
if len(func_list) <= 0:
print("Empty List of Functions!!")
return ""
outname = "/tmp/"+func_list[0].strip()
funcs = ""
for func_name in func_list:
funcs += func_name.strip() + ":"
funcs = funcs[:-1] #trim dangling ':'
# ida run command
ida_command = [IDA_PATH, "-Ohexrays:-nosave:"+outname+":"+funcs, "-A", binary_path]
print("Running: ", " ".join(ida_command))
subprocess.run(ida_command)
functionLines = ""
if not os.path.exists(outname+".c"):
print(" !!! ERROR DECOMPILING FILE", outname+".c")
return ""
with open(outname+".c", "r") as decompFile:
functionLines = decompFile.read()
decompFile.close()
os.remove(outname+".c")
# print("="*30, "DECOMPILATION OUTPUT", "="*30)
# print(functionLines)
# print("="*70)
return functionLines
# # given a decompiled ida string, find all func calls in that string
# get all typedef mappings
def get_typedef_mappings(self, binary_path):
typedefMap = dict()
ida_command = [IDA_PATH, '-B', '-S'+"\""+self.typedefScriptPath+"\"", "-A", binary_path]
tmpName = ""
with tempfile.NamedTemporaryFile(mode="r", dir="/tmp", prefix="genprog-ida-",delete=True) as tmpFile:
print("RUNNING: ", " ".join(ida_command))
env = os.environ
env["IDALOG"] = tmpFile.name
sp = subprocess.run(ida_command, env=env)
typedefs = tmpFile.read()
tmpName = tmpFile.name
print(" -> Temp File Name:", tmpName)
tmpFile.close()
structDump = ""
latch = False
for line in typedefs.splitlines():
if TYPEDEF_START in line:
latch = True
elif TYPEDEF_END in line:
latch = False
elif latch:
if "/*" in line and "*/" in line:
structDump += "\n"
continue
structDump += line
print("FINISHED RUNNING")
return structDump
class CodeCleaner:
def __init__(self):
self.weakFuncs = []
def getTypeAndLabel(self, header):
if ")" in header and "((aligned(" not in header and "()" not in header:
array = header.rsplit(")", maxsplit=1)
hType = array[0].strip()+")"
else:
array = header.rsplit(maxsplit=1)
hType = array[0].strip()
if len(array) > 1:
hLabel = array[1].strip()
else:
hLabel = ""
hLabel = hLabel.strip("()")
while hLabel.startswith("*"):
hType = hType + " *"
hLabel = hLabel[1:]
if "__stdcall" in hType:
hType = hType.replace("__stdcall", "")
hLabel = hLabel.strip(";")
return hType, hLabel
def is_basic_typedef(self, line):
if not line.startswith("typedef"):
return False
if line.startswith("typedef struct"):
return False
if line.startswith("typedef union"):
return False
if "(" in line or "{" in line:
return False
return True
def get_type_label(self,argTypeRaw):
argType = self.get_typebase(argTypeRaw)
argTypeArray = argType.strip().rsplit(maxsplit=1)
if len(argTypeArray) > 1:
argTypeLabel = argTypeArray[-1]
else:
argTypeLabel = argTypeArray[0]
return argTypeLabel
def is_function_prototype(self,argType):
func=re.match("((struct\s+)?\w+)\s+\*?(\(\*\w+\)|\w+)\((.*)\)",argType)
types=[]
ret=False
if func:
print("Function: "+argType)
types.append(self.get_type_label(func.group(1)))
x=re.split(r',',func.group(4))
for t in x:
types.append(self.get_type_label(t))
ret=True
return ret,types
def get_typebase(self, argType):
argType = argType.strip()
while argType.endswith("*") or argType.startswith("*"):
argType= argType.strip("*")
argType = argType.strip()
return argType
def get_struct_args(self, argString):
argString = argString.strip()
args = argString.split(";")
argList = []
for arg in args:
arg = arg.split(":")[0]
arg = arg.strip()
if arg:
argType, argName = self.getTypeAndLabel(arg)
argList.append((argType, argName, arg))
return argList
# seperate each ordinal, filter out cases, establish bindings
def typedef_firstpass(self, structDump):
print(" > RUNNING FIRSTPASS")
lineDump = ""
for line in structDump.splitlines():
if "{" not in line and "}" not in line:
if line.count(";") > 1:
line = line.strip()
line = line.replace(";", ";\n")
lineDump += line+"\n"
return lineDump
def typedef_secondpass(self, structDump):
print(" > RUNNING SECOND PASS")
lineDump = ""
typedefMap = {}
structMap = {}
substituteMap = {}
for line in structDump.splitlines():
if "Elf" in line:
continue #skip
if line.startswith("typedef"):
elements = line.strip()[8:] #strip typedef + space
array = elements.split("(", maxsplit=1)
orig = ""
if len(array) > 1:
orig = array[0].strip()
newVal = "("+array[1].strip().strip(";")
else:
array = array[0].rsplit(maxsplit=1)
orig = array[0].strip()
newVal = array[1].strip().strip(";")
print(" << read line ", line)
typedefMap[orig] = (newVal, line)
elif line.startswith("struct ") or line.startswith("union "):
elements = line.strip().split(maxsplit=1)
header = elements[0].strip()
array = elements[1].split("{", maxsplit=1)
structDec = header+" "+array[0].strip()
structName = structDec.rsplit(maxsplit=1)[1]
print(" << read struct [%s] == %s" % (structName, structDec))
structMap[structName] = structDec
if structDec not in typedefMap.keys():
typedefMap[structDec] = (structName, line)
for line in structDump.splitlines():
if "Elf" in line:
continue #skip
if line.strip():
print(" !! Processing line ", line)
done = set()
for origName, typedefTuple in typedefMap.items():
if not line.startswith("typedef "+origName) and \
("{" in line or "(" in line): # found struct defines
# substitute typedefs with their original value
# this is so we can move the struct to before the typedefs themselves
if "{" in line:
argLine = "{"+line.split("{", maxsplit=1)[1]
else:
argLine = "("+line.split("(", maxsplit=1)[1]
newVal = typedefTuple[0]
print(" - Check for use of %s, originally [%s]" % (newVal, origName))
# print(" argline [[%s]] " % argLine)
typedefLine = typedefTuple[1]
escapedNewVal = re.escape(newVal)
matches = re.findall("[\\(\\{\\)\\}\\;\\,][\s]*"+escapedNewVal+"[\\(\\{\\)\\}\\;\\,\s]+", argLine)
for match in matches:
if origName in structMap.keys():
origName = structMap[origName]
if origName not in done:
newLine = match.replace(newVal, origName)
# print(" - replacing %s >> %s" % (match, newLine))
# print(" - original: ", line)
line = line.replace(match, newLine)
if matches:
print(" Associating %s with %s [%s]" % (typedefLine, line, origName))
done.add(origName)
# substituteMap[typedefLine] = line
# break
lineDump += line+"\n"
# print("Done, processing replacement structs")
return lineDump
def typedef_lastpass(self, structDump):
definitions = ""
for line in structDump.splitlines():
defLine = ""
if line.startswith("typedef"):
defLine = line
# print(" ---> ", defLine)
elif line.startswith("union"):
array = line.split("{")
name = array[0].split()[1].strip()
elems = array[1].strip().strip(";")
defLine = "typedef " + array[0] + "{" + elems + " " + name + ";\n"
elif line.startswith("enum"):
array = line.split("{")
header = array[0].split(":")[0].strip()
name = header.split()[1].strip()
enums = array[1]
enumLine = header + "{" + enums
typeDefLine = "typedef " + header + " " + name + ";"
defLine = enumLine + "\n" + typeDefLine
elif line.startswith("struct"):
# print("STRUCT: ", line)
line = line.strip(";") # prune out ending semicolon
header = line.split("{")[0].strip()
typeName = header.rsplit(maxsplit=1)[1].strip() # get name, drop struct prefixes
matches = re.findall("[\\(\\{\\;][\s]*"+typeName+" ", line)
for match in matches:
newLine = match.replace(typeName, header)
line = line.replace(match, newLine)
defLine = "typedef "+line+" "+typeName+";"
print(" ---> ", defLine)
definitions += defLine+"\n"
return definitions
def process_one_defline(self, definitions, line, waitingStructs, defined, forward_declared, typeDefMap):
print(" CHECKING: ", line)
rearranged = False
resolved = True
defline = line.strip(";") # prune out ending semicolon
# get rid of attributes, we don't care
defline,num = re.subn(r"__attribute__\(\(\w+(\(\w+\))?\)\)",r"",defline)
argString = ""
typedef_decl=False
if "{" in line or "(" in line:
array = defline.split("{")
header = array[0].strip()
f = header.rsplit(maxsplit=1)
struct_or_union = f[0].strip() # get struct_or_union
typeName = f[1].strip() # get name, drop struct prefixes
if line.startswith("typedef struct") or line.startswith("typedef union") :
body = array[1].strip()
argString = body.split("}")[0]
typedef_decl=True
else:
array = defline.split(")", maxsplit=1)
header = array[0].split("(")[1].strip()
typeName = header
body = array[1].strip()
args = body.split(")", maxsplit=1)[0].strip().strip("(")
argsArray = args.split(",")
argsArray.append("")
argString = " dummy;".join(argsArray)
elif line.startswith("typedef "):
array = defline.rsplit(maxsplit=1)
header = array[0]
simpleType = header.split(maxsplit=1)[1] #trim typedef
argString = simpleType+" dummy" # add dummy
typeName = array[1]
typeDefMap[self.get_typebase(typeName)] = simpleType
typeName = self.get_typebase(typeName)
# print(" - typeName [%s] args [%s]" % (typeName, argString))
if typeName in defined:
print(" - Already Processed!")
return definitions, rearranged
if argString:
args = self.get_struct_args(argString)
# print("ARGSTRING", argString)
# print(args)
for argTypeRaw, argName, argOrig in args:
isfunc,type_labels = self.is_function_prototype(argTypeRaw)
if not isfunc:
argType = self.get_typebase(argTypeRaw)
argTypeArray = argType.strip().rsplit(maxsplit=1)
# print(argTypeArray)
if len(argTypeArray) > 1:
type_labels = [ argTypeArray[-1] ]
else:
type_labels = [ argTypeArray[0] ]
# print(" --> baseType %s" % (argTypeLabel))
# print(" --> typeName %s" % (typeName))
# print(" - defined: ", defined)
for argTypeLabel in type_labels:
if argTypeLabel == typeName:
continue # skip self references
if argTypeLabel not in PRIMITIVES and argTypeLabel not in defined:
if not(argTypeLabel in forward_declared and \
(not self.is_basic_typedef(line))): # move non-struct non-union typedefs
if argTypeLabel not in waitingStructs.keys():
waitingStructs[argTypeLabel] = []
waitingStructs[argTypeLabel].append((line, argTypeLabel, argTypeRaw))
rearranged = True
resolved = False
print(" --> unresolved type", argTypeLabel)
print(" ", forward_declared, (argTypeLabel in forward_declared))
print(" %s || %s" % (line, argTypeRaw))
# print(" defined: ", defined)
if resolved:
print(" !! DEFINED", typeName)
defined.append(typeName)
definitions += line+"\n"
if typeName in waitingStructs.keys():
print(" !--> RESOLVING: ", typeName)
lines = waitingStructs[typeName]
waitingStructs.pop(typeName)
for line, argTypeLabel, argTypeRaw in lines:
print(" - Recursive process", typeName)
definitions, child_rearranged = self.process_one_defline(definitions, line, waitingStructs, defined, forward_declared, typeDefMap)
if not resolved or child_rearranged:
rearranged = True
print(" - RESOLVED!", typeName)
else:
rearranged = True
return definitions, rearranged
def recursive_dep_check(self, typeDefMap, waitingStructs, key):
if key in waitingStructs.keys():
return True
elif key in typeDefMap.keys():
print(" ## Recursing", key, "->", newKey)
newKey = typeDefMap[key]
return self.recursive_dep_check(typeDefMap, waitingStructs, newKey)
return False
def resolve_defs(self, structDump):
definitions = ""
defined= []
forward_declared = []
waitingStructs = {}
typeDefMap = {}
rearranged = False
for line in structDump.splitlines():
if line.startswith("typedef"):
definitions, child_rearranged = self.process_one_defline(definitions, line, waitingStructs, defined, forward_declared, typeDefMap)
if child_rearranged:
rearranged = True
elif line.startswith("struct"):
# forward declaration
typeName = line.strip().strip(";").rsplit(maxsplit=1)[1];
print(" !! FORWARD DECLARATION - STRUCT", typeName)
definitions += "struct "+typeName+";\n"
forward_declared.append(typeName)
elif line.startswith("union"):
# forward declaration
typeName = line.strip().strip(";").rsplit(maxsplit=1)[1];
print(" !! FORWARD DECLARATION - UNION", typeName)
forward_declared.append(typeName)
definitions += "union "+typeName+";\n"
else:
definitions += line+"\n"
remainingLines = []
potentialPlaceholders = set()
rejectedPlaceholders = set()
for needed, lines in waitingStructs.items():
for line, argTypeLabel, argTypeRaw in lines:
if line not in remainingLines:
remainingLines.append(line)
if not self.is_basic_typedef(line):
array = line.split("{")
header = array[0].strip()
f=header.rsplit(maxsplit=1) # get name, drop struct prefixes
struct_or_union = f[0].strip() # get name, drop struct prefixes
typeName = f[1].strip() # get name, drop struct prefixes
print("EVALUTING [%s] as Placeholder!" % typeName)
print(" ==", argTypeRaw)
print(" == ", (self.recursive_dep_check(typeDefMap, waitingStructs, typeName)))
print(" ==", (typeName not in rejectedPlaceholders))
if self.recursive_dep_check(typeDefMap, waitingStructs, typeName) and typeName not in rejectedPlaceholders:
if "union" in struct_or_union:
potentialPlaceholders.add("union "+typeName)
else:
potentialPlaceholders.add(typeName)
# if any of the waitingStructs use the needed placeholder without a pointer, reject this placeholder
if "*" not in argTypeRaw:
print("removing ", argTypeLabel, "as placeholder. argTypeLabel", argTypeLabel)
print(" line: ", line)
if argTypeLabel in potentialPlaceholders:
potentialPlaceholders.remove(argTypeLabel)
rejectedPlaceholders.add(argTypeLabel)
for placeholder in potentialPlaceholders:
# create placeholder forward declaration
if placeholder not in forward_declared:
if "union" in placeholder:
print("Adding Placeholder ", placeholder)
definitions += placeholder+";\n"
else:
print("Adding Placeholder Struct ", placeholder)
definitions += "struct "+placeholder+";\n"
for line in remainingLines:
definitions += line+"\n"
rearranged = True
return definitions, rearranged
def rearrange_typedefs(self, structDump):
MAXTRIES = len(structDump.splitlines())
rearranged = True
definitions = structDump
tries = 0
while tries < MAXTRIES and rearranged:
passCount = " Reordering typedefs [Pass %d] " % (tries+1)
print(("-"*5) + passCount + ("-"*5))
definitions, rearranged = self.resolve_defs(definitions)
tries += 1
print(("-"*10) + " Rearranged: " + str(rearranged) + " " + ("-"*10))
if tries >= MAXTRIES:
print(" !! ERROR !! Unable to resolve typedef order")
return definitions
# given typedef mapping, create defs
def cleanup_typedefs(self, structDump):
definitions = ""
structDump = self.typedef_firstpass(structDump)
structDump = self.typedef_secondpass(structDump)
structDump = self.typedef_lastpass(structDump)
structDump = self.rearrange_typedefs(structDump)
return structDump
def get_funcBody(self, lines, funcHeaders):
inFunc = False
funcBody = ""
funcList = []
for line in lines.splitlines():
if not inFunc:
for header in funcHeaders:
if header.strip(";") == line.strip():
inFunc = True
elif IDA_SECTION_END in line:
inFunc = False
funcList.append(funcBody)
funcBody = ""
else:
funcBody += line + "\n"
return funcList
def get_consts(self, lines):
consts = {}
constMap = {}
assignMap = {}
for line in lines.splitlines():
line = line.strip()
if line.startswith("const"):
print("Found const variable: ", line)
header = line.strip().split(";")[0]
name = header.rsplit(maxsplit=1)[1].strip()
consts[line] = name
elif "=" in line:
for init, name in consts.items():
if line.startswith(name):
array = line.split("=")
value = array[1].strip().strip(";")
constMap[init] = value
assignMap[init] = line
break
return constMap, assignMap
def handle_const_assigns(self, lines, funcHeaders):
funcs = self.get_funcBody(lines, funcHeaders)
# print(funcs)
for f in funcs:
constMap, assignLineMap = self.get_consts(f)
newFunc = f
for line, value in constMap.items():
assignLine = assignLineMap[line]
print("removing assignment", assignLine)
newFunc = newFunc.replace(assignLine, "")
array = line.strip().split(";")
header = array[0]
comments = array[1]
newHeader = header.strip().strip(";") + " = " + value + "; " + comments + "\n"
newFunc = newFunc.replace(line, newHeader)
lines = lines.replace(f, newFunc)
return lines
def remove_nonCGC_calls(self, output, targets):
for target in targets:
matches = re.findall("\\\n[\w\s*]*"+target+"\\(", output)
for m in matches:
newLine = m.replace(target, "// "+target)
output = output.replace(m, newLine)
return output
# remove decompilation artifacts
# basic string replacement to standardize the typedef replacements
def remove_artifacts(self, lines):
newlines = ""
for line in lines.splitlines():
if "<defs.h>" in line:
continue
# print(line)
# print("---------")
if "__cdecl" in line:
# print("replacing line")
line = line.replace("__cdecl", "")
# print("newline", line)
# handle :: classes
line = line.replace("::", "__")
# replace namings
line = line.replace("int64", "long")
line = line.replace("int32", "int")
line = line.replace("int16", "short")
line = line.replace("int8", "char")
line = line.replace("bool", "_Bool")
line = line.replace("_Bool", "_BoolDef") # TODO: dont use this dumbass workaround
line = line.replace("_DWORD", "int")
line = line.replace("_WORD", "short")
line = line.replace("_BYTE", "char")
line = line.replace("_UNKNOWN", "void")
# strip __ precursors
line = line.replace(" __long", " long")
line = line.replace(" __int", " int")
line = line.replace(" __short", " short")
line = line.replace(" __char", " char")
# line = line.replace(" __(", " (")
# line = line.replace("*__", "*")
# line = line.replace("LOWORD", "")
newlines += line+"\n"
return newlines
def get_data_declarations(self, lines):
inData = False
dataLines = []
dataMap = {}
removeList = []
for line in lines.splitlines():
if IDA_DATA_START in line:
inData = True
continue
elif IDA_SECTION_END in line:
inData = False
continue
if inData and len(line.strip()) > 0:
dataLines.append(line)
print("DATA LINES")
line = ""
for dataLine in dataLines:
if "Elf" in dataLine:
# removeList.append(dataLine+"\n")
continue
line += dataLine
if ";" not in dataLine:
line += "\n"
continue
line = line.strip().split(";")[0] + ";"
print("Original:", line)
header = line.split("=")[0].strip()
if header.startswith("//"):
header = header[3:] # handle commented out cases
dataType, dataName = self.getTypeAndLabel(header)
array_size=len(re.findall("\[\d*\]",dataName))
print("Array Size:", array_size)
defLine=""
if array_size>=2:
print("// --- WARNING! Two-dimensional array objects are not yet supported")
defLine += "%s *(p%s);\n" %(dataType, dataName)
dataName = dataName.split("[")[0] # handle arrays
defLine += "#define %s (*p%s)\n" % (dataName, dataName)
print(" // --- END OF WARNING!\n")
elif array_size==1 and "*" not in dataType:
dataName = dataName.split("[")[0] # handle arrays
defLine = "%s *(p%s);\n" %(dataType, dataName)
defLine += "#define %s (p%s)\n" % (dataName, dataName)
else:
defLine = "%s *(p%s);\n" %(dataType, dataName)
dataName = dataName.split("[")[0] # handle arrays
defLine += "#define %s (*p%s)\n" % (dataName, dataName)
if line.startswith("//"):
defLine += "//"
print(" ---->", defLine)
dataMap[line] = defLine
line = ""
print("REMOVE LIST", removeList)
return dataMap, removeList
def split_func_args(self, argString):
args = []
currentArg = ""
inBracket = False
for c in argString:
if c == "," and not inBracket:
args.append(currentArg)
currentArg = ""
else:
if c == "(":
inBracket = True
elif c == ")":
inBracket = False
currentArg += c
if currentArg: #add last elem
args.append(currentArg)
return args
def get_stubs(self, lines):
instubs = False
isFunc = False
stubs = []
funcs = []
for line in lines.splitlines():
if IDA_STUB_START in line:
instubs = True
continue
elif IDA_SECTION_END in line:
instubs = False
isFunc = True
continue
line = line.strip()
if instubs and len(line.strip()) > 0:
stubs.append(line)
elif isFunc and len(line.strip()) > 0 and not line.startswith("//"): #is part of function declarations
funcs.append(line)
isFunc = False
return stubs, funcs
def get_stub_name(self, stubLine):
header = stubLine.split("(", maxsplit=1)[0]
if header.startswith("//"):
header = header[3:]
stubType, name = self.getTypeAndLabel(header)
return name
# given list of stubs, create stubs
def make_pcgc_stubs(self, stublines, funcs):
stubMap = {}
nonCGCList = []
for stub in stublines:
skip = False
for funcline in funcs:
if funcline in stub:
skip = True # skip functions that are already declared below
# print(funcline, "in", stub)
break
if skip:
# print("Skipping ", stub)
continue
print("Processing stub ", stub)
array = stub.split("(", maxsplit=1)
header = array[0]
if IDA_WEAK_LABEL in stub:
self.weakFuncs.append(stub)
# handle comments
args = array[1].split(";", maxsplit=1)[0][:-1] #strip ) and ;
argsList = self.split_func_args(args) # args.split(",")
argTypes = []
# print("arglist", argsList)
count = 0
for arg in argsList:
arg = arg.strip()
if arg: # skip empty args
if arg == "...":
argTypes.append("...")
continue
argTuple = self.getTypeAndLabel(arg)
argType = argTuple[0]
if len(argTuple) < 2:
argName = "arg%d" % count
count += 1
else:
argName = argTuple[1]
print(" - ", argType, "||", argName)
argTypes.append(argType)
if header.startswith("//"):
header = header[3:] # handle commented out cases
ret_type, label = self.getTypeAndLabel(header)
# if "cgc_" not in label:
# print("----- WARN: Non-CGC stub found!! ------")
# print(label)
# nonCGCList.append(label)
# continue
print(" - RET [%s] LABEL [%s] ARGTYPES[%s]" % (ret_type, label, argTypes))
stubArgs = ", ".join(argTypes)
stubLine = "typedef " + ret_type + " (*p" + label + ")("+stubArgs+");\n"
stubNull = "p"+label+" "+label+" = NULL;\n"
stubMap[stub] = stubLine + stubNull
return stubMap, nonCGCList
def replace_stubs(self, output, stubMap):
for stub, replacement in stubMap.items():
output = output.replace(stub, replacement)
return output
def replace_data_defines(self, output, dataMap, removeList):
for data, replacement in dataMap.items():
print(" ---> Replacing [[%s]] with [[%s]]" %(data, replacement))
output = output.replace(data, replacement)
for target in removeList:
output = output.replace(target, "")
return output
def rename_target(self, output, target):
if target == "main": # special case
output = output.replace(target, "mypatch"+target)
else:
output = output.replace(target, "my"+target)
return output
def generate_det_placeholders(self):
placeholders = "void __prd_init() {\n}\n"
placeholders += "void __prd_exit() {\n}\n"
return placeholders
def generate_wrapper(self, target, funcs, stubMap, dataMap):
mainStub = "void main()\n" + \
"{\n\t%s(\n" % target
wrapperStub = ""
args = []
targetHeader = ""
targetRetType = "void"
for f in funcs:
print(target, f)
if target in f:
# print(f)
targetHeader = f
array = f.split("(", maxsplit=1)
targetRetType, targetName = self.getTypeAndLabel(array[0])
if len(array)<2:
break #no arguments
else:
argLine = array[1].strip(";").strip(")")
# print(f, argLine)
if len(argLine.strip()) <= 0:
break #no arguments
argArray = argLine.split(",")
for arg in argArray:
arg = arg.strip()
argTuple = self.getTypeAndLabel(arg)
args.append(argTuple)
break
if target == "main":
target = "patch" + target
targetHeader = targetHeader.replace("main", "patchmain")
wrapperStub += targetHeader.split("(", maxsplit=1)[0] #remove arguments
wrapperStub += "(\n"
print("dataMap", dataMap)
# arguments to wrapper function
for s in stubMap.keys():
mainStub += "\t\tNULL,\n"
wrapperStub += "\tvoid*"
if s in self.weakFuncs:
wrapperStub += "*"
wrapperStub += " my%s,\n" % self.get_stub_name(s)
print(s)
print(" - STUBNAME: ", self.get_stub_name(s))
# note from pdr: looks like when data declarations are included, the
# function prototype and funcstubs order of symbol definitions
# are not consistent
for d in dataMap.keys():
print("data", d)
mainStub += "\t\tNULL,\n"
dataDef = d.split(";")[0]
dataDef = dataDef.split("=")[0].strip()
dataType, dataName = self.getTypeAndLabel(dataDef)
array_size=len(re.findall("\[\d*\]",dataName))
if array_size>=2:
print("SORRY: two-dimensional array objects just aren't working right now")
print(" ==> "+dataType+" "+dataName)
wrapperStub += "// --- WARNING! Two-dimensional array objects are not yet supported"
wrapperStub += "\tvoid* my%s,\n" % dataName
elif array_size==1 and "*" not in dataType:
dataNamex = dataName.split("[")[0] # handle arrays
wrapperStub += "\tvoid* my%s,\n" % dataNamex
else:
wrapperStub += "\tvoid* my%s,\n" % dataName
print(" - DATA DECL: ", dataName)
for argTuple in args:
argType = argTuple[0]
argName = argTuple[1]
mainStub += "\t\t(%s) NULL,\n" % argType
wrapperStub += "\t%s %s,\n" % (argType, argName)
if stubMap or args: # list not empty
mainStub = mainStub[:-2] #strip ,\n
wrapperStub = wrapperStub[:-2] #strip ,\n
mainStub += "\n\t);\n"
mainStub += "}\n"
wrapperStub += "\n)\n{\n"
# create ret variable if needed
if targetRetType != "void":
wrapperStub += "\n\t%s retValue;\n\n" % targetRetType
# body
for d in dataMap.keys():
dataDef = d.split(";")[0]
if dataDef.startswith("//"):
dataDef = dataDef[3:] # handle commented out cases
dataDef = dataDef.split("=")[0].strip()
dataType, dataName = self.getTypeAndLabel(dataDef)
array_size=len(re.findall("\[\d*\]",dataName))
if array_size>=2:
print("// --- WARNING! Two-dimensional array objects are not yet supported\n")
wrapperStub += "\tp%s = (%s*) my%s;\n" % (dataName, dataType, dataName)
print(" // --- END OF WARNING!\n")
elif array_size==1 and "*" not in dataType:
dataNamex = dataName.split("[")[0] # handle arrays
wrapperStub += "\tp%s = (%s*) my%s;\n" % (dataNamex, dataType, dataNamex)
else:
wrapperStub += "\tp%s = (%s*) my%s;\n" % (dataName, dataType, dataName)
for s in stubMap.keys():
name = self.get_stub_name(s)
wrapperStub += "\t%s = (p%s) (" % (name, name)
if s in self.weakFuncs:
wrapperStub += "*"
wrapperStub += "my%s);\n" % (name)
numStubs = len(stubMap)
numFuncArgs = len(args)
wrapperStub += "\n\t__prd_init();\n"