-
Notifications
You must be signed in to change notification settings - Fork 56
/
canary-bypass.c
120 lines (101 loc) · 4.55 KB
/
canary-bypass.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Compiled with:
// g++ -static -Wl,-z,relro,-z,now -masm=intel ./chall1.c -o ./chall1
// Compiled in ubuntu:22.04
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
int ssp;
size_t *stack_buf;
size_t temp_num;
void setup() {
setvbuf(stdin, 0LL, 2, 0LL);
setvbuf(stdout, 0LL, 2, 0LL);
setvbuf(stderr, 0LL, 2, 0LL);
stack_buf =
(size_t *)mmap((void *)(0x31336 << 12), 0x8000, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (stack_buf == MAP_FAILED)
exit(-1);
ssp = 0x1000 / sizeof(size_t);
}
void banner() {
try {
// clang-format off
puts(" ,--, ");
puts(",---.'| ,--. ");
puts("| | : ,----.. ,--/ /| ");
puts(": : | ,--, / / \\ ,---,': / ' ,---, ");
puts("| ' : ,'_ /|| : :: : '/ / /_ ./| ");
puts("; ; ' .--. | | :. | ;. /| ' ,---, | ' : ");
puts("' | |__ ,'_ /| : . |. ; /--` ' | /___/ \\. : | ");
puts("| | :.'|| ' | | . .; | ; | ; ;. \\ \\ ,' ' ");
puts("' : ;| | ' | | || : | : ' \\\\ ; ` ,' ");
puts("| | ./ : | | : ' ;. | '___ | | '\\ \\ ' ");
puts("; : ; | ; ' | | '' ; : .'|' : |. \\' \\ | ");
puts("| ,/ : | : ; ; |' | '/ :| | '_\\.' \\ ; ; ");
puts("'---' ' : `--' \\ : / ' : | : \\ \\ ");
puts(" : , .-./\\ \\ .' ; |,' \\ ' ; ");
puts(" `--`----' `---` '---' `--` ");
puts(" ");
puts("\t ");
puts("\t ,--. ____ ");
puts("\t ,--.'| ,' , `. ,---,. ,---,.,-.----. ");
puts("\t ,--,: : | ,--, ,-+-,.' _ | ,' .' \\ ,' .' |\\ / \\ ");
puts("\t,`--.'`| ' : ,'_ /| ,-+-. ; , ||,---.' .' |,---.' |; : \\ ");
puts("\t| : : | | .--. | | : ,--.'|' | ;|| | |: || | .'| | .\\ : ");
puts("\t: | \\ | :,'_ /| : . | | | ,', | ':: : : /: : |-,. : |: | ");
puts("\t| : ' '; || ' | | . . | | / | | ||: | ; : | ;/|| | \\ : ");
puts("\t' ' ;. ;| | ' | | | ' | : | : |,| : \\| : .'| : . / ");
puts("\t| | | \\ |: | | : ' ; ; . | ; |--' | | . || | |-,; | | \\ ");
puts("\t' : | ; .'| ; ' | | ' | : | | , ' : '; |' : ;/|| | ;\\ \\ ");
puts("\t| | '`--' : | : ; ; | | : ' |/ | | | ; | | \\: ' | \\.' ");
puts("\t' : | ' : `--' \\; | |`-' | : / | : .': : :-' ");
puts("\t; |.' : , .-./| ;/ | | ,' | | ,' | |.' ");
puts("\t'---' `--`----' '---' `----' `----' `---' ");
puts("\t ");
// clang-format on
puts("================ Make ROP Great Again! ================");
puts("========== Try to bypass canary without leak! =========");
puts("Please share your lucky number.");
puts("I won’t stop reading until I receive the correct one.");
} catch (int x) {
puts("What?");
}
}
void vuln() {
__asm__("mov r14, qword ptr [rbp + 8];"
"mov temp_num, r14;");
stack_buf[ssp++] = temp_num;
size_t buf[2];
for (int i = 0; i < 0x100 / sizeof(size_t); i++) {
if (scanf("%lu", &temp_num) != 1) {
while (getchar() != '\n' && getchar() != EOF)
;
i--;
continue;
}
if (temp_num == 0x31337) {
break;
}
buf[i] = temp_num;
}
__asm__("mov r14, qword ptr [rbp + 8];"
"mov temp_num, r14;");
if (temp_num != stack_buf[ssp - 1])
throw 1;
}
int main() {
setup();
banner();
try {
while (1) {
vuln();
puts("Alright, even if you guessed it right, I won't stop—the next "
"round has begun.");
}
} catch (int x) {
puts("Hacker!");
}
return 0;
}