mem_rs/process/mod.rs
1// This file is part of the mem-rs distribution (https://github.com/FrankvdStam/mem-rs).
2// Copyright (c) 2022 Frank van der Stam.
3// https://github.com/FrankvdStam/mem-rs/blob/main/LICENSE
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, version 3.
8//
9// This program is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17use std::cell::RefCell;
18use std::rc::Rc;
19use windows::Win32::Foundation::HANDLE;
20use crate::memory::MemoryType;
21use crate::process_data::{ProcessData};
22use crate::process_module::ProcessModule;
23mod inject_dll;
24mod scanning;
25mod read_write;
26mod refresh;
27mod process_modules;
28mod process_name;
29
30const STILL_ACTIVE: u32 = 259;
31
32/// Wraps a native process and allows memory access/manipulation
33///
34/// # Examples
35///
36/// ```
37/// use mem_rs::prelude::*;
38///
39/// let mut process = Process::new("name_of_process.exe");
40/// if process.refresh().is_ok()
41/// {
42/// process.write_memory_abs(0x1234, &u32::to_ne_bytes(10));
43/// let result = process.read_u32_rel(Some(0x1234));
44/// println!("Result: {}", result);
45/// }
46/// ```
47pub struct Process
48{
49 main_module: Option<ProcessModule>, //cache for pattern scans
50 process_data: Rc<RefCell<ProcessData>>
51}
52
53impl Process
54{
55 /// Creates a new process based on the process name.
56 ///
57 /// # Examples
58 ///
59 /// ```
60 /// use mem_rs::prelude::*;
61 ///
62 /// let mut process = Process::new("name_of_process.exe");
63 /// ```
64 pub fn new(name: &str) -> Self
65 {
66 Process
67 {
68 main_module: None,
69 process_data: Rc::new(RefCell::new(ProcessData
70 {
71 name: String::from(name),
72 attached: false,
73 memory_type: MemoryType::Win32Api,
74 id: 0,
75 handle: HANDLE::default(),
76 is_64_bit: true,
77 filename: String::new(),
78 path: String::new(),
79 }))
80 }
81 }
82
83
84 /// Creates a new process based on the process name.
85 ///
86 /// # Examples
87 ///
88 /// ```
89 /// use mem_rs::prelude::*;
90 ///
91 /// let mut process = Process::new_with_memory_type("name_of_process.exe", MemoryType::Direct);
92 /// ```
93 pub fn new_with_memory_type(name: &str, memory_type: MemoryType) -> Self
94 {
95 Process
96 {
97 main_module: None,
98 process_data: Rc::new(RefCell::new(ProcessData
99 {
100 name: String::from(name),
101 attached: false,
102 memory_type,
103 id: 0,
104 handle: HANDLE::default(),
105 is_64_bit: true,
106 filename: String::new(),
107 path: String::new(),
108 }))
109 }
110 }
111
112 /// Returns if the process is "attached" and can be read/written from/to
113 ///
114 /// # Examples
115 ///
116 /// ```
117 /// use mem_rs::prelude::*;
118 ///
119 /// let mut process = Process::new("name_of_process.exe");
120 /// //returns false
121 /// let not_attached = process.is_attached();
122 ///
123 /// //refreshing the process will cause it to become attached
124 /// process.refresh().unwrap();
125 ///
126 /// //if name_of_process.exe is running, will return true
127 /// let attached = process.is_attached();
128 /// ```
129 pub fn is_attached(&self) -> bool {return self.process_data.borrow().attached;}
130
131 /// Returns file path of the processes' executable
132 ///
133 /// # Examples
134 ///
135 /// ```
136 /// use mem_rs::prelude::*;
137 ///
138 /// let mut process = Process::new("name_of_process.exe");
139 /// process.refresh().unwrap();
140 ///
141 /// println!("{}", process.get_path());
142 /// ```
143 pub fn get_path(&self) -> String {return self.process_data.borrow().path.clone();}
144
145 pub fn is_64_bit(&self) -> bool {return self.process_data.borrow().is_64_bit.clone(); }
146
147 /// Returns handle of a process
148 pub fn get_handle(&self) -> HANDLE {
149 return self.process_data.borrow().handle.clone();
150 }
151
152 /// Returns id of a process
153 pub fn get_id(&self) -> u32 {
154 return self.process_data.borrow().id.clone();
155 }
156
157 ///Returns a copy of the main module
158 pub fn get_main_module(&self) -> ProcessModule
159 {
160 return self.main_module.as_ref().unwrap().clone();
161 }
162
163 ///returns a copy of all modules
164 pub fn get_modules(&self) -> Vec<ProcessModule>
165 {
166 return Process::get_process_modules(self.process_data.borrow().handle.clone(), &self.process_data);
167 }
168 ///returns if the process is using win32 API's to read/write memory
169 pub fn get_memory_type(&self) -> MemoryType
170 {
171 return self.process_data.borrow().memory_type.clone();
172 }
173}