Advertisement
tyler569

CTF

Apr 7th, 2019
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1.  
  2. /* telnet philbrick.io 20012 */
  3.  
  4. /* (credit Patrick Coleman) */
  5.  
  6. #include <stdio.h>
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <error.h>
  12. #include <sys/mman.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <fcntl.h>
  16. #include <errno.h>
  17. #include <limits.h>
  18.  
  19. void err_exit(char *e) {
  20.     fprintf(stderr, "%s\n", e);
  21.     _exit(1);
  22. }
  23.  
  24. char* getln(void) {
  25.     char *buf = NULL;
  26.     size_t len = 0;
  27.     if (getdelim(&buf, &len, '\n', stdin) <= 0) {
  28.         err_exit("getdelim() failed");
  29.     }
  30.     return buf;
  31. }
  32.  
  33. int main(void) {
  34.     int fd;
  35.     char b[50];
  36.  
  37.     setbuf(stdin, NULL);
  38.     setbuf(stdout, NULL);
  39.     setbuf(stderr, NULL);
  40.  
  41.     printf("Quote of the day v0.3. Answer a few questions to get one\n");
  42.  
  43.     printf("Your double number: ");
  44.     char *buf = getln();
  45.  
  46.     double d = strtod(buf, NULL);
  47.     if (errno != 0) {
  48.         err_exit("strtold() failed");
  49.     }
  50.     /* Is there any IEEE floating point number which can pass those checks? */
  51.     if (d <= 42.0l) {
  52.         err_exit("d is too low");
  53.     }
  54.     if (d > 42.0l) {
  55.         err_exit("d is too high");
  56.     }
  57.  
  58.     printf("Your integer number: ");
  59.     buf = getln();
  60.  
  61.     long l = strtol(buf, NULL, 0);
  62.     if (errno != 0) {
  63.         err_exit("strtol() failed");
  64.     }
  65.     if (l < 0) {
  66.         l = -l;
  67.     }
  68.     l %= 3;
  69.     /* number%3 results in either 0, 1 or 2. So, we're safe here. Most likely :) */
  70.     if (l == 0) {
  71.         err_exit("Your quote: Never miss a chance to keep your mouth shut");
  72.     }
  73.     if (l == 1) {
  74.         err_exit("Your quote: I'm still an atheist, thank God");
  75.     }
  76.     if (l == 2) {
  77.         err_exit("Your quote: Nothing ever goes away");
  78.     }
  79.  
  80.     printf("Getting to this point means you deserve a flag: ");
  81.     fd = open("./flag.txt", O_RDONLY);
  82.     read(fd, &b, sizeof(b));
  83.     printf("%s\n", b);
  84.     close(fd);
  85.  
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement