Skip to content

Commit 6616b9e

Browse files
committed
Allocate memory using memmap2 crate and execute Shellcode
1 parent af25c15 commit 6616b9e

File tree

6 files changed

+111
-0
lines changed

6 files changed

+111
-0
lines changed

Process/MmapOptions/Cargo.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Process/MmapOptions/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "MmapOptions"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
memmap2 = "0.9.5"

Process/MmapOptions/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## SHELLCODE EXECUTION USING memmap2 crate
2+
3+
This PoC uses the memmap2's MmapOptions to create an anonymous memory mapping with read/write permissions.
4+
5+
![PoC Image](./image.png)
6+
## Source / Resources
7+
8+
* https://docs.rs/memmap2/latest/memmap2/struct.MmapOptions.html
9+
* Refered some C Programs:
10+
* https://gist.github.com/libcrack/8ccc5e75e164c7959fa070ba9061e51b
11+

Process/MmapOptions/image.png

75.2 KB
Loading
328 Bytes
Binary file not shown.

Process/MmapOptions/src/main.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use memmap2::MmapOptions;
2+
use std::fs::File;
3+
use std::io::{self, Read};
4+
use std::path::Path;
5+
6+
fn load_shellcode<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
7+
let mut file = File::open(path)?;
8+
let mut shellcode = Vec::new();
9+
10+
file.read_to_end(&mut shellcode)?;
11+
12+
Ok(shellcode)
13+
}
14+
15+
fn execute_shellcode(shellcode: &[u8]) -> io::Result<()> {
16+
if shellcode.is_empty() {
17+
return Err(io::Error::new(
18+
io::ErrorKind::InvalidInput,
19+
"Shellcode is empty",
20+
));
21+
}
22+
23+
let shellcode_size = shellcode.len();
24+
25+
let mut mmap = MmapOptions::new()
26+
.len(shellcode_size)
27+
.map_anon()
28+
.map_err(|e| {
29+
io::Error::new(io::ErrorKind::Other, format!("Failed to map memory: {}", e))
30+
})?;
31+
32+
mmap.copy_from_slice(shellcode);
33+
34+
let mmap = mmap.make_exec().map_err(|e| {
35+
io::Error::new(
36+
io::ErrorKind::Other,
37+
format!("Failed to make executable: {}", e),
38+
)
39+
})?;
40+
41+
if mmap.as_ptr().align_offset(std::mem::align_of::<usize>()) != 0 {
42+
return Err(io::Error::new(
43+
io::ErrorKind::Other,
44+
"Memory is not properly aligned",
45+
));
46+
}
47+
48+
// exec using fn !
49+
unsafe {
50+
let shell: unsafe extern "C" fn() = std::mem::transmute(mmap.as_ptr());
51+
shell();
52+
}
53+
54+
Ok(())
55+
}
56+
57+
fn main() -> io::Result<()> {
58+
let shellcode_path = "msgbox_shellcode.bin";
59+
60+
let shellcode = load_shellcode(shellcode_path)?;
61+
println!("[+] Loading shellcode ({} bytes)", shellcode.len());
62+
63+
println!("[+] Executing shellcode...");
64+
execute_shellcode(&shellcode)?;
65+
66+
println!("[+] Shellcode executed successfully");
67+
Ok(())
68+
}

0 commit comments

Comments
 (0)