Advertisement
bueddl

Untitled

Nov 1st, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.36 KB | None | 0 0
  1. /* Sebastian Büttner <sebastian.buettner@iem.thm.de>
  2.  * GNU GPLv3
  3.  */
  4.  
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <signal.h>
  8. #include <fcntl.h>
  9.  
  10. #define READ_FD pipefd[0]
  11. #define WRITE_FD pipefd[1]
  12. #define PHP "php"
  13. #define BUFFER_SIZE 0x200
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17.     pid_t child_pid;
  18.     int pipefd[2], fd, bytes_read, total_bytes_read, bytes_to_read,
  19.         bytes_executed, total_bytes_executed,
  20.         stop = 0, match = 1;
  21.     char *filename;
  22.     char buffer_read[BUFFER_SIZE], buffer_execute[BUFFER_SIZE];
  23.  
  24.     if (argc < 2) {
  25.         dprintf(STDERR_FILENO, "Usage: phpce file\n");
  26.         return 1;
  27.     }
  28.     filename = argv[1];
  29.  
  30.     if (pipe(pipefd) < 0) {
  31.         perror("Unable to create pipe");
  32.         return 1;
  33.     }
  34.  
  35.     if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) == -1) {
  36.         perror("fcntl()");
  37.         return 1;
  38.     }
  39.  
  40.     switch (child_pid = fork()) {
  41.         case 0:
  42.             /* Child */
  43.             close(READ_FD);
  44.             dup2(WRITE_FD, STDOUT_FILENO);
  45.             close(WRITE_FD);
  46.             execlp(PHP, PHP, "-f", filename, NULL);
  47.             break;
  48.  
  49.         default:
  50.             /* Parent */
  51.             close(WRITE_FD);
  52.             if ((fd = open(filename, O_RDONLY)) < 0) {
  53.                 perror("Unable to open file for reading");
  54.                 kill(child_pid, SIGKILL);
  55.                 return 1;
  56.             }
  57.  
  58.             total_bytes_read = 0;
  59.             total_bytes_executed = 0;
  60.             while (!stop) {
  61.                 /* Read a chunk from the php process */
  62.                 if ((bytes_executed = read(READ_FD, buffer_execute, BUFFER_SIZE)) > 0)
  63.                 {
  64.                     total_bytes_executed += bytes_executed;
  65.                     /* Read as many bytes as we got through the pipe in last invocation */
  66.                     bytes_to_read = bytes_executed;
  67.                     do {
  68.                         if ((bytes_read = read(fd, buffer_read, bytes_to_read)) > 0)
  69.                         {
  70.                             total_bytes_read += bytes_read;
  71.                             bytes_to_read -= bytes_read;
  72.                         } else
  73.                         {
  74.                             /* End of file */
  75.                             stop = 1;
  76.                             break;
  77.                         }
  78.                     } while (bytes_to_read);
  79.  
  80.                     /* Compare both chunks */
  81.                     if (memcmp(buffer_execute, buffer_read, bytes_executed) == 0)
  82.                         continue;
  83.  
  84.                     /* Outputs do not match! */
  85.                     match = 0;
  86.                     break;
  87.                 } else if (bytes_executed == 0) {
  88.                     break;
  89.                 }
  90.             }
  91.             kill(child_pid, SIGKILL);
  92.             break;
  93.  
  94.         case -1:
  95.             /* Fatal */
  96.             perror("Unable to fork");
  97.             break;
  98.     }
  99.  
  100.     if (!match)
  101.         dprintf(STDERR_FILENO, "Mismatch detected near offset %*X (%d bytes chunk)!\n",
  102.                 total_bytes_executed-bytes_executed, bytes_executed);
  103.  
  104.     return match == 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement