Python angr.SimProcedure() Examples
The following are 20
code examples of angr.SimProcedure().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
angr
, or try the search function
.
Example #1
Source File: scaffold16.py From angr_ctf with GNU General Public License v3.0 | 8 votes |
def main(argv): path_to_binary = argv[1] project = angr.Project(path_to_binary) # You can either use a blank state or an entry state; just make sure to start # at the beginning of the program. initial_state = ??? class ReplacementScanf(angr.SimProcedure): # Hint: scanf("%u %20s") def run(self, format_string, ...???): # %u scanf0 = claripy.BVS('scanf0', ???) # %20s scanf1 = claripy.BVS('scanf1', ???) for char in scanf1.chop(bits=8): self.state.add_constraints(char >= ???, char <= ???) scanf0_address = ??? self.state.memory.store(scanf0_address, scanf0, endness=project.arch.memory_endness) ... self.state.globals['solutions'] = ???
Example #2
Source File: solve.py From angr-doc with BSD 2-Clause "Simplified" License | 6 votes |
def main(): proj = angr.Project('./baby-re', auto_load_libs=False) # let's provide the exact variables received through the scanf so we don't have to worry about parsing stdin into a bunch of ints. flag_chars = [claripy.BVS('flag_%d' % i, 32) for i in range(13)] class my_scanf(angr.SimProcedure): def run(self, fmt, ptr): # pylint: disable=arguments-differ,unused-argument self.state.mem[ptr].dword = flag_chars[self.state.globals['scanf_count']] self.state.globals['scanf_count'] += 1 proj.hook_symbol('__isoc99_scanf', my_scanf(), replace=True) sm = proj.factory.simulation_manager() sm.one_active.options.add(angr.options.LAZY_SOLVES) sm.one_active.globals['scanf_count'] = 0 # search for just before the printf("%c%c...") # If we get to 0x402941, "Wrong" is going to be printed out, so definitely avoid that. sm.explore(find=0x4028E9, avoid=0x402941) # evaluate each of the flag chars against the constraints on the found state to construct the flag flag = ''.join(chr(sm.one_found.solver.eval(c)) for c in flag_chars) return flag
Example #3
Source File: filter.py From hase with BSD 2-Clause "Simplified" License | 6 votes |
def __init__( self, project: Project, trace: List[Instruction], hooked_symbol: Dict[str, SimProcedure], gdb: "CoredumpGDB", omitted_section: List[List[int]], static_link: bool, name: str = "(unamed)", ) -> None: super().__init__(project, trace, hooked_symbol, gdb, omitted_section) self.trace_idx = [] # type: List[int] self.hook_target = {} # type: Dict[int, int] self.hook_entry = [] # type: List[Tuple[int, Instruction, str]] self.static_link = static_link self.name = name self.analyze_trace()
Example #4
Source File: scaffold16.py From angr_ctf with GNU General Public License v3.0 | 6 votes |
def main(argv): path_to_binary = argv[1] project = angr.Project(path_to_binary) # You can either use a blank state or an entry state; just make sure to start # at the beginning of the program. initial_state = ??? class ReplacementScanf(angr.SimProcedure): # Hint: scanf("%u %20s") def run(self, format_string, ...???): # %u scanf0 = claripy.BVS('scanf0', ???) # %20s scanf1 = claripy.BVS('scanf1', ???) for char in scanf1.chop(bits=8): self.state.add_constraints(char >= ???, char <= ???) scanf0_address = ??? self.state.memory.store(scanf0_address, scanf0, endness=project.arch.memory_endness) ... self.state.globals['solutions'] = ???
Example #5
Source File: scaffold16.py From angr_ctf with GNU General Public License v3.0 | 6 votes |
def main(argv): path_to_binary = argv[1] project = angr.Project(path_to_binary) # You can either use a blank state or an entry state; just make sure to start # at the beginning of the program. initial_state = ??? class ReplacementScanf(angr.SimProcedure): # Hint: scanf("%u %20s") def run(self, format_string, ...???): # %u scanf0 = claripy.BVS('scanf0', ???) # %20s scanf1 = claripy.BVS('scanf1', ???) for char in scanf1.chop(bits=8): self.state.add_constraints(char >= ???, char <= ???) scanf0_address = ??? self.state.memory.store(scanf0_address, scanf0, endness=project.arch.memory_endness) ... self.state.globals['solutions'] = ???
Example #6
Source File: test_sim_procedure.py From angr with BSD 2-Clause "Simplified" License | 6 votes |
def test_ret_float(): p = angr.load_shellcode(b'X', arch='i386') class F1(angr.SimProcedure): def run(self): return 12.5 p.hook(0x1000, F1(cc=p.factory.cc(func_ty=angr.sim_type.parse_file('float (x)();')[0]['x']))) p.hook(0x2000, F1(cc=p.factory.cc(func_ty=angr.sim_type.parse_file('double (x)();')[0]['x']))) s = p.factory.call_state(addr=0x1000, ret_addr=0) succ = s.step() nose.tools.assert_equal(len(succ.successors), 1) s2 = succ.flat_successors[0] nose.tools.assert_false(s2.regs.st0.symbolic) nose.tools.assert_equal(s2.solver.eval(s2.regs.st0.get_bytes(4, 4).raw_to_fp()), 12.5) s = p.factory.call_state(addr=0x2000, ret_addr=0) succ = s.step() nose.tools.assert_equal(len(succ.successors), 1) s2 = succ.flat_successors[0] nose.tools.assert_false(s2.regs.st0.symbolic) nose.tools.assert_equal(s2.solver.eval(s2.regs.st0.raw_to_fp()), 12.5)
Example #7
Source File: angr.py From deepstate with Apache License 2.0 | 5 votes |
def hook_function(project, ea, cls): """Hook the function `ea` with the SimProcedure `cls`.""" project.hook(ea, cls(project=project))
Example #8
Source File: filter.py From hase with BSD 2-Clause "Simplified" License | 5 votes |
def __init__( self, project: Project, trace: List[Instruction], hooked_symbol: Dict[str, SimProcedure], gdb: "CoredumpGDB", omitted_section: List[List[int]], ) -> None: self.project = project self.trace = trace self.hooked_symbol = hooked_symbol self.gdb = gdb self.new_trace = [] # type:List[Instruction] self.omitted_section = omitted_section self.hooked_symname = list(self.hooked_symbol.keys()) self.hooked_addon = {} # type: Dict[str, int] self.analyze_unsupported() self.syms = {} # type: Dict[Any, List[int]] self.syms_dict = {} # type: Dict[Any, Dict[int, Any]] for lib in self.project.loader.all_elf_objects: self.syms_dict[lib] = lib.symbols_by_addr.copy() self.syms[lib] = list(self.syms_dict[lib].keys()) self.syms[lib].sort()
Example #9
Source File: hook.py From hase with BSD 2-Clause "Simplified" License | 5 votes |
def hook_user_procedures(dct: Dict[str, Any], hook_IO: bool = True) -> None: procedures = [ memory_operation, group_operation, miscs, socket_operation, string_operation, time_operation, syscall, ] if hook_IO: procedures.append(file_operation) for module in procedures: for op in dir(module): obj = getattr(module, op) if isinstance(obj, type) and SimProcedure in obj.__mro__: if not getattr(obj, "INCOMPLETE", False): dct[op] = obj if getattr(obj, "IS_SYSCALL", False): ins = obj(display_name=op) ins.cc = None ins.is_syscall = True ins.NO_RET = False ins.ADDS_EXITS = False SIM_LIBRARIES["linux"].procedures[op] = ins
Example #10
Source File: readdir.py From angr with BSD 2-Clause "Simplified" License | 5 votes |
def instrument(self): """ Override this function to instrument the SimProcedure. The two useful variables you can override are self.struct, a named tuple of all the struct fields, and self.condition, the condition for whether the function succeeds. """ pass
Example #11
Source File: readdir.py From angr with BSD 2-Clause "Simplified" License | 5 votes |
def run(self, dirp): # pylint: disable=arguments-differ # TODO: make sure argument is actually a dir struct if self.state.arch.name != 'AMD64': l.error('readdir SimProcedure is only implemented for AMD64') return 0 self._build_amd64() self.instrument() malloc = angr.SIM_PROCEDURES['libc']['malloc'] pointer = self.inline_call(malloc, 19 + 256).ret_expr self._store_amd64(pointer) return self.state.solver.If(self.condition, pointer, self.state.solver.BVV(0, self.state.arch.bits))
Example #12
Source File: scaffold10.py From angr_ctf with GNU General Public License v3.0 | 4 votes |
def main(argv): path_to_binary = argv[1] project = angr.Project(path_to_binary) initial_state = project.factory.entry_state() # Define a class that inherits angr.SimProcedure in order to take advantage # of Angr's SimProcedures. class ReplacementCheckEquals(angr.SimProcedure): # A SimProcedure replaces a function in the binary with a simulated one # written in Python. Other than it being written in Python, the function # acts largely the same as any function written in C. Any parameter after # 'self' will be treated as a parameter to the function you are replacing. # The parameters will be bitvectors. Additionally, the Python can return in # the ususal Pythonic way. Angr will treat this in the same way it would # treat a native function in the binary returning. An example: # # int add_if_positive(int a, int b) { # if (a >= 0 && b >= 0) return a + b; # else return 0; # } # # could be simulated with... # # class ReplacementAddIfPositive(angr.SimProcedure): # def run(self, a, b): # if a >= 0 and b >=0: # return a + b # else: # return 0 # # Finish the parameters to the check_equals_ function. Reminder: # int check_equals_AABBCCDDEEFFGGHH(char* to_check, int length) { ... # (!) def run(self, to_check, ...???): # We can almost copy and paste the solution from the previous challenge. # Hint: Don't look up the address! It's passed as a parameter. # (!) user_input_buffer_address = ??? user_input_buffer_length = ??? # Note the use of self.state to find the state of the system in a # SimProcedure. user_input_string = self.state.memory.load( user_input_buffer_address, user_input_buffer_length ) check_against_string = ??? # Finally, instead of setting eax, we can use a Pythonic return statement # to return the output of this function. # Hint: Look at the previous solution. return claripy.If(???, ???, ???) # Hook the check_equals symbol. Angr automatically looks up the address # associated with the symbol. Alternatively, you can use 'hook' instead # of 'hook_symbol' and specify the address of the function. To find the # correct symbol, disassemble the binary. # (!)
Example #13
Source File: scaffold11.py From angr_ctf with GNU General Public License v3.0 | 4 votes |
def main(argv): path_to_binary = argv[1] project = angr.Project(path_to_binary) initial_state = project.factory.entry_state() class ReplacementScanf(angr.SimProcedure): # Finish the parameters to the scanf function. Hint: 'scanf("%u %u", ...)'. # (!) def run(self, format_string, scanf0_address, ...): scanf0 = claripy.BVS('scanf0', ???) ... # The scanf function writes user input to the buffers to which the # parameters point. self.state.memory.store(scanf0_address, scanf0, endness=project.arch.memory_endness) ... # Now, we want to 'set aside' references to our symbolic values in the # globals plugin included by default with a state. You will need to # store multiple bitvectors. You can either use a list, tuple, or multiple # keys to reference the different bitvectors. # (!) self.state.globals['solution0'] = ??? self.state.globals['solution1'] = ??? scanf_symbol = ??? project.hook_symbol(scanf_symbol, ReplacementScanf()) simulation = project.factory.simgr(initial_state) def is_successful(state): stdout_output = state.posix.dumps(sys.stdout.fileno()) return ??? def should_abort(state): stdout_output = state.posix.dumps(sys.stdout.fileno()) return ??? simulation.explore(find=is_successful, avoid=should_abort) if simulation.found: solution_state = simulation.found[0] # Grab whatever you set aside in the globals dict. stored_solutions0 = solution_state.globals['solution0'] ... solution = ??? print solution else: raise Exception('Could not find the solution')
Example #14
Source File: scaffoldxx.py From angr_ctf with GNU General Public License v3.0 | 4 votes |
def main(argv): path_to_binary = argv[1] project = angr.Project(path_to_binary) # You can either use a blank state or an entry state; just make sure to start # at the beginning of the program. # (!) initial_state = ??? # Again, scanf needs to be replaced. class ReplacementScanf(angr.SimProcedure): # Hint: scanf("%u %20s") def run(self, format_string, ...???): # %u scanf0 = claripy.BVS('scanf0', ???) # %20s scanf1 = claripy.BVS('scanf1', ???) # The bitvector.chop(bits=n) function splits the bitvector into a Python # list containing the bitvector in segments of n bits each. In this case, # we are splitting them into segments of 8 bits (one byte.) for char in scanf1.chop(bits=8): # Ensure that each character in the string is printable. An interesting # experiment, once you have a working solution, would be to run the code # without constraining the characters to the capital letters. # Even though the solution will technically work without this, it's more # difficult to enter in a solution that contains character you can't # copy, paste, or type into your terminal or the web form that checks # your solution. # If you are using the web form to submit answers, your solution must be # entirely alphanumeric except for spaces. # (!) self.state.add_constraints(char >= ???, char <= ???) # Warning: Endianness only applies to integers. If you store a string in # memory and treat it as a little-endian integer, it will be backwards. scanf0_address = ??? self.state.memory.store(scanf0_address, scanf0, endness=project.arch.memory_endness) ... self.state.globals['solution0'] = ??? ...
Example #15
Source File: scaffold15.py From angr_ctf with GNU General Public License v3.0 | 4 votes |
def main(argv): path_to_binary = argv[1] project = angr.Project(path_to_binary) # You can either use a blank state or an entry state; just make sure to start # at the beginning of the program. # (!) initial_state = ??? # Again, scanf needs to be replaced. class ReplacementScanf(angr.SimProcedure): # Hint: scanf("%u %20s") def run(self, format_string, ...???): # %u scanf0 = claripy.BVS('scanf0', ???) # %20s scanf1 = claripy.BVS('scanf1', ???) # The bitvector.chop(bits=n) function splits the bitvector into a Python # list containing the bitvector in segments of n bits each. In this case, # we are splitting them into segments of 8 bits (one byte.) for char in scanf1.chop(bits=8): # Ensure that each character in the string is printable. An interesting # experiment, once you have a working solution, would be to run the code # without constraining the characters to the capital letters. # Even though the solution will technically work without this, it's more # difficult to enter in a solution that contains character you can't # copy, paste, or type into your terminal or the web form that checks # your solution. # If you are using the web form to submit answers, your solution must be # entirely alphanumeric except for spaces. # (!) self.state.add_constraints(char >= ???, char <= ???) # Warning: Endianness only applies to integers. If you store a string in # memory and treat it as a little-endian integer, it will be backwards. scanf0_address = ??? self.state.memory.store(scanf0_address, scanf0, endness=project.arch.memory_endness) ... self.state.globals['solution0'] = ??? ...
Example #16
Source File: scaffold10.py From angr_ctf with GNU General Public License v3.0 | 4 votes |
def main(argv): path_to_binary = argv[1] project = angr.Project(path_to_binary) initial_state = project.factory.entry_state() # Define a class that inherits angr.SimProcedure in order to take advantage # of Angr's SimProcedures. class ReplacementCheckEquals(angr.SimProcedure): # A SimProcedure replaces a function in the binary with a simulated one # written in Python. Other than it being written in Python, the function # acts largely the same as any function written in C. Any parameter after # 'self' will be treated as a parameter to the function you are replacing. # The parameters will be bitvectors. Additionally, the Python can return in # the ususal Pythonic way. Angr will treat this in the same way it would # treat a native function in the binary returning. An example: # # int add_if_positive(int a, int b) { # if (a >= 0 && b >= 0) return a + b; # else return 0; # } # # could be simulated with... # # class ReplacementAddIfPositive(angr.SimProcedure): # def run(self, a, b): # if a >= 0 and b >=0: # return a + b # else: # return 0 # # Finish the parameters to the check_equals_ function. Reminder: # int check_equals_AABBCCDDEEFFGGHH(char* to_check, int length) { ... # (!) def run(self, to_check, ...???): # We can almost copy and paste the solution from the previous challenge. # Hint: Don't look up the address! It's passed as a parameter. # (!) user_input_buffer_address = ??? user_input_buffer_length = ??? # Note the use of self.state to find the state of the system in a # SimProcedure. user_input_string = self.state.memory.load( user_input_buffer_address, user_input_buffer_length ) check_against_string = ??? # Finally, instead of setting eax, we can use a Pythonic return statement # to return the output of this function. # Hint: Look at the previous solution. return claripy.If(???, ???, ???) # Hook the check_equals symbol. Angr automatically looks up the address # associated with the symbol. Alternatively, you can use 'hook' instead # of 'hook_symbol' and specify the address of the function. To find the # correct symbol, disassemble the binary. # (!)
Example #17
Source File: scaffold15.py From angr_ctf with GNU General Public License v3.0 | 4 votes |
def main(argv): path_to_binary = argv[1] project = angr.Project(path_to_binary) # You can either use a blank state or an entry state; just make sure to start # at the beginning of the program. # (!) initial_state = ??? # Again, scanf needs to be replaced. class ReplacementScanf(angr.SimProcedure): # Hint: scanf("%u %20s") def run(self, format_string, ...???): # %u scanf0 = claripy.BVS('scanf0', ???) # %20s scanf1 = claripy.BVS('scanf1', ???) # The bitvector.chop(bits=n) function splits the bitvector into a Python # list containing the bitvector in segments of n bits each. In this case, # we are splitting them into segments of 8 bits (one byte.) for char in scanf1.chop(bits=8): # Ensure that each character in the string is printable. An interesting # experiment, once you have a working solution, would be to run the code # without constraining the characters to the capital letters. # Even though the solution will technically work without this, it's more # difficult to enter in a solution that contains character you can't # copy, paste, or type into your terminal or the web form that checks # your solution. # If you are using the web form to submit answers, your solution must be # entirely alphanumeric except for spaces. # (!) self.state.add_constraints(char >= ???, char <= ???) # Warning: Endianness only applies to integers. If you store a string in # memory and treat it as a little-endian integer, it will be backwards. scanf0_address = ??? self.state.memory.store(scanf0_address, scanf0, endness=project.arch.memory_endness) ... self.state.globals['solution0'] = ??? ...
Example #18
Source File: solve11.py From angr_ctf with GNU General Public License v3.0 | 4 votes |
def main(argv): path_to_binary = argv[1] project = angr.Project(path_to_binary) initial_state = project.factory.entry_state() class ReplacementScanf(angr.SimProcedure): # Finish the parameters to the scanf function. Hint: 'scanf("%u %u", ...)'. # (!) def run(self, format_string, param0, param1): scanf0 = claripy.BVS('scanf0', 32) scanf1 = claripy.BVS('scanf1', 32) # The scanf function writes user input to the buffers to which the # parameters point. # Hint: scanf0_address is passed as a parameter, isn't it? scanf0_address = param0 self.state.memory.store(scanf0_address, scanf0, endness=project.arch.memory_endness) scanf1_address = param1 self.state.memory.store(scanf1_address, scanf1, endness=project.arch.memory_endness) # Now, we want to 'set aside' references to our symbolic values in the # globals plugin included by default with a state. You will need to # store multiple bitvectors. You can either use a list, tuple, or multiple # keys to reference the different bitvectors. # (!) self.state.globals['solutions'] = (scanf0, scanf1) scanf_symbol = '__isoc99_scanf' project.hook_symbol(scanf_symbol, ReplacementScanf()) simulation = project.factory.simgr(initial_state) def is_successful(state): stdout_output = state.posix.dumps(sys.stdout.fileno()) return 'Good Job.' in stdout_output def should_abort(state): stdout_output = state.posix.dumps(sys.stdout.fileno()) return 'Try again.' in stdout_output simulation.explore(find=is_successful, avoid=should_abort) if simulation.found: solution_state = simulation.found[0] # Grab whatever you set aside in the globals dict. stored_solutions = solution_state.globals['solutions'] solution = ' '.join(map(str, map(solution_state.se.eval, stored_solutions))) print solution else: raise Exception('Could not find the solution')
Example #19
Source File: scaffold10.py From angr_ctf with GNU General Public License v3.0 | 4 votes |
def main(argv): path_to_binary = argv[1] project = angr.Project(path_to_binary) initial_state = project.factory.entry_state() # Define a class that inherits angr.SimProcedure in order to take advantage # of Angr's SimProcedures. class ReplacementCheckEquals(angr.SimProcedure): # A SimProcedure replaces a function in the binary with a simulated one # written in Python. Other than it being written in Python, the function # acts largely the same as any function written in C. Any parameter after # 'self' will be treated as a parameter to the function you are replacing. # The parameters will be bitvectors. Additionally, the Python can return in # the ususal Pythonic way. Angr will treat this in the same way it would # treat a native function in the binary returning. An example: # # int add_if_positive(int a, int b) { # if (a >= 0 && b >= 0) return a + b; # else return 0; # } # # could be simulated with... # # class ReplacementAddIfPositive(angr.SimProcedure): # def run(self, a, b): # if a >= 0 and b >=0: # return a + b # else: # return 0 # # Finish the parameters to the check_equals_ function. Reminder: # int check_equals_AABBCCDDEEFFGGHH(char* to_check, int length) { ... # (!) def run(self, to_check, ...???): # We can almost copy and paste the solution from the previous challenge. # Hint: Don't look up the address! It's passed as a parameter. # (!) user_input_buffer_address = ??? user_input_buffer_length = ??? # Note the use of self.state to find the state of the system in a # SimProcedure. user_input_string = self.state.memory.load( user_input_buffer_address, user_input_buffer_length ) check_against_string = ??? # Finally, instead of setting eax, we can use a Pythonic return statement # to return the output of this function. # Hint: Look at the previous solution. return claripy.If(???, ???, ???) # Hook the check_equals symbol. Angr automatically looks up the address # associated with the symbol. Alternatively, you can use 'hook' instead # of 'hook_symbol' and specify the address of the function. To find the # correct symbol, disassemble the binary. # (!)
Example #20
Source File: scaffold15.py From angr_ctf with GNU General Public License v3.0 | 4 votes |
def main(argv): path_to_binary = argv[1] project = angr.Project(path_to_binary) # You can either use a blank state or an entry state; just make sure to start # at the beginning of the program. # (!) initial_state = ??? # Again, scanf needs to be replaced. class ReplacementScanf(angr.SimProcedure): # Hint: scanf("%u %20s") def run(self, format_string, ...???): # %u scanf0 = claripy.BVS('scanf0', ???) # %20s scanf1 = claripy.BVS('scanf1', ???) # The bitvector.chop(bits=n) function splits the bitvector into a Python # list containing the bitvector in segments of n bits each. In this case, # we are splitting them into segments of 8 bits (one byte.) for char in scanf1.chop(bits=8): # Ensure that each character in the string is printable. An interesting # experiment, once you have a working solution, would be to run the code # without constraining the characters to the capital letters. # Even though the solution will technically work without this, it's more # difficult to enter in a solution that contains character you can't # copy, paste, or type into your terminal or the web form that checks # your solution. # If you are using the web form to submit answers, your solution must be # entirely alphanumeric except for spaces. # (!) self.state.add_constraints(char >= ???, char <= ???) # Warning: Endianness only applies to integers. If you store a string in # memory and treat it as a little-endian integer, it will be backwards. scanf0_address = ??? self.state.memory.store(scanf0_address, scanf0, endness=project.arch.memory_endness) ... self.state.globals['solutions'] = ???