angr 9.2.87__py3-none-manylinux2014_x86_64.whl → 9.2.89__py3-none-manylinux2014_x86_64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of angr might be problematic. Click here for more details.
- angr/__init__.py +4 -1
- angr/analyses/decompiler/clinic.py +16 -0
- angr/analyses/decompiler/decompiler.py +3 -0
- angr/analyses/decompiler/optimization_passes/__init__.py +5 -0
- angr/analyses/decompiler/optimization_passes/cross_jump_reverter.py +108 -0
- angr/analyses/decompiler/optimization_passes/optimization_pass.py +17 -4
- angr/analyses/decompiler/optimization_passes/return_duplicator.py +4 -32
- angr/analyses/decompiler/structured_codegen/c.py +12 -2
- angr/analyses/decompiler/utils.py +13 -0
- angr/analyses/typehoon/dfa.py +108 -0
- angr/analyses/typehoon/lifter.py +34 -2
- angr/analyses/typehoon/simple_solver.py +1043 -503
- angr/analyses/typehoon/translator.py +13 -4
- angr/analyses/typehoon/typeconsts.py +117 -36
- angr/analyses/typehoon/typehoon.py +31 -11
- angr/analyses/typehoon/typevars.py +88 -21
- angr/analyses/typehoon/variance.py +10 -0
- angr/analyses/variable_recovery/engine_ail.py +28 -9
- angr/analyses/variable_recovery/engine_base.py +50 -43
- angr/analyses/variable_recovery/variable_recovery_base.py +16 -3
- angr/analyses/variable_recovery/variable_recovery_fast.py +14 -5
- angr/exploration_techniques/tracer.py +2 -0
- angr/misc/autoimport.py +26 -0
- angr/procedures/definitions/__init__.py +32 -3
- angr/utils/constants.py +1 -0
- angr/utils/graph.py +20 -1
- {angr-9.2.87.dist-info → angr-9.2.89.dist-info}/METADATA +7 -6
- {angr-9.2.87.dist-info → angr-9.2.89.dist-info}/RECORD +32 -244
- angr-9.2.89.dist-info/top_level.txt +1 -0
- angr/procedures/definitions/ntdll.py +0 -12
- angr-9.2.87.dist-info/top_level.txt +0 -2
- tests/__init__.py +0 -0
- tests/analyses/__init__.py +0 -0
- tests/analyses/cfg/__init__.py +0 -0
- tests/analyses/cfg/test_cfg_clflush.py +0 -43
- tests/analyses/cfg/test_cfg_get_any_node.py +0 -34
- tests/analyses/cfg/test_cfg_manager.py +0 -32
- tests/analyses/cfg/test_cfg_model.py +0 -55
- tests/analyses/cfg/test_cfg_patching.py +0 -378
- tests/analyses/cfg/test_cfg_rust_got_resolution.py +0 -36
- tests/analyses/cfg/test_cfg_thumb_firmware.py +0 -50
- tests/analyses/cfg/test_cfg_vex_postprocessor.py +0 -27
- tests/analyses/cfg/test_cfgemulated.py +0 -634
- tests/analyses/cfg/test_cfgfast.py +0 -1123
- tests/analyses/cfg/test_cfgfast_soot.py +0 -38
- tests/analyses/cfg/test_const_resolver.py +0 -38
- tests/analyses/cfg/test_iat_resolver.py +0 -37
- tests/analyses/cfg/test_jumptables.py +0 -3008
- tests/analyses/cfg/test_noop_blocks.py +0 -54
- tests/analyses/cfg_slice_to_sink/__init__.py +0 -0
- tests/analyses/cfg_slice_to_sink/test_cfg_slice_to_sink.py +0 -93
- tests/analyses/cfg_slice_to_sink/test_graph.py +0 -114
- tests/analyses/cfg_slice_to_sink/test_transitions.py +0 -28
- tests/analyses/decompiler/__init__.py +0 -0
- tests/analyses/decompiler/test_baseptr_save_simplifier.py +0 -80
- tests/analyses/decompiler/test_decompiler.py +0 -3336
- tests/analyses/decompiler/test_peephole_optimizations.py +0 -48
- tests/analyses/decompiler/test_propagator_loops.py +0 -101
- tests/analyses/decompiler/test_structurer.py +0 -275
- tests/analyses/reaching_definitions/__init__.py +0 -0
- tests/analyses/reaching_definitions/test_dep_graph.py +0 -432
- tests/analyses/reaching_definitions/test_function_handler.py +0 -131
- tests/analyses/reaching_definitions/test_heap_allocator.py +0 -46
- tests/analyses/reaching_definitions/test_rd_state.py +0 -78
- tests/analyses/reaching_definitions/test_reachingdefinitions.py +0 -463
- tests/analyses/reaching_definitions/test_subject.py +0 -76
- tests/analyses/test_bindiff.py +0 -52
- tests/analyses/test_block_simplifier.py +0 -112
- tests/analyses/test_boyscout.py +0 -104
- tests/analyses/test_calling_convention_analysis.py +0 -352
- tests/analyses/test_callsite_maker.py +0 -60
- tests/analyses/test_cdg.py +0 -165
- tests/analyses/test_cfb.py +0 -37
- tests/analyses/test_class_identifier.py +0 -46
- tests/analyses/test_clinic.py +0 -30
- tests/analyses/test_codetagging.py +0 -32
- tests/analyses/test_constantpropagation.py +0 -88
- tests/analyses/test_ddg.py +0 -95
- tests/analyses/test_ddg_global_var_dependencies.py +0 -83
- tests/analyses/test_ddg_memvar_addresses.py +0 -40
- tests/analyses/test_disassembly.py +0 -121
- tests/analyses/test_find_objects_static.py +0 -35
- tests/analyses/test_flirt.py +0 -49
- tests/analyses/test_identifier.py +0 -33
- tests/analyses/test_init_finder.py +0 -38
- tests/analyses/test_proximitygraph.py +0 -31
- tests/analyses/test_reassembler.py +0 -295
- tests/analyses/test_regionidentifier.py +0 -27
- tests/analyses/test_slicing.py +0 -164
- tests/analyses/test_stack_pointer_tracker.py +0 -74
- tests/analyses/test_static_hooker.py +0 -28
- tests/analyses/test_typehoon.py +0 -55
- tests/analyses/test_variablerecovery.py +0 -464
- tests/analyses/test_vfg.py +0 -221
- tests/analyses/test_vtable.py +0 -31
- tests/analyses/test_xrefs.py +0 -77
- tests/common.py +0 -128
- tests/engines/__init__.py +0 -0
- tests/engines/light/__init__.py +0 -0
- tests/engines/light/test_data.py +0 -17
- tests/engines/pcode/__init__.py +0 -0
- tests/engines/pcode/test_emulate.py +0 -607
- tests/engines/pcode/test_pcode.py +0 -84
- tests/engines/test_actions.py +0 -27
- tests/engines/test_hook.py +0 -112
- tests/engines/test_java.py +0 -697
- tests/engines/test_unicorn.py +0 -518
- tests/engines/vex/__init__.py +0 -0
- tests/engines/vex/test_lifter.py +0 -124
- tests/engines/vex/test_vex.py +0 -574
- tests/exploration_techniques/__init__.py +0 -0
- tests/exploration_techniques/test_cacher.py +0 -45
- tests/exploration_techniques/test_director.py +0 -67
- tests/exploration_techniques/test_driller_core.py +0 -48
- tests/exploration_techniques/test_loop_seer.py +0 -158
- tests/exploration_techniques/test_memory_watcher.py +0 -46
- tests/exploration_techniques/test_oppologist.py +0 -65
- tests/exploration_techniques/test_spiller.py +0 -82
- tests/exploration_techniques/test_stochastic.py +0 -40
- tests/exploration_techniques/test_tech_builder.py +0 -61
- tests/exploration_techniques/test_tracer.py +0 -856
- tests/exploration_techniques/test_unique.py +0 -40
- tests/exploration_techniques/test_veritesting.py +0 -120
- tests/factory/__init__.py +0 -0
- tests/factory/block/__init__.py +0 -0
- tests/factory/block/test_block_cache.py +0 -33
- tests/factory/block/test_keystone.py +0 -106
- tests/factory/test_argc.py +0 -101
- tests/factory/test_argc_sym.py +0 -110
- tests/factory/test_argv.py +0 -158
- tests/factory/test_callable.py +0 -266
- tests/factory/test_windows_args.py +0 -36
- tests/knowledge_plugins/__init__.py +0 -0
- tests/knowledge_plugins/cfg/__init__.py +0 -0
- tests/knowledge_plugins/cfg/test_cfg_manager.py +0 -36
- tests/knowledge_plugins/functions/__init__.py +0 -0
- tests/knowledge_plugins/functions/test_function.py +0 -91
- tests/knowledge_plugins/functions/test_function2.py +0 -79
- tests/knowledge_plugins/functions/test_function_manager.py +0 -139
- tests/knowledge_plugins/functions/test_prototypes.py +0 -53
- tests/knowledge_plugins/key_definitions/__init__.py +0 -0
- tests/knowledge_plugins/key_definitions/test_atoms.py +0 -24
- tests/knowledge_plugins/key_definitions/test_environment.py +0 -126
- tests/knowledge_plugins/key_definitions/test_heap_address.py +0 -27
- tests/knowledge_plugins/key_definitions/test_live_definitions.py +0 -72
- tests/knowledge_plugins/test_dwarf_variables.py +0 -240
- tests/knowledge_plugins/test_kb_plugins.py +0 -91
- tests/knowledge_plugins/test_kb_plugins_dwarf.py +0 -36
- tests/knowledge_plugins/test_patches.py +0 -48
- tests/misc/__init__.py +0 -0
- tests/misc/test_hookset.py +0 -57
- tests/perf/__init__.py +0 -0
- tests/perf/perf_cfgemulated.py +0 -19
- tests/perf/perf_cfgfast.py +0 -18
- tests/perf/perf_concrete_execution.py +0 -41
- tests/perf/perf_siminspect_nop.py +0 -36
- tests/perf/perf_state_copy.py +0 -33
- tests/perf/perf_unicorn_0.py +0 -27
- tests/perf/perf_unicorn_1.py +0 -23
- tests/procedures/__init__.py +0 -0
- tests/procedures/glibc/__init__.py +0 -0
- tests/procedures/glibc/test_ctype_locale.py +0 -164
- tests/procedures/libc/__init__.py +0 -0
- tests/procedures/libc/test_fgets.py +0 -53
- tests/procedures/libc/test_scanf.py +0 -205
- tests/procedures/libc/test_sprintf.py +0 -44
- tests/procedures/libc/test_sscanf.py +0 -63
- tests/procedures/libc/test_strcasecmp.py +0 -37
- tests/procedures/libc/test_string.py +0 -1102
- tests/procedures/libc/test_strtol.py +0 -78
- tests/procedures/linux_kernel/__init__.py +0 -0
- tests/procedures/linux_kernel/test_lseek.py +0 -174
- tests/procedures/posix/__init__.py +0 -0
- tests/procedures/posix/test_chroot.py +0 -33
- tests/procedures/posix/test_getenv.py +0 -78
- tests/procedures/posix/test_pwrite_pread.py +0 -57
- tests/procedures/posix/test_sim_time.py +0 -46
- tests/procedures/posix/test_unlink.py +0 -46
- tests/procedures/test_project_resolve_simproc.py +0 -43
- tests/procedures/test_sim_procedure.py +0 -117
- tests/procedures/test_stub_procedure_args.py +0 -53
- tests/serialization/__init__.py +0 -0
- tests/serialization/test_db.py +0 -197
- tests/serialization/test_pickle.py +0 -95
- tests/serialization/test_serialization.py +0 -132
- tests/serialization/test_vault.py +0 -169
- tests/sim/__init__.py +0 -3
- tests/sim/exec_func/__init__.py +0 -0
- tests/sim/exec_func/test_mem_funcs.py +0 -55
- tests/sim/exec_func/test_str_funcs.py +0 -93
- tests/sim/exec_func/test_syscall_result.py +0 -39
- tests/sim/exec_insn/__init__.py +0 -0
- tests/sim/exec_insn/test_adc.py +0 -44
- tests/sim/exec_insn/test_ops.py +0 -83
- tests/sim/exec_insn/test_rcr.py +0 -26
- tests/sim/exec_insn/test_rol.py +0 -51
- tests/sim/exec_insn/test_signed_div.py +0 -34
- tests/sim/exec_insn/test_sqrt.py +0 -56
- tests/sim/options/__init__.py +0 -0
- tests/sim/options/test_0div.py +0 -54
- tests/sim/options/test_symbolic_fd.py +0 -59
- tests/sim/options/test_unsupported.py +0 -34
- tests/sim/test_accuracy.py +0 -137
- tests/sim/test_checkbyte.py +0 -53
- tests/sim/test_echo.py +0 -36
- tests/sim/test_fauxware.py +0 -202
- tests/sim/test_self_modifying_code.py +0 -65
- tests/sim/test_simple_api.py +0 -36
- tests/sim/test_simulation_manager.py +0 -147
- tests/sim/test_stack_alignment.py +0 -65
- tests/sim/test_state.py +0 -303
- tests/sim/test_state_customization.py +0 -54
- tests/sim/test_symbol_hooked_by.py +0 -49
- tests/simos/__init__.py +0 -0
- tests/simos/windows/__init__.py +0 -0
- tests/simos/windows/test_windows_stack_cookie.py +0 -58
- tests/state_plugins/__init__.py +0 -0
- tests/state_plugins/inspect/__init__.py +0 -0
- tests/state_plugins/inspect/test_inspect.py +0 -310
- tests/state_plugins/inspect/test_syscall_override.py +0 -90
- tests/state_plugins/posix/__init__.py +0 -0
- tests/state_plugins/posix/test_file_struct_funcs.py +0 -56
- tests/state_plugins/posix/test_files.py +0 -69
- tests/state_plugins/posix/test_posix.py +0 -72
- tests/state_plugins/solver/__init__.py +0 -0
- tests/state_plugins/solver/test_simsolver.py +0 -58
- tests/state_plugins/solver/test_symbolic.py +0 -153
- tests/state_plugins/solver/test_variable_registration.py +0 -46
- tests/state_plugins/test_callstack.py +0 -54
- tests/state_plugins/test_gdb_plugin.py +0 -35
- tests/state_plugins/test_multi_open_file.py +0 -47
- tests/state_plugins/test_symbolization.py +0 -38
- tests/storage/__init__.py +0 -0
- tests/storage/test_memory.py +0 -960
- tests/storage/test_memory_merge.py +0 -114
- tests/storage/test_memview.py +0 -205
- tests/storage/test_mmap.py +0 -26
- tests/storage/test_multivalues.py +0 -44
- tests/storage/test_permissions.py +0 -32
- tests/storage/test_ptmalloc.py +0 -291
- tests/storage/test_relro_perm.py +0 -49
- tests/test_calling_conventions.py +0 -86
- tests/test_types.py +0 -329
- tests/utils/__init__.py +0 -0
- tests/utils/test_graph.py +0 -41
- {angr-9.2.87.dist-info → angr-9.2.89.dist-info}/LICENSE +0 -0
- {angr-9.2.87.dist-info → angr-9.2.89.dist-info}/WHEEL +0 -0
- {angr-9.2.87.dist-info → angr-9.2.89.dist-info}/entry_points.txt +0 -0
|
@@ -1,1102 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
# pylint: disable=missing-class-docstring,no-self-use,line-too-long
|
|
3
|
-
__package__ = __package__ or "tests.procedures.libc" # pylint:disable=redefined-builtin
|
|
4
|
-
|
|
5
|
-
import logging
|
|
6
|
-
import random
|
|
7
|
-
import unittest
|
|
8
|
-
from itertools import combinations
|
|
9
|
-
|
|
10
|
-
import angr
|
|
11
|
-
from angr import SimState, SIM_LIBRARIES
|
|
12
|
-
|
|
13
|
-
from ...common import broken, slow_test
|
|
14
|
-
|
|
15
|
-
log = logging.getLogger("angr.tests.string")
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
def make_state_with_stdin(content):
|
|
19
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
20
|
-
stdin_storage = angr.storage.file.SimFile("stdin", content=content)
|
|
21
|
-
stdin = angr.storage.file.SimFileDescriptor(stdin_storage)
|
|
22
|
-
s.register_plugin("posix", angr.state_plugins.SimSystemPosix(stdin=stdin_storage, fd={0: stdin}))
|
|
23
|
-
return s
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
def make_func(name):
|
|
27
|
-
return (
|
|
28
|
-
lambda state, arguments: SIM_LIBRARIES["libc.so.6"]
|
|
29
|
-
.get(name, "AMD64")
|
|
30
|
-
.execute(state, arguments=arguments)
|
|
31
|
-
.ret_expr
|
|
32
|
-
)
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
strstr = make_func("strstr")
|
|
36
|
-
strtok_r = make_func("strtok_r")
|
|
37
|
-
strcmp = make_func("strcmp")
|
|
38
|
-
strchr = make_func("strchr")
|
|
39
|
-
strncmp = make_func("strncmp")
|
|
40
|
-
strlen = make_func("strlen")
|
|
41
|
-
strncpy = make_func("strncpy")
|
|
42
|
-
strcpy = make_func("strcpy")
|
|
43
|
-
memset = make_func("memset")
|
|
44
|
-
memcpy = make_func("memcpy")
|
|
45
|
-
memcmp = make_func("memcmp")
|
|
46
|
-
sprintf = make_func("sprintf")
|
|
47
|
-
getc = make_func("_IO_getc")
|
|
48
|
-
fgetc = make_func("fgetc")
|
|
49
|
-
getchar = make_func("getchar")
|
|
50
|
-
scanf = make_func("scanf")
|
|
51
|
-
wcscmp = make_func("wcscmp")
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
class TestStringSimProcedures(unittest.TestCase):
|
|
55
|
-
def test_strlen(self):
|
|
56
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
57
|
-
|
|
58
|
-
log.info("fully concrete string")
|
|
59
|
-
a_str = s.solver.BVV(0x41414100, 32)
|
|
60
|
-
a_addr = s.solver.BVV(0x10, 64)
|
|
61
|
-
s.memory.store(a_addr, a_str, endness="Iend_BE")
|
|
62
|
-
a_len = strlen(s, arguments=[a_addr])
|
|
63
|
-
assert s.solver.unique(a_len)
|
|
64
|
-
assert s.solver.eval(a_len) == 3
|
|
65
|
-
|
|
66
|
-
log.info("concrete-terminated string")
|
|
67
|
-
b_str = s.solver.Concat(s.solver.BVS("mystring", 24), s.solver.BVV(0, 8))
|
|
68
|
-
b_addr = s.solver.BVV(0x20, 64)
|
|
69
|
-
s.memory.store(b_addr, b_str, endness="Iend_BE")
|
|
70
|
-
b_len = strlen(s, arguments=[b_addr])
|
|
71
|
-
assert s.solver.max_int(b_len) == 3
|
|
72
|
-
assert tuple(sorted(s.solver.eval_upto(b_len, 10))) == (0, 1, 2, 3)
|
|
73
|
-
|
|
74
|
-
log.info("fully unconstrained")
|
|
75
|
-
u_addr = s.solver.BVV(0x50, 64)
|
|
76
|
-
u_len_sp = strlen(s, arguments=[u_addr])
|
|
77
|
-
u_len = u_len_sp
|
|
78
|
-
assert len(s.solver.eval_upto(u_len, 100)) == s.libc.buf_symbolic_bytes
|
|
79
|
-
assert s.solver.max_int(u_len) == s.libc.buf_symbolic_bytes - 1
|
|
80
|
-
# print u_len_sp.solver.maximum_null
|
|
81
|
-
|
|
82
|
-
# s.add_constraints(u_len < 16)
|
|
83
|
-
|
|
84
|
-
assert s.solver.eval_upto(s.memory.load(0x50 + u_len, 1), 300) == [0]
|
|
85
|
-
#
|
|
86
|
-
# This tests if a strlen can influence a symbolic str.
|
|
87
|
-
#
|
|
88
|
-
log.info("Trying to influence length.")
|
|
89
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
90
|
-
str_c = s.solver.BVS("some_string", 8 * 16)
|
|
91
|
-
c_addr = s.solver.BVV(0x10, 64)
|
|
92
|
-
s.memory.store(c_addr, str_c, endness="Iend_BE")
|
|
93
|
-
c_len = strlen(s, arguments=[c_addr])
|
|
94
|
-
assert len(s.solver.eval_upto(c_len, 100)) == s.libc.buf_symbolic_bytes
|
|
95
|
-
assert s.solver.max_int(c_len) == s.libc.buf_symbolic_bytes - 1
|
|
96
|
-
|
|
97
|
-
one_s = s.copy()
|
|
98
|
-
one_s.add_constraints(c_len == 1)
|
|
99
|
-
assert one_s.solver.eval(str_c, cast_to=bytes).index(b"\x00") == 1
|
|
100
|
-
str_test = one_s.memory.load(c_addr, 2, endness="Iend_BE")
|
|
101
|
-
assert len(one_s.solver.eval_upto(str_test, 300, cast_to=bytes)) == 255
|
|
102
|
-
|
|
103
|
-
for i in range(16):
|
|
104
|
-
test_s = s.copy()
|
|
105
|
-
test_s.add_constraints(c_len == i)
|
|
106
|
-
str_test = test_s.memory.load(c_addr, i + 1, endness="Iend_BE")
|
|
107
|
-
assert test_s.solver.eval(str_test, cast_to=bytes).index(b"\x00") == i
|
|
108
|
-
for j in range(i):
|
|
109
|
-
assert not test_s.solver.unique(test_s.memory.load(c_addr + j, 1))
|
|
110
|
-
|
|
111
|
-
def test_strcmp(self):
|
|
112
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
113
|
-
str_a = s.solver.BVV(0x41414100, 32)
|
|
114
|
-
str_b = s.solver.BVS("mystring", 32)
|
|
115
|
-
|
|
116
|
-
a_addr = s.solver.BVV(0x10, 64)
|
|
117
|
-
b_addr = s.solver.BVV(0xB0, 64)
|
|
118
|
-
|
|
119
|
-
s.memory.store(a_addr, str_a, endness="Iend_BE")
|
|
120
|
-
s.memory.store(b_addr, str_b, endness="Iend_BE")
|
|
121
|
-
|
|
122
|
-
s_cmp = s.copy()
|
|
123
|
-
cmpres = strcmp(s_cmp, arguments=[a_addr, b_addr])
|
|
124
|
-
s_match = s_cmp.copy()
|
|
125
|
-
s_nomatch = s_cmp.copy()
|
|
126
|
-
s_match.add_constraints(cmpres == 0)
|
|
127
|
-
s_nomatch.add_constraints(cmpres != 0)
|
|
128
|
-
|
|
129
|
-
assert s_match.solver.unique(str_b)
|
|
130
|
-
assert not s_nomatch.solver.unique(str_b)
|
|
131
|
-
assert s_match.solver.eval(str_b, cast_to=bytes) == b"AAA\x00"
|
|
132
|
-
|
|
133
|
-
s_ncmp = s.copy()
|
|
134
|
-
ncmpres = strncmp(s_ncmp, arguments=[a_addr, b_addr, s.solver.BVV(2, s.arch.bits)])
|
|
135
|
-
s_match = s_ncmp.copy()
|
|
136
|
-
s_nomatch = s_ncmp.copy()
|
|
137
|
-
s_match.add_constraints(ncmpres == 0)
|
|
138
|
-
s_nomatch.add_constraints(ncmpres != 0)
|
|
139
|
-
|
|
140
|
-
assert not s_match.solver.unique(str_b)
|
|
141
|
-
assert s_match.solver.unique(s_match.memory.load(b_addr, 2))
|
|
142
|
-
assert len(s_match.solver.eval_upto(s_match.memory.load(b_addr, 3), 300)) == 256
|
|
143
|
-
assert not s_nomatch.solver.unique(str_b)
|
|
144
|
-
|
|
145
|
-
log.info("concrete a, symbolic b")
|
|
146
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
147
|
-
str_a = s.solver.BVV(0x41424300, 32)
|
|
148
|
-
str_b = s.solver.BVS("mystring", 32)
|
|
149
|
-
a_addr = s.solver.BVV(0x10, 64)
|
|
150
|
-
b_addr = s.solver.BVV(0xB0, 64)
|
|
151
|
-
s.memory.store(a_addr, str_a, endness="Iend_BE")
|
|
152
|
-
s.memory.store(b_addr, str_b, endness="Iend_BE")
|
|
153
|
-
|
|
154
|
-
s_cmp = s.copy()
|
|
155
|
-
cmpres = strncmp(s_cmp, arguments=[a_addr, b_addr, s.solver.BVV(2, s_cmp.arch.bits)])
|
|
156
|
-
s_match = s_cmp.copy()
|
|
157
|
-
s_nomatch = s_cmp.copy()
|
|
158
|
-
s_match.add_constraints(cmpres == 0)
|
|
159
|
-
s_nomatch.add_constraints(cmpres != 0)
|
|
160
|
-
|
|
161
|
-
assert s_match.solver.solution(str_b, 0x41420000)
|
|
162
|
-
assert s_match.solver.solution(str_b, 0x41421234)
|
|
163
|
-
assert s_match.solver.solution(str_b, 0x41424300)
|
|
164
|
-
assert not s_nomatch.solver.solution(str_b, 0x41420000)
|
|
165
|
-
assert not s_nomatch.solver.solution(str_b, 0x41421234)
|
|
166
|
-
assert not s_nomatch.solver.solution(str_b, 0x41424300)
|
|
167
|
-
|
|
168
|
-
log.info("symbolic a, symbolic b")
|
|
169
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
170
|
-
a_addr = s.solver.BVV(0x10, 64)
|
|
171
|
-
b_addr = s.solver.BVV(0xB0, 64)
|
|
172
|
-
|
|
173
|
-
s_cmp = s.copy()
|
|
174
|
-
cmpres = strcmp(s_cmp, arguments=[a_addr, b_addr])
|
|
175
|
-
s_match = s_cmp.copy()
|
|
176
|
-
s_nomatch = s_cmp.copy()
|
|
177
|
-
s_match.add_constraints(cmpres == 0)
|
|
178
|
-
s_nomatch.add_constraints(cmpres != 0)
|
|
179
|
-
|
|
180
|
-
m_res = strcmp(s_match, arguments=[a_addr, b_addr])
|
|
181
|
-
s_match.add_constraints(m_res != 0)
|
|
182
|
-
nm_res = strcmp(s_nomatch, arguments=[a_addr, b_addr])
|
|
183
|
-
s_nomatch.add_constraints(nm_res == 0)
|
|
184
|
-
|
|
185
|
-
assert not s_match.satisfiable()
|
|
186
|
-
assert not s_match.satisfiable()
|
|
187
|
-
|
|
188
|
-
def test_strncmp(self):
|
|
189
|
-
log.info("symbolic left, symbolic right, symbolic len")
|
|
190
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
191
|
-
left = s.solver.BVS("left", 32)
|
|
192
|
-
left_addr = s.solver.BVV(0x1000, 64)
|
|
193
|
-
right = s.solver.BVS("right", 32)
|
|
194
|
-
right_addr = s.solver.BVV(0x2000, 64)
|
|
195
|
-
maxlen = s.solver.BVS("len", 64)
|
|
196
|
-
|
|
197
|
-
s.memory.store(left_addr, left)
|
|
198
|
-
s.memory.store(right_addr, right)
|
|
199
|
-
|
|
200
|
-
s.add_constraints(strlen(s, arguments=[left_addr]) == 3)
|
|
201
|
-
s.add_constraints(strlen(s, arguments=[right_addr]) == 0)
|
|
202
|
-
|
|
203
|
-
s.add_constraints(maxlen != 0)
|
|
204
|
-
c = strncmp(s, arguments=[left_addr, right_addr, maxlen])
|
|
205
|
-
|
|
206
|
-
s_match = s.copy()
|
|
207
|
-
s_match.add_constraints(c == 0)
|
|
208
|
-
assert not s_match.satisfiable()
|
|
209
|
-
# assert s_match.solver.min_int(maxlen) == 3
|
|
210
|
-
|
|
211
|
-
s_nomatch = s.copy()
|
|
212
|
-
s_nomatch.add_constraints(c != 0)
|
|
213
|
-
assert s_nomatch.satisfiable()
|
|
214
|
-
# assert s_nomatch.solver.max_int(maxlen) == 2
|
|
215
|
-
|
|
216
|
-
log.info("zero-length")
|
|
217
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
218
|
-
left = s.solver.BVS("left", 32)
|
|
219
|
-
left_addr = s.solver.BVV(0x1000, 64)
|
|
220
|
-
right = s.solver.BVS("right", 32)
|
|
221
|
-
right_addr = s.solver.BVV(0x2000, 64)
|
|
222
|
-
maxlen = s.solver.BVS("len", 64)
|
|
223
|
-
left_len = strlen(s, arguments=[left_addr])
|
|
224
|
-
right_len = strlen(s, arguments=[right_addr])
|
|
225
|
-
c = strncmp(s, arguments=[left_addr, right_addr, maxlen])
|
|
226
|
-
|
|
227
|
-
s.add_constraints(right_len == 0)
|
|
228
|
-
s.add_constraints(left_len == 0)
|
|
229
|
-
# s.add_constraints(c == 0)
|
|
230
|
-
s.add_constraints(maxlen == 0)
|
|
231
|
-
assert s.satisfiable()
|
|
232
|
-
|
|
233
|
-
def test_strncmp_longer_limit(self):
|
|
234
|
-
log.info("concrete a, concrete b, concrete n")
|
|
235
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
236
|
-
str_a = s.solver.BVV(b"ABC\0")
|
|
237
|
-
str_b = s.solver.BVV(b"AB\0")
|
|
238
|
-
addr_a = s.solver.BVV(0x10, 64)
|
|
239
|
-
addr_b = s.solver.BVV(0xB0, 64)
|
|
240
|
-
s.memory.store(addr_a, str_a, endness="Iend_BE")
|
|
241
|
-
s.memory.store(addr_b, str_b, endness="Iend_BE")
|
|
242
|
-
|
|
243
|
-
ss_res = strncmp(s, arguments=[addr_a, addr_b, s.solver.BVV(3, 64)])
|
|
244
|
-
assert s.solver.unique(ss_res)
|
|
245
|
-
assert s.solver.eval(ss_res) != 0
|
|
246
|
-
|
|
247
|
-
def test_strncmp_find_limits(self):
|
|
248
|
-
log.info("concrete a, concrete b, symbolic n")
|
|
249
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
250
|
-
str_a = s.solver.BVV(b"ABCD\0")
|
|
251
|
-
str_b = s.solver.BVV(b"ABCE\0")
|
|
252
|
-
addr_a = s.solver.BVV(0x10, 64)
|
|
253
|
-
addr_b = s.solver.BVV(0xB0, 64)
|
|
254
|
-
s.memory.store(addr_a, str_a, endness="Iend_BE")
|
|
255
|
-
s.memory.store(addr_b, str_b, endness="Iend_BE")
|
|
256
|
-
|
|
257
|
-
n = s.solver.BVS("n", 64)
|
|
258
|
-
ss_res = strncmp(s, arguments=[addr_a, addr_b, n])
|
|
259
|
-
s.add_constraints(ss_res == 0)
|
|
260
|
-
assert set(s.solver.eval_upto(n, 10)) == {0, 1, 2, 3}
|
|
261
|
-
|
|
262
|
-
def test_strncmp_find_prefix(self):
|
|
263
|
-
log.info("concrete a, symbolic b, symbolic n")
|
|
264
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
265
|
-
str_a = s.solver.BVV(b"ABCD\0")
|
|
266
|
-
str_b = s.solver.BVS("str_b", len(str_a))
|
|
267
|
-
addr_a = s.solver.BVV(0x10, 64)
|
|
268
|
-
addr_b = s.solver.BVV(0xB0, 64)
|
|
269
|
-
s.memory.store(addr_a, str_a, endness="Iend_BE")
|
|
270
|
-
s.memory.store(addr_b, str_b, endness="Iend_BE")
|
|
271
|
-
|
|
272
|
-
# All prefixes should be valid
|
|
273
|
-
n = strlen(s, arguments=[addr_b])
|
|
274
|
-
ss_res = strncmp(s, arguments=[addr_a, addr_b, n])
|
|
275
|
-
s.add_constraints(ss_res == 0)
|
|
276
|
-
assert set(s.solver.eval_upto(n, 10)) == {0, 1, 2, 3, 4}
|
|
277
|
-
|
|
278
|
-
def test_strncmp_find_prefix_unsat(self):
|
|
279
|
-
log.info("concrete a, concrete b, symbolic n")
|
|
280
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
281
|
-
str_a = s.solver.BVV(b"\0\0\0\0\0")
|
|
282
|
-
str_b = s.solver.BVV(b"ABCE\0")
|
|
283
|
-
addr_a = s.solver.BVV(0x10, 64)
|
|
284
|
-
addr_b = s.solver.BVV(0xB0, 64)
|
|
285
|
-
s.memory.store(addr_a, str_a, endness="Iend_BE")
|
|
286
|
-
s.memory.store(addr_b, str_b, endness="Iend_BE")
|
|
287
|
-
|
|
288
|
-
# No prefix should be valid
|
|
289
|
-
n = strlen(s, arguments=[addr_b])
|
|
290
|
-
ss_res = strncmp(s, arguments=[addr_a, addr_b, n])
|
|
291
|
-
s.add_constraints(ss_res == 0)
|
|
292
|
-
assert not s.satisfiable()
|
|
293
|
-
|
|
294
|
-
def test_strncmp_find_input_for_limit(self):
|
|
295
|
-
log.info("concrete a, symbolic b, symbolic n")
|
|
296
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
297
|
-
str_a = s.solver.BVV(b"ABCD\0")
|
|
298
|
-
str_b = s.solver.BVS("str_b", len(str_a))
|
|
299
|
-
addr_a = s.solver.BVV(0x10, 64)
|
|
300
|
-
addr_b = s.solver.BVV(0xB0, 64)
|
|
301
|
-
s.memory.store(addr_a, str_a, endness="Iend_BE")
|
|
302
|
-
s.memory.store(addr_b, str_b, endness="Iend_BE")
|
|
303
|
-
|
|
304
|
-
n = s.solver.BVS("n", 64)
|
|
305
|
-
ss_res = strncmp(s, arguments=[addr_a, addr_b, n])
|
|
306
|
-
s.add_constraints(ss_res == 0)
|
|
307
|
-
|
|
308
|
-
# Check constrained limit produces only expected input
|
|
309
|
-
for i in range(1, len(str_b) // 8):
|
|
310
|
-
s2 = s.copy()
|
|
311
|
-
s2.add_constraints(n == i)
|
|
312
|
-
hi, lo = 5 * 8 - 1, (5 - i) * 8
|
|
313
|
-
substr_a, substr_b = str_a[hi:lo], str_b[hi:lo]
|
|
314
|
-
assert s2.solver.unique(substr_b)
|
|
315
|
-
assert s2.solver.solution(substr_b, substr_a)
|
|
316
|
-
|
|
317
|
-
def test_strstr_conc_haystack_conc_needle(self):
|
|
318
|
-
log.info("concrete haystack and needle")
|
|
319
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
320
|
-
str_haystack = s.solver.BVV(0x41424300, 32)
|
|
321
|
-
str_needle = s.solver.BVV(0x42430000, 32)
|
|
322
|
-
addr_haystack = s.solver.BVV(0x10, 64)
|
|
323
|
-
addr_needle = s.solver.BVV(0xB0, 64)
|
|
324
|
-
s.memory.store(addr_haystack, str_haystack, endness="Iend_BE")
|
|
325
|
-
s.memory.store(addr_needle, str_needle, endness="Iend_BE")
|
|
326
|
-
|
|
327
|
-
ss_res = strstr(s, arguments=[addr_haystack, addr_needle])
|
|
328
|
-
assert s.solver.unique(ss_res)
|
|
329
|
-
assert s.solver.eval(ss_res) == 0x11
|
|
330
|
-
|
|
331
|
-
def test_strstr_conc_haystack_sym_needle(self):
|
|
332
|
-
log.info("concrete haystack, symbolic needle")
|
|
333
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
334
|
-
s.libc.max_symbolic_strstr = 20
|
|
335
|
-
haystack = b"ABCD"
|
|
336
|
-
str_haystack = s.solver.BVV(haystack + b"\0")
|
|
337
|
-
str_needle = s.solver.BVS("needle", 32)
|
|
338
|
-
addr_haystack = s.solver.BVV(0x10, 64)
|
|
339
|
-
addr_needle = s.solver.BVV(0xB0, 64)
|
|
340
|
-
s.memory.store(addr_haystack, str_haystack, endness="Iend_BE")
|
|
341
|
-
s.memory.store(addr_needle, str_needle, endness="Iend_BE")
|
|
342
|
-
s.memory.store(addr_needle + 4, s.solver.BVV(0, 8))
|
|
343
|
-
|
|
344
|
-
ss_res = strstr(s, arguments=[addr_haystack, addr_needle])
|
|
345
|
-
results = set(s.solver.eval_upto(ss_res, len(haystack) * 2))
|
|
346
|
-
expected = {(addr_haystack.cv + i) for i in range(len(haystack))} | {0}
|
|
347
|
-
assert results == expected
|
|
348
|
-
|
|
349
|
-
s_match = s.copy()
|
|
350
|
-
s_nomatch = s.copy()
|
|
351
|
-
s_match.add_constraints(ss_res != 0)
|
|
352
|
-
s_nomatch.add_constraints(ss_res == 0)
|
|
353
|
-
|
|
354
|
-
# Check all substrings
|
|
355
|
-
substrings = [haystack[l:h] for l, h in combinations(range(len(haystack) + 1), 2)]
|
|
356
|
-
needle_len = len(str_needle) // 8
|
|
357
|
-
for substring in substrings:
|
|
358
|
-
solution = s.solver.BVV(substring.ljust(needle_len, b"\0"))
|
|
359
|
-
assert s_match.solver.solution(str_needle, solution)
|
|
360
|
-
assert not s_match.solver.solution(str_needle, s.solver.BVV(b"AC\0\0"))
|
|
361
|
-
assert not s_match.solver.solution(str_needle, s.solver.BVV(b"AD\0\0"))
|
|
362
|
-
assert s_nomatch.solver.satisfiable()
|
|
363
|
-
|
|
364
|
-
def test_strstr_sym_haystack_conc_needle(self):
|
|
365
|
-
log.info("symbolic haystack, concrete needle")
|
|
366
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
367
|
-
s.libc.max_symbolic_strstr = 20
|
|
368
|
-
str_haystack = s.solver.BVS("haystack", 5 * 8).concat(s.solver.BVV(0, 8))
|
|
369
|
-
str_needle = s.solver.BVV(b"ABC\0")
|
|
370
|
-
addr_haystack = s.solver.BVV(0x10, 64)
|
|
371
|
-
addr_needle = s.solver.BVV(0xB0, 64)
|
|
372
|
-
s.memory.store(addr_haystack, str_haystack, endness="Iend_BE")
|
|
373
|
-
s.memory.store(addr_needle, str_needle, endness="Iend_BE")
|
|
374
|
-
|
|
375
|
-
ss_res = strstr(s, arguments=[addr_haystack, addr_needle])
|
|
376
|
-
|
|
377
|
-
s_match = s.copy()
|
|
378
|
-
s_match.add_constraints(ss_res != 0)
|
|
379
|
-
s_nomatch = s.copy()
|
|
380
|
-
s_nomatch.add_constraints(ss_res == 0)
|
|
381
|
-
|
|
382
|
-
num_possible = min(s.libc.max_symbolic_strstr, 1 + (len(str_haystack) - len(str_needle)) // 8)
|
|
383
|
-
expected = set(range(addr_haystack.cv, addr_haystack.cv + num_possible))
|
|
384
|
-
results = set(s_match.solver.eval_exact(ss_res, num_possible))
|
|
385
|
-
assert results == expected
|
|
386
|
-
assert s_nomatch.solver.satisfiable()
|
|
387
|
-
|
|
388
|
-
def test_strstr_sym_haystack_sym_needle(self):
|
|
389
|
-
log.info("symbolic haystack, symbolic needle")
|
|
390
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
391
|
-
s.libc.max_symbolic_strstr = 20
|
|
392
|
-
s.libc.buf_symbolic_bytes = 10
|
|
393
|
-
str_haystack = s.solver.BVS("haystack", s.libc.buf_symbolic_bytes * 8)
|
|
394
|
-
str_needle = s.solver.BVS("needle", s.libc.buf_symbolic_bytes * 8)
|
|
395
|
-
addr_haystack = s.solver.BVV(0x10, 64)
|
|
396
|
-
addr_needle = s.solver.BVV(0xB0, 64)
|
|
397
|
-
s.memory.store(addr_haystack, str_haystack, endness="Iend_BE")
|
|
398
|
-
s.memory.store(addr_needle, str_needle, endness="Iend_BE")
|
|
399
|
-
|
|
400
|
-
ss_res = strstr(s, arguments=[addr_haystack, addr_needle])
|
|
401
|
-
|
|
402
|
-
s_match = s.copy()
|
|
403
|
-
s_nomatch = s.copy()
|
|
404
|
-
s_match.add_constraints(ss_res != 0)
|
|
405
|
-
s_nomatch.add_constraints(ss_res == 0)
|
|
406
|
-
|
|
407
|
-
s_i = s_match.copy()
|
|
408
|
-
s_i.add_constraints(str_needle == s.solver.BVV(b"123".ljust(s.libc.buf_symbolic_bytes, b"\0")))
|
|
409
|
-
|
|
410
|
-
# Check needle is not found after end of haystack
|
|
411
|
-
s_ez = s_i.copy()
|
|
412
|
-
s_ez.add_constraints(str_haystack[len(str_haystack) - 1 : len(str_haystack) - 8] == 0)
|
|
413
|
-
assert not s_ez.satisfiable()
|
|
414
|
-
|
|
415
|
-
# Check target of specific offset
|
|
416
|
-
for i in range(5):
|
|
417
|
-
s_n = s_i.copy()
|
|
418
|
-
s_n.add_constraints(ss_res == addr_haystack + i)
|
|
419
|
-
h = s_n.solver.eval(str_haystack, cast_to=bytes)
|
|
420
|
-
assert h.find(b"123") == i
|
|
421
|
-
|
|
422
|
-
len_needle = strlen(s_match, arguments=[addr_needle])
|
|
423
|
-
match_cmp = strncmp(s_match, arguments=[ss_res, addr_needle, len_needle])
|
|
424
|
-
assert s_match.solver.eval_upto(match_cmp, 10) == [0]
|
|
425
|
-
|
|
426
|
-
r_mm = strstr(s_match, arguments=[addr_haystack, addr_needle])
|
|
427
|
-
s_match.add_constraints(r_mm == 0)
|
|
428
|
-
assert not s_match.satisfiable()
|
|
429
|
-
|
|
430
|
-
assert s_nomatch.satisfiable()
|
|
431
|
-
s_nss = s_nomatch.copy()
|
|
432
|
-
nomatch_ss = strstr(s_nss, arguments=[addr_haystack, addr_needle])
|
|
433
|
-
s_nss.add_constraints(nomatch_ss != 0)
|
|
434
|
-
assert not s_nss.satisfiable()
|
|
435
|
-
|
|
436
|
-
def test_strstr_inconsistency(self):
|
|
437
|
-
log.info("symbolic haystack, symbolic needle")
|
|
438
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
439
|
-
s.libc.buf_symbolic_bytes = 2
|
|
440
|
-
addr_haystack = s.solver.BVV(0x10, 64)
|
|
441
|
-
addr_needle = s.solver.BVV(0xB0, 64)
|
|
442
|
-
# len_needle = strlen(s, inline=True, arguments=[addr_needle])
|
|
443
|
-
|
|
444
|
-
ss_res = strstr(s, arguments=[addr_haystack, addr_needle])
|
|
445
|
-
|
|
446
|
-
# slh_res = strlen(s, inline=True, arguments=[addr_haystack])
|
|
447
|
-
# sln_res = strlen(s, inline=True, arguments=[addr_needle])
|
|
448
|
-
# print "LENH:", s.solver.eval_upto(slh_res, 100)
|
|
449
|
-
# print "LENN:", s.solver.eval_upto(sln_res, 100)
|
|
450
|
-
|
|
451
|
-
assert not s.solver.unique(ss_res)
|
|
452
|
-
assert sorted(s.solver.eval_upto(ss_res, 100)) == [0] + list(range(0x10, 0x10 + s.libc.buf_symbolic_bytes - 1))
|
|
453
|
-
|
|
454
|
-
s.add_constraints(ss_res != 0)
|
|
455
|
-
ss2 = strstr(s, arguments=[addr_haystack, addr_needle])
|
|
456
|
-
s.add_constraints(ss2 == 0)
|
|
457
|
-
assert not s.satisfiable()
|
|
458
|
-
|
|
459
|
-
@slow_test
|
|
460
|
-
def test_memcpy(self):
|
|
461
|
-
log.info("concrete src, concrete dst, concrete len")
|
|
462
|
-
log.debug("... full copy")
|
|
463
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
464
|
-
dst = s.solver.BVV(0x41414141, 32)
|
|
465
|
-
dst_addr = s.solver.BVV(0x1000, 64)
|
|
466
|
-
src = s.solver.BVV(0x42424242, 32)
|
|
467
|
-
src_addr = s.solver.BVV(0x2000, 64)
|
|
468
|
-
|
|
469
|
-
s.memory.store(dst_addr, dst)
|
|
470
|
-
s.memory.store(src_addr, src)
|
|
471
|
-
memcpy(s, arguments=[dst_addr, src_addr, s.solver.BVV(4, 64)])
|
|
472
|
-
new_dst = s.memory.load(dst_addr, 4, endness="Iend_BE")
|
|
473
|
-
assert s.solver.eval_upto(new_dst, 2, cast_to=bytes) == [b"BBBB"]
|
|
474
|
-
|
|
475
|
-
log.info("giant copy")
|
|
476
|
-
s = SimState(arch="AMD64", mode="symbolic", remove_options=angr.options.simplification)
|
|
477
|
-
s.memory._maximum_symbolic_size = 0x2000000
|
|
478
|
-
size = s.solver.BVV(0x1000000, 64)
|
|
479
|
-
data = s.solver.BVS("giant", 8 * 0x1_000_000)
|
|
480
|
-
dst_addr = s.solver.BVV(0x2000000, 64)
|
|
481
|
-
src_addr = s.solver.BVV(0x4000000, 64)
|
|
482
|
-
s.memory.store(src_addr, data)
|
|
483
|
-
|
|
484
|
-
memcpy(s, arguments=[dst_addr, src_addr, size])
|
|
485
|
-
assert s.memory.load(dst_addr, size) is s.memory.load(src_addr, size)
|
|
486
|
-
|
|
487
|
-
log.debug("... partial copy")
|
|
488
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
489
|
-
s.memory.store(dst_addr, dst)
|
|
490
|
-
s.memory.store(src_addr, src)
|
|
491
|
-
memcpy(s, arguments=[dst_addr, src_addr, s.solver.BVV(2, 64)])
|
|
492
|
-
new_dst = s.memory.load(dst_addr, 4, endness="Iend_BE")
|
|
493
|
-
assert s.solver.eval_upto(new_dst, 2, cast_to=bytes) == [b"BBAA"]
|
|
494
|
-
|
|
495
|
-
log.info("symbolic src, concrete dst, concrete len")
|
|
496
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
497
|
-
dst = s.solver.BVV(0x41414141, 32)
|
|
498
|
-
dst_addr = s.solver.BVV(0x1000, 64)
|
|
499
|
-
src = s.solver.BVS("src", 32)
|
|
500
|
-
src_addr = s.solver.BVV(0x2000, 64)
|
|
501
|
-
|
|
502
|
-
s.memory.store(dst_addr, dst)
|
|
503
|
-
s.memory.store(src_addr, src)
|
|
504
|
-
|
|
505
|
-
# make sure it copies it all
|
|
506
|
-
memcpy(s, arguments=[dst_addr, src_addr, s.solver.BVV(4, 64)])
|
|
507
|
-
assert s.satisfiable()
|
|
508
|
-
s.add_constraints(src != s.memory.load(dst_addr, 4))
|
|
509
|
-
assert not s.satisfiable()
|
|
510
|
-
|
|
511
|
-
log.info("symbolic src, concrete dst, symbolic len")
|
|
512
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
513
|
-
dst = s.solver.BVV(0x41414141, 32)
|
|
514
|
-
dst_addr = s.solver.BVV(0x1000, 64)
|
|
515
|
-
src = s.solver.BVS("src", 32)
|
|
516
|
-
src_addr = s.solver.BVV(0x2000, 64)
|
|
517
|
-
cpylen = s.solver.BVS("len", 64)
|
|
518
|
-
|
|
519
|
-
s.memory.store(dst_addr, dst)
|
|
520
|
-
s.memory.store(src_addr, src)
|
|
521
|
-
s.add_constraints(cpylen < 10)
|
|
522
|
-
memcpy(s, arguments=[dst_addr, src_addr, cpylen])
|
|
523
|
-
result = s.memory.load(dst_addr, 4, endness="Iend_BE")
|
|
524
|
-
|
|
525
|
-
# make sure it copies it all
|
|
526
|
-
s1 = s.copy()
|
|
527
|
-
s1.add_constraints(cpylen == 1)
|
|
528
|
-
assert s1.solver.unique(s1.memory.load(dst_addr + 1, 3))
|
|
529
|
-
assert len(s1.solver.eval_upto(s1.memory.load(dst_addr, 1), 300)) == 256
|
|
530
|
-
|
|
531
|
-
s2 = s.copy()
|
|
532
|
-
s2.add_constraints(cpylen == 2)
|
|
533
|
-
assert len(s2.solver.eval_upto(result[31:24], 300)) == 256
|
|
534
|
-
assert len(s2.solver.eval_upto(result[23:16], 300)) == 256
|
|
535
|
-
assert s2.solver.eval_upto(result[15:0], 300, cast_to=bytes) == [b"AA"]
|
|
536
|
-
|
|
537
|
-
log.info("concrete src, concrete dst, symbolic len")
|
|
538
|
-
dst = s2.solver.BVV(0x41414141, 32)
|
|
539
|
-
dst_addr = s2.solver.BVV(0x1000, 64)
|
|
540
|
-
src = s2.solver.BVV(0x42424242, 32)
|
|
541
|
-
src_addr = s2.solver.BVV(0x2000, 64)
|
|
542
|
-
|
|
543
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
544
|
-
s.memory.store(dst_addr, dst)
|
|
545
|
-
s.memory.store(src_addr, src)
|
|
546
|
-
cpylen = s.solver.BVS("len", 64)
|
|
547
|
-
|
|
548
|
-
s.add_constraints(s.solver.ULE(cpylen, 4))
|
|
549
|
-
memcpy(s, arguments=[dst_addr, src_addr, cpylen])
|
|
550
|
-
new_dst = s.memory.load(dst_addr, 4, endness="Iend_BE")
|
|
551
|
-
assert sorted(s.solver.eval_upto(new_dst, 300, cast_to=bytes)) == [b"AAAA", b"BAAA", b"BBAA", b"BBBA", b"BBBB"]
|
|
552
|
-
|
|
553
|
-
def test_memcmp(self):
|
|
554
|
-
log.info("concrete src, concrete dst, concrete len")
|
|
555
|
-
|
|
556
|
-
log.debug("... full cmp")
|
|
557
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
558
|
-
dst = s.solver.BVV(0x41414141, 32)
|
|
559
|
-
dst_addr = s.solver.BVV(0x1000, 64)
|
|
560
|
-
src = s.solver.BVV(0x42424242, 32)
|
|
561
|
-
src_addr = s.solver.BVV(0x2000, 64)
|
|
562
|
-
s.memory.store(dst_addr, dst)
|
|
563
|
-
s.memory.store(src_addr, src)
|
|
564
|
-
r = memcmp(s, arguments=[dst_addr, src_addr, s.solver.BVV(4, 64)])
|
|
565
|
-
assert s.satisfiable()
|
|
566
|
-
|
|
567
|
-
s_pos = s.copy()
|
|
568
|
-
s_pos.add_constraints(r.SGE(0))
|
|
569
|
-
assert not s_pos.satisfiable()
|
|
570
|
-
|
|
571
|
-
s_neg = s.copy()
|
|
572
|
-
s_neg.add_constraints(r.SLT(0))
|
|
573
|
-
assert s_neg.satisfiable()
|
|
574
|
-
|
|
575
|
-
log.debug("... zero cmp")
|
|
576
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
577
|
-
s.memory.store(dst_addr, dst)
|
|
578
|
-
s.memory.store(src_addr, src)
|
|
579
|
-
r = memcmp(s, arguments=[dst_addr, src_addr, s.solver.BVV(0, 64)])
|
|
580
|
-
assert s.solver.eval_upto(r, 2) == [0]
|
|
581
|
-
|
|
582
|
-
log.info("symbolic src, concrete dst, concrete len")
|
|
583
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
584
|
-
dst = s.solver.BVV(0x41414141, 32)
|
|
585
|
-
dst_addr = s.solver.BVV(0x1000, 64)
|
|
586
|
-
src = s.solver.BVS("src", 32)
|
|
587
|
-
|
|
588
|
-
src_addr = s.solver.BVV(0x2000, 64)
|
|
589
|
-
|
|
590
|
-
s.memory.store(dst_addr, dst)
|
|
591
|
-
s.memory.store(src_addr, src)
|
|
592
|
-
|
|
593
|
-
# make sure it copies it all
|
|
594
|
-
r = memcmp(s, arguments=[dst_addr, src_addr, s.solver.BVV(4, 64)])
|
|
595
|
-
|
|
596
|
-
s_match = s.copy()
|
|
597
|
-
s_match.add_constraints(r == 0)
|
|
598
|
-
m = s_match.memory.load(src_addr, 4)
|
|
599
|
-
assert s_match.solver.eval_upto(m, 2) == [0x41414141]
|
|
600
|
-
|
|
601
|
-
s_nomatch = s.copy()
|
|
602
|
-
s_nomatch.add_constraints(r != 0)
|
|
603
|
-
m = s_nomatch.memory.load(src_addr, 4)
|
|
604
|
-
assert not s_nomatch.solver.solution(m, 0x41414141)
|
|
605
|
-
|
|
606
|
-
log.info("symbolic src, concrete dst, symbolic len")
|
|
607
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
608
|
-
dst = s.solver.BVV(0x41414141, 32)
|
|
609
|
-
dst_addr = s.solver.BVV(0x1000, 64)
|
|
610
|
-
src = s.solver.BVS("src", 32)
|
|
611
|
-
src_addr = s.solver.BVV(0x2000, 64)
|
|
612
|
-
cmplen = s.solver.BVS("len", 64)
|
|
613
|
-
|
|
614
|
-
s.memory.store(dst_addr, dst)
|
|
615
|
-
s.memory.store(src_addr, src)
|
|
616
|
-
r = memcmp(s, arguments=[dst_addr, src_addr, cmplen])
|
|
617
|
-
|
|
618
|
-
# look at effects of different lengths
|
|
619
|
-
s1 = s.copy()
|
|
620
|
-
s1.add_constraints(cmplen == 1)
|
|
621
|
-
s1.add_constraints(r == 0)
|
|
622
|
-
log.debug("... simplifying")
|
|
623
|
-
s1.solver._solver.simplify()
|
|
624
|
-
log.debug("... solving")
|
|
625
|
-
assert s1.solver.eval_upto(src[31:24], 2) == [0x41]
|
|
626
|
-
assert not s1.solver.unique(src[31:16])
|
|
627
|
-
log.debug("... solved")
|
|
628
|
-
|
|
629
|
-
s2 = s.copy()
|
|
630
|
-
s2.add_constraints(cmplen == 2)
|
|
631
|
-
s2.add_constraints(r == 0)
|
|
632
|
-
assert s2.solver.eval_upto(s2.memory.load(src_addr, 2), 2) == [0x4141]
|
|
633
|
-
assert not s2.solver.unique(s2.memory.load(src_addr, 3))
|
|
634
|
-
|
|
635
|
-
s2u = s.copy()
|
|
636
|
-
s2u.add_constraints(cmplen == 2)
|
|
637
|
-
s2u.add_constraints(r == 1)
|
|
638
|
-
assert not s2u.solver.solution(s2u.memory.load(src_addr, 2), 0x4141)
|
|
639
|
-
|
|
640
|
-
def test_strncpy(self):
|
|
641
|
-
log.info("concrete src, concrete dst, concrete len")
|
|
642
|
-
log.debug("... full copy")
|
|
643
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
644
|
-
dst = s.solver.BVV(0x41414100, 32)
|
|
645
|
-
dst_addr = s.solver.BVV(0x1000, 64)
|
|
646
|
-
src = s.solver.BVV(0x42420000, 32)
|
|
647
|
-
src_addr = s.solver.BVV(0x2000, 64)
|
|
648
|
-
|
|
649
|
-
s.memory.store(dst_addr, dst)
|
|
650
|
-
s.memory.store(src_addr, src)
|
|
651
|
-
strncpy(s, arguments=[dst_addr, src_addr, s.solver.BVV(3, 64)])
|
|
652
|
-
new_dst = s.memory.load(dst_addr, 4, endness="Iend_BE")
|
|
653
|
-
assert s.solver.eval(new_dst, cast_to=bytes) == b"BB\x00\x00"
|
|
654
|
-
|
|
655
|
-
log.debug("... partial copy")
|
|
656
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
657
|
-
s.memory.store(dst_addr, dst)
|
|
658
|
-
s.memory.store(src_addr, src)
|
|
659
|
-
strncpy(s, arguments=[dst_addr, src_addr, s.solver.BVV(2, 64)])
|
|
660
|
-
new_dst = s.memory.load(dst_addr, 4, endness="Iend_BE")
|
|
661
|
-
assert s.solver.eval_upto(new_dst, 2, cast_to=bytes) == [b"BBA\x00"]
|
|
662
|
-
|
|
663
|
-
log.info("symbolic src, concrete dst, concrete len")
|
|
664
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
665
|
-
dst = s.solver.BVV(0x41414100, 32)
|
|
666
|
-
dst_addr = s.solver.BVV(0x1000, 64)
|
|
667
|
-
src = s.solver.BVS("src", 32)
|
|
668
|
-
src_addr = s.solver.BVV(0x2000, 64)
|
|
669
|
-
|
|
670
|
-
s.memory.store(dst_addr, dst)
|
|
671
|
-
s.memory.store(src_addr, src)
|
|
672
|
-
|
|
673
|
-
# make sure it copies it all
|
|
674
|
-
s.add_constraints(strlen(s, arguments=[src_addr]) == 2)
|
|
675
|
-
|
|
676
|
-
# sanity check
|
|
677
|
-
s_false = s.copy()
|
|
678
|
-
s_false.add_constraints(strlen(s_false, arguments=[src_addr]) == 3)
|
|
679
|
-
assert not s_false.satisfiable()
|
|
680
|
-
|
|
681
|
-
strncpy(s, arguments=[dst_addr, src_addr, 3])
|
|
682
|
-
assert s.satisfiable()
|
|
683
|
-
c = strcmp(s, arguments=[dst_addr, src_addr])
|
|
684
|
-
|
|
685
|
-
assert s.solver.eval_upto(c, 10) == [0]
|
|
686
|
-
|
|
687
|
-
log.info("symbolic src, concrete dst, symbolic len")
|
|
688
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
689
|
-
dst = s.solver.BVV(0x41414100, 32)
|
|
690
|
-
dst_addr = s.solver.BVV(0x1000, 64)
|
|
691
|
-
src = s.solver.BVS("src", 32)
|
|
692
|
-
src_addr = s.solver.BVV(0x2000, 64)
|
|
693
|
-
maxlen = s.solver.BVS("len", 64)
|
|
694
|
-
|
|
695
|
-
s.memory.store(dst_addr, dst)
|
|
696
|
-
s.memory.store(src_addr, src)
|
|
697
|
-
|
|
698
|
-
# make sure it copies it all
|
|
699
|
-
s.add_constraints(strlen(s, arguments=[src_addr]) == 2)
|
|
700
|
-
strncpy(s, arguments=[dst_addr, src_addr, maxlen])
|
|
701
|
-
c = strcmp(s, arguments=[dst_addr, src_addr])
|
|
702
|
-
|
|
703
|
-
s_match = s.copy()
|
|
704
|
-
s_match.add_constraints(c == 0)
|
|
705
|
-
assert s_match.solver.min_int(maxlen) == 3
|
|
706
|
-
|
|
707
|
-
s_nomatch = s.copy()
|
|
708
|
-
s_nomatch.add_constraints(c != 0)
|
|
709
|
-
assert s_nomatch.solver.max_int(maxlen) == 2
|
|
710
|
-
|
|
711
|
-
log.info("concrete src, concrete dst, symbolic len")
|
|
712
|
-
log.debug("... full copy")
|
|
713
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
714
|
-
|
|
715
|
-
dst = s.solver.BVV(0x41414100, 32)
|
|
716
|
-
dst_addr = s.solver.BVV(0x1000, 64)
|
|
717
|
-
src = s.solver.BVV(0x42420000, 32)
|
|
718
|
-
src_addr = s.solver.BVV(0x2000, 64)
|
|
719
|
-
maxlen = s.solver.BVS("len", 64)
|
|
720
|
-
|
|
721
|
-
s.memory.store(dst_addr, dst)
|
|
722
|
-
s.memory.store(src_addr, src)
|
|
723
|
-
strncpy(s, arguments=[dst_addr, src_addr, maxlen])
|
|
724
|
-
r = s.memory.load(dst_addr, 4, endness="Iend_BE")
|
|
725
|
-
# print repr(r.solver.eval_upto(r, 10, cast_to=bytes))
|
|
726
|
-
assert sorted(s.solver.eval_upto(r, 10, cast_to=bytes)) == [b"AAA\x00", b"BAA\x00", b"BB\x00\x00", b"BBA\x00"]
|
|
727
|
-
|
|
728
|
-
def test_strcpy(self):
|
|
729
|
-
log.info("concrete src, concrete dst")
|
|
730
|
-
|
|
731
|
-
log.debug("... full copy")
|
|
732
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
733
|
-
dst = s.solver.BVV(0x41414100, 32)
|
|
734
|
-
dst_addr = s.solver.BVV(0x1000, 64)
|
|
735
|
-
src = s.solver.BVV(0x42420000, 32)
|
|
736
|
-
src_addr = s.solver.BVV(0x2000, 64)
|
|
737
|
-
s.memory.store(dst_addr, dst)
|
|
738
|
-
s.memory.store(src_addr, src)
|
|
739
|
-
strcpy(s, arguments=[dst_addr, src_addr])
|
|
740
|
-
new_dst = s.memory.load(dst_addr, 4, endness="Iend_BE")
|
|
741
|
-
assert s.solver.eval(new_dst, cast_to=bytes) == b"BB\x00\x00"
|
|
742
|
-
|
|
743
|
-
log.info("symbolic src, concrete dst")
|
|
744
|
-
dst = s.solver.BVV(0x41414100, 32)
|
|
745
|
-
dst_addr = s.solver.BVV(0x1000, 64)
|
|
746
|
-
src = s.solver.BVS("src", 32)
|
|
747
|
-
src_addr = s.solver.BVV(0x2000, 64)
|
|
748
|
-
|
|
749
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
750
|
-
s.memory.store(dst_addr, dst)
|
|
751
|
-
s.memory.store(src_addr, src)
|
|
752
|
-
|
|
753
|
-
ln = strlen(s, arguments=[src_addr])
|
|
754
|
-
|
|
755
|
-
strcpy(s, arguments=[dst_addr, src_addr])
|
|
756
|
-
|
|
757
|
-
cm = strcmp(s, arguments=[dst_addr, src_addr])
|
|
758
|
-
|
|
759
|
-
s.add_constraints(cm == 0)
|
|
760
|
-
|
|
761
|
-
s.add_constraints(ln == 15)
|
|
762
|
-
# readsize = 16
|
|
763
|
-
# both_strs = s.solver.Concat(
|
|
764
|
-
# *[
|
|
765
|
-
# s.memory.load(dst_addr, readsize, endness="Iend_BE"),
|
|
766
|
-
# s.memory.load(src_addr, readsize, endness="Iend_BE"),
|
|
767
|
-
# ]
|
|
768
|
-
# )
|
|
769
|
-
# for i in s.solver.eval_upto(both_strs, 50, cast_to=bytes):
|
|
770
|
-
# assert s.solver.eval_upto(10) == [0]
|
|
771
|
-
# assert s.solver.solution(s.memory.load(dst_addr, 4, endness="Iend_BE"), 0x42434400)
|
|
772
|
-
# assert s.solver.solution(s.memory.load(dst_addr, 4, endness="Iend_BE"), 0x42434445)
|
|
773
|
-
# assert s.solver.solution(s.memory.load(dst_addr, 4, endness="Iend_BE"), 0x00414100)
|
|
774
|
-
# assert not s.solver.solution(s.memory.load(dst_addr, 4, endness="Iend_BE"), 0x00010203)
|
|
775
|
-
|
|
776
|
-
@broken
|
|
777
|
-
def test_sprintf(self):
|
|
778
|
-
log.info("concrete src, concrete dst, concrete len")
|
|
779
|
-
s = SimState(mode="symbolic", arch="PPC32")
|
|
780
|
-
format_str = s.solver.BVV(0x25640000, 32)
|
|
781
|
-
format_addr = s.solver.BVV(0x2000, 32)
|
|
782
|
-
# dst = s.solver.BVV("destination", 128)
|
|
783
|
-
dst_addr = s.solver.BVV(0x1000, 32)
|
|
784
|
-
arg = s.solver.BVS("some_number", 32)
|
|
785
|
-
|
|
786
|
-
s.memory.store(format_addr, format_str)
|
|
787
|
-
|
|
788
|
-
sprintf(s, arguments=[dst_addr, format_addr, arg])
|
|
789
|
-
|
|
790
|
-
for i in range(9):
|
|
791
|
-
j = random.randint(10**i, 10 ** (i + 1))
|
|
792
|
-
s2 = s.copy()
|
|
793
|
-
s2.add_constraints(arg == j)
|
|
794
|
-
# print s2.solver.eval_upto(s2.memory.load(dst_addr, i+2), 2, cast_to=bytes), repr(b"%d\x00" % j)
|
|
795
|
-
assert s2.solver.eval_upto(s2.memory.load(dst_addr, i + 2), 2, cast_to=bytes) == [b"%d\x00" % j]
|
|
796
|
-
|
|
797
|
-
s2 = s.copy()
|
|
798
|
-
s2.add_constraints(arg == 0)
|
|
799
|
-
# print s2.solver.eval_upto(s2.memory.load(dst_addr, 2), 2, cast_to=bytes), repr(b"%d\x00" % 0)
|
|
800
|
-
assert s2.solver.eval_upto(s2.memory.load(dst_addr, 2), 2, cast_to=bytes) == [b"%d\x00" % 0]
|
|
801
|
-
|
|
802
|
-
def test_memset(self):
|
|
803
|
-
log.info("concrete src, concrete dst, concrete len")
|
|
804
|
-
s = SimState(arch="PPC32", mode="symbolic")
|
|
805
|
-
dst = s.solver.BVV(0, 128)
|
|
806
|
-
dst_addr = s.solver.BVV(0x1000, 32)
|
|
807
|
-
char = s.solver.BVV(0x00000041, 32)
|
|
808
|
-
char2 = s.solver.BVV(0x50505050, 32)
|
|
809
|
-
length = s.solver.BVS("some_length", 32)
|
|
810
|
-
|
|
811
|
-
s.memory.store(dst_addr, dst)
|
|
812
|
-
memset(s, arguments=[dst_addr, char, s.solver.BVV(3, 32)])
|
|
813
|
-
assert s.solver.eval(s.memory.load(dst_addr, 4)) == 0x41414100
|
|
814
|
-
|
|
815
|
-
log.debug("Symbolic length")
|
|
816
|
-
s = SimState(arch="PPC32", mode="symbolic")
|
|
817
|
-
s.memory.store(dst_addr, dst)
|
|
818
|
-
length = s.solver.BVS("some_length", 32)
|
|
819
|
-
s.add_constraints(length < 10)
|
|
820
|
-
memset(s, arguments=[dst_addr, char2, length])
|
|
821
|
-
|
|
822
|
-
log.debug("Trying 2")
|
|
823
|
-
s_two = s.copy()
|
|
824
|
-
s_two.add_constraints(length == 2)
|
|
825
|
-
assert s_two.solver.eval(s_two.memory.load(dst_addr, 4)) == 0x50500000
|
|
826
|
-
|
|
827
|
-
log.debug("Trying 0")
|
|
828
|
-
s_zero = s.copy()
|
|
829
|
-
s_zero.add_constraints(length == 0)
|
|
830
|
-
assert s_zero.solver.eval(s_zero.memory.load(dst_addr, 4)) == 0x00000000
|
|
831
|
-
|
|
832
|
-
log.debug("Trying 5")
|
|
833
|
-
s_five = s.copy()
|
|
834
|
-
s_five.add_constraints(length == 5)
|
|
835
|
-
assert s_five.solver.eval(s_five.memory.load(dst_addr, 6)) == 0x505050505000
|
|
836
|
-
|
|
837
|
-
def test_strchr(self):
|
|
838
|
-
log.info("concrete haystack and needle")
|
|
839
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
840
|
-
str_haystack = s.solver.BVV(0x41424300, 32)
|
|
841
|
-
str_needle = s.solver.BVV(0x42, 64)
|
|
842
|
-
addr_haystack = s.solver.BVV(0x10, 64)
|
|
843
|
-
s.memory.store(addr_haystack, str_haystack, endness="Iend_BE")
|
|
844
|
-
|
|
845
|
-
ss_res = strchr(s, arguments=[addr_haystack, str_needle])
|
|
846
|
-
assert s.solver.unique(ss_res)
|
|
847
|
-
assert s.solver.eval(ss_res) == 0x11
|
|
848
|
-
|
|
849
|
-
log.info("concrete haystack, symbolic needle")
|
|
850
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
851
|
-
str_haystack = s.solver.BVV(0x41424300, 32)
|
|
852
|
-
str_needle = s.solver.BVS("wtf", 64)
|
|
853
|
-
chr_needle = str_needle[7:0]
|
|
854
|
-
addr_haystack = s.solver.BVV(0x10, 64)
|
|
855
|
-
s.memory.store(addr_haystack, str_haystack, endness="Iend_BE")
|
|
856
|
-
|
|
857
|
-
ss_res = strchr(s, arguments=[addr_haystack, str_needle])
|
|
858
|
-
assert not s.solver.unique(ss_res)
|
|
859
|
-
assert len(s.solver.eval_upto(ss_res, 10)) == 5
|
|
860
|
-
|
|
861
|
-
s_match = s.copy()
|
|
862
|
-
s_nomatch = s.copy()
|
|
863
|
-
s_match.add_constraints(ss_res != 0)
|
|
864
|
-
s_nomatch.add_constraints(ss_res == 0)
|
|
865
|
-
|
|
866
|
-
assert s_match.satisfiable()
|
|
867
|
-
assert s_nomatch.satisfiable()
|
|
868
|
-
assert len(s_match.solver.eval_upto(chr_needle, 300)) == 4
|
|
869
|
-
assert len(s_nomatch.solver.eval_upto(chr_needle, 300)) == 252
|
|
870
|
-
assert sorted(s_match.solver.eval_upto(ss_res, 300)) == [0x10, 0x11, 0x12, 0x13]
|
|
871
|
-
assert sorted(s_match.solver.eval_upto(chr_needle, 300)) == [0x00, 0x41, 0x42, 0x43]
|
|
872
|
-
|
|
873
|
-
s_match.memory.store(ss_res, s_match.solver.BVV(0x44, 8))
|
|
874
|
-
assert sorted(s_match.solver.eval_upto(s_match.memory.load(0x10, 1), 300)) == [0x41, 0x44]
|
|
875
|
-
assert sorted(s_match.solver.eval_upto(s_match.memory.load(0x11, 1), 300)) == [0x42, 0x44]
|
|
876
|
-
assert sorted(s_match.solver.eval_upto(s_match.memory.load(0x12, 1), 300)) == [0x43, 0x44]
|
|
877
|
-
assert sorted(s_match.solver.eval_upto(s_match.memory.load(0x13, 1), 300)) == [0x00, 0x44]
|
|
878
|
-
|
|
879
|
-
# l.info("symbolic haystack, symbolic needle")
|
|
880
|
-
# s = SimState(arch="AMD64", mode="symbolic")
|
|
881
|
-
# s.libc.buf_symbolic_bytes = 5
|
|
882
|
-
# addr_haystack = s.solver.BVV(0x10, 64)
|
|
883
|
-
# addr_needle = s.solver.BVV(0xb0, 64)
|
|
884
|
-
# len_needle = strlen(s, inline=True, arguments=[addr_needle])
|
|
885
|
-
|
|
886
|
-
# ss_res = strstr(s, inline=True, arguments=[addr_haystack, addr_needle])
|
|
887
|
-
# ss_val = s.expr_value(ss_res)
|
|
888
|
-
|
|
889
|
-
# assert not ss_val.is_unique()
|
|
890
|
-
# assert len(ss_val.solver.eval_upto(100)) == s.libc.buf_symbolic_bytes
|
|
891
|
-
|
|
892
|
-
# s_match = s.copy()
|
|
893
|
-
# s_nomatch = s.copy()
|
|
894
|
-
# s_match.add_constraints(ss_res != 0)
|
|
895
|
-
# s_nomatch.add_constraints(ss_res == 0)
|
|
896
|
-
|
|
897
|
-
# match_cmp = strncmp(s_match, inline=True, arguments=[ss_res, addr_needle, len_needle])
|
|
898
|
-
# match_cmp_val = s_match.expr_value(match_cmp)
|
|
899
|
-
# assert match_cmp_val.solver.eval_upto(10) == [0]
|
|
900
|
-
|
|
901
|
-
# r_mm = strstr(s_match, inline=True, arguments=[addr_haystack, addr_needle])
|
|
902
|
-
# s_match.add_constraints(r_mm == 0)
|
|
903
|
-
# assert not s_match.satisfiable()
|
|
904
|
-
|
|
905
|
-
# assert s_nomatch.satisfiable()
|
|
906
|
-
# s_nss = s_nomatch.copy()
|
|
907
|
-
# nomatch_ss = strstr(s_nss, inline=True, arguments=[addr_haystack, addr_needle])
|
|
908
|
-
# s_nss.add_constraints(nomatch_ss != 0)
|
|
909
|
-
# assert not s_nss.satisfiable()
|
|
910
|
-
|
|
911
|
-
@broken
|
|
912
|
-
def test_strtok_r(self):
|
|
913
|
-
log.debug("CONCRETE MODE")
|
|
914
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
915
|
-
s.memory.store(100, s.solver.BVV(0x4141414241414241424300, 88), endness="Iend_BE")
|
|
916
|
-
s.memory.store(200, s.solver.BVV(0x4200, 16), endness="Iend_BE")
|
|
917
|
-
str_ptr = s.solver.BVV(100, s.arch.bits)
|
|
918
|
-
delim_ptr = s.solver.BVV(200, s.arch.bits)
|
|
919
|
-
state_ptr = s.solver.BVV(300, s.arch.bits)
|
|
920
|
-
|
|
921
|
-
st1 = strtok_r(s, arguments=[str_ptr, delim_ptr, state_ptr])
|
|
922
|
-
assert s.solver.eval_upto(st1, 10) == [104]
|
|
923
|
-
assert s.solver.eval_upto(s.memory.load(st1 - 1, 1), 10) == [0]
|
|
924
|
-
assert s.solver.eval_upto(s.memory.load(200, 2), 10) == [0x4200]
|
|
925
|
-
|
|
926
|
-
st2 = strtok_r(s, arguments=[s.solver.BVV(0, s.arch.bits), delim_ptr, state_ptr])
|
|
927
|
-
assert s.solver.eval_upto(st2, 10) == [107]
|
|
928
|
-
assert s.solver.eval_upto(s.memory.load(st2 - 1, 1), 10) == [0]
|
|
929
|
-
|
|
930
|
-
st3 = strtok_r(s, arguments=[s.solver.BVV(0, s.arch.bits), delim_ptr, state_ptr])
|
|
931
|
-
assert s.solver.eval_upto(st3, 10) == [109]
|
|
932
|
-
assert s.solver.eval_upto(s.memory.load(st3 - 1, 1), 10) == [0]
|
|
933
|
-
|
|
934
|
-
st4 = strtok_r(s, arguments=[s.solver.BVV(0, s.arch.bits), delim_ptr, state_ptr])
|
|
935
|
-
assert s.solver.eval_upto(st4, 10) == [0]
|
|
936
|
-
assert s.solver.eval_upto(s.memory.load(300, s.arch.bytes, endness=s.arch.memory_endness), 10) == [109]
|
|
937
|
-
|
|
938
|
-
st5 = strtok_r(s, arguments=[s.solver.BVV(0, s.arch.bits), delim_ptr, state_ptr])
|
|
939
|
-
assert s.solver.eval_upto(st5, 10) == [0]
|
|
940
|
-
assert s.solver.eval_upto(s.memory.load(300, s.arch.bytes, endness=s.arch.memory_endness), 10) == [109]
|
|
941
|
-
|
|
942
|
-
s.memory.store(1000, s.solver.BVV(0x4141414241414241424300, 88), endness="Iend_BE")
|
|
943
|
-
s.memory.store(2000, s.solver.BVV(0x4200, 16), endness="Iend_BE")
|
|
944
|
-
str_ptr = s.solver.BVV(1000, s.arch.bits)
|
|
945
|
-
delim_ptr = s.solver.BVV(2000, s.arch.bits)
|
|
946
|
-
state_ptr = s.solver.BVV(3000, s.arch.bits)
|
|
947
|
-
|
|
948
|
-
st1 = strtok_r(s, arguments=[str_ptr, delim_ptr, state_ptr])
|
|
949
|
-
assert s.solver.eval_upto(st1, 10) == [1004]
|
|
950
|
-
assert s.solver.eval_upto(s.memory.load(st1 - 1, 1), 10) == [0]
|
|
951
|
-
assert s.solver.eval_upto(s.memory.load(2000, 2), 10) == [0x4200]
|
|
952
|
-
|
|
953
|
-
st2 = strtok_r(s, arguments=[s.solver.BVV(0, s.arch.bits), delim_ptr, state_ptr])
|
|
954
|
-
assert s.solver.eval_upto(st2, 10) == [1007]
|
|
955
|
-
assert s.solver.eval_upto(s.memory.load(st2 - 1, 1), 10) == [0]
|
|
956
|
-
|
|
957
|
-
st3 = strtok_r(s, arguments=[s.solver.BVV(0, s.arch.bits), delim_ptr, state_ptr])
|
|
958
|
-
assert s.solver.eval_upto(st3, 10) == [1009]
|
|
959
|
-
assert s.solver.eval_upto(s.memory.load(st3 - 1, 1), 10) == [0]
|
|
960
|
-
|
|
961
|
-
st4 = strtok_r(s, arguments=[s.solver.BVV(0, s.arch.bits), delim_ptr, state_ptr])
|
|
962
|
-
assert s.solver.eval_upto(st4, 10) == [0]
|
|
963
|
-
assert s.solver.eval_upto(s.memory.load(3000, s.arch.bytes, endness=s.arch.memory_endness), 10) == [1009]
|
|
964
|
-
|
|
965
|
-
st5 = strtok_r(s, arguments=[s.solver.BVV(0, s.arch.bits), delim_ptr, state_ptr])
|
|
966
|
-
assert s.solver.eval_upto(st5, 10) == [0]
|
|
967
|
-
assert s.solver.eval_upto(s.memory.load(3000, s.arch.bytes, endness=s.arch.memory_endness), 10) == [1009]
|
|
968
|
-
|
|
969
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
970
|
-
str_ptr = s.solver.BVV(100, s.arch.bits)
|
|
971
|
-
delim_ptr = s.solver.BVV(200, s.arch.bits)
|
|
972
|
-
state_ptr = s.solver.BVV(300, s.arch.bits)
|
|
973
|
-
|
|
974
|
-
s.add_constraints(s.memory.load(delim_ptr, 1) != 0)
|
|
975
|
-
|
|
976
|
-
st1 = strtok_r(s, arguments=[str_ptr, delim_ptr, state_ptr])
|
|
977
|
-
s.add_constraints(st1 != 0)
|
|
978
|
-
assert s.solver.eval_upto(s.memory.load(st1 - 1, 1), 10) == [0]
|
|
979
|
-
|
|
980
|
-
def test_getc(self):
|
|
981
|
-
s = make_state_with_stdin(b"1234")
|
|
982
|
-
stdin = s.posix.get_fd(0)
|
|
983
|
-
s.mem[0x1000 + 0x70].int = 0
|
|
984
|
-
|
|
985
|
-
assert s.solver.eval_one(stdin.tell()) == 0
|
|
986
|
-
|
|
987
|
-
# The argument of getc should be a FILE *
|
|
988
|
-
c = getc(s, [0x1000])
|
|
989
|
-
assert s.solver.eval_upto(c, 300) == [0x31]
|
|
990
|
-
assert s.solver.eval_upto(stdin.tell(), 300) == [1]
|
|
991
|
-
|
|
992
|
-
c = getc(s, [0x1000])
|
|
993
|
-
assert s.solver.eval_upto(c, 300) == [0x32]
|
|
994
|
-
assert s.solver.eval_upto(stdin.tell(), 300) == [2]
|
|
995
|
-
|
|
996
|
-
c = getc(s, [0x1000])
|
|
997
|
-
assert s.solver.eval_upto(c, 300) == [0x33]
|
|
998
|
-
assert s.solver.eval_upto(stdin.tell(), 300) == [3]
|
|
999
|
-
|
|
1000
|
-
c = getc(s, [0x1000])
|
|
1001
|
-
assert s.solver.eval_upto(c, 300) == [0x34]
|
|
1002
|
-
assert s.solver.eval_upto(stdin.tell(), 300) == [4]
|
|
1003
|
-
|
|
1004
|
-
def test_getchar(self):
|
|
1005
|
-
s = make_state_with_stdin(b"1234")
|
|
1006
|
-
stdin = s.posix.get_fd(0)
|
|
1007
|
-
|
|
1008
|
-
assert s.solver.eval_upto(stdin.tell(), 300) == [0]
|
|
1009
|
-
c = getchar(s, arguments=[])
|
|
1010
|
-
assert s.solver.eval_upto(c, 300) == [0x31]
|
|
1011
|
-
assert s.solver.eval_upto(stdin.tell(), 300) == [1]
|
|
1012
|
-
|
|
1013
|
-
c = getchar(s, arguments=[])
|
|
1014
|
-
assert s.solver.eval_upto(c, 300) == [0x32]
|
|
1015
|
-
assert s.solver.eval_upto(stdin.tell(), 300) == [2]
|
|
1016
|
-
|
|
1017
|
-
c = getchar(s, arguments=[])
|
|
1018
|
-
assert s.solver.eval_upto(c, 300) == [0x33]
|
|
1019
|
-
assert s.solver.eval_upto(stdin.tell(), 300) == [3]
|
|
1020
|
-
|
|
1021
|
-
c = getchar(s, arguments=[])
|
|
1022
|
-
assert s.solver.eval_upto(c, 300) == [0x34]
|
|
1023
|
-
assert s.solver.eval_upto(stdin.tell(), 300) == [4]
|
|
1024
|
-
|
|
1025
|
-
def test_scanf(self):
|
|
1026
|
-
s = make_state_with_stdin(b"Hello\n")
|
|
1027
|
-
s.memory.store(0x2000, b"%1s\0")
|
|
1028
|
-
scanf(s, [0x2000, 0x1000])
|
|
1029
|
-
assert s.solver.eval_upto(s.memory.load(0x1000, 2), 2, cast_to=bytes) == [b"H\x00"]
|
|
1030
|
-
|
|
1031
|
-
def test_strcmp_concrete(self):
|
|
1032
|
-
log.info("concrete a, concrete b")
|
|
1033
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
1034
|
-
a_addr = s.solver.BVV(0x10, 64)
|
|
1035
|
-
b_addr = s.solver.BVV(0xB0, 64)
|
|
1036
|
-
|
|
1037
|
-
s.memory.store(a_addr, b"heck\x00")
|
|
1038
|
-
s.memory.store(b_addr, b"heck\x00")
|
|
1039
|
-
|
|
1040
|
-
r = strcmp(s, arguments=[a_addr, b_addr])
|
|
1041
|
-
assert s.solver.eval_upto(r, 2) == [0]
|
|
1042
|
-
|
|
1043
|
-
log.info("concrete a, empty b")
|
|
1044
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
1045
|
-
a_addr = s.solver.BVV(0x10, 64)
|
|
1046
|
-
b_addr = s.solver.BVV(0xB0, 64)
|
|
1047
|
-
|
|
1048
|
-
s.memory.store(a_addr, b"heck\x00")
|
|
1049
|
-
s.memory.store(b_addr, b"\x00")
|
|
1050
|
-
|
|
1051
|
-
r = strcmp(s, arguments=[a_addr, b_addr])
|
|
1052
|
-
assert s.solver.eval_upto(r, 2) == [1]
|
|
1053
|
-
|
|
1054
|
-
log.info("empty a, concrete b")
|
|
1055
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
1056
|
-
a_addr = s.solver.BVV(0x10, 64)
|
|
1057
|
-
b_addr = s.solver.BVV(0xB0, 64)
|
|
1058
|
-
|
|
1059
|
-
s.memory.store(a_addr, b"\x00")
|
|
1060
|
-
s.memory.store(b_addr, b"heck\x00")
|
|
1061
|
-
|
|
1062
|
-
r = strcmp(s, arguments=[a_addr, b_addr])
|
|
1063
|
-
assert s.solver.eval_upto(r, 2) == [0xFFFFFFFF]
|
|
1064
|
-
|
|
1065
|
-
log.info("empty a, empty b")
|
|
1066
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
1067
|
-
a_addr = s.solver.BVV(0x10, 64)
|
|
1068
|
-
b_addr = s.solver.BVV(0xB0, 64)
|
|
1069
|
-
|
|
1070
|
-
s.memory.store(a_addr, b"\x00")
|
|
1071
|
-
s.memory.store(b_addr, b"\x00")
|
|
1072
|
-
|
|
1073
|
-
r = strcmp(s, arguments=[a_addr, b_addr])
|
|
1074
|
-
assert s.solver.eval_upto(r, 2) == [0]
|
|
1075
|
-
|
|
1076
|
-
def test_wcscmp(self):
|
|
1077
|
-
# concrete cases for the wide char version sufficiently overlap with strcmp and friends
|
|
1078
|
-
log.info("concrete a, symbolic b")
|
|
1079
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
1080
|
-
heck = "heck\x00".encode("utf-16")[2:] # remove encoding prefix
|
|
1081
|
-
a_addr = s.solver.BVV(0x10, 64)
|
|
1082
|
-
b_addr = s.solver.BVV(0xB0, 64)
|
|
1083
|
-
b_bvs = s.solver.BVS("b", len(heck) * 8)
|
|
1084
|
-
|
|
1085
|
-
s.memory.store(a_addr, heck)
|
|
1086
|
-
s.memory.store(b_addr, b_bvs)
|
|
1087
|
-
|
|
1088
|
-
r = wcscmp(s, arguments=[a_addr, b_addr])
|
|
1089
|
-
|
|
1090
|
-
solutions = s.solver.eval_upto(b_bvs, 2, cast_to=bytes, extra_constraints=(r == 0,))
|
|
1091
|
-
assert solutions == [heck]
|
|
1092
|
-
|
|
1093
|
-
def test_string_without_null(self):
|
|
1094
|
-
s = SimState(arch="AMD64", mode="symbolic")
|
|
1095
|
-
str_ = b"abcd"
|
|
1096
|
-
str_addr = s.solver.BVV(0x10, 64)
|
|
1097
|
-
s.memory.store(str_addr, str_)
|
|
1098
|
-
assert s.solver.eval(s.mem[str_addr].string.resolved, cast_to=bytes) == b"abcd"
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
if __name__ == "__main__":
|
|
1102
|
-
unittest.main()
|