@@ -43,13 +43,13 @@ def test_modify_instruction_patch(self):
43
43
self .run_one (
44
44
"printf_nopie" ,
45
45
[
46
- ModifyInstructionPatch (0x40113e , "lea rax, [0x402007]" ),
46
+ ModifyInstructionPatch (0x40113E , "lea rax, [0x402007]" ),
47
47
ModifyInstructionPatch (0x401145 , "mov rsi, rax" ),
48
48
],
49
49
expected_output = b"%s" ,
50
50
expected_returnCode = 0 ,
51
51
)
52
-
52
+
53
53
def test_insert_instruction_patch (self ):
54
54
instrs = """
55
55
mov rax, 0x1
@@ -60,7 +60,7 @@ def test_insert_instruction_patch(self):
60
60
"""
61
61
self .run_one (
62
62
"printf_nopie" ,
63
- [InsertInstructionPatch (0x40115c , instrs )],
63
+ [InsertInstructionPatch (0x40115C , instrs )],
64
64
expected_output = b"Hi\x00 Hi" ,
65
65
expected_returnCode = 0 ,
66
66
)
@@ -75,7 +75,7 @@ def test_insert_instruction_patch_2(self):
75
75
"printf_nopie" ,
76
76
[
77
77
InsertInstructionPatch ("return_0x32" , instrs ),
78
- ModifyInstructionPatch (0x40115c , "jmp {return_0x32}" ),
78
+ ModifyInstructionPatch (0x40115C , "jmp {return_0x32}" ),
79
79
],
80
80
expected_returnCode = 0x32 ,
81
81
)
@@ -107,7 +107,7 @@ def test_insert_data_patch(self, tlen=5):
107
107
mov rdx, %s
108
108
syscall
109
109
""" % hex (tlen )
110
- p2 = InsertInstructionPatch (0x40115c , instrs )
110
+ p2 = InsertInstructionPatch (0x40115C , instrs )
111
111
self .run_one (
112
112
"printf_nopie" ,
113
113
[p1 , p2 ],
@@ -160,45 +160,45 @@ def test_replace_function_patch_with_function_reference_and_rodata(self):
160
160
)
161
161
162
162
def run_one (
163
- self ,
164
- filename ,
165
- patches ,
166
- set_oep = None ,
167
- inputvalue = None ,
168
- expected_output = None ,
169
- expected_returnCode = None ,
170
- ):
171
- filepath = os .path .join (self .bin_location , filename )
172
- pipe = subprocess .PIPE
173
-
174
- with tempfile .TemporaryDirectory () as td :
175
- tmp_file = os .path .join (td , "patched" )
176
- p = Patcherex (filepath )
177
- for patch in patches :
178
- p .patches .append (patch )
179
- p .apply_patches ()
180
- p .binfmt_tool .save_binary (tmp_file )
181
- # os.system(f"readelf -hlS {tmp_file}")
182
-
183
- p = subprocess .Popen (
184
- [tmp_file ],
185
- stdin = pipe ,
186
- stdout = pipe ,
187
- stderr = pipe ,
188
- )
189
- res = p .communicate (inputvalue )
190
- if expected_output :
191
- if res [0 ] != expected_output :
192
- self .fail (
193
- f"AssertionError: { res [0 ]} != { expected_output } , binary dumped: { self .dump_file (tmp_file )} "
194
- )
195
- # self.assertEqual(res[0], expected_output)
196
- if expected_returnCode :
197
- if p .returncode != expected_returnCode :
198
- self .fail (
199
- f"AssertionError: { p .returncode } != { expected_returnCode } , binary dumped: { self .dump_file (tmp_file )} "
200
- )
201
- # self.assertEqual(p.returncode, expected_returnCode)
163
+ self ,
164
+ filename ,
165
+ patches ,
166
+ set_oep = None ,
167
+ inputvalue = None ,
168
+ expected_output = None ,
169
+ expected_returnCode = None ,
170
+ ):
171
+ filepath = os .path .join (self .bin_location , filename )
172
+ pipe = subprocess .PIPE
173
+
174
+ with tempfile .TemporaryDirectory () as td :
175
+ tmp_file = os .path .join (td , "patched" )
176
+ p = Patcherex (filepath )
177
+ for patch in patches :
178
+ p .patches .append (patch )
179
+ p .apply_patches ()
180
+ p .binfmt_tool .save_binary (tmp_file )
181
+ # os.system(f"readelf -hlS {tmp_file}")
182
+
183
+ p = subprocess .Popen (
184
+ [tmp_file ],
185
+ stdin = pipe ,
186
+ stdout = pipe ,
187
+ stderr = pipe ,
188
+ )
189
+ res = p .communicate (inputvalue )
190
+ if expected_output :
191
+ if res [0 ] != expected_output :
192
+ self .fail (
193
+ f"AssertionError: { res [0 ]} != { expected_output } , binary dumped: { self .dump_file (tmp_file )} "
194
+ )
195
+ # self.assertEqual(res[0], expected_output)
196
+ if expected_returnCode :
197
+ if p .returncode != expected_returnCode :
198
+ self .fail (
199
+ f"AssertionError: { p .returncode } != { expected_returnCode } , binary dumped: { self .dump_file (tmp_file )} "
200
+ )
201
+ # self.assertEqual(p.returncode, expected_returnCode)
202
202
203
203
def dump_file (self , file ):
204
204
shutil .copy (file , "/tmp/patcherex_failed_binary" )
0 commit comments