flagtool.c (1272B)
1#include <stdio.h> 2#include <string.h> 3#include <stdlib.h> 4#include <unistd.h> 5 6int main(int argc, char** argv) { 7 if (argc <= 1) { 8 printf("Usage: <flagtool> <writeflag/readflag>\nWrites flag to /root/\nUsed as SUID binary for CTF challenges\n"); 9 return -1; 10 } 11 12 setuid(0); 13 setgid(0); 14 seteuid(0); 15 16 if(strcmp(argv[1], "writeflag") == 0) { 17 char* FLAG_ENV = getenv("FLAG"); 18 if (FLAG_ENV == NULL) { 19 printf("FLAG env not set\n"); 20 return -1; 21 } 22 23 FILE *f = fopen("/root/flag", "w"); 24 if (f == NULL) 25 { 26 printf("Error opening file!\n"); 27 exit(1); 28 } 29 30 /* print some text */ 31 fprintf(f, "%s\n", FLAG_ENV); 32 fclose(f); 33 } 34 35 else if(strcmp(argv[1], "readflag") == 0) { 36 char content[256]; 37 FILE *f = fopen("/root/flag", "r"); 38 if (f == NULL) 39 { 40 printf("Error opening file!\n"); 41 exit(1); 42 } 43 44 fgets(content, sizeof(content), f); 45 46 /* print some text */ 47 printf("%s\n", content); 48 fclose(f); 49 } 50 else { 51 printf("Usage: <flagtool> <writeflag/readflag>\nWrites flag to /root/\nUsed as SUID binary for CTF challenges\n"); 52 } 53}