From 8aa55bdb23f444523d0c89524d070a3d42f5e380 Mon Sep 17 00:00:00 2001 From: Garrett Dickinson Date: Mon, 10 Apr 2023 22:24:44 -0500 Subject: [PATCH] More progress towards patch injection framework --- notes/todo.md | 9 +++++++++ src/patcher.rs | 11 +++++------ src/util.rs | 7 ++++++- testing/hello_patched | Bin 15960 -> 15960 bytes testing/patches/hello.patch | 17 ++++++++++++++--- testing/src/functions.c | 12 ++++++++++++ 6 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 notes/todo.md create mode 100644 testing/src/functions.c diff --git a/notes/todo.md b/notes/todo.md new file mode 100644 index 0000000..013aa18 --- /dev/null +++ b/notes/todo.md @@ -0,0 +1,9 @@ +To Do + +Code injection + - Have a script with 3 functions, (main, funcA, funcB), and replace refs to funcA with funcB + - Trampoline definitions of a function to a new modified function that gets places in memory + - + +Injected code: 0x680 +Call: 0x1160 \ No newline at end of file diff --git a/src/patcher.rs b/src/patcher.rs index 2b9aa42..dd30245 100644 --- a/src/patcher.rs +++ b/src/patcher.rs @@ -37,8 +37,7 @@ pub fn patch_binary(binary_contents: Vec, binary_name: String, patch_file_pa file.write_all(&bytes) .expect("[Error] Could not write to patched binary file"); - - file.flush(); + } @@ -57,11 +56,11 @@ fn parse_patch_file(patch_path: &String) -> HashMap>{ for line in contents { let unwrapped = line.unwrap(); - if unwrapped.trim().starts_with("#") { - + if unwrapped.trim().starts_with("#") || unwrapped.is_empty() { + //Skip } else { let mut statement = unwrapped.split(":"); - let address: usize = statement.next().unwrap().trim().parse::().unwrap(); + let address: usize = util::hex_to_int(statement.next().unwrap().trim()).unwrap(); let data: &str = statement.next().unwrap().trim(); if !data.is_empty() { @@ -85,7 +84,7 @@ fn parse_patch_file(patch_path: &String) -> HashMap>{ } else { // Data is comma seperated list or a single value let byte_str: String = data.replace(",", ""); - let bytes: Vec = util::decode_hex(&byte_str).unwrap(); + let bytes: Vec = util::hex_to_buff(&byte_str).unwrap(); print!("{}: ", address); diff --git a/src/util.rs b/src/util.rs index 93fb05f..cf47695 100644 --- a/src/util.rs +++ b/src/util.rs @@ -348,9 +348,14 @@ pub fn read_lines(filename: String) -> io::Lines> { // Borrowed from the following Stack Overflow post // https://stackoverflow.com/questions/52987181/how-can-i-convert-a-hex-string-to-a-u8-slice -pub fn decode_hex(s: &str) -> Result, ParseIntError> { +pub fn hex_to_buff(s: &str) -> Result, ParseIntError> { (0..s.len()) .step_by(2) .map(|i| u8::from_str_radix(&s[i..i + 2], 16)) .collect() +} + + +pub fn hex_to_int(s: &str) -> Result { + return usize::from_str_radix(s, 16) } \ No newline at end of file diff --git a/testing/hello_patched b/testing/hello_patched index 7cf478a79d2899ea750f2d1198e600eb0c70e0cd..899b49d7cb1d3ad235d0ed823468cca1da7dfe9e 100755 GIT binary patch delta 78 zcmcanbE9U%1VLMm;|CZS7#KW`ABRxK4hVvHhj@X48QDPgVITtuc%