Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Sebastian Büttner <sebastian.buettner@iem.thm.de>
- * GNU GPLv3
- */
- #include <unistd.h>
- #include <sys/types.h>
- #include <signal.h>
- #include <fcntl.h>
- #define READ_FD pipefd[0]
- #define WRITE_FD pipefd[1]
- #define PHP "php"
- #define BUFFER_SIZE 0x200
- int main(int argc, char *argv[])
- {
- pid_t child_pid;
- int pipefd[2], fd, bytes_read, total_bytes_read, bytes_to_read,
- bytes_executed, total_bytes_executed,
- stop = 0, match = 1;
- char *filename;
- char buffer_read[BUFFER_SIZE], buffer_execute[BUFFER_SIZE];
- if (argc < 2) {
- dprintf(STDERR_FILENO, "Usage: phpce file\n");
- return 1;
- }
- filename = argv[1];
- if (pipe(pipefd) < 0) {
- perror("Unable to create pipe");
- return 1;
- }
- if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) == -1) {
- perror("fcntl()");
- return 1;
- }
- switch (child_pid = fork()) {
- case 0:
- /* Child */
- close(READ_FD);
- dup2(WRITE_FD, STDOUT_FILENO);
- close(WRITE_FD);
- execlp(PHP, PHP, "-f", filename, NULL);
- break;
- default:
- /* Parent */
- close(WRITE_FD);
- if ((fd = open(filename, O_RDONLY)) < 0) {
- perror("Unable to open file for reading");
- kill(child_pid, SIGKILL);
- return 1;
- }
- total_bytes_read = 0;
- total_bytes_executed = 0;
- while (!stop) {
- /* Read a chunk from the php process */
- if ((bytes_executed = read(READ_FD, buffer_execute, BUFFER_SIZE)) > 0)
- {
- total_bytes_executed += bytes_executed;
- /* Read as many bytes as we got through the pipe in last invocation */
- bytes_to_read = bytes_executed;
- do {
- if ((bytes_read = read(fd, buffer_read, bytes_to_read)) > 0)
- {
- total_bytes_read += bytes_read;
- bytes_to_read -= bytes_read;
- } else
- {
- /* End of file */
- stop = 1;
- break;
- }
- } while (bytes_to_read);
- /* Compare both chunks */
- if (memcmp(buffer_execute, buffer_read, bytes_executed) == 0)
- continue;
- /* Outputs do not match! */
- match = 0;
- break;
- } else if (bytes_executed == 0) {
- break;
- }
- }
- kill(child_pid, SIGKILL);
- break;
- case -1:
- /* Fatal */
- perror("Unable to fork");
- break;
- }
- if (!match)
- dprintf(STDERR_FILENO, "Mismatch detected near offset %*X (%d bytes chunk)!\n",
- total_bytes_executed-bytes_executed, bytes_executed);
- return match == 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement