Patches
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
Patch that allows instructions to be inserted into binary. These instructions are inserted at a free place in the binary. Then, At the address given, an instruction is inserted that jumps to this block (also in the block are the instructions this overwrites). At the end of the block, it jumps back to right after the initial jump. The initial jump must be able to be inserted within the basic block of the given address.
Source code in src/patcherex2/patches/instruction_patches.py
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 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 |
|
__init__(addr_or_name, instr, 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, 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
|
|
**kwargs |
Extra options. Can have a boolean "save_context" for whether context should be saved. |
{}
|
Source code in src/patcherex2/patches/instruction_patches.py
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 |
|
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
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 |
|
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
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 |
|
__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
27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
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
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 |
|
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 |
|
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
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 |
|
__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
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
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
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
|