Skip to content

Commit

Permalink
Merge pull request #37 from willox/ckey_override
Browse files Browse the repository at this point in the history
add guest_override command
  • Loading branch information
willox authored Apr 4, 2021
2 parents a30912e + 4881867 commit 905ffc9
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions debug_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ serde = { version = "1.0.117", features = ["derive"] }
bincode = "1.3.1"
clap = "2.33.3"
dmasm = { git = "https://github.com/willox/dmasm" }
region = "2.2.0"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.9", features = ["winuser", "libloaderapi", "errhandlingapi"] }
Expand Down
52 changes: 52 additions & 0 deletions debug_server/src/ckey_override.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use region::Protection;
use std::{ffi::CString, os::raw::c_char};

use auxtools::*;

static mut STRING_PTR: *mut *const c_char = std::ptr::null_mut();

#[init(full)]
fn ckey_override_init() -> Result<(), String> {
let byondcore = sigscan::Scanner::for_module(BYONDCORE).unwrap();

// This feature soft-fails
#[cfg(windows)]
if let Some(ptr) = byondcore.find(signature!(
"68 ?? ?? ?? ?? 50 E8 ?? ?? ?? ?? 83 C4 0C 8D 8D ?? ?? ?? ?? E8 ?? ?? ?? ?? 8B 85 ?? ?? ?? ??"
)) {
unsafe {
STRING_PTR = ptr.add(1) as *mut *const c_char;
}
}

Ok(())
}

#[derive(Debug)]
pub enum Error {
UnsupportedByondVersion,
InvalidString,
}

pub fn override_guest_ckey(name: &str) -> Result<(), Error> {
unsafe {
if STRING_PTR.is_null() {
return Err(Error::UnsupportedByondVersion);
}
}

let name = name.replace('%', "%%");

let new_ptr = CString::new(name)
.map_err(|_| Error::InvalidString)?
.into_raw();

unsafe {
region::protect(STRING_PTR as *const u8, 4, Protection::READ_WRITE_EXECUTE).unwrap();

// Leak is fine
*STRING_PTR = new_ptr;
}

Ok(())
}
1 change: 1 addition & 0 deletions debug_server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod assemble_env;
mod ckey_override;
mod disassemble_env;
mod instruction_hooking;
mod server;
Expand Down
22 changes: 21 additions & 1 deletion debug_server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,15 @@ impl Server {
.help("Id of the proc to disassemble (for when multiple procs are defined with the same path)")
.takes_value(true),
)
)
)
.subcommand(
App::new("guest_override")
.about("Override the CKey used by guest connections")
.arg(
Arg::with_name("ckey")
.takes_value(true),
)
)
}

pub fn connect(addr: &SocketAddr) -> std::io::Result<Server> {
Expand Down Expand Up @@ -743,6 +751,18 @@ impl Server {
}
}

("guest_override", Some(matches)) => match matches.value_of("ckey") {
Some(ckey) => match crate::ckey_override::override_guest_ckey(ckey) {
Ok(()) => "Success".to_owned(),

Err(e) => {
format!("Failed: {:?}", e)
}
},

None => "no ckey provided".to_owned(),
},

_ => "unknown command".to_owned(),
}
}
Expand Down

0 comments on commit 905ffc9

Please sign in to comment.