-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathextract_modules.rs
138 lines (111 loc) · 4 KB
/
extract_modules.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Extract Modules From Running Process.
By @5mukx
*/
macro_rules! log {
($level:expr, $msg:expr) => {{
let color = match $level {
"DEBUG" => "\x1b[94m",
"INFO" => "\x1b[32m",
"WARN" => "\x1b[33m",
"ERROR" => "\x1b[31m",
_ => "\x1b[0m",
};
match $level {
"DEBUG" => println!("{}[||] {}\x1b[0m", color, $msg),
"INFO" => println!("{}[+] {}\x1b[0m", color, $msg),
"WARN" => println!("{}[!] {}\x1b[0m", color, $msg),
"ERROR" => println!("{}[-] {}\x1b[0m", color, $msg),
_ => println!("{}{}\x1b[0m", color, $msg),
};
}};
}
use std::{ffi::CString, mem};
use winapi::um::{
errhandlingapi::GetLastError, handleapi::CloseHandle, processthreadsapi::OpenProcess, tlhelp32::{CreateToolhelp32Snapshot, Module32First, Module32Next, Process32First, Process32Next, MODULEENTRY32, PROCESSENTRY32, TH32CS_SNAPPROCESS
}};
fn get_pid(process_name: &str) -> u32{
unsafe{
let mut pe: PROCESSENTRY32 = std::mem::zeroed();
pe.dwSize = mem::size_of::<PROCESSENTRY32>() as u32;
let snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if snap.is_null(){
log!("ERROR", format!("Error while snapshoting processes : Error : {}",GetLastError()));
std::process::exit(0);
}
let mut pid = 0;
let mut result = Process32First(snap, &mut pe) != 0;
while result{
let exe_file = CString::from_vec_unchecked(pe.szExeFile
.iter()
.map(|&file| file as u8)
.take_while(|&c| c!=0)
.collect::<Vec<u8>>(),
);
if exe_file.to_str().unwrap() == process_name {
pid = pe.th32ProcessID;
break;
}
result = Process32Next(snap, &mut pe) !=0;
}
if pid == 0{
log!("ERROR", format!("Unable to get PID for {}: {}",process_name , "PROCESS DOESNT EXISTS"));
std::process::exit(0);
}
CloseHandle(snap);
pid
}
}
fn main(){
let args: Vec<String> = std::env::args().collect();
if args.len() != 2{
log!("ERROR", format!("Usage: ./importer.exe {}","<PROCESS NAME>"));
return;
}
let pid = get_pid(&args[1]);
unsafe{
let process_handle = OpenProcess(
0x040 | 0x0010,
0,
pid,
);
if process_handle.is_null(){
log!("ERROR", format!("Failed to open process: {}", GetLastError()));
return;
}
let mut me32: MODULEENTRY32 = std::mem::zeroed();
me32.dwSize = std::mem::size_of::<MODULEENTRY32>() as u32;
let snapshot = CreateToolhelp32Snapshot(
// TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32,
0x00000008 | 0x00000010,
pid
);
if snapshot.is_null(){
log!("ERROR", format!("Failed to create SnapShot: {}",GetLastError()));
CloseHandle(process_handle);
return;
}
let module_info = Module32First(snapshot, &mut me32);
if module_info != 0 {
println!("{:<34} | {:<20}", "Module Name", "Base Address");
println!("{:-<50}", "");
loop {
// printing module name along with its base address.
let module_name = std::ffi::CStr::from_ptr(me32.szModule.as_ptr());
log!(
"INFO",
format!("{:<30} | 0x{:016x}",
module_name.to_str().unwrap_or("Unknown Conversion"),
me32.modBaseAddr as usize
));
if Module32Next(snapshot, &mut me32) == 0 {
break;
}
}
} else {
eprintln!("Failed to get first module: {}", GetLastError());
}
CloseHandle(snapshot);
CloseHandle(process_handle);
}
}