verity_memory/ops/
write.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use winapi::um::winnt::PAGE_EXECUTE_READWRITE;
use winapi::{shared::minwindef::LPVOID, um::memoryapi::VirtualProtect};

#[cfg(feature = "advanced-write")]
use crate::macros::match_number::{FloatType, IntegerType, IntegralType, NumberType};
#[cfg(feature = "advanced-write")]
use crate::types::Instruction;
use crate::{errors::WriteMemoryError, utils};
#[cfg(feature = "advanced-write")]
use crate::match_number;

#[cfg(feature = "advanced-write")]
use super::asm::{float_ret, get_instruction, integer_ret, integral_ret};

/// Writes a value of type `T` to the specified memory location.
///
/// # Safety
/// This function is unsafe because it directly manipulates raw pointers, which can cause undefined behavior
/// if the pointer is invalid or points to memory that is not writable.
///
/// # Parameters
/// - `dest_ptr`: A mutable pointer to the destination memory where the value will be written.
/// - `value`: The value to write at the destination memory.
///
/// # Returns
/// - `Ok(())` if the value was successfully written to memory.
/// - `Err(WriteMemoryError)` if an error occurred, such as a null pointer or invalid alignment.
///
/// # Errors
/// - `WriteMemoryError::NullPointer` if `dest_ptr` is null.
/// - `WriteMemoryError::InvalidAlignment` if `dest_ptr` is not correctly aligned.
/// - `WriteMemoryError::FailedToChangeProtection` if memory protection could not be modified.
/// - `WriteMemoryError::FailedToRestoreProtection` if memory protection could not be restored.
///
/// # Example
/// ```rust
/// use verity_memory::ops::write;
/// unsafe {
///     let mut value: i32 = 42;
///     let result = write::write_memory(&mut value as *mut i32, 100);
///     assert!(result.is_ok());
///     assert_eq!(value, 100);
/// }
/// ```
pub unsafe fn write_memory<T: Copy>(dest_ptr: *mut T, value: T) -> Result<(), WriteMemoryError> {
    if dest_ptr.is_null() {
        return Err(WriteMemoryError::NullPointer);
    }

    if !utils::check_alignment(dest_ptr) {
        return Err(WriteMemoryError::InvalidAlignment);
    }

    let mut old_protect = 0;
    let size = std::mem::size_of::<T>();

    let res = VirtualProtect(
        dest_ptr as LPVOID,
        size,
        PAGE_EXECUTE_READWRITE,
        &mut old_protect,
    );
    if res == 0 {
        return Err(WriteMemoryError::FailedToChangeProtection);
    }

    *dest_ptr = value;

    let res_restore = VirtualProtect(dest_ptr as LPVOID, size, old_protect, &mut old_protect);
    if res_restore == 0 {
        return Err(WriteMemoryError::FailedToRestoreProtection);
    }

    Ok(())
}

/// Replaces a specified number of instructions at a memory location with NOPs (0x90).
///
/// # Safety
/// This function is unsafe because it directly modifies memory, which can corrupt the process
/// if the memory is not writable or if the replaced instructions are critical.
///
/// # Parameters
/// - `dest_ptr`: A mutable pointer to the memory location where instructions will be replaced.
/// - `num_instructions`: The number of instructions to replace with NOPs.
///
/// # Returns
/// - `Some(Vec<Instruction>)` containing the original instructions if successful.
/// - `None` if it failed to read instructions or write memory.
///
/// # Example
/// ```rust
/// use verity_memory::ops::write;
/// unsafe {
///     let buffer = vec![0x55, 0x48, 0x89, 0xE5]; // Some sample machine code (push rbp; mov rbp, rsp)
///     let original_instructions = write::nop_instructions(buffer.as_ptr() as *mut u8, 2);
///     assert!(original_instructions.is_some());
/// }
/// ```
#[cfg(feature = "advanced-write")]
pub unsafe fn nop_instructions(dest_ptr: *mut u8, num_instructions: usize) -> Option<Vec<Instruction>> {
    let mut instructions = Vec::new();
    let mut current_ptr = dest_ptr;

    if num_instructions <= 0 {
        panic!("You must nop at least one instruction...");
    }

    for _ in 0..num_instructions {
        if let Some(instr) = get_instruction(current_ptr, 16) {
            instructions.push(instr.clone());
            current_ptr = current_ptr.add(instr.size);
        } else {
            eprintln!("Failed to get instruction at memory address: {:?}", current_ptr);
            return None;
        }
    }

    let total_size: usize = instructions.iter().map(|instr| instr.size).sum();

    let nops = vec![0x90; total_size];
    let mut written_size = 0;
    for i in 0..num_instructions {
        let instruction = &instructions[i];
        for j in 0..instruction.size {
            let res = write_memory(dest_ptr.add(written_size + j), nops[written_size + j]);
            if let Err(e) = res {
                eprintln!("Failed to write memory at offset {}: {:?}", written_size + j, e);
                return None;
            }
        }
        written_size += instruction.size;
    }

    Some(instructions)
}

/// Replaces the return value of a function with a specified value or inserts a `RET` instruction.
///
/// # Safety
/// This function is unsafe because it directly modifies memory, which can cause undefined behavior
/// if the memory is not writable or if the return value type is not correctly handled.
///
/// # Parameters
/// - `dest_ptr`: A mutable pointer to the function's first instruction.
/// - `return_value`: An optional value to return. If `None`, a `RET` instruction is written instead.
///
/// # Returns
/// - `Some(Instruction)` containing the original instruction if successful.
/// - `None` if an error occurred during instruction writing.
///
/// # Example
/// ```rust
/// use verity_memory::ops::write;
/// unsafe {
///     let buffer = vec![0x55, 0x48, 0x89, 0xE5]; // Example machine code
///     let result = write::replace_return_value::<i32>(buffer.as_ptr() as *mut u8, Some(123));
///     assert!(result.is_some());
/// }
/// ```
#[cfg(feature = "advanced-write")]
pub unsafe fn replace_return_value<T: Copy + 'static>(
    dest_ptr: *mut u8,
    return_value: Option<T>,
) -> Option<Instruction> {
    let original_instruction = get_instruction(dest_ptr, 16)?;

    let value = match return_value {
        Some(val) => val,
        None => {
            if let Err(e) = write_memory(dest_ptr, 0xC3) {
                eprintln!("Failed to write RET instruction: {:?}", e);
                return None;
            }
            return Some(original_instruction);
        }
    };

    let result = match_number!(value);
    if result.is_none() {
        return None;
    }

    let number_type = result.unwrap();
    let instruction_bytes = match number_type {
        NumberType::Float(float_type) => float_ret(float_type),
        NumberType::Integer(integer_type) => integer_ret(integer_type),
        NumberType::Integral(integral_type) => integral_ret(integral_type),
        NumberType::Unknown => {
            return None;
        }
    };

    let mut current_ptr = dest_ptr;
    for instruction_byte in instruction_bytes {
        if write_memory(current_ptr, instruction_byte).is_err() {
            return None;
        }
        current_ptr = current_ptr.add(1);
    }

    Some(original_instruction)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::ptr;

    fn mock_dest_ptr<T: Copy>(value: T) -> *mut T {
        let mut boxed_value = Box::new(value);
        let ptr = &mut *boxed_value as *mut T;
        std::mem::forget(boxed_value);
        ptr
    }

    #[test]
    fn test_write_memory_success() {
        let value: u32 = 42;
        let dest_ptr = mock_dest_ptr(value);

        let result = unsafe { write_memory(dest_ptr, 100_u32) };
        assert!(result.is_ok());
        assert_eq!(unsafe { *dest_ptr }, 100_u32);
    }

    #[test]
    fn test_write_memory_null_pointer() {
        let dest_ptr: *mut u32 = ptr::null_mut();
        
        let result = unsafe { write_memory(dest_ptr, 100_u32) };
        assert!(matches!(result, Err(WriteMemoryError::NullPointer)));
    }
    
    #[test]
    #[cfg(feature = "advanced-write")]
    fn test_nop_instructions_success() {
        let data: Vec<u8> = vec![0x55, 0x48, 0x8B, 0xEC, 0x90];
        let dest_ptr = data.as_ptr() as *mut u8;

        unsafe {
            if let Some(instructions) = nop_instructions(dest_ptr, 2) {
                assert_eq!(instructions.len(), 2);
            } else {
                panic!("Failed to retrieve instructions");
            }
        }
    }

    #[test]
    #[cfg(feature = "advanced-write")]
    fn test_nop_instructions_failure() {
        let dest_ptr: *mut u8 = ptr::null_mut();

        unsafe {
            let instructions = nop_instructions(dest_ptr, 1);
            assert!(instructions.is_none());
        }
    }

    #[test]
    #[cfg(feature = "advanced-write")]
    fn test_replace_return_value_integer() {
        let data: Vec<u8> = vec![0x55, 0x48, 0x8B, 0xEC];
        let dest_ptr = data.as_ptr() as *mut u8;

        unsafe {
            let result = replace_return_value(dest_ptr, Some(123_u32));
            assert!(result.is_some());
        }
    }

    #[test]
    #[cfg(feature = "advanced-write")]
    fn test_replace_return_value_float() {
        let data: Vec<u8> = vec![0x55, 0x48, 0x8B, 0xEC];
        let dest_ptr = data.as_ptr() as *mut u8;

        unsafe {
            let result = replace_return_value(dest_ptr, Some(123.45_f32));
            assert!(result.is_some());
        }
    }

    #[test]
    #[cfg(feature = "advanced-write")]
    fn test_replace_return_value_none() {
        let data: Vec<u8> = vec![0x55, 0x48, 0x8B, 0xEC];
        let dest_ptr = data.as_ptr() as *mut u8;

        unsafe {
            let result = replace_return_value::<i32>(dest_ptr, None);
            assert!(result.is_some());
        }
    }
}