API Reference
The patcher
Main patcher utility
Patcher
Patch processor, applies patches to the target binary
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
binary |
Union[Path, str, bytes]
|
Either a path to the target binary or the raw bytes of the binary |
required |
cle_opts |
Optional[Dict[str, bool]]
|
An optional replacement set of options to pass to cle.Loader |
None
|
Source code in pypatches/patcher.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 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 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 | |
__init__(binary, use_angr=True, cle_opts=None, cfg_opts=None)
Set up patcher with the target binary
Source code in pypatches/patcher.py
39 40 41 42 43 44 45 46 47 | |
apply(patch)
Apply a patch to the target binary
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patch |
PatchType
|
The patch to apply, can be any of the patch types |
required |
Source code in pypatches/patcher.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
apply_add_code_patch(patch)
Apply an add code patch to the target binary
Source code in pypatches/patcher.py
156 157 158 159 160 | |
apply_always_branch_patch(patch)
Apply a branch patch to the target binary
Source code in pypatches/patcher.py
109 110 111 112 | |
apply_caller_replace_patch(patch)
Apply a caller replace patch to the target binary
Source code in pypatches/patcher.py
132 133 134 135 136 | |
apply_data_patch(patch)
Apply a data patch to the target binary
Source code in pypatches/patcher.py
150 151 152 153 154 | |
apply_fini_patch(patch)
Apply a fini patch to the target binary
Source code in pypatches/patcher.py
144 145 146 147 148 | |
apply_function_replace_patch(patch)
Apply a function replace patch to the target binary
Source code in pypatches/patcher.py
126 127 128 129 130 | |
apply_init_patch(patch)
Apply an init patch to the target binary
Source code in pypatches/patcher.py
138 139 140 141 142 | |
apply_invert_branch_patch(patch)
Apply a branch patch to the target binary
Source code in pypatches/patcher.py
103 104 105 106 107 | |
apply_never_branch_patch(patch)
Apply a branch patch to the target binary
Source code in pypatches/patcher.py
114 115 116 117 118 | |
apply_nop_patch(patch)
Apply a nop patch to the target binary
Source code in pypatches/patcher.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | |
apply_replace_code_patch(patch)
Apply a replace code patch to the target binary
Source code in pypatches/patcher.py
162 163 164 165 166 | |
apply_skip_and_return_patch(patch)
Apply a branch patch to the target binary
Source code in pypatches/patcher.py
120 121 122 123 124 | |
save(path)
Save the patched binary to the given path
Source code in pypatches/patcher.py
72 73 74 75 76 77 78 79 80 81 | |
Patch Types
Patch descriptors for various types of patches
Patches require various parameters to specify how to apply them, but some are common.
label, if available, allows a label to be specified. In any later patch, that label
can be used to reference the location of that patch (this is especially useful for
data patches)
AddCodePatch
dataclass
Bases: CodePatch
Patch that adds some code to the binary at some labeled location
Attributes:
| Name | Type | Description |
|---|---|---|
label |
Optional[str]
|
The label to add the code at |
Source code in pypatches/patches.py
187 188 189 190 191 192 193 194 195 | |
AlwaysBranchPatch
dataclass
Bases: BranchPatch
Patch that converts a conditional branch into an unconditional branch that always takes the "true" branch
Source code in pypatches/patches.py
63 64 65 66 67 | |
BranchPatch
dataclass
Base patch that modifies a conditional branch at a particular address
Attributes:
| Name | Type | Description |
|---|---|---|
address |
int
|
The address of the branch to modify |
Source code in pypatches/patches.py
47 48 49 50 51 52 53 54 55 | |
CallerReplacePatch
dataclass
Patch that will redirect all or some callers of a function elsewhere
Attributes:
| Name | Type | Description |
|---|---|---|
new_code |
Code
|
The new code to replace the function with |
function_address |
Optional[int]
|
The name of the function to replace |
callers |
Set[int]
|
The set of callers to redirect, optional |
Source code in pypatches/patches.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
__post_init__()
Check that we either got a function address or callers, or both
Source code in pypatches/patches.py
120 121 122 123 124 125 126 127 | |
CodePatch
dataclass
Base patch that holds some code
Attributes:
| Name | Type | Description |
|---|---|---|
code |
Code
|
The code to add |
Source code in pypatches/patches.py
175 176 177 178 179 180 181 182 183 184 | |
DataPatch
dataclass
Patch that adds some data with some protections
Attributes:
| Name | Type | Description |
|---|---|---|
data |
bytes
|
The data to add |
label |
Optional[str]
|
The label to give the data, optional |
read |
bool
|
Whether the data should be readable, defaults to True |
write |
bool
|
Whether the data should be writable, defaults to False |
execute |
bool
|
Whether the data should be executable, defaults to False |
Source code in pypatches/patches.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
FiniPatch
dataclass
Patch that will run some code upon exit from the program
Attributes:
| Name | Type | Description |
|---|---|---|
code |
Code
|
The code to run |
priority |
int
|
The priority of the code to run, optional |
Source code in pypatches/patches.py
143 144 145 146 147 148 149 150 151 152 153 | |
FunctionReplacePatch
dataclass
Patch that will replace a function's contents with new code
Attributes:
| Name | Type | Description |
|---|---|---|
function_address |
int
|
The name of the function to replace |
new_code |
Code
|
The new code to replace the function with |
Source code in pypatches/patches.py
92 93 94 95 96 97 98 99 100 101 102 | |
InitPatch
dataclass
Patch that will run some code immediately on entry to the program
Attributes:
| Name | Type | Description |
|---|---|---|
code |
Code
|
The code to run |
priority |
int
|
The priority of the code to run, optional |
Source code in pypatches/patches.py
130 131 132 133 134 135 136 137 138 139 140 | |
InvertBranchPatch
dataclass
Bases: BranchPatch
Patch that inverts the true/false branch targets of a conditional branch
Source code in pypatches/patches.py
58 59 60 | |
NeverBranchPatch
dataclass
Bases: BranchPatch
Patch that converts a conditional branch into an unconditional branch that always takes the "false" branch
Source code in pypatches/patches.py
70 71 72 73 74 | |
NopPatch
dataclass
Patch that converts specific addresses or ranges of addresses to no-operations
Attributes:
| Name | Type | Description |
|---|---|---|
address_ranges |
Set[AddressRange]
|
The address ranges to convert to no-operations |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
addresses |
InitVar[Optional[List[int]]]
|
Individual addresses to convert to no-operations, optional. |
None
|
address_ranges |
Set[AddressRange]
|
The address ranges to convert to no-operations, optional. |
field(default_factory=set)
|
Source code in pypatches/patches.py
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 | |
__post_init__(addresses)
Convert optionally provided addresses to address ranges
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
addresses |
Optional[List[int]]
|
Addresses that can be provided in lieu of address ranges |
required |
Source code in pypatches/patches.py
34 35 36 37 38 39 40 41 42 43 44 | |
ReplaceCodePatch
dataclass
Bases: CodePatch
Patch that replaces some code with some other code
Attributes:
| Name | Type | Description |
|---|---|---|
address |
Union[str, Callable[[TransformInfo], int], int, List[int], Callable[[TransformInfo], List[int]]]
|
The address to replace the code at, optional. This "address" can either be a label, an actual address, a list of addresses, a function that takes a [TransformInfo][] and returns an address, or a function that takes a [TransformInfo][] and returns a list of addresses. |
Source code in pypatches/patches.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
RuntimeResolverPatch
dataclass
Bases: CodePatch
Patch that adds a runtime resolver to the binary
Source code in pypatches/patches.py
219 220 221 | |
SkipAndReturnPatch
dataclass
Patch that skips a call to a subroutine at a particular address and fakes a return value as if the called function had returned it using the default calling convention
Attributes:
| Name | Type | Description |
|---|---|---|
address |
The address of the call to skip |
|
return_value |
int
|
The value to return from the call |
Source code in pypatches/patches.py
77 78 79 80 81 82 83 84 85 86 87 88 89 | |
Binary Manager
Binary program wrapper providing modification and analysis via LIEF and angr
BinaryManager
Wrapper for a binary program to provide information for patching
Attributes:
| Name | Type | Description |
|---|---|---|
path |
Optional[Path]
|
The path to the binary, if it exists on disk |
blob |
BytesIO
|
The binary blob of the binary |
lief_binary |
LIEFBinary
|
The LIEF binary object, which provides the majority of the program information for modification |
angr_project |
Project
|
The angr project, which provides the CFG and other information for analysis and deep inspection |
cle_binary |
Backend
|
The CLE binary object, which provides angr's loader information |
cle_opts |
Dict[str, bool]
|
The kwargs passed to |
cfg_opts |
Dict[str, bool]
|
The kwargs passed to |
writes |
List[WriteOperation]
|
A list of |
code_to_add |
Dict[str, Code]
|
A dictionary of |
alignment |
The alignment to use when aligning addresses |
|
data_to_add |
Dict[str, bytes]
|
A dictionary of data to add to the binary, keyed by the label the data is associated with |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
binary |
Union[Path, str, bytes]
|
If |
required |
cle_opts |
Optional[Dict[str, bool]]
|
If provided, will override the kwargs passed to |
None
|
cfg_opts |
Optional[Dict[str, bool]]
|
If provided, will override the kwargs passed to
|
None
|
silence_angr_logs |
bool
|
If |
False
|
alignment |
int
|
The alignment to use when aligning addresses, defaults to 8, but will be overridden by lief's alignment if it discovers a different alignment when loading the binary |
8
|
Source code in pypatches/binary_manager.py
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 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 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 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | |
__init__(binary, use_angr=True, cle_opts=None, cfg_opts=None, silence_angr_logs=False, alignment=8)
Initialize the binary wrapper via one of several methods.
Source code in pypatches/binary_manager.py
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 | |
add_code(code, label=None)
Mark some code as being added on next save
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code |
Code
|
The binary code being added |
required |
label |
Optional[str]
|
The label to associate with the code for resolution of other patches referencing it |
None
|
Source code in pypatches/binary_manager.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
add_data(data, label=None)
Add some data to the binary
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data |
bytes
|
The data to add |
required |
label |
Optional[str]
|
The label to associate with the data location |
None
|
Source code in pypatches/binary_manager.py
275 276 277 278 279 280 281 282 283 284 285 286 287 | |
align(address, alignment=None)
Align an address to the specified alignment. If no alignment is provided, the default alignment will be used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address |
int
|
The address to align |
required |
alignment |
Optional[int]
|
The alignment to use, defaults to the default alignment of the binary |
None
|
Source code in pypatches/binary_manager.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
apply()
Apply patches to the binary
Source code in pypatches/binary_manager.py
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 | |
asm(asm, vaddr)
Assemble the given assembly code at the given virtual address
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
asm |
str
|
The assembly code to assemble |
required |
vaddr |
int
|
The virtual address to assemble at |
required |
Source code in pypatches/binary_manager.py
448 449 450 451 452 453 454 455 456 457 458 459 | |
disasm(vaddr)
Disassemble one instruction from the binary at an address
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vaddr |
int
|
The virtual address to disassemble at |
required |
Source code in pypatches/binary_manager.py
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | |
load_angr_project()
Load the angr project from the binary blob
Source code in pypatches/binary_manager.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |
load_lief_binary()
Load the binary into LIEF
Source code in pypatches/binary_manager.py
161 162 163 | |
read(vaddr, size)
Read data from the binary at the given virtual address
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vaddr |
int
|
The virtual address to read from |
required |
size |
int
|
The number of bytes to read |
required |
Source code in pypatches/binary_manager.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
reload_blob_from_lief()
Reload the blob from the current LIEF binary
Source code in pypatches/binary_manager.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
save(where)
Apply any pending operations and save the binary to a file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
where |
Path
|
The path to the destination to save the binary |
required |
Source code in pypatches/binary_manager.py
436 437 438 439 440 441 442 443 444 445 446 | |
write(where, data)
Write data to the binary at the given virtual address
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
where |
Union[str, Callable[[TransformInfo], int], int, List[int], Callable[[TransformInfo], List[int]]]
|
Either an address or a label that will resolve to an address, or a function that takes the transform info after segment modification and returns an address |
required |
data |
Union[bytes, Callable[[Dict[str, int]], bytes], Code]
|
The bytes or a function that takes a dictionary of labels and addresses and returns bytes to write to the patch |
required |
Source code in pypatches/binary_manager.py
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 | |
Patch Code Types
Superclass for all code types
Code
Bases: ABC
Superclass for other code types defining required functions
Code objects are used to represent code that can be compiled or assembled into machine code. The code object is used to represent the code in a way that can be modified by the patching process, and then compiled or assembled into machine code for insertion into the binary.
Generally, each code object goes through the following life cycle: - dummy is called to generate a dummy version of the code that can be used to estimate the size of the code in the binary. - build is called to generate the final version of the code that will be compiled or assembled. - compile is called to compile or assemble the code into machine code bytes. - post_build is called to modify the compiled code after compilation (for example, to add a jump to the end of the code).
Attributes:
| Name | Type | Description |
|---|---|---|
code |
str
|
The code to be compiled or assembled |
original_code |
str
|
The original code, before any modifications |
dummy_transformer |
Callable[[str], str]
|
A function that takes this container's
code and returns a valid compile/assemble-able code (see |
build_transformer |
Callable[[TransformInfo, str], str]
|
A function that takes a
|
post_transformer |
Callable[[TransformInfo, bytes], bytes]
|
A function that
takes
a |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code |
str
|
The code, to initialize with |
required |
dummy_transformer |
Callable[[str], str]
|
A function that takes this container's
code and returns a valid compile/assemble-able code (see |
None
|
build_transformer |
Callable[[TransformInfo, str], str]
|
A function that takes a
|
None
|
post_transformer |
Callable[[TransformInfo, bytes], bytes]
|
A function that
takes a |
None
|
Source code in pypatches/code/code.py
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 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 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 | |
__init__(code, dummy_transformer=None, build_transformer=None, post_transformer=None)
Initialize the Code object
Source code in pypatches/code/code.py
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 | |
build(info)
Build the code into the final version that can be built with the binary context into the final patch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
info |
TransformInfo
|
The |
required |
Source code in pypatches/code/code.py
137 138 139 140 141 142 143 144 145 | |
compile(label, info=None)
Compile or assemble the code (or do not change it if raw)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label |
str
|
The label to use for this code |
required |
info |
Optional[TransformInfo]
|
The |
None
|
Source code in pypatches/code/code.py
157 158 159 160 161 162 163 164 | |
dummy()
Generate valid dummy code to determine the size required to fit this code in the binary.
This function will be called to create a dummy result from the code -- the dummy result should be valid code (ie if this Code instance is C code it should compile, and if it is assembly it should assemble) but is not necessarily the correct code to go into the final patch.
For example, a code snippet:
int get_data(void) {
return {DATA_VALUE};
}
might have a "dummy" result of:
int get_data(void) {
return 0x41414141;
}
The code returned by this function will never be used to produce a binary, only to estimate the required size for segment insertion.
Source code in pypatches/code/code.py
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 | |
post_build(info)
Modify the built code after compilation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
info |
TransformInfo
|
The |
required |
Source code in pypatches/code/code.py
147 148 149 150 151 152 153 154 155 | |
reset()
Reset the code to its original state without any modifications
Source code in pypatches/code/code.py
102 103 104 | |
ASM subclass for assembly code used in patches
ASMCode
Bases: Code
Assembly code container for patching
Assembly code should be written in a format that can be assembled by keystone-engine. Generally, that means AT&T syntax for x86 and standard syntax for everything else.
Source code in pypatches/code/asm.py
11 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 | |
compile(label, info=None)
Assemble the assembly code to raw bytes of machine code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label |
str
|
The label to use for the code |
required |
info |
Optional[TransformInfo]
|
Optional transform info to use for the compilation |
None
|
Returns:
| Type | Description |
|---|---|
bytes
|
The raw bytes of the compiled code |
Source code in pypatches/code/asm.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
Code type for C code
CCode
Bases: Code
C code container for patching
C code should be written such that it can be compiled by
squishy. Generally, that means that the
inline keyword should not be used, code should be written as C99-like as possible,
and that most importantly external library calls must not be used. However, you can
write code that uses system headers using the #include <...> syntax, and you can
use data structures and macros from those headers.
Source code in pypatches/code/c_code.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
compile(label, info=None)
Compile the C code to raw machine code bytes
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label |
str
|
The label to use for the code |
required |
info |
Optional[TransformInfo]
|
Optional transform info to use for the compilation |
None
|
Returns:
| Type | Description |
|---|---|
bytes
|
The raw bytes of the compiled code |
Source code in pypatches/code/c_code.py
24 25 26 27 28 29 30 31 32 33 34 | |
build_c_code(main_body, helpers=None, includes=None, extra_code=None, dummy_transformer=None, build_transformer=None, post_transformer=None)
Build a C code snippet a main body, helpers, and include files
Generally, you should use this function instead of manually creating a CCode object manually.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
main_body |
str
|
The body of the main function to build, do not include |
required |
helpers |
Optional[List[str]]
|
A list of filenames or paths to include into the program as helpers from
|
None
|
includes |
Optional[List[str]]
|
A list of include files like |
None
|
extra_code |
Optional[str]
|
Extra code to include in the program at global scope, write any
functions you want to use in |
None
|
dummy_transformer |
Optional[Callable[[Union[str, bytes]], Union[str, bytes]]]
|
A function that is passed self.code and returns a string of code to use for the dummy compilation. The result must compile but is not the code that will be inserted into the final patched binary, it is only used to obtain a size estimate. If not provided, the default is to return self.code unchanged. |
None
|
build_transformer |
Optional[Callable[[TransformInfo, Union[str, bytes]], Union[str, bytes]]]
|
A function that is passed [self.code][pypatches.code.code.Code.code] and returns a string of code to use for the final compilation. The result must compile and will be inserted into the final patched binary. If not provided, the code will be passed through unchanged. |
None
|
post_transformer |
Optional[Callable[[TransformInfo, bytes], bytes]]
|
A function that is passed the compiled code and returns the final bytes to use for the patch. This is useful for doing things like padding the code to a certain size. If not provided, the result of compilation will be used unchanged. |
None
|
Source code in pypatches/code/c_code.py
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 | |
Raw machine code Code subclass
RawCode
Bases: Code
Raw code bytes
Raw code is a container for raw bytes of machine code. It can be used to insert arbitrary machine code into a binary.
Source code in pypatches/code/raw.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
compile(label, info=None)
Return the raw bytes of the code
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label |
str
|
The label to use for the code |
required |
info |
Optional[TransformInfo]
|
Optional transform info to use for the compilation |
None
|
Returns:
| Type | Description |
|---|---|
bytes
|
The raw bytes of the code |
Source code in pypatches/code/raw.py
15 16 17 18 19 20 21 22 23 24 25 | |
Patch context info
Transform info
TransformInfo
dataclass
Info for transformations of Code objects
Attributes:
| Name | Type | Description |
|---|---|---|
lief_binary |
LIEFBinary
|
The lief binary for the binary |
angr_project |
Optional[Project]
|
The angr project for the binary |
code_base |
int
|
The base address of the code in the binary |
code_size |
int
|
The size of the code in the binary |
code_offsets |
Dict[str, int]
|
A dictionary mapping labels to offsets in the binary |
data_base |
int
|
The base address of the data in the binary |
data_size |
int
|
The size of the data in the binary |
data_offsets |
Dict[str, int]
|
A dictionary mapping labels to offsets in the binary |
current_offset |
int
|
The offset of the current operation, usually a write to the binay. For example, a replacecodepatch will have at least one write, so this value will be set to the offset of the write before passing the [TransformInfo][] object to the build transformer |
Source code in pypatches/transform/info.py
10 11 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 | |
Utilities
Errors specific to the patches package.
BinaryCreateError
Bases: Exception
An error that occurs when a binary code file cannot be produced
Source code in pypatches/error.py
20 21 | |
CodegenError
Bases: Exception
An error that occurs when code generation fails
Source code in pypatches/error.py
16 17 | |
CompilationError
Bases: Exception
An error that occurs when a snippet of C/C++ code fails to compile
Source code in pypatches/error.py
6 7 | |
NoSectionError
Bases: Exception
An error that occurs when a section is not found
Source code in pypatches/error.py
24 25 | |
TransformationError
Bases: Exception
An error that occurs when transformation of LLVM bitcode using the wrapper fails
Source code in pypatches/error.py
10 11 12 13 | |
Dataclass implementing comparable address ranges.
AddressRange
dataclass
An address range with a start and end. If start == end, this address range is just one address
Attributes:
| Name | Type | Description |
|---|---|---|
start |
int
|
The start address of the range |
end |
int
|
The end address of the range |
Source code in pypatches/address_range.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
address()
property
Returns whether this range is "an address"
Source code in pypatches/address_range.py
20 21 22 23 | |
Write operation to be performed on a binary.
WriteOperation
dataclass
A write operation to be performed on a section
Attributes:
| Name | Type | Description |
|---|---|---|
data |
Union[bytes, Callable[[Dict[str, int]], bytes], Code]
|
Either raw bytes or a function that takes the resolved labels for all patches and returns raw bytes |
vaddr |
Union[bytes, Callable[[Dict[str, int]], bytes], Code]
|
The virtual address or label location to write to |
where |
Union[str, Callable[[TransformInfo], int], int, List[int], Callable[[TransformInfo], List[int]]]
|
The address to replace the code at, optional. This "address" can either be a label, an actual address, a list of addresses, a function that takes a [TransformInfo][] and returns an address, or a function that takes a [TransformInfo][] and returns a list of addresses. |
Source code in pypatches/write_operation.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
Check if two capstone MemOp objects are equal, since they don't have a eq method that works.
cs_memop_eq(first, second)
Check equality of two memory operands
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
first |
Structure
|
The first memory operand |
required |
second |
Structure
|
The second memory operand |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the memory operands are equal, False otherwise |
Source code in pypatches/util/cs_memop_eq.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 | |
Accessors for various libraries to add to patches
get_lib(name)
Return the text of a library file with a given name
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name |
str
|
The name of the library file to get, either a relative path to this file to retrieve a built in library or a full path to a library file to retrieve a custom library |
required |
Source code in pypatches/util/libs/libs.py
9 10 11 12 13 14 15 16 17 18 19 20 | |