Patcherex
InsertDataPatch
Bases: Patch
Patch that inserts data into the binary.
Source code in src/patcherex2/patches/data_patches.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
__init__(addr_or_name, data)
Constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
addr_or_name
|
int | str
|
If an integer, data is inserted at the address. If a string, it is placed in a free spot in the binary and added as a symbol (with this as its name). |
required |
data
|
bytes
|
New data to place in binary. |
required |
Source code in src/patcherex2/patches/data_patches.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
apply(p)
Applies the patch to the binary, intended to be called by a Patcherex instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
Patcherex
|
Patcherex instance. |
required |
Source code in src/patcherex2/patches/data_patches.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
InsertFunctionPatch
Bases: Patch
Inserts a function into the binary.
Source code in src/patcherex2/patches/function_patches.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
__init__(addr_or_name, code, force_insert=False, detour_pos=-1, symbols=None, is_thumb=False, **kwargs)
Constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
addr_or_name
|
int | str
|
If an integer, an intermediate function is created in a free spot in the binary, and at that address, a jump to the function is made with necessary context saves. If a string, the function is created in a free spot in the binary with that name. |
required |
code
|
str
|
C code for the new function. "SAVE_CONTEXT" and "RESTORE_CONTEXT" can be used to save and restore context. |
required |
force_insert
|
If Patcherex should ignore whether instructions can be moved when inserting, defaults to False |
False
|
|
detour_pos
|
If address is used, this is the address to place trampoline code for jumping to function. If name is used, this is where the new function will be placed, defaults to -1 |
-1
|
|
symbols
|
dict[str, int] | None
|
Symbols to include when compiling/assembling, in format {symbol name: memory address}, defaults to None |
None
|
is_thumb
|
Whether the instructions given are thumb, defaults to False |
False
|
|
kwargs
|
Extra options. Can include "prefunc" and "postfunc", instructions to go before or after your function if you give an address. Can also have "save_context" for whether context should be saved and "compile_opts" for extra compile options. |
{}
|
Source code in src/patcherex2/patches/function_patches.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
apply(p)
Applies the patch to the binary, intended to be called by a Patcherex instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
Patcherex
|
Patcherex instance. |
required |
Source code in src/patcherex2/patches/function_patches.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
InsertInstructionPatch
Bases: Patch
Source code in src/patcherex2/patches/instruction_patches.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
|
CConfig
Source code in src/patcherex2/patches/instruction_patches.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
__init__(c_forward_header='', scratch_regs=None, regs_sort=None, asm_header='', asm_footer='')
Used to configure an InsertInstructionPatch when language == "C"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
c_forward_header
|
str
|
C code that will be inserted before the micropatch code. This is useful when you want to use C types, C headers, and C function forward declarations. |
''
|
scratch_regs
|
Iterable[str]
|
It is generally a good idea to mark some registers as scratch to give the compiler breathing room for allocating registers to use for intermediate variables in your micropatch. All of the registers that we mark as scratch can be freely clobbered by the compiler Note that you can still read from scratch registers stored in the variables. What the scratch register denotation will indicate however is that the register can be re-used after the variable is no longer live. |
None
|
regs_sort
|
Iterable[str | tuple[str, int] | tuple[str, str]] | None
|
Some architectures have subregisters which allow access to a portion of a register, such as the lower 32 bits of a 64 bit register. If you want to use a subregister instead of the full register you can request this by passing in a list of subregisters here. Note that if you specify a subregister here, the full parent register is not available in the C patch. Some registers allow values of smaller types to be stored in larger registers. For example, you can store float values in xmm0 on x64, but there is no 32 bit subregister. To indicate that a register should be treated as a specific size, you may specify register size in bits in the second element of the tuple. Alternatively, you may specify the type as a string. An example of a valid list for x64 is ['eax', ('xmm0', 32), ('xmm1', 'double')]. |
None
|
asm_header
|
str
|
The asm_header is inserted in the main body of the patch before the C code. This header is primarily useful for gaining access to the stack pointer, which is a register that is typically unavailable in our C patch code. |
''
|
asm_footer
|
str
|
The asm_footer is the same as the asm_header, except that it runs after the C code executes. This is typically less useful than asm_header, but is still available here if you need to do any cleanup in assembly. |
''
|
Source code in src/patcherex2/patches/instruction_patches.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
__init__(addr_or_name, instr, force_insert=False, detour_pos=-1, symbols=None, is_thumb=False, language='ASM', c_config=None, **kwargs)
Constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
addr_or_name
|
int | str
|
If an integer, the new instructions are placed in a free spot in the binary and the jump to them is inserted at that memory address. If a string, the new instructions are placed in a free spot in the binary and added as a symbol (with this as its name). |
required |
instr
|
str
|
Instructions to insert. You can use "SAVE_CONTEXT" and "RESTORE_CONTEXT" wherever you want to save and restore program context. If you want to use any symbols from the program or from previous patches, you must surround them with curly braces. |
required |
force_insert
|
If Patcherex should ignore whether instructions can be moved when inserting, defaults to False |
False
|
|
detour_pos
|
If given a name, specifies the file address to place the new instructions, defaults to -1 |
-1
|
|
symbols
|
dict[str, int] | None
|
Symbols to include when assembling, in format {symbol name: memory address}, defaults to None |
None
|
is_thumb
|
Whether the instructions given are thumb, defaults to False |
False
|
|
language
|
str
|
The language of the patch, can be either "ASM" or "C" |
'ASM'
|
c_config
|
CConfig | None
|
Configuration options for when language == "C" |
None
|
**kwargs
|
Extra options. Can have a boolean "save_context" for whether context should be saved. |
{}
|
Source code in src/patcherex2/patches/instruction_patches.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
|
apply(p)
Applies the patch to the binary, intended to be called by a Patcherex instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
Patcherex
|
Patcherex instance. |
required |
Source code in src/patcherex2/patches/instruction_patches.py
230 231 232 233 234 235 236 237 238 239 240 |
|
ModifyDataPatch
Bases: ModifyRawBytesPatch
Extends ModifyRawBytesPatch to only be used for memory addresses, used for modifying data in binary.
Source code in src/patcherex2/patches/data_patches.py
17 18 19 20 21 22 23 24 25 26 |
|
__init__(addr, new_bytes)
Same as ModifyRawBytesPatch constructor, but address type of memory is assumed.
Source code in src/patcherex2/patches/data_patches.py
22 23 24 25 26 |
|
ModifyFunctionPatch
Bases: Patch
Patch that replaces an existing function in the binary with your own. If there is enough room in the existing function, your code is compiled and placed there. If not, your code is placed in a free spot in the binary, and the function will jump there instead.
Source code in src/patcherex2/patches/function_patches.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
__init__(addr_or_name, code, detour_pos=-1, symbols=None, **kwargs)
Constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
addr_or_name
|
int | str
|
The name or file address of the function. |
required |
code
|
str
|
C code to replace the function. |
required |
detour_pos
|
If original function is not big enough, file address to place the given code, defaults to -1 |
-1
|
|
symbols
|
dict[str, int] | None
|
Symbols to include when compiling, in format {symbol name: memory address}, defaults to None |
None
|
Source code in src/patcherex2/patches/function_patches.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
apply(p)
Applies the patch to the binary, intended to be called by a Patcherex instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
Patcherex
|
Patcherex instance. |
required |
Source code in src/patcherex2/patches/function_patches.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
ModifyInstructionPatch
Bases: Patch
Patch that directly modifies instructions in a binary (overwrites them) starting at address given. If ISA is variable length, then if there are remaining bytes in the last overwritten instruction, it will fill them with nops, but it will fail if remaining bytes are not divisible by nop length.
Source code in src/patcherex2/patches/instruction_patches.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|
__init__(addr, instr, symbols=None)
Constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
addr
|
int
|
Memory address of instruction(s) to overwrite. |
required |
instr
|
str
|
Assembly instruction(s) to place in binary. If you want to use any symbols from the program or from previous patches, you must surround them with curly braces. |
required |
symbols
|
dict[str, int] | None
|
Symbols to include when assembling, in format {symbol name: memory address}, defaults to None |
None
|
Source code in src/patcherex2/patches/instruction_patches.py
28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
apply(p)
Applies the patch to the binary, intended to be called by a Patcherex instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
Patcherex
|
Patcherex instance. |
required |
Source code in src/patcherex2/patches/instruction_patches.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|
ModifyRawBytesPatch
Bases: Patch
Patch that modifies bytes of the binary.
Source code in src/patcherex2/patches/raw_patches.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
__init__(addr, new_bytes, addr_type='mem')
Constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
addr
|
int
|
Starting address of bytes you want to change. |
required |
new_bytes
|
bytes
|
New bytes to replace original ones. |
required |
addr_type
|
The type of address given, "mem" (memory address) or "raw" (file address), defaults to "mem" |
'mem'
|
Source code in src/patcherex2/patches/raw_patches.py
17 18 19 20 21 22 23 24 25 26 27 |
|
apply(p)
Applies the patch to the binary, intended to be called by a Patcherex instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
Patcherex instance. |
required |
Raises:
Type | Description |
---|---|
NotImplementedError
|
Raised if an address type other than "raw" or "mem" is specified. |
Source code in src/patcherex2/patches/raw_patches.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
Patcherex
The main class of the library. This is how you are intended to interact with patches.
Source code in src/patcherex2/patcherex.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
|
__init__(binary_path, target_cls=None, target_opts=None, components_opts=None)
Constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
binary_path
|
str
|
The path of the binary to patch. |
required |
target_cls
|
type[Target] | None
|
Specified architecture class to use, otherwise it is automatically detected, defaults to None |
None
|
target_opts
|
dict[str, str] | None
|
Options to specify components for the target, defaults to None |
None
|
components_opts
|
dict[str, dict[str, str]] | None
|
Options for configuring each component for the target, defaults to None |
None
|
Source code in src/patcherex2/patcherex.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
|
apply_patches()
Applies all added patches to the binary. Call this when you have added all the patches you want.
Source code in src/patcherex2/patcherex.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
save_binary(filename=None)
Save the patched binary to a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename
|
str
|
Name of file to save to, defaults to ' |
None
|
Source code in src/patcherex2/patcherex.py
112 113 114 115 116 117 118 |
|
shutdown()
Shuts down any resources used by Patcherex2. This needs to be called when using Ghidra as the binary analyzer when done patching.
Source code in src/patcherex2/patcherex.py
88 89 90 91 92 93 94 |
|
RemoveDataPatch
Bases: ModifyRawBytesPatch
Extends ModifyRawBytesPatch for removing data in the binary (fills it with null bytes starting at address given). Expects a memory address.
Source code in src/patcherex2/patches/data_patches.py
66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
__init__(addr, size)
Same as ModifyRawBytes Patch constructor, but adds size parameter and assumes memory address.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
The number of bytes to remove. |
required |
Source code in src/patcherex2/patches/data_patches.py
72 73 74 75 76 77 78 |
|
RemoveFunctionPatch
Bases: Patch
Patch that removes a function from the binary. Not implemented.
Source code in src/patcherex2/patches/function_patches.py
229 230 231 232 233 234 235 236 237 238 |
|
__init__(parent=None)
Constructor.
Source code in src/patcherex2/patches/function_patches.py
234 235 236 237 238 |
|
RemoveInstructionPatch
Bases: Patch
Patch that removes instructions in the binary. Currently only takes in a number of bytes and an starting address. The number of bytes must be divisible by the nop size of the architecture, otherwise it will fail.
Source code in src/patcherex2/patches/instruction_patches.py
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
|
__init__(addr, num_instr=None, num_bytes=None)
Constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
addr
|
int
|
Memory address to remove instructions at. |
required |
num_instr
|
int | None
|
Number of instructions to remove, currently not used, defaults to None |
None
|
num_bytes
|
int | None
|
Number of bytes to remove, must be divisible by nop size, defaults to None |
None
|
Source code in src/patcherex2/patches/instruction_patches.py
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
|
apply(p)
Applies the patch to the binary, intended to be called by a Patcherex instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
Patcherex
|
Patcherex instance. |
required |
Source code in src/patcherex2/patches/instruction_patches.py
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
|