MCPcopy Create free account
hub / github.com/Kudaes/Split / get_function_address

Function get_function_address

dinvoke/src/lib.rs:69–132  ·  view source on GitHub ↗

Retrieves the address of an exported function from the specified module. This functions is analogous to GetProcAddress from Win32. The exported function's address is obtained by walking and parsing the EAT of the specified module. In case that the function's address can't be retrieved, it will return 0. # Examples ``` let ntdll = dinvoke::get_module_base_address("ntdll.dll"); if ntdll != 0 {

(module_base_address: i64, function: &str)

Source from the content-addressed store, hash-verified

67/// }
68/// ```
69pub fn get_function_address(module_base_address: i64, function: &str) -> i64 {
70
71 unsafe
72 {
73
74 let mut function_ptr:*mut i32 = ptr::null_mut();
75 let pe_header = *((module_base_address + 0x3C) as *mut i32);
76 let opt_header: i64 = module_base_address + (pe_header as i64) + 0x18;
77 let magic = *(opt_header as *mut i16);
78 let p_export: i64;
79
80 if magic == 0x010b
81 {
82 p_export = opt_header + 0x60;
83 }
84 else
85 {
86 p_export = opt_header + 0x70;
87 }
88
89 let export_rva = *(p_export as *mut i32);
90 let ordinal_base = *((module_base_address + export_rva as i64 + 0x10) as *mut i32);
91 let number_of_names = *((module_base_address + export_rva as i64 + 0x18) as *mut i32);
92 let functions_rva = *((module_base_address + export_rva as i64 + 0x1C) as *mut i32);
93 let names_rva = *((module_base_address + export_rva as i64 + 0x20) as *mut i32);
94 let ordinals_rva = *((module_base_address + export_rva as i64 + 0x24) as *mut i32);
95
96 for x in 0..number_of_names
97 {
98
99 let address = *((module_base_address + names_rva as i64 + x as i64 * 4) as *mut i32);
100 let mut function_name_ptr = (module_base_address + address as i64) as *mut u8;
101 let mut function_name: String = "".to_string();
102
103 while *function_name_ptr as char != '\0' // null byte
104 {
105 function_name.push(*function_name_ptr as char);
106 function_name_ptr = function_name_ptr.add(1);
107 }
108
109 if function_name.to_lowercase() == function.to_lowercase()
110 {
111 let function_ordinal = *((module_base_address + ordinals_rva as i64 + x as i64 * 2) as *mut i16) as i32 + ordinal_base;
112 let function_rva = *(((module_base_address + functions_rva as i64 + (4 * (function_ordinal - ordinal_base)) as i64 )) as *mut i32);
113 function_ptr = (module_base_address + function_rva as i64) as *mut i32;
114
115 function_ptr = get_forward_address(function_ptr as *mut u8) as *mut i32;
116
117 break;
118 }
119
120 }
121
122 let mut ret: i64 = 0;
123
124 if function_ptr != ptr::null_mut()
125 {
126 ret = function_ptr as i64;

Callers 1

get_forward_addressFunction · 0.85

Calls 1

get_forward_addressFunction · 0.85

Tested by

no test coverage detected