Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to work on Python 3 #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions deflatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ def rel(addr):
# Unconditional jmp
if self.is_uncond:
next_addr = self.true_block.start
print '[+] Patching from {:x} to {:x}'.format(base_addr, next_addr)
print('[+] Patching from {:x} to {:x}'.format(base_addr, next_addr))
return safe_asm(bv, 'jmp {}'.format(rel(next_addr)))

# Branch based on original cmovcc
else:
assert self.il is not None
true_addr = self.true_block.start
false_addr = self.false_block.start
print '[+] Patching from {:x} to T: {:x} F: {:x}'.format(base_addr,
print('[+] Patching from {:x} to T: {:x} F: {:x}'.format(base_addr,
true_addr,
false_addr)
false_addr))

# Find the cmovcc by looking at the def il's incoming edges
# Both parent blocks are part of the same cmov
Expand Down Expand Up @@ -240,7 +240,7 @@ def _fix_call(bv, addr, newaddr):

# Rebuild the block, skipping the bad instrs
addr = block.start
data = ''
data = b''
while addr < block.end:
# How much data to read
ilen = bv.get_instruction_length(addr)
Expand Down Expand Up @@ -268,7 +268,7 @@ def gather_full_backbone(backbone_map):
set: All BasicBlocks involved in any form in the backbone
"""
# Get the immediately known blocks from the map
backbone_blocks = backbone_map.values()
backbone_blocks = list(backbone_map.values())
backbone_blocks += [bb.outgoing_edges[1].target for bb in backbone_blocks]

# Some of these blocks might be part of a chain of unconditional jumps back to the top of the backbone
Expand Down Expand Up @@ -296,21 +296,21 @@ def deflatten_cfg(bv, addr):

# compute all usages of the state_var
backbone = compute_backbone_map(bv, mlil, state_var)
print '[+] Computed backbone'
print('[+] Computed backbone')
pprint(backbone)

# compute all the defs of the state_var in the original basic blocks
original = compute_original_blocks(bv, mlil, state_var)
print '[+] Usages of the state variable in original basic blocks'
print('[+] Usages of the state variable in original basic blocks')
pprint(original)

# at this point we have all the information to reconstruct the CFG
CFG = [resolve_cfg_link(bv, mlil, il, backbone) for il in original]
print '[+] Computed original CFG'
print('[+] Computed original CFG')
pprint(CFG)

# patch in all the changes
print '[+] Patching all discovered links'
print('[+] Patching all discovered links')
for link in CFG:
# Clean out instructions we don't need to make space
blockdata, cave_addr, orig_len = clean_block(bv, mlil, link)
Expand All @@ -321,10 +321,10 @@ def deflatten_cfg(bv, addr):
bv.write(link.block.start, blockdata)

# Do some final cleanup
print '[+] NOPing backbone'
print('[+] NOPing backbone')
nop = safe_asm(bv, 'nop')
for bb in gather_full_backbone(backbone):
print '[+] NOPing block: {}'.format(bb)
print('[+] NOPing block: {}'.format(bb))
bv.write(bb.start, nop * bb.length)


Expand Down