-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathvuln.c
83 lines (68 loc) · 1.94 KB
/
vuln.c
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Don't include unistd.h otherwise a secure version of read will be used
// #include <unistd.h>
// #include <alloca.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#if !defined(__x86_64__) && !defined(__i386__)
# error "Unsupported architecture"
#endif
char put_me_in_bss[1024] = {0};
int play_with_stack(int i) {
int *local = alloca(10);
local[0] = 123;
intptr_t memcpy_ptr = (intptr_t) memcpy;
return local[i] + memcpy_ptr;
}
void add(int *a, int b) {
*a += b;
}
void mem_to_mem(int *dst, int *src) {
*dst = *src;
}
void writemem(void **in, void *val) {
*in = val;
}
int do_read() {
char buffer[100];
read(0, buffer, 10000);
}
void deref_and_write_with_offset() {
#if defined(__x86_64__)
__asm__("pop rax; pop rbx; pop rcx; mov rax,QWORD PTR [rax]; mov QWORD PTR [rax+rcx*1],rbx; ret;");
#elif defined(__i386__)
__asm__("pop eax; pop ebx; pop ecx; mov eax,DWORD PTR [eax]; mov DWORD PTR [eax+ecx*1],ebx; ret;");
#endif
}
void deref_with_offset_and_save() {
#if defined(__x86_64__)
__asm__("pop rax; pop rbx; pop rcx; mov rax, [rax]; mov rax,QWORD PTR [rax+rbx]; mov QWORD PTR [rcx],rax; ret;");
#elif defined(__i386__)
__asm__("pop eax; pop ebx; pop ecx; mov eax, [eax]; mov eax,DWORD PTR [eax+ebx]; mov DWORD PTR [ecx],eax; ret;");
#endif
}
void copy_to_stack() {
#if defined(__x86_64__)
__asm__("pop rbx; pop rcx; mov rbx, QWORD PTR [rbx]; mov QWORD PTR [rsp+rcx*1],rbx; ret;");
#elif defined(__i386__)
__asm__("pop ebx; pop ecx; mov ebx, DWORD PTR [ebx]; mov DWORD PTR [esp+ecx*1],ebx; ret;");
#endif
}
void load_memcpy() {
#if defined(__x86_64__)
__asm__("pop rsi; add rsi,rsp; ret;");
__asm__("pop rax; add rsp,rax; ret;");
#elif defined(__i386__)
__asm__("pop eax; pop esi; add esi,esp; mov DWORD PTR [esp+eax], esi; ret;");
__asm__("pop ebx; add esp,ebx; ret;");
#endif
}
void args() {
#if defined(__x86_64__)
__asm__("pop rdi; pop rsi; pop rdx; ret;");
#endif
}
int main() {
do_read();
}