I'm using ld_preload on linux to hook the C's read()
function and I want to modify the data being sent through a socket. Consider the following code:
#[no_mangle] // or #[export_name = "read"]
unsafe fn read(d: c_int, buf: *mut c_void, count: size_t) -> ssize_t {
let ptr = libc::dlsym(libc::RTLD_NEXT, CString::new("read").unwrap().into_raw());
let c_read: fn(d: c_int, buf: *const c_void, count: size_t) -> ssize_t = std::mem::transmute(ptr);
let result = c_read(d, buf, count);
let cstr = CStr::from_ptr(buf as *const _).to_string_lossy().trim().to_string();
result
}
As you can see I can read the data and store it in cstr
, But how can I modify the data that user receives ?