Repository navigation
Expand file tree
/
Copy pathchallenge.c
More file actions
66 lines (59 loc) · 1.51 KB
/
Copy pathchallenge.c
File metadata and controls
66 lines (59 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/utsname.h>
#include <unistd.h>
unsigned char kBuiltinShellcode[] = {
0x00, 0x00, 0x11, 0x42, 0x01, 0x00, 0xfa, 0x0b,
};
void PrintCurrentArch() {
struct utsname result;
uname(&result);
printf("Challenge running on %s\n", result.machine);
}
ssize_t ReadN(int fd, void *buf, size_t count) {
size_t read_bytes = 0;
while (read_bytes < count) {
ssize_t ret = read(fd, buf + read_bytes, count - read_bytes);
if (ret < 0) {
return ret;
}
read_bytes += ret;
}
return read_bytes;
}
int main(int argc, char *argv[]) {
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
PrintCurrentArch();
void *page = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (page == MAP_FAILED) {
puts("mmap failed?");
return 1;
}
printf("Give me a shellcode to compute A+B, size (0 to use the builtin): ");
size_t size = 0;
scanf("%zu", &size);
if (size > 4096) {
puts("too big! ❤️");
return 1;
}
if (size) {
printf("ok, give me %zu bytes of shellcode: ", size);
if (ReadN(0, page, size) != size) {
return 1;
}
} else {
memcpy(page, kBuiltinShellcode, sizeof(kBuiltinShellcode));
}
if (mprotect(page, 4096, PROT_READ | PROT_EXEC) != 0) {
puts("mprotect failed??");
return 1;
}
int a = 1;
int b = 2;
int (*a_plus_b)(int, int) = page;
printf("%d + %d = %d\n", a, b, a_plus_b(a, b));
return 0;
}