forked from phracker/HopperScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demangle Strings.py
executable file
·51 lines (49 loc) · 1.86 KB
/
Demangle Strings.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
doc = Document.getCurrentDocument()
seg = doc.getCurrentSegment()
adr = seg.getStartingAddress()
last = adr + seg.getLength()
PIC = 0
#Loop through the whole code segment
#if you want to do a single proceedure
#it should be trivial to add in
while adr < last:
instr = seg.getInstructionAtAddress(adr)
#If the instruction sets the PIC Register value in a register
if instr.getInstructionString() == "mov":
register = instr.getFormattedArgument(0)
value = instr.getFormattedArgument(1)
if value.endswith("+_PIC_register_"):
adr += instr.getInstructionLength()
instr = seg.getInstructionAtAddress(adr)
value = instr.getFormattedArgument(1)
#And if it uses the newly set register in the next insturction
if register in value:
register += "+"
value = value.replace(register,"")
if value.startswith("0x"):
offset = int(value, 0)
offset = offset+PIC
#Extract the comment
segment = doc.getSegmentAtAddress(offset)
comment = "\""
while segment.readByte(offset) != 0:
comment += chr(segment.readByte(offset))
offset += 1
comment += "\" at " + hex(offset)
#Set it
seg.setInlineCommentAtAddress(adr, comment)
doc.log("[0x%X] Found calculated address of string %s" % (adr, comment))
#if we call the address of the next instruction
else:
if instr.getInstructionString() == "call":
value = instr.getFormattedArgument(0)
if value.startswith("0x"):
callTo = int(value, 0)
adr += instr.getInstructionLength()
#And if te call was to the next address, which was a pop
if callTo == adr:
instr = seg.getInstructionAtAddress(adr)
if instr.getInstructionString() == "pop":
#Reset our PIC
PIC = callTo
adr += instr.getInstructionLength()