Skip to content

Commit 54de9d2

Browse files
committed
fixing enum issues and detour entry calls in main
1 parent 41a8469 commit 54de9d2

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

prd_multidecomp_ida.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
TYPES_REQUIRING_STDIO=['FILE']
6262

6363
DIETLIBC_TYPES=[
64-
'uint32_t','int32_t'
64+
'uint32_t','int32_t','uint8_t','int8_t','uint16_t','int16_t'
6565
]
6666

6767
STD_HEADER_TYPES=TYPES_REQUIRING_STDIO+CHDR_TYPES+DIETLIBC_TYPES
@@ -974,6 +974,9 @@ def typedef_resolution(self,structDump):
974974
idx=inline_def.index(True)
975975
_ltype['storage']=prefixes[idx].strip()
976976
base_type=req_type
977+
elif base_type in enum_types:
978+
t=re.sub(r"\btypedef\b",f"typedef enum",t)
979+
977980
elif base_type in collective_types:
978981
ref_line=type_to_dependencies[base_type]['line']
979982
ref_stor=type_to_dependencies[base_type]['storage']
@@ -1155,7 +1158,7 @@ def typedef_resolution(self,structDump):
11551158
# which allows for forward declaration use in the case of circular references in type definitions
11561159
# => i.e., when another struct or a function pointer has a field that's a struct/union
11571160
# without 'struct|union' keyword, prepend it to the field
1158-
# 4) similar to 3, but when a simple typedef references a struct or union type without that keyword, prepend it
1161+
# 4) similar to 3, but when a simple typedef references a enum, struct or union type without that keyword, prepend it
11591162
for i in list(type_to_dependencies.keys()):
11601163
line=type_to_dependencies[i]['line']
11611164
reqs=type_to_dependencies[i]['reqs']
@@ -1237,6 +1240,10 @@ def typedef_resolution(self,structDump):
12371240
(not line.startswith("typedef "+prefix))
12381241
):
12391242
line=re.sub(r"\btypedef\b",f"typedef {prefix}",line.strip())
1243+
elif i in enum_types:
1244+
print(f"UPDATING {i} DUE TO ENUM TYPE => {line}");
1245+
line=re.sub(r"\btypedef\b",f"typedef enum",line.strip())
1246+
12401247
type_to_dependencies[i]['line']=line
12411248

12421249
pass
@@ -2423,6 +2430,9 @@ def generate_wrapper(self, target_list, funcs, stubMap, dataMap, detour_prefix,
24232430
rev_trans={v:k for k,v in translation_dict.items()}
24242431
mainStub = "int main()\n" + \
24252432
"{\n"
2433+
mainStub_t="";
2434+
mainStub_pre=list();
2435+
mainStub_post=list();
24262436
wrapperStub = ""
24272437
#translation_dict = dict()
24282438
# keys are the expected decompiled function name, value is actual decompiled function name
@@ -2439,7 +2449,7 @@ def generate_wrapper(self, target_list, funcs, stubMap, dataMap, detour_prefix,
24392449
#if target == "main":
24402450
# #translation_dict["main"]="patchmain"
24412451
# detour_target="{}{}".format(detour_prefix,"patchmain")
2442-
mainStub += f"\t{detour_target}(\n"
2452+
mainStub_t += f"\t{detour_target}(\n"
24432453
print("Detour target: {}:{} => {} ".format(ltarget,trans_targ,detour_target))
24442454

24452455
args = []
@@ -2518,15 +2528,15 @@ def generate_wrapper(self, target_list, funcs, stubMap, dataMap, detour_prefix,
25182528
#print("dataMap", dataMap)
25192529
ebx_prefix=False
25202530
if glibc_symbols and (len(stubMap[target].keys())>0 or len(dataMap[target].keys())>0):
2521-
mainStub += "\t\tNULL,\n"
2531+
mainStub_t += "\t\tNULL,\n"
25222532
wrapperStub += "\tvoid* EBX,\n"
25232533
init_mainBody= "\torigPLT_EBX = (unsigned int) EBX;\n"
25242534
ebx_prefix=True
25252535
# arguments to wrapper function
25262536
for s in stubMap[target].keys():
25272537
s_name=self.get_stub_name(s)
25282538
s_name=translation_dict.get(s_name,s_name)
2529-
mainStub += "\t\tNULL,\n"
2539+
mainStub_t += "\t\tNULL,\n"
25302540
wrapperStub += "\tvoid*"
25312541
if s in self.weakFuncs:
25322542
wrapperStub += "*"
@@ -2546,7 +2556,7 @@ def generate_wrapper(self, target_list, funcs, stubMap, dataMap, detour_prefix,
25462556
ZERO_PARAMS=True
25472557
for d in dataMap[target].keys():
25482558
print("data", d)
2549-
mainStub += "\t\tNULL,\n"
2559+
mainStub_t += "\t\tNULL,\n"
25502560
dataDef = d.split(";")[0]
25512561
dataDef = dataDef.split("=")[0].strip()
25522562
dataType, dataName = self.getTypeAndLabel(dataDef)
@@ -2576,17 +2586,22 @@ def generate_wrapper(self, target_list, funcs, stubMap, dataMap, detour_prefix,
25762586
argType = argTuple[0]
25772587
argName = argTuple[1]
25782588
if "double" in argType or "float" in argType or "int" in argType:
2579-
mainStub += "\t\t(%s) 0,\n" % argType
2589+
mainStub_t += "\t\t(%s) 0,\n" % argType
25802590
else:
2581-
mainStub += "\t\t(%s) NULL,\n" % argType
2591+
varname=f"v{len(mainStub_pre)}";
2592+
malloc=f"\t{argType}* {varname}=malloc(sizeof({argType}));"
2593+
free=f"\tfree({varname});"
2594+
mainStub_pre.append(malloc);
2595+
mainStub_post.append(free);
2596+
mainStub_t += f"\t\t{varname},\n"
25822597
wrapperStub += "\t%s %s,\n" % (argType, argName)
25832598

2584-
if mainStub.rstrip().endswith(','):
2585-
mainStub = mainStub.rstrip()[:-1] #strip ,\n
2599+
if mainStub_t.rstrip().endswith(','):
2600+
mainStub_t = mainStub_t.rstrip()[:-1] #strip ,\n
25862601
if wrapperStub.rstrip().endswith(','):
25872602
wrapperStub = wrapperStub.rstrip()[:-1] #strip ,\n
25882603

2589-
mainStub += "\n\t);\n"
2604+
mainStub_t += "\n\t);\n"
25902605
# pdr : need to move this outside of FOR loop
25912606
#mainStub += "}\n"
25922607

@@ -2690,6 +2705,9 @@ def generate_wrapper(self, target_list, funcs, stubMap, dataMap, detour_prefix,
26902705
# print(wrapperStub)
26912706

26922707
# pdr : move this to outside of FOR loop
2708+
mainStub += "\n".join(mainStub_pre)+"\n";
2709+
mainStub += mainStub_t;
2710+
mainStub += "\n".join(mainStub_post)+"\n";
26932711
mainStub += "\treturn 0;\n"
26942712
mainStub += "}\n"
26952713

0 commit comments

Comments
 (0)