Facebook
From Howard, 3 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 425
  1. #include <signal.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6.  
  7. char * read_file(char *);
  8. void give_flag();
  9. void give_error();
  10. void timeout();
  11.  
  12. int i, input_char;
  13.  
  14. int main(int argc, char * argv[]) {
  15.   setvbuf(stdin,  NULL, _IONBF, 0);   // Switch off I/O buffering
  16.   setvbuf(stdout, NULL, _IONBF, 0);   // Switch off I/O buffering
  17.   setvbuf(stderr, NULL, _IONBF, 0);   // Switch off I/O buffering
  18.  
  19.   signal(SIGALRM, timeout);           // Set program to exit on timeout
  20.   alarm(10);                          // Exit program after 10s
  21.  
  22.   void (*call_function)() = &give_error;  // function pointer to invoke function later on
  23.   char input[128];
  24.  
  25.   // Check program usage
  26.   if (argc < 2) {
  27.     printf("Secret value needs to be supplied as an argument to this program!\n");
  28.     printf("Example: ./unlockme secret\n");
  29.     return 1;
  30.   }
  31.   else if (strlen(argv[1]) != 128) {
  32.     printf("Secret value must be exactly 128 characters long!\n");
  33.     return 1;
  34.   }
  35.  
  36.   printf("Enter the secret value to unlock the flag => ");
  37.   for (i = 0; i <= 128; i++) {
  38.     input_char = getchar();
  39.     // Exit loop if end of user input is reached
  40.     if (feof(stdin) || ferror(stdin) || input_char == '\n' || input_char == '\x00') {
  41.       input[i] = '\x00';
  42.       break;
  43.     }
  44.     else {
  45.       input[i] = input_char;
  46.     }
  47.   }
  48.  
  49.   // Check if user input equals to secret value
  50.   if (strcmp(input, argv[1]) == 0) {
  51.     call_function = &give_flag;  // give user flag!
  52.   }
  53.  
  54.   // If user input is incorrect, print error
  55.   if (call_function != &give_flag) {
  56.     call_function = &give_error;
  57.   }
  58.  
  59.   call_function();
  60.  
  61.   return 0;
  62. }
  63.  
  64. // Read contents of file and return a string containing the contents
  65. char * read_file(char * filename) {
  66.   char * file_contents = malloc(4096 * sizeof(char));
  67.  
  68.   FILE * file;
  69.   file = fopen(filename, "r");
  70.  
  71.   if (file == NULL) {
  72.     printf("\nUnable to open file!\n");
  73.     _exit(1);
  74.   }
  75.  
  76.   fread(file_contents, 4096, sizeof(char), file);
  77.   fclose(file);
  78.  
  79.   return file_contents;
  80. }
  81.  
  82. // Print flag
  83. void give_flag() {
  84.   printf("\nOkay here you go: %s", read_file("flag.txt"));
  85.   _exit(0);
  86. }
  87.  
  88. // Print error message
  89. void give_error() {
  90.   printf("\nNah the secret value you've provided is incorrect!\n");
  91.   _exit(1);
  92. }
  93.  
  94. // Exit program after 10s
  95. void timeout() {
  96.   printf("\nToo slow!\n");
  97.   _exit(1);
  98. }