Created
June 9, 2016 11:50
-
-
Save devnoname120/261c4f6d95b2c079c00705492f91e7a1 to your computer and use it in GitHub Desktop.
uOFW: Extract function prototypes from the assembly and improve them and add documentation from header files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Makeshift RE'ing base file creator. The code is really messy, don't expect it to be robust. | |
import re | |
asm = 'E:/Programmation/PSP/Reverse engineering/threadman - 660/threadman.s' | |
proto = ['E:/Programmation/PSP/Reverse engineering/threadman - 660/pspthreadman.h','E:/Programmation/PSP/Reverse engineering/threadman - 660/pspthreadman_kernel.h'] | |
protos = {} | |
def protoBase(): | |
global protos | |
# Extract comments, and function prototype | |
# Optionally retrieve the comments right before a function, (?!\/\*\*) ensures that we only retrieve the closest comment section, not above sections as well | |
# (?: \*)? enables to extract pointer arguments as well | |
# ([^;]*) retrieves the arguments until a ; (newline allowed in arguments) | |
m1 = re.compile("(\/\*\*(?:[\s\S](?!\/\*\*))+?(?=\*\/)\*\/\n)?((?:(?:unsigned|enum|struct) )?\w+(?: \*)? ?)(\w+)\(([^;]*)\);") | |
for file in proto: | |
pf = open(file).read() | |
iterator = m1.finditer(pf) | |
for match in iterator: | |
# Name, return, args | |
protos[match.group(3)] = {'ret': match.group(2), 'args': match.group(4), 'comments':match.group(1)} | |
protoBase() | |
f = open(asm).read() | |
# The second part avoids to take imported functions | |
m = re.compile("; Subroutine ([^ ]+) - Address (0x[0-9A-F]{8})(?! ?\n; Imported from)", re.M) | |
iterator = m.finditer(f) | |
for match in iterator: | |
# Prototype externally available | |
if match.group(1) in protos: | |
print("\n" + protos[match.group(1)]['comments'] + "// TODO: Reverse function " + match.group(1) + "\n// Subroutine " + match.group(1) + " - Address " + match.group(2) + "\n" + protos[match.group(1)]['ret'] + match.group(1) + "(" + protos[match.group(1)]['args'] + ")\n{\n \n}") | |
else: | |
print("\n// TODO: Reverse function " + match.group(1) + "\n// Subroutine " + match.group(1) + " - Address " + match.group(2) + "\nvoid " + match.group(1) + "()\n{\n \n}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment