-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrackme.c
322 lines (272 loc) · 8.48 KB
/
crackme.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "libc/lib.h"
#include "table.h"
static struct user_regs_struct regs;
static uint8_t call_code[] = {0xe8, 0x00, 0x00, 0x00, 0x00, 0xcc, 0x00, 0x00};
static long long regs_bak[6];
static ssize_t write_all(int fd, char *buf, size_t size) {
while(size != 0) {
ssize_t wb = nano_write(fd, buf, size);
if(wb < 0)
return wb;
size -= wb;
}
return size;
}
static void print_usage(char *prog_name, char *buf) {
char usage_msg[] = "Usage: ";
char flags_msg[] = " -[uc]\nCrackme v0.1.666 by cyberfined\n";
size_t prog_name_len = strlen(prog_name);
char *cur_char = buf;
nano_memcpy(cur_char, usage_msg, 7);
cur_char += 7;
nano_memcpy(cur_char, prog_name, prog_name_len);
cur_char += prog_name_len;
nano_memcpy(cur_char, flags_msg, 38);
cur_char += 38;
write_all(STDERR, buf, cur_char - buf);
}
static void reverse(char *buf, size_t size) {
for(size_t i = 0, j = size - 1; i < j; i++, j--) {
char c = buf[i];
buf[i] = buf[j];
buf[j] = c;
}
}
static size_t itoa(size_t num, char *buf) {
size_t i = 0;
do {
buf[i++] = num % 10 + '0';
num /= 10;
} while(num != 0);
reverse(buf, i);
return i;
}
typedef struct FILE {
int fd;
char buf[4096];
size_t offset;
size_t size;
} FILE;
static FILE stdin = {0, {0}, 0, 0};
char* fgets(char *buf, size_t size, FILE *f) {
size_t cpy_size = 0;
for(;;) {
if(f->size > 0) {
char *newline = strchr(&f->buf[f->offset], '\n');
if(newline) {
cpy_size = newline - f->buf - f->offset + 1;
if(cpy_size >= size)
cpy_size = size - 1;
goto ret;
} else if(f->offset + f->size >= sizeof(f->buf) - 2) {
if(f->offset == 0) {
cpy_size = f->size;
if(cpy_size >= size)
cpy_size = size - 1;
goto ret;
} else {
nano_memmove(f->buf, &f->buf[f->offset], f->size);
f->offset = 0;
}
}
}
ssize_t rb = nano_read(
f->fd,
&f->buf[f->offset + f->size],
sizeof(f->buf) - 1 - f->offset - f->size
);
if(rb == 0) {
return NULL;
} else if(rb < 0) {
return NULL;
}
f->size += rb;
f->buf[f->size] = 0;
continue;
ret:
nano_memcpy(buf, &f->buf[f->offset], cpy_size);
buf[cpy_size] = 0;
f->offset += cpy_size;
f->size -= cpy_size;
if(f->size == 0)
f->offset = 0;
break;
}
return buf;
}
// Interpreter
#include "table.inc"
__attribute__((always_inline))
static inline void prepare_regs(cmd_entry *entry) {
void *args = ®s;
long long src[6];
for(int i = 0; i < entry->num_args; i++)
src[i] = *(long long*)(args + entry->args_offsets[i]);
for(int i = 0; i < entry->num_args; i++) {
regs_bak[i] = *(long long*)(args + fun_args_offsets[i]);
*(long long*)(args + fun_args_offsets[i]) = src[i];
}
if(entry->type == CMD_SYSCALL)
regs.orig_rax = entry->number;
else if(entry->type == CMD_PROC)
regs.orig_rax = 39;
}
__attribute__((always_inline))
static inline void reset_regs(cmd_entry *entry) {
void *args = ®s;
for(int i = 0; i < entry->num_args; i++)
*(long long*)(args + fun_args_offsets[i]) = regs_bak[i];
}
__attribute__((always_inline))
static inline void wait_or_die(void) {
int status;
wait(&status);
if(WIFEXITED(status))
exit(EXIT_SUCCESS);
}
__attribute__((always_inline))
static inline void call_interpreter(pid_t pid) {
long backup = 0;
for(;;) {
ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
wait_or_die();
ptrace(PTRACE_GETREGS, pid, NULL, ®s);
unsigned long long rip_bak = regs.rip;
cmd_entry *entry = nano_lookup(regs.rip);
if(!entry) {
ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
wait_or_die();
continue;
}
if(entry->type == CMD_SYSCALL || entry->type == CMD_PROC) {
prepare_regs(entry);
} else if(entry->type == CMD_REGS) {
entry->regs_proc(®s);
}
ptrace(PTRACE_SETREGS, pid, NULL, ®s);
ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
wait_or_die();
if(entry->type == CMD_PROC) {
int32_t offset = entry->proc_func - ((void*)regs.rip + 5);
*(int32_t*)&call_code[1] = offset;
ptrace(PTRACE_PEEKTEXT, pid, (void*)regs.rip, &backup);
long patch = *(long*)call_code;
ptrace(PTRACE_POKETEXT, pid, (void*)regs.rip, (void*)patch);
ptrace(PTRACE_CONT, pid, NULL, NULL);
wait_or_die();
ptrace(PTRACE_POKETEXT, pid, (void*)regs.rip, (void*)backup);
} else if(entry->type == CMD_REGS) {
continue;
}
ptrace(PTRACE_GETREGS, pid, NULL, ®s);
reset_regs(entry);
if(entry->type == CMD_PROC)
regs.rip = rip_bak;
ptrace(PTRACE_SETREGS, pid, NULL, ®s);
}
}
__attribute__((always_inline))
static inline void int3() {
__asm__ __volatile__ ("int3");
}
typedef struct {
int offset;
uint16_t key;
} pass_info_t;
static bool check_password(pass_info_t *pass_info, size_t count) {
uint16_t password[8] = {42824,42175,27030,55028,31997,4578,9264};
if(pass_info->offset == 7) {
struct timespec tv;
nano_clock_gettime(CLOCK_REALTIME, &tv);
password[7] = (tv.tv_sec / (24 * 3600) % 229) ^ 40704;
}
if(pass_info->offset < 8) {
if((password[pass_info->offset] ^ pass_info->key) == count) {
pass_info->key *= (pass_info->offset + 10) * 5;
pass_info->offset++;
return true;
}
} else {
return true;
}
return false;
}
int main(int argc, char **argv) {
// killer256
char greetings[] = "Crackme v0.1.666 by cyberfined\nReading from stdin\n";
char success_msg[] = "Congratulations, dear hacker!\n";
char fail_msg[] = "Password is wrong!\n";
bool only_uniq = false, with_count = false;
char num_buf[16], buf1[4096], buf2[4096] = {0};
char *cur_line = buf1, *prev_line = buf2;
pid_t pid = fork();
if(pid > 0) {
// parent
wait_or_die();
sigset_t sigmask = 1 << (SIGCHLD - 1);
if(sigprocmask(SIG_BLOCK, &sigmask, NULL) < 0)
exit(EXIT_FAILURE);
if(prctl(PR_SET_PTRACER, pid, 0, 0, 0) < 0)
exit(EXIT_FAILURE);
call_interpreter(pid);
exit(EXIT_SUCCESS);
} else if(pid < 0) {
exit(EXIT_FAILURE);
}
// children
if(ptrace(PTRACE_TRACEME, 0, NULL, NULL) < 0)
exit(EXIT_FAILURE);
int3();
pid_t parent = getppid();
if(ptrace(PTRACE_SEIZE, parent, NULL, NULL) < 0)
exit(EXIT_FAILURE);
if(argc > 3) {
print_usage(argv[0], buf1);
nano_exit(EXIT_FAILURE);
} else if(argc == 1) {
nano_write(2, greetings, sizeof(greetings) - 1);
}
for(int i = 1; i < argc; i++) {
if(!strcmp(argv[i], "-u")) {
only_uniq = true;
} else if(!strcmp(argv[i], "-c")) {
with_count = true;
} else {
print_usage(argv[0], buf1);
nano_exit(EXIT_FAILURE);
}
}
pass_info_t pass_info = {
.key = 42787,
.offset = 0,
};
bool is_auth = true;
size_t count = 0;
for(;;) {
char *is_eof = fgets(cur_line, sizeof(buf1), &stdin);
if(strcmp(cur_line, prev_line) || !is_eof) {
if(prev_line[0] != 0 && (!only_uniq || count == 1)) {
if(with_count) {
size_t num_size = nano_itoa(count, num_buf);
num_buf[num_size++] = ' ';
write_all(1, num_buf, num_size);
}
write_all(1, prev_line, strlen(prev_line));
is_auth = is_auth && check_password(&pass_info, count);
}
if(!is_eof)
break;
count = 0;
}
char *next_line = prev_line;
prev_line = cur_line;
cur_line = next_line;
count++;
}
if(is_auth && pass_info.offset == 8) {
nano_write(1, success_msg, sizeof(success_msg) - 1);
} else {
nano_write(1, fail_msg, sizeof(fail_msg) - 1);
}
nano_exit(EXIT_SUCCESS);
}