|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | +#include <unistd.h> |
| 5 | +#include <sys/types.h> |
| 6 | +#include <wchar.h> |
| 7 | +#include <locale.h> |
| 8 | + |
| 9 | +#define BUFSIZE 64 |
| 10 | +#define FLAGSIZE 64 |
| 11 | +#define CANARY_SIZE 4 |
| 12 | + |
| 13 | +void win() { |
| 14 | + char buf[FLAGSIZE]; |
| 15 | + FILE *f = fopen("flag.txt","r"); |
| 16 | + if (f == NULL) { |
| 17 | + printf("%s %s", "Please create 'flag.txt' in this directory with your", |
| 18 | + "own debugging flag.\n"); |
| 19 | + exit(0); |
| 20 | + } |
| 21 | + |
| 22 | + fgets(buf,FLAGSIZE,f); // size bound read |
| 23 | + puts(buf); |
| 24 | + fflush(stdout); |
| 25 | +} |
| 26 | + |
| 27 | +char global_canary[CANARY_SIZE]; |
| 28 | +void read_canary() { |
| 29 | + FILE *f = fopen("canary.txt","r"); |
| 30 | + if (f == NULL) { |
| 31 | + printf("%s %s", "Please create 'canary.txt' in this directory with your", |
| 32 | + "own debugging canary.\n"); |
| 33 | + exit(0); |
| 34 | + } |
| 35 | + |
| 36 | + fread(global_canary,sizeof(char),CANARY_SIZE,f); |
| 37 | + fclose(f); |
| 38 | +} |
| 39 | + |
| 40 | +void vuln(){ |
| 41 | + char canary[CANARY_SIZE]; |
| 42 | + char buf[BUFSIZE]; |
| 43 | + char length[BUFSIZE]; |
| 44 | + int count; |
| 45 | + int x = 0; |
| 46 | + memcpy(canary,global_canary,CANARY_SIZE); |
| 47 | + printf("How Many Bytes will You Write Into the Buffer?\n> "); |
| 48 | + while (x<BUFSIZE) { |
| 49 | + read(0,length+x,1); |
| 50 | + if (length[x]=='\n') break; |
| 51 | + x++; |
| 52 | + } |
| 53 | + sscanf(length,"%d",&count); |
| 54 | + |
| 55 | + printf("Input> "); |
| 56 | + read(0,buf,count); |
| 57 | + |
| 58 | + if (memcmp(canary,global_canary,CANARY_SIZE)) { |
| 59 | + printf("***** Stack Smashing Detected ***** : Canary Value Corrupt!\n"); // crash immediately |
| 60 | + exit(-1); |
| 61 | + } |
| 62 | + printf("Ok... Now Where's the Flag?\n"); |
| 63 | + fflush(stdout); |
| 64 | +} |
| 65 | + |
| 66 | +int main(int argc, char **argv){ |
| 67 | + |
| 68 | + setvbuf(stdout, NULL, _IONBF, 0); |
| 69 | + |
| 70 | + // Set the gid to the effective gid |
| 71 | + // this prevents /bin/sh from dropping the privileges |
| 72 | + gid_t gid = getegid(); |
| 73 | + setresgid(gid, gid, gid); |
| 74 | + read_canary(); |
| 75 | + vuln(); |
| 76 | + return 0; |
| 77 | +} |
0 commit comments