feat: added find function, to locate byte pattern

This commit is contained in:
əlemi 2023-04-06 01:54:56 +02:00
parent b786c92277
commit 8717ded418
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 19 additions and 0 deletions

View file

@ -57,6 +57,24 @@ pub fn lua_write(_: &Lua, (addr, data): (usize, Vec<u8>)) -> Result<usize, Error
Ok(data.len())
}
pub fn lua_find(
_: &Lua, (start, size, pattern, first): (usize, usize, Vec<u8>, Option<bool>)
) -> Result<Vec<usize>, Error> {
let window = pattern.len();
let first_only = first.unwrap_or(false);
let mut matches = vec![];
for i in 0..(size-window) {
let slice = unsafe { std::slice::from_raw_parts((start + i) as *const u8, window) };
if slice == pattern {
matches.push(start + i);
if first_only { break; }
}
}
Ok(matches)
}
pub fn lua_procmaps(lua: &Lua, ret: Option<bool>) -> Result<Value, Error> {
let mut out = String::new();
let maps = get_process_maps(std::process::id() as i32)

View file

@ -26,6 +26,7 @@ pub fn register_builtin_fn(lua: &Lua, console: broadcast::Sender<String>) -> Res
lua.globals().set("hexdump", lua.create_function(lua_hexdump)?)?;
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)?)?;
lua.globals().set("procmaps", lua.create_function(lua_procmaps)?)?;
lua.globals().set("exit", lua.create_function(lua_exit)?)?;
lua.globals().set("mmap", lua.create_function(lua_mmap)?)?;