Skip to content

Commit

Permalink
Search for metadata if not found at end of file
Browse files Browse the repository at this point in the history
The runner will now search for its metadata if it is not found at the end of the executable.
This can arise when the runner is modified after packing and other data is appended after it.
  • Loading branch information
Systemcluster committed Mar 15, 2024
1 parent 77fe8a8 commit 08e8f94
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Expand Up @@ -282,7 +282,7 @@ fn main() {
);

let info = StarterInfo {
signature: [0x50, 0x45, 0x33, 0x44, 0x41, 0x54, 0x41, 0x00],
signature: WRAPPE_SIGNATURE,
show_console,
current_dir,
verification,
Expand Down
1 change: 1 addition & 0 deletions src/types.rs
@@ -1,6 +1,7 @@
pub use zerocopy::AsBytes;

pub const WRAPPE_FORMAT: u8 = 201;
pub const WRAPPE_SIGNATURE: [u8; 8] = [0x50, 0x45, 0x33, 0x44, 0x41, 0x54, 0x41, 0x00];
pub const NAME_SIZE: usize = 128;
pub const ARGS_SIZE: usize = 512;

Expand Down
1 change: 1 addition & 0 deletions startpe/Cargo.toml
Expand Up @@ -30,6 +30,7 @@ strip = "symbols"
dirs = "5.0.1"
filetime = "0.2.23"
fslock = "0.2.1"
memchr = "2.7.1"
memmap2 = "0.9.4"
rayon = "1.9.0"
twox-hash = { version = "1.6.3", default-features = false }
Expand Down
39 changes: 27 additions & 12 deletions startpe/src/main.rs
Expand Up @@ -17,6 +17,7 @@ use std::time::SystemTime;
#[cfg(windows)]
use winapi::um::wincon::{AttachConsole, ATTACH_PARENT_PROCESS};

use memchr::memmem;
use memmap2::MmapOptions;
use zerocopy::Ref;

Expand Down Expand Up @@ -65,11 +66,35 @@ fn main() {
.expect("couldn't memory map current executable")
};
let end = mmap.len();
if end < size_of::<StarterInfo>() {
panic!("file is too small ({} < {})", end, size_of::<StarterInfo>())
}

let mut signature = Vec::with_capacity(8);
signature.extend_from_slice(&WRAPPE_SIGNATURE_1[..4]);
signature.extend_from_slice(&WRAPPE_SIGNATURE_2[..4]);

let mut info_start = end - size_of::<StarterInfo>();
if mmap[info_start..info_start + 8] != signature[..] {
if let Some(pos) = memmem::rfind(&mmap[..info_start], &signature) {
info_start = pos;
} else {
panic!("couldn't find starter info")
}
}

let info_start = end - size_of::<StarterInfo>();
let info = Ref::<_, StarterInfo>::new(&mmap[info_start..end])
let info = Ref::<_, StarterInfo>::new(&mmap[info_start..info_start + size_of::<StarterInfo>()])
.expect("couldn't read starter info")
.into_ref();
if info.signature != signature[..] {
panic!("file signature is invalid")
}
if info.wrappe_format != WRAPPE_FORMAT {
panic!(
"runner version ({}) differs from wrapper version ({})",
WRAPPE_FORMAT, info.wrappe_format
);
}

let show_information = info.show_information;
let show_console = info.show_console;
Expand All @@ -94,16 +119,6 @@ fn main() {
);
}

if info.signature != [0x50, 0x45, 0x33, 0x44, 0x41, 0x54, 0x41, 0x00] {
panic!("file signature is invalid");
}
if info.wrappe_format != WRAPPE_FORMAT {
panic!(
"runner version ({}) differs from wrapper version ({})",
WRAPPE_FORMAT, info.wrappe_format
);
}

let unpack_dir_name = std::str::from_utf8(
&info.unpack_directory[0..(info
.unpack_directory
Expand Down
2 changes: 2 additions & 0 deletions startpe/src/types.rs
@@ -1,6 +1,8 @@
pub use zerocopy::{FromBytes, FromZeroes};

pub const WRAPPE_FORMAT: u8 = 201;
pub const WRAPPE_SIGNATURE_1: [u8; 6] = [0x50, 0x45, 0x33, 0x44, 0x00, 0x00];
pub const WRAPPE_SIGNATURE_2: [u8; 4] = [0x41, 0x54, 0x41, 0x00];
pub const NAME_SIZE: usize = 128;
pub const ARGS_SIZE: usize = 512;

Expand Down

0 comments on commit 08e8f94

Please sign in to comment.