Advertisement
wowonline

Untitled

Dec 12th, 2021
672
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <fcntl.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <sys/wait.h>
  5. #include <limits.h>
  6. #include <stdlib.h>
  7.  
  8. enum { NAME_SIZE = 23 };
  9.  
  10. int
  11. main(int argc, char *argv[])
  12. {
  13.     int fd_r = open("/dev/urandom", O_RDONLY);
  14.     if (fd_r == -1) {
  15.         return 1;
  16.     }
  17.     unsigned long long num_r;
  18.     read(fd_r, &num_r, sizeof(num_r));
  19.     char filename[NAME_SIZE];
  20.     snprintf(filename, NAME_SIZE, "%llu%s", num_r, ".c");
  21.  
  22.     char fullpath[PATH_MAX];
  23.     char *env = getenv("XDG_RUNTIME_DIR");
  24.     if (env != NULL) {
  25.         //
  26.     } else if ((env = getenv("TMPDIR")) != NULL) {
  27.         //
  28.     } else {
  29.         env = "/tmp";
  30.     }
  31.     snprintf(fullpath, PATH_MAX, "%s%s", env, filename);
  32.  
  33.  
  34.     pid_t pid;
  35.     int fd = open(fullpath , O_WRONLY | O_CREAT | O_CLOEXEC, 0600);
  36.     FILE *f = fdopen(fd, "w");
  37.     if (f == NULL) {
  38.         return 1;
  39.     }
  40.     char *prog_beg =
  41.     "#include <stdio.h>\n"
  42.     "#include <stdlib.h>\n"
  43.     "int\n"
  44.     "main(int argc, char *argv[])\n"
  45.     "{\n"
  46.     "\tint x;\n"
  47.     "\tchar *summon = \"summon\";\n"
  48.     "\tchar *reject = \"reject\";\n"
  49.     "\tchar *disqualify = \"disqualify\";\n"
  50.     "\twhile (scanf(\"%d\", &x) == 1) {\n"
  51.     "\t\tprintf(\"%s\\n\", ";
  52.     char *prog_end =
  53.     ");\n"
  54.     "\t}\n"
  55.     "\tremove(argv[0]);\n"
  56.     "\tremove(argv[1]);\n"
  57.     "\treturn 0;\n"
  58.     "}";
  59.  
  60.     fprintf(f, "%s", prog_beg);
  61.     fprintf(f, "%s", argv[1]);
  62.     fprintf(f, "%s", prog_end);
  63.  
  64.     char fullpath_exec[PATH_MAX];
  65.     read(fd_r, &num_r, sizeof(num_r));
  66.     close(fd_r);
  67.     snprintf(fullpath_exec, sizeof(fullpath_exec), ".%s%llu.o", env, num_r);
  68.  
  69.     fclose(f);
  70.     pid = fork();
  71.     if (pid == -1) {
  72.         return 1;
  73.     } else if (!pid) {
  74.         execlp("gcc", "gcc", fullpath, "-o", fullpath_exec, NULL);
  75.         _exit(1);
  76.     }
  77.     wait(NULL);
  78.  
  79.     pid = fork();
  80.     if (pid == -1) {
  81.         return 1;
  82.     } else if (!pid) {
  83.         execlp(fullpath_exec, fullpath_exec, fullpath, NULL);
  84.         _exit(1);
  85.     }
  86.     wait(NULL);
  87.  
  88.  
  89.     return 0;
  90. }
Advertisement
RAW Paste Data Copied
Advertisement