feat: add ExtractBuiltins and LoadNumbered scheme hooks
Browse filesComplete the scheme subsystem: all 10 functions now have Rust hooks.
- ExtractBuiltins (0x4D5720): full Rust replacement that extracts
built-in scheme .wsc files from PE resources to User\Schemes\.
Uses hardcoded scheme names (MFC string resources not accessible
via plain LoadStringA). Skips slot 13 feature check (always
available on Steam).
- LoadNumbered (0x4D4E00): logging sentinel — believed dead code
(zero xrefs), delegates to original with a warning log.
- Add openwa-lib::wa::resource module with Win32 PE resource helpers
(FindResourceA, LoadResource, SizeofResource, LockResource).
- Unify GetModuleHandleA signature across rebase.rs and resource.rs.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
crates/openwa-lib/src/rebase.rs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
//! ASLR rebasing — convert Ghidra VAs to runtime addresses.
|
| 2 |
|
| 3 |
-
use std::ffi::c_void;
|
| 4 |
use std::sync::atomic::{AtomicU32, Ordering};
|
| 5 |
|
| 6 |
use openwa_types::address::va;
|
|
@@ -15,11 +14,11 @@ pub fn rb(ghidra_addr: u32) -> u32 {
|
|
| 15 |
}
|
| 16 |
|
| 17 |
extern "system" {
|
| 18 |
-
fn GetModuleHandleA(lpModuleName: *const u8) ->
|
| 19 |
}
|
| 20 |
|
| 21 |
pub fn init() -> i32 {
|
| 22 |
-
let base = unsafe { GetModuleHandleA(std::ptr::null()) }
|
| 23 |
let delta = base.wrapping_sub(va::IMAGE_BASE);
|
| 24 |
REBASE_DELTA.store(delta, Ordering::Relaxed);
|
| 25 |
delta as i32
|
|
|
|
| 1 |
//! ASLR rebasing — convert Ghidra VAs to runtime addresses.
|
| 2 |
|
|
|
|
| 3 |
use std::sync::atomic::{AtomicU32, Ordering};
|
| 4 |
|
| 5 |
use openwa_types::address::va;
|
|
|
|
| 14 |
}
|
| 15 |
|
| 16 |
extern "system" {
|
| 17 |
+
fn GetModuleHandleA(lpModuleName: *const u8) -> u32;
|
| 18 |
}
|
| 19 |
|
| 20 |
pub fn init() -> i32 {
|
| 21 |
+
let base = unsafe { GetModuleHandleA(std::ptr::null()) };
|
| 22 |
let delta = base.wrapping_sub(va::IMAGE_BASE);
|
| 23 |
REBASE_DELTA.store(delta, Ordering::Relaxed);
|
| 24 |
delta as i32
|
crates/openwa-lib/src/wa/mod.rs
CHANGED
|
@@ -2,3 +2,4 @@
|
|
| 2 |
|
| 3 |
pub mod frontend;
|
| 4 |
pub mod mfc;
|
|
|
|
|
|
| 2 |
|
| 3 |
pub mod frontend;
|
| 4 |
pub mod mfc;
|
| 5 |
+
pub mod resource;
|
crates/openwa-lib/src/wa/resource.rs
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//! Win32 PE resource extraction helpers.
|
| 2 |
+
|
| 3 |
+
type LPCSTR = *const u8;
|
| 4 |
+
|
| 5 |
+
extern "system" {
|
| 6 |
+
fn GetModuleHandleA(lpModuleName: *const u8) -> u32;
|
| 7 |
+
fn FindResourceA(hModule: u32, lpName: LPCSTR, lpType: LPCSTR) -> u32;
|
| 8 |
+
fn LoadResource(hModule: u32, hResInfo: u32) -> u32;
|
| 9 |
+
fn SizeofResource(hModule: u32, hResInfo: u32) -> u32;
|
| 10 |
+
fn LockResource(hResData: u32) -> *const u8;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
#[link(name = "user32")]
|
| 14 |
+
extern "system" {
|
| 15 |
+
fn LoadStringA(hInstance: u32, uID: u32, lpBuffer: *mut u8, cchBufferMax: i32) -> i32;
|
| 16 |
+
fn CharLowerBuffA(lpsz: *mut u8, cchLength: u32) -> u32;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
/// Load a string resource from WA.exe by ID.
|
| 20 |
+
/// Returns the string content, or an empty string on failure.
|
| 21 |
+
pub unsafe fn load_string_resource(id: u32) -> String {
|
| 22 |
+
let hmodule = GetModuleHandleA(core::ptr::null());
|
| 23 |
+
let mut buf = [0u8; 256];
|
| 24 |
+
let len = LoadStringA(hmodule, id, buf.as_mut_ptr(), buf.len() as i32);
|
| 25 |
+
if len <= 0 {
|
| 26 |
+
return String::new();
|
| 27 |
+
}
|
| 28 |
+
String::from_utf8_lossy(&buf[..len as usize]).into_owned()
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
/// Lowercase an ANSI string using the system's Active Code Page.
|
| 32 |
+
pub fn lowercase_ansi(s: &str) -> String {
|
| 33 |
+
let mut buf: Vec<u8> = s.bytes().collect();
|
| 34 |
+
if !buf.is_empty() {
|
| 35 |
+
unsafe {
|
| 36 |
+
CharLowerBuffA(buf.as_mut_ptr(), buf.len() as u32);
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
String::from_utf8_lossy(&buf).into_owned()
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
/// Load a PE resource from WA.exe by type name and integer ID.
|
| 43 |
+
/// Returns a slice of the resource data, or None on failure.
|
| 44 |
+
/// The returned slice is valid for the lifetime of the module (i.e., forever).
|
| 45 |
+
pub unsafe fn load_pe_resource(type_name: &str, id: u32) -> Option<&'static [u8]> {
|
| 46 |
+
let hmodule = GetModuleHandleA(core::ptr::null());
|
| 47 |
+
|
| 48 |
+
// MAKEINTRESOURCE(id)
|
| 49 |
+
let lp_name = id as LPCSTR;
|
| 50 |
+
|
| 51 |
+
// Type name must be null-terminated
|
| 52 |
+
let mut type_buf = [0u8; 32];
|
| 53 |
+
let type_bytes = type_name.as_bytes();
|
| 54 |
+
if type_bytes.len() >= type_buf.len() {
|
| 55 |
+
return None;
|
| 56 |
+
}
|
| 57 |
+
type_buf[..type_bytes.len()].copy_from_slice(type_bytes);
|
| 58 |
+
// type_buf is already zero-initialized, so null terminator is in place
|
| 59 |
+
|
| 60 |
+
let h_res = FindResourceA(hmodule, lp_name, type_buf.as_ptr());
|
| 61 |
+
if h_res == 0 {
|
| 62 |
+
return None;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
let size = SizeofResource(hmodule, h_res) as usize;
|
| 66 |
+
if size == 0 {
|
| 67 |
+
return None;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
let h_data = LoadResource(hmodule, h_res);
|
| 71 |
+
if h_data == 0 {
|
| 72 |
+
return None;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
let ptr = LockResource(h_data);
|
| 76 |
+
if ptr.is_null() {
|
| 77 |
+
return None;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
Some(core::slice::from_raw_parts(ptr, size))
|
| 81 |
+
}
|
crates/openwa-types/src/address.rs
CHANGED
|
@@ -241,6 +241,8 @@ pub mod va {
|
|
| 241 |
pub const SCHEME_VALIDATE_EXTENDED_OPTIONS: u32 = 0x004D_5110;
|
| 242 |
/// Scans User\Schemes\ for {NN} name.wsc files, marks found indices in global array
|
| 243 |
pub const SCHEME_SCAN_DIRECTORY: u32 = 0x004D_54E0;
|
|
|
|
|
|
|
| 244 |
|
| 245 |
// === Scheme data (in .rdata/.data) ===
|
| 246 |
|
|
|
|
| 241 |
pub const SCHEME_VALIDATE_EXTENDED_OPTIONS: u32 = 0x004D_5110;
|
| 242 |
/// Scans User\Schemes\ for {NN} name.wsc files, marks found indices in global array
|
| 243 |
pub const SCHEME_SCAN_DIRECTORY: u32 = 0x004D_54E0;
|
| 244 |
+
/// Slot 13 feature/availability check — returns bool (obfuscated hash check)
|
| 245 |
+
pub const SCHEME_SLOT13_CHECK: u32 = 0x004D_A4C0;
|
| 246 |
|
| 247 |
// === Scheme data (in .rdata/.data) ===
|
| 248 |
|
crates/openwa-wormkit/src/replacements/scheme.rs
CHANGED
|
@@ -489,6 +489,107 @@ unsafe fn scan_directory_recursive(dir: &str) {
|
|
| 489 |
}
|
| 490 |
}
|
| 491 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 492 |
// ============================================================
|
| 493 |
// Hook installation
|
| 494 |
// ============================================================
|
|
@@ -544,6 +645,19 @@ pub fn install() -> Result<(), String> {
|
|
| 544 |
va::SCHEME_SCAN_DIRECTORY,
|
| 545 |
hook_scan_directory as *const (),
|
| 546 |
)?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 547 |
}
|
| 548 |
|
| 549 |
Ok(())
|
|
|
|
| 489 |
}
|
| 490 |
}
|
| 491 |
|
| 492 |
+
// ============================================================
|
| 493 |
+
// Scheme__ExtractBuiltins replacement (0x4D5720)
|
| 494 |
+
// ============================================================
|
| 495 |
+
|
| 496 |
+
/// Built-in scheme names, indexed by slot (1-13).
|
| 497 |
+
/// Original loads these from MFC string resources (IDs 0x3CA-0x3D6) via
|
| 498 |
+
/// AfxFindStringResourceHandle, which is not accessible via plain LoadStringA.
|
| 499 |
+
/// The names are fixed across all WA 3.8.1 installations.
|
| 500 |
+
const BUILTIN_SCHEME_NAMES: [&str; 14] = [
|
| 501 |
+
"", // slot 0 unused
|
| 502 |
+
"Beginner", // slot 1, resource 0x3CA
|
| 503 |
+
"Intermediate", // slot 2, resource 0x3CB
|
| 504 |
+
"Pro", // slot 3, resource 0x3CC
|
| 505 |
+
"Tournament", // slot 4, resource 0x3D1
|
| 506 |
+
"Classic", // slot 5, resource 0x3CD
|
| 507 |
+
"Retro", // slot 6, resource 0x3D2
|
| 508 |
+
"Artillery", // slot 7, resource 0x3D5
|
| 509 |
+
"Sudden Sinking", // slot 8, resource 0x3D0
|
| 510 |
+
"Strategic", // slot 9, resource 0x3D3
|
| 511 |
+
"The Darkside", // slot 10, resource 0x3D4
|
| 512 |
+
"Armageddon", // slot 11, resource 0x3CE
|
| 513 |
+
"Blast Zone", // slot 12, resource 0x3CF
|
| 514 |
+
"Full Wormage", // slot 13, resource 0x3D6
|
| 515 |
+
];
|
| 516 |
+
|
| 517 |
+
/// Rust replacement for Scheme__ExtractBuiltins (0x4D5720).
|
| 518 |
+
/// No params, no return value. Called from Frontend__MainNavigationLoop at startup.
|
| 519 |
+
///
|
| 520 |
+
/// Zeros slot flags, scans for existing scheme files, then extracts missing
|
| 521 |
+
/// built-in schemes from PE resources (type "SCHEMES") to User\Schemes\.
|
| 522 |
+
unsafe extern "stdcall" fn hook_extract_builtins() {
|
| 523 |
+
use openwa_lib::wa::resource;
|
| 524 |
+
|
| 525 |
+
// Step 1: Zero 16 bytes of slot flags (4 DWORDs covering slots 0-15)
|
| 526 |
+
let slot_flags = rb(va::SCHEME_SLOT_FLAGS) as *mut u8;
|
| 527 |
+
core::ptr::write_bytes(slot_flags, 0, 16);
|
| 528 |
+
|
| 529 |
+
// Step 2: Ensure directory exists
|
| 530 |
+
let _ = std::fs::create_dir_all("User\\Schemes");
|
| 531 |
+
|
| 532 |
+
// Step 3: Scan for existing scheme files (reuse our Rust implementation)
|
| 533 |
+
scan_directory_recursive("User\\Schemes");
|
| 534 |
+
|
| 535 |
+
// Step 4: Extract missing built-in schemes
|
| 536 |
+
for slot in 1u32..=13 {
|
| 537 |
+
if *slot_flags.add(slot as usize) != 0 {
|
| 538 |
+
continue; // file already exists
|
| 539 |
+
}
|
| 540 |
+
|
| 541 |
+
// Slot 13: original has an obfuscated feature check (FUN_004DA4C0) that
|
| 542 |
+
// uses __usercall (implicit EAX/ECX). On Steam copies all slots are available,
|
| 543 |
+
// so we skip the check. If needed, this could be replicated later.
|
| 544 |
+
|
| 545 |
+
let name = BUILTIN_SCHEME_NAMES[slot as usize];
|
| 546 |
+
|
| 547 |
+
// Format output path: User\Schemes\{{DD}} name.wsc
|
| 548 |
+
let path = format!("User\\Schemes\\{{{{{slot:02}}}}} {name}.wsc");
|
| 549 |
+
|
| 550 |
+
// Load PE resource: type "SCHEMES", ID = slot + 0x2742
|
| 551 |
+
let resource_id = slot + 0x2742;
|
| 552 |
+
match resource::load_pe_resource("SCHEMES", resource_id) {
|
| 553 |
+
Some(data) => {
|
| 554 |
+
if let Err(e) = std::fs::write(&path, data) {
|
| 555 |
+
let _ = log_line(&format!(
|
| 556 |
+
"[Scheme] ExtractBuiltins: failed to write {path}: {e}"
|
| 557 |
+
));
|
| 558 |
+
}
|
| 559 |
+
}
|
| 560 |
+
None => {
|
| 561 |
+
let _ = log_line(&format!(
|
| 562 |
+
"[Scheme] ExtractBuiltins: PE resource 0x{resource_id:X} not found for slot {slot}"
|
| 563 |
+
));
|
| 564 |
+
}
|
| 565 |
+
}
|
| 566 |
+
}
|
| 567 |
+
|
| 568 |
+
let _ = log_line("[Scheme] ExtractBuiltins completed (Rust)");
|
| 569 |
+
}
|
| 570 |
+
|
| 571 |
+
// ============================================================
|
| 572 |
+
// Scheme__LoadNumbered logging sentinel (0x4D4E00)
|
| 573 |
+
// ============================================================
|
| 574 |
+
|
| 575 |
+
/// Trampoline to original Scheme__LoadNumbered.
|
| 576 |
+
static ORIG_LOAD_NUMBERED: AtomicU32 = AtomicU32::new(0);
|
| 577 |
+
|
| 578 |
+
/// Logging sentinel for Scheme__LoadNumbered (0x4D4E00).
|
| 579 |
+
/// This function has zero xrefs in WA.exe (believed dead code).
|
| 580 |
+
/// We hook it to detect if it's actually called at runtime.
|
| 581 |
+
unsafe extern "stdcall" fn hook_load_numbered(name: u32) -> u32 {
|
| 582 |
+
let c_name = CStr::from_ptr(name as *const i8)
|
| 583 |
+
.to_str()
|
| 584 |
+
.unwrap_or("???");
|
| 585 |
+
let _ = log_line(&format!(
|
| 586 |
+
"[Scheme] WARNING: LoadNumbered called (believed dead code): {c_name}"
|
| 587 |
+
));
|
| 588 |
+
let orig: unsafe extern "stdcall" fn(u32) -> u32 =
|
| 589 |
+
core::mem::transmute(ORIG_LOAD_NUMBERED.load(Ordering::Relaxed));
|
| 590 |
+
orig(name)
|
| 591 |
+
}
|
| 592 |
+
|
| 593 |
// ============================================================
|
| 594 |
// Hook installation
|
| 595 |
// ============================================================
|
|
|
|
| 645 |
va::SCHEME_SCAN_DIRECTORY,
|
| 646 |
hook_scan_directory as *const (),
|
| 647 |
)?;
|
| 648 |
+
|
| 649 |
+
let _ = crate::hook::install(
|
| 650 |
+
"Scheme__ExtractBuiltins",
|
| 651 |
+
va::SCHEME_EXTRACT_BUILTINS,
|
| 652 |
+
hook_extract_builtins as *const (),
|
| 653 |
+
)?;
|
| 654 |
+
|
| 655 |
+
let trampoline_load = crate::hook::install(
|
| 656 |
+
"Scheme__LoadNumbered",
|
| 657 |
+
va::SCHEME_FILE_EXISTS_NUMBERED,
|
| 658 |
+
hook_load_numbered as *const (),
|
| 659 |
+
)?;
|
| 660 |
+
ORIG_LOAD_NUMBERED.store(trampoline_load as u32, Ordering::Relaxed);
|
| 661 |
}
|
| 662 |
|
| 663 |
Ok(())
|