pub unsafe fn hook_function(&self, name: &str, newfunc: *const c_void) -> bool {
        let mut symindex: isize = -1;
        for (i, symbol) in self.elf.dynsyms.iter().enumerate() {
            if name == self.elf.dynstrtab.get(symbol.st_name).unwrap().unwrap() {
                symindex = i as isize;
                break;
            }
        }

        if symindex == -1 {
            println!("failed to find function {:?}", name);
            return false;
        }

        let mut offset: isize = -1;
        for reloc in self.elf.pltrelocs.iter() {
            if reloc.r_sym == symindex as usize {
                offset = reloc.r_offset as isize;
                break;
            }
        }
        if offset == -1 {
            println!("failed to find relocation for {:?}", name);
            return false;
        }

        let address = self.start + offset as usize;
        let value = std::ptr::read(address as *const *const c_void);
        println!(
            "found {:?} at address {:x}, with value {:x}, replacing...",
            name, address, value as usize
        );
        mprotect(
            ((address / 0x1000) * 0x1000) as *mut c_void,
            0x1000,
            ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
        )
        .expect("Failed to mprotect to read/write");
        std::ptr::replace(address as *mut *const c_void, newfunc);
        mprotect(
            ((address / 0x1000) * 0x1000) as *mut c_void,
            0x1000,
            ProtFlags::PROT_READ,
        )
        .expect("Failed to mprotect back to read-only");

        let value = std::ptr::read(address as *const *const c_void);
        println!(
            "verified value set to {:x}, expected {:x}",
            value as usize, newfunc as usize
        );

        value == newfunc
    }
/// Hook the function specified by name
///
/// #Safety:
/// This function is inherently unsafe as it modifies the got.plt of the target library,
/// dereferncing raw pointers as necessary.
Hook the function specified by name
Hook the function specified by name
pub unsafe fn hook_function(&self, name: &str, newfunc: *const c_void) -> bool {
        let mut symindex: isize = -1;
        for (i, symbol) in self.elf.dynsyms.iter().enumerate() {
            if name == self.elf.dynstrtab.get(symbol.st_name).unwrap().unwrap() {
                symindex = i as isize;
                break;
            }
        }
        if symindex == -1 {
            println!("failed to find function {:?}", name);
            return false;
        }
        let mut offset: isize = -1;
        for reloc in self.elf.pltrelocs.iter() {
            if reloc.r_sym == symindex as usize {
                offset = reloc.r_offset as isize;
                break;
            }
        }
        if offset == -1 {
            println!("failed to find relocation for {:?}", name);
            return false;
        }
        let address = self.start + offset as usize;
        let value = std::ptr::read(address as *const *const c_void);
        println!(
            "found {:?} at address {:x}, with value {:x}, replacing...",
            name, address, value as usize
        );
        mprotect(
            ((address / 0x1000) * 0x1000) as *mut c_void,
            0x1000,
            ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
        )
        .expect("Failed to mprotect to read/write");
        std::ptr::replace(address as *mut *const c_void, newfunc);
        mprotect(
            ((address / 0x1000) * 0x1000) as *mut c_void,
            0x1000,
            ProtFlags::PROT_READ,
        )
        .expect("Failed to mprotect back to read-only");
        let value = std::ptr::read(address as *const *const c_void);
        println!(
            "verified value set to {:x}, expected {:x}",
            value as usize, newfunc as usize
        );
        value == newfunc
    }
pub unsafe fn hook_function ( & self , name : & str , newfunc : * const c_void ) -> bool { let mut symindex : isize = - 1 ; for ( i , symbol ) in self . elf . dynsyms . iter ( ) . enumerate ( ) { if name == self . elf . dynstrtab . get ( symbol . st_name ) . unwrap ( ) . unwrap ( ) { symindex = i as isize ; break ; } } if symindex == - 1 { println ! ( " " name ) ; return false ; } let mut offset : isize = - 1 ; for reloc in self . elf . pltrelocs . iter ( ) { if reloc . r_sym == symindex as usize { offset = reloc . r_offset as isize ; break ; } } if offset == - 1 { println ! ( " " name ) ; return false ; } let address = self . start + offset as usize ; let value = std :: ptr :: read ( address as * const * const c_void ) ; println ! ( " " name address value as usize ) ; mprotect ( ( ( address / 0x1000 ) * 0x1000 ) as * mut c_void , 0x1000 , ProtFlags :: PROT_READ | ProtFlags :: PROT_WRITE , ) . expect ( " " ) ; std :: ptr :: replace ( address as * mut * const c_void , newfunc ) ; mprotect ( ( ( address / 0x1000 ) * 0x1000 ) as * mut c_void , 0x1000 , ProtFlags :: PROT_READ , ) . expect ( " " ) ; let value = std :: ptr :: read ( address as * const * const c_void ) ; println ! ( " " value as usize newfunc as usize ) ; value == newfunc }
Hook the function specified by name
Hook the function specified by name
&self
name
&str
newfunc
*const c_void
&self
name
&str
newfunc
*const c_void
safety:
This function is inherently unsafe as it modifies the got.plt of the target library,
dereferncing raw pointers as necessary.
This function is inherently unsafe as it modifies the got . plt of the target library dereferncing raw pointers as necessary .