Advertisement
lokotochek

Untitled

Mar 30th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <fcntl.h>
  8.  
  9. extern char** environ;
  10.  
  11. int main() {
  12.    
  13.     int input_pipes[2];
  14.     if (pipe(input_pipes) < 0){
  15.         printf("can't open pipe");
  16.         exit(1);
  17.     }
  18.    
  19.     char* bufToWrite = (char*)malloc(1024*1024);
  20.     strcpy(bufToWrite, "http://yandex.ru");
  21.     if (write(input_pipes[1], &bufToWrite, sizeof(bufToWrite)) != sizeof(bufToWrite)){
  22.         printf("can't write into pipe");
  23.         exit(1);
  24.     }
  25.    
  26.     char* bufToRead = (char*)malloc(1024*1024);
  27.     read(input_pipes[0], &bufToRead, sizeof(bufToRead));
  28.    
  29.     char* fifoName = (char*)malloc(255);
  30.     strcpy(fifoName, "bashFifo");
  31.    
  32.     unlink(fifoName);
  33.     if (mkfifo(fifoName, O_RDWR | O_CREAT | S_IRWXU | O_NONBLOCK) < 0){
  34.         printf("can't create bash fifo");
  35.         exit(1);
  36.     }
  37.    
  38.     char* query = (char*)malloc(1024*1024);
  39.     strcpy(query, "curl ");
  40.     strcat(query, bufToRead);
  41.     strcat(query, " >");
  42.     strcat(query, fifoName);
  43.  
  44.     char* args[] = {"/bin/bash", "-c", query, NULL};
  45.  
  46.     pid_t pid = fork();
  47.     if (pid == 0){
  48.         execv(args[0], args);
  49.         printf("error in exec process"); // successful exec shouldn't be here
  50.     }
  51.  
  52.     char* readFromBash = (char*)malloc(256);
  53.     int bashFD;
  54.     if ((bashFD = open(fifoName, O_RDONLY)) < 0){
  55.         printf("error opening bash fifo");
  56.         exit(1);
  57.     }
  58.     read(bashFD, &readFromBash, 256);
  59.     printf("\n--------\n%s", readFromBash);
  60.     close(bashFD);
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement