-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
112 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Advanced Usage | ||
|
||
## Reuse Unreachable Code Locations | ||
Patcherex2 can be used to reuse unreachable code locations in the binary. | ||
Add the following code anywhere before `apply_patches` to reuse unreachable code. | ||
|
||
```python | ||
for func in p.binary_analyzer.get_unused_funcs(): | ||
p.allocation_manager.add_free_space(func["addr"], func["size"], "RX") | ||
``` | ||
|
||
## Pre- and Post- Function Hooks | ||
Patcherex2 allows you to add pre- and post- function hooks to the function call when using `InsertFunctionPatch` and first argument is a address. | ||
|
||
```python | ||
InsertFunctionPatch(0xdeadbeef, "int foo(int a) { return bar(); }", prefunc="mov rdi, 0x10", postfunc="mov rdi, rax") | ||
``` | ||
At the address `0xdeadbeef`, pre-function hook `mov rdi, 0x10` will be executed before the function `foo` is called and post-function hook `mov rdi, rax` will be executed after the function `foo` is called. This is useful when you want to pass arguments to the function or get the return value from the function. | ||
|
||
## Save Context and Restore Context when using `Insert*Patch` | ||
When using `InsertInstructionPatch` or `InsertFunctionPatch`, it is possible to save the context before the inserted content and restore the context after the inserted content. This is useful when the inserted content modifies the context. | ||
|
||
```python | ||
InsertInstructionPatch(0xdeadbeef, "push rbp", save_context=True) | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
:::patcherex2.patcherex |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
:::patcherex2.patches |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[data-md-color-scheme="pursec"] { | ||
--md-primary-fg-color: #4f6da9; | ||
--md-accent-fg-color: #bd9020; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# ModifyFunctionPatch | ||
|
||
Consider a simple C program: | ||
|
||
```c title="examples/modify_function_patch/add.c" | ||
--8<-- "examples/modify_function_patch/add.c" | ||
``` | ||
|
||
After compiling and executing this program, the output is: | ||
|
||
```bash | ||
$ gcc -o add add.c && ./add | ||
2 + 3 = 5 | ||
``` | ||
|
||
Now, we can use Patcherex2 to modify the `add` function to multiply the two arguments instead of adding them. | ||
|
||
```python title="examples/modify_function_patch/patch.py" | ||
--8<-- "examples/modify_function_patch/patch.py" | ||
``` | ||
|
||
Executing the patched program yields a different result: | ||
|
||
```bash | ||
$ ./add_patched | ||
2 + 3 = 6 | ||
``` | ||
|
||
💥 We've successfully modified the binary with Patcherex2! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
This site contains the documentation for the Patcherex2 project, a tool for patching binaries. | ||
--- | ||
title: "Home" | ||
--- | ||
|
||
Patcherex2 is a rewritten adaptation of the original Patcherex project, aimed at building upon its core ideas and extending its capabilities. It attempts to be a comprehensive and reliable replacement for other patching tools, allowing for patching at the byte, data, instruction, and function level, as well as allowing others to easily implement additional architectures. | ||
|
||
## Table Of Contents | ||
|
||
1. [Examples](examples.md) | ||
2. [Patch Types](patch_types.md) | ||
3. [API Reference](api_reference.md) | ||
---8<--- 'README.md' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include <stdio.h> | ||
|
||
int add(int a, int b) { | ||
return a + b; | ||
} | ||
|
||
int main() { | ||
printf("2 + 3 = %d\n", add(2, 3)); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from patcherex2 import * | ||
|
||
p = Patcherex("add") | ||
|
||
new_add_func = """ | ||
int add(int a, int b) { | ||
return a * b; | ||
} | ||
""" | ||
|
||
p.patches.append(ModifyFunctionPatch("add", new_add_func)) | ||
|
||
p.apply_patches() | ||
p.binfmt_tool.save_binary("add_patched") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,34 @@ | ||
site_name: Patcherex2 Docs | ||
site_name: Patcherex2 Documentation | ||
repo_url: https://github.com/purseclab/patcherex2 | ||
repo_name: purseclab/patcherex2 | ||
edit_uri: edit/main/docs/ | ||
|
||
theme: | ||
name: "material" | ||
features: | ||
- search.suggest | ||
- content.code.copy | ||
- toc.follow | ||
- toc.integrate | ||
- content.action.edit | ||
palette: | ||
- scheme: pursec | ||
|
||
plugins: | ||
- mkdocstrings: | ||
handlers: | ||
python: | ||
options: | ||
docstring_style: sphinx | ||
show_symbol_type_heading: true | ||
show_symbol_type_toc: true | ||
- search | ||
|
||
extra_css: | ||
- css/extra.css | ||
|
||
nav: | ||
- index.md | ||
- Examples: examples.md | ||
- Reference: reference.md | ||
markdown_extensions: | ||
- pymdownx.highlight | ||
- pymdownx.inlinehilite | ||
- pymdownx.snippets | ||
- pymdownx.superfences |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters