Skip to content

Commit ddd15d8

Browse files
committed
feat: determine ProcessEvent function pointer at runtime with pattern matching
1 parent 9f30e3e commit ddd15d8

1 file changed

Lines changed: 72 additions & 6 deletions

File tree

src/hooks/process_event.rs

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use super::mem::use_memory;
2-
use crate::game::{
3-
core::{FString, UFunction, UObject, UObjectExt},
4-
sfxgame::{FSFXOnlineMOTDInfo, USFXOnlineComponentUI},
2+
use crate::{
3+
game::{
4+
core::{FString, UFunction, UObject, UObjectExt},
5+
sfxgame::{FSFXOnlineMOTDInfo, USFXOnlineComponentUI},
6+
},
7+
hooks::mem::find_pattern,
58
};
6-
use log::debug;
9+
use log::{debug, warn};
710
use serde::{Deserialize, Serialize};
811
use std::os::raw::c_void;
912
use windows_sys::Win32::System::Memory::{
@@ -17,7 +20,60 @@ type ProcessEvent =
1720
static mut PROCESS_EVENT_ORIGINAL: Option<ProcessEvent> = None;
1821

1922
/// Memory address the process event function is stored at
20-
const PROCESS_EVENT_OFFSET: usize = 0x00453120;
23+
// const PROCESS_EVENT_OFFSET: usize = 0x00453120;
24+
25+
/// Address to start matching from
26+
const PROCESS_EVENT_START_OFFSET: usize = 0x401000;
27+
/// Address to end matching at
28+
const PROCESS_EVENT_END_OFFSET: usize = 0xFFFFFF;
29+
/// Mask to use while matching the opcodes below
30+
const PROCESS_EVENT_MASK: &str = "xxxxxxxxxxxx?xxxxxxxxxxxx?xxxxxxxxxxxxx?xxxxxx?x????????x?xx?x?x?x?xx?xx?xxxxxxxxxxx?xx?x?x?x?xxxxxxxx?xxxx?x?x?xx?x?x?x?xxxxxxxx?xxx?xx?xx?x?x?x?xx?xx?x?xx?x?xxxx?xxxxxxxxx?x?x";
31+
/// Op codes to match against
32+
const PROCESS_EVENT_OP_CODES: &[u8] = &[
33+
0x55, // push ebp
34+
0x8B, 0xEC, // mov ebp, esp
35+
0x6A, 0xFF, // push 0xFF
36+
0x68, 0xC8, 0x43, 0x1A, 0x01, // push 0x1A43C8
37+
0x64, 0xA1, 0x00, 0x00, 0x00, 0x00, // mov eax, [fs:0x0]
38+
0x50, // push eax
39+
0x83, 0xEC, 0x48, // sub esp, 0x48
40+
0xA1, 0x80, 0x5B, 0x90, 0x01, // mov eax, [0x1905B80]
41+
0x33, 0xC5, // xor eax, ebp
42+
0x89, 0x45, 0xEC, // mov [ebp-0x14], eax
43+
0x53, // push ebx
44+
0x56, // push esi
45+
0x57, // push edi
46+
0x50, // push eax
47+
0x8D, 0x45, 0xF4, // lea eax, [ebp-0xC]
48+
0x64, 0xA3, 0x00, 0x00, 0x00, 0x00, // mov [fs:0x0], eax
49+
0x8B, 0xF1, // mov esi, ecx
50+
0x89, 0x75, 0xE8, // mov [ebp-0x18], esi
51+
0x8B, 0x5D, 0x08, // mov ebx, [ebp+0x8]
52+
0xF7, 0x83, 0x88, 0x00, 0x00, 0x00, // test dword ptr [ebx+0x88], 0
53+
0x02, 0x04, 0x00, 0x00, // add [ebx+0x4], al
54+
0x0F, 0x84, 0x21, 0x02, 0x00, 0x00, // je 0x222
55+
0x83, 0x7B, 0x04, 0xFF, // cmp dword ptr [ebx+0x4], 0xFF
56+
0x75, 0x13, // jnz 0x13
57+
0x6A, 0x01, // push 0x1
58+
0x6A, 0x01, // push 0x1
59+
0x68, 0x30, 0x71, 0x6A, 0x01, // push 0x1A6730
60+
0x33, 0xC9, // xor ecx, ecx
61+
0x8D, 0x55, 0xE0, // lea edx, [ebp-0x20]
62+
0xE8, 0xC4, 0x79, 0x05, 0x00, // call 0x5A79C4
63+
0x8B, 0x06, // mov eax, [esi]
64+
0x8B, 0x50, 0x44, // mov edx, [eax+0x44]
65+
0x8B, 0xCE, // mov ecx, esi
66+
0xFF, 0xD2, // call edx
67+
0x85, 0xC0, // test eax, eax
68+
0x0F, 0x85, 0xF7, 0x01, 0x00, 0x00, // jne 0x1F7
69+
0x66, 0x39, 0x83, // cmp word ptr [ebx+0x83], ax
70+
0x8C, 0x00, 0x00, 0x00, // cmp word ptr [ebx], 0
71+
0x0F, 0x85, 0xEA, 0x01, 0x00, 0x00, // jne 0x1EAC
72+
0xF7, 0x83, 0x88, 0x00, 0x00, 0x00, // test dword ptr [ebx+0x88], 0
73+
0x00, 0x04, 0x00, 0x00, // add [ebx+0x4], al
74+
0x8B, 0x7D, 0x0C, // mov edi, [ebp+0xC]
75+
0x74, 0x18, // je 0x18
76+
];
2177

2278
/// Hooks the game [ProcessEvent] function to use [fake_process_event] instead
2379
/// to allow processing events that occur in the game
@@ -26,7 +82,17 @@ pub unsafe fn hook_process_event() {
2682
const JMP: u8 = 0xE9 /* jmp */;
2783
const JMP_SIZE: usize = 5; // Size of a near jump instruction in x86
2884

29-
let target = PROCESS_EVENT_OFFSET as *const u8 as *mut u8;
85+
let Some(target) = find_pattern(
86+
PROCESS_EVENT_START_OFFSET,
87+
PROCESS_EVENT_END_OFFSET,
88+
PROCESS_EVENT_MASK,
89+
PROCESS_EVENT_OP_CODES,
90+
) else {
91+
warn!("Failed to find process_event hook position");
92+
return;
93+
};
94+
95+
// let target = PROCESS_EVENT_OFFSET as *const u8 as *mut u8;
3096
let hook = fake_process_event as *const u8;
3197

3298
let mut original_bytes: [u8; JMP_SIZE] = [0; JMP_SIZE];

0 commit comments

Comments
 (0)