From da45c7f0251d40d9bd54175bb5ea93dcac496f30 Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 30 Mar 2023 06:12:44 +0200 Subject: [PATCH] fix: imports and deps --- src/needle/main.rs | 20 +++++++++++--------- src/rc/executors.rs | 3 ++- src/rc/explorers.rs | 4 +--- src/rc/mod.rs | 10 +++++----- src/rc/senders.rs | 2 +- src/rc/syscalls.rs | 2 +- 6 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/needle/main.rs b/src/needle/main.rs index 3f4fe0e..c8e49b2 100644 --- a/src/needle/main.rs +++ b/src/needle/main.rs @@ -2,14 +2,14 @@ use std::path::PathBuf; use tracing::{metadata::LevelFilter, info, error}; -use nix::{Result, {sys::{ptrace, wait::waitpid}, unistd::Pid}}; +use nix::{sys::{ptrace, wait::waitpid}, unistd::Pid}; use clap::Parser; -use rustyneedle::{ +use rustyneedle::{rc::{ injector::RemoteOperation, executors::RemoteShellcode, senders::RemoteString, syscalls::RemoteExit, explorers::step_to_syscall, -}; +}, locators::{procmaps::map_addr_path, exec::offset_in_elf}}; mod monitor; @@ -20,7 +20,7 @@ struct NeedleArgs { pid: i32, /// shared object to inject into target process - #[arg(short, long, default_value = "./target/debug/libtetanus.so")] + #[arg(short, long)] payload: String, /// exact address of dlopen function, calculated with `base + offset` if not given @@ -48,7 +48,7 @@ struct NeedleArgs { monitor: bool, } -fn nasty_stuff(args: NeedleArgs) -> Result<()> { +fn nasty_stuff(args: NeedleArgs) -> Result<(), Box> { let pid = Pid::from_raw(args.pid); ptrace::attach(pid)?; @@ -77,7 +77,9 @@ fn nasty_stuff(args: NeedleArgs) -> Result<()> { } else { let (mut calc_base, mut calc_fpath) = (0, "".into()); // rust complains about uninitialized... if args.path.is_none() || args.base.is_none() { // if user gives both no need to calculate it - (calc_base, calc_fpath) = find_libc(pid).expect("could not read proc maps of process"); + if let Some((b, p)) = map_addr_path(pid.as_raw(), "libc.so.6")? { + (calc_base, calc_fpath) = (b, p); + } } let base = match args.base { @@ -91,8 +93,8 @@ fn nasty_stuff(args: NeedleArgs) -> Result<()> { }; let offset = match args.offset { - Some(o) => o, - None => find_dlopen(&fpath).expect("could not read libc shared object") + Some(o) => o, // TODO catch error if dlopen is not in symbols + None => offset_in_elf(&fpath, "dlopen")?.expect("no dlopen symbol available"), }; dlopen_addr = base + offset; @@ -141,7 +143,7 @@ fn main() { let monitor = args.monitor; if let Err(e) = nasty_stuff(args) { - error!("error injecting shared object: {} ({})", e, e.desc()); + error!("error injecting shared object: {}", e); return; } diff --git a/src/rc/executors.rs b/src/rc/executors.rs index 8cbface..55bda7e 100644 --- a/src/rc/executors.rs +++ b/src/rc/executors.rs @@ -1,13 +1,14 @@ use nix::{unistd::Pid, Result, libc::{PROT_READ, MAP_PRIVATE, MAP_ANON, PROT_EXEC}, sys::{ptrace, wait::waitpid}}; use tracing::{debug, info}; -use crate::{syscalls::{RemoteMMap, RemoteMUnmap}, senders::write_buffer, injector::RemoteOperation}; +use crate::rc::{injector::RemoteOperation, syscalls::{RemoteMMap, RemoteMUnmap}, senders::write_buffer}; pub struct RemoteShellcode<'a> { code: &'a [u8], ptr: Option, } +#[allow(unused)] impl<'a> RemoteShellcode<'a> { pub fn new(code: &'a [u8]) -> Self { RemoteShellcode { code, ptr: None } diff --git a/src/rc/explorers.rs b/src/rc/explorers.rs index 084dba6..60b5033 100644 --- a/src/rc/explorers.rs +++ b/src/rc/explorers.rs @@ -1,8 +1,6 @@ -use std::{ffi::c_void, path::{Path, PathBuf}, io::{ErrorKind, Error}}; +use std::ffi::c_void; -use elf::{ElfBytes, endian::AnyEndian}; use nix::{unistd::Pid, sys::{ptrace, wait::waitpid}}; -use proc_maps::get_process_maps; pub fn step_to_syscall(pid: Pid) -> nix::Result { let mut registers; diff --git a/src/rc/mod.rs b/src/rc/mod.rs index 90b29b0..94f1313 100644 --- a/src/rc/mod.rs +++ b/src/rc/mod.rs @@ -1,7 +1,7 @@ -mod jnjector; +pub mod injector; -mod executors; -mod explores; -mod senders; +pub mod executors; +pub mod explorers; +pub mod senders; -mod syscalls; +pub mod syscalls; diff --git a/src/rc/senders.rs b/src/rc/senders.rs index 8cb1971..16342d5 100644 --- a/src/rc/senders.rs +++ b/src/rc/senders.rs @@ -3,7 +3,7 @@ use std::{ffi::c_void, mem::size_of}; use nix::{Result, unistd::Pid, sys::ptrace, libc::{PROT_READ, PROT_WRITE, MAP_PRIVATE, MAP_ANON}}; use tracing::{debug, info}; -use crate::{injector::RemoteOperation, syscalls::{RemoteMMap, RemoteMUnmap}}; +use crate::rc::{injector::RemoteOperation, syscalls::{RemoteMMap, RemoteMUnmap}}; const WORD_SIZE : usize = size_of::(); diff --git a/src/rc/syscalls.rs b/src/rc/syscalls.rs index a841362..c9b8fcd 100644 --- a/src/rc/syscalls.rs +++ b/src/rc/syscalls.rs @@ -1,7 +1,7 @@ use nix::{libc::user_regs_struct, Result, sys::{ptrace, wait::waitpid}, unistd::Pid}; use tracing::debug; -use crate::{injector::RemoteOperation, senders::RemoteString}; +use crate::rc::{injector::RemoteOperation, senders::RemoteString}; pub trait RemoteSyscall { fn registers(&self, regs: &mut user_regs_struct);