feat: added decomp command

This commit is contained in:
əlemi 2023-04-06 05:27:46 +02:00
parent 2a10226a0e
commit 536612f356
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 49 additions and 0 deletions

View file

@ -23,3 +23,4 @@ nix = "0.26.2"
pretty-hex = "0.3"
signal-hook = "0.3.15"
procfs = "0.15.1"
iced-x86 = "1.18.0"

View file

@ -1,5 +1,6 @@
use std::{ffi::{c_void, c_int}, num::NonZeroUsize, sync::atomic::{AtomicBool, Ordering}};
use iced_x86::{Decoder, DecoderOptions, IntelFormatter, Instruction, Formatter};
use mlua::{Lua, Error, Variadic, Value, ToLua, Table};
use nix::sys::{mman::{mprotect, ProtFlags, mmap, MapFlags, munmap}, signal::{Signal::SIGSEGV, SigHandler}};
use procfs::{process::{Process, MemoryMaps, TasksIter, Status, Task, MemoryMap}, ProcError, ProcResult};
@ -39,6 +40,52 @@ pub fn lua_hexdump(lua: &Lua, (bytes, ret): (Vec<u8>, Option<bool>)) -> Result<V
Ok(Value::Nil)
}
fn padding(size: i32) -> String {
if size <= 0 {
"".into()
} else {
(0..size as usize).map(|_| " ").collect::<String>()
}
}
pub fn lua_decomp(lua: &Lua, (bytes, ret): (Vec<u8>, Option<bool>)) -> Result<Value, Error> {
let ret_value = ret.unwrap_or(false);
let bitness = 8 * std::mem::size_of::<usize>() as u32;
let mut decoder = Decoder::with_ip(bitness, bytes.as_slice(), 0, DecoderOptions::NONE);
let mut formatter = IntelFormatter::new();
let mut instr_buffer = String::new();
let mut raw_buffer = String::new();
let mut instruction = Instruction::default();
let mut output = String::new();
let mut retval = vec![];
let mut count = 0;
while decoder.can_decode() {
decoder.decode_out(&mut instruction);
instr_buffer.clear();
formatter.format(&instruction, &mut instr_buffer);
if ret_value {
retval.push(instr_buffer.clone());
continue;
}
raw_buffer.clear();
let start_index = instruction.ip() as usize;
let instrs_bytes = &bytes[start_index..start_index+instruction.len()];
for b in instrs_bytes {
raw_buffer.push_str(&format!("{:02x} ", b));
}
let padding = padding(30 - raw_buffer.len() as i32);
output.push_str(&format!("{:08X}: {}{}{}\n", instruction.ip(), raw_buffer, padding, instr_buffer));
count += 1;
}
if ret_value {
Ok(retval.to_lua(lua)?)
} else {
let console : Console = lua.globals().get(GLOBAL_CONSOLE)?;
console.send(output)?;
Ok(count.to_lua(lua)?)
}
}
pub fn lua_hex(l: &Lua, (value, prefix): (Value, Option<bool>)) -> Result<String, Error> {
let pre = if prefix.unwrap_or(true) { "0x" } else { "" };
match value {

View file

@ -26,6 +26,7 @@ pub fn register_builtin_fn(lua: &Lua, console: broadcast::Sender<String>) -> Res
lua.globals().set("log", lua.create_function(lua_log)?)?;
lua.globals().set("hexdump", lua.create_function(lua_hexdump)?)?;
lua.globals().set("decomp", lua.create_function(lua_decomp)?)?;
lua.globals().set("read", lua.create_function(lua_read)?)?;
lua.globals().set("write", lua.create_function(lua_write)?)?;
lua.globals().set("find", lua.create_function(lua_find)?)?;