Skip to content

Commit

Permalink
make sure all blocks are valid after the execution
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Kyle committed Apr 29, 2024
1 parent f566a9c commit 8c7d117
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
11 changes: 10 additions & 1 deletion angrop/gadget_finder/gadget_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def analyze_gadget(self, addr):

init_state, final_state = self._reach_unconstrained_or_syscall(addr)

if self._change_arch_state(init_state, final_state):
if not self._valid_state(init_state, final_state):
return None

ctrl_type = self._check_for_control_type(init_state, final_state)
Expand Down Expand Up @@ -101,6 +101,15 @@ def analyze_gadget(self, addr):
l.debug("... Appending gadget!")
return gadget

def _valid_state(self, init_state, final_state):
if self._change_arch_state(init_state, final_state):
return False
for addr in final_state.history.bbl_addrs:
b = final_state.project.factory.block(addr)
if not self.arch.block_make_sense(b):
return False
return True

def _change_arch_state(self, init_state, final_state):
if isinstance(self.arch, X86):
for reg in self.arch.segment_regs:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_find_gadgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,30 @@ def test_shift_gadget():
assert all(not gadget_exists(rop, x) for x in [0x438a91, 0x516fb2])
assert all(gadget_exists(rop, x) for x in [0x454e75, 0x5622d5, 0x490058])

def test_i386_syscall():
proj = angr.Project(os.path.join(tests_dir, "i386", "angrop_syscall_test"), auto_load_libs=False)

rop = proj.analyses.ROP()
"""
804918c int 0x80
"""
"""
8049195 mov esp, 0x804c038
804919a ret
"""

assert all(gadget_exists(rop, x) for x in [0x804918c, 0x8049195])

"""
8049189 syscall
"""

"""
804918f mov esp, 0x804c020
8049194 ret
"""
assert all(not gadget_exists(rop, x) for x in [0x8049189, 0x804918f])

def run_all():
functions = globals()
all_functions = {x:y for x, y in functions.items() if x.startswith('test_')}
Expand Down

0 comments on commit 8c7d117

Please sign in to comment.