Sponsored Content
Top Forums Shell Programming and Scripting Troubles with pipes, fork, and dup2 Post 302872819 by dano88 on Sunday 10th of November 2013 07:42:36 AM
Old 11-10-2013
Troubles with pipes, fork, and dup2

I want to execute metasploit by two pipes to communicate with it, but I have troubles with that communication. When I run my program, I get this error: "stty: standard input: Inappropriate ioctl for device" and I don't receive the metasploit promt.
just select an exploit.
This is my code:


Code:
#include <stdio.h>
 #include <stdlib.h>
 #include <string.h> 
#include <errno.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <unistd.h> 
#include <iostream> 

 int main(int argc, char** argv) { 

     int pipeIn[2];    
     int pipeOut[2];     
     pipe(pipeIn);
     pipe(pipeOut);
     pid_t hijo=fork();

     if (hijo==0) {
         dup2(pipeIn[0], STDIN_FILENO);
         dup2(pipeOut[1], STDOUT_FILENO);
         dup2(pipeOut[1], STDERR_FILENO); 
         close(pipeIn[0]);         close(pipeOut[1]);
          close(pipeIn[1]);         close(pipeOut[0]); 

        char* argv[]={"msfconsole", NULL};
         execvp("msfconsole", argv);

     }else if (hijo==-1) { 
        perror("fork");         exit(EXIT_FAILURE);     

} else{//padre  
       close(pipeIn[0]);         close(pipeOut[1]); 

        char buf[1024];
         int count;
           bool b=true;
         int ccount=0;
          for (;b;){
             while (b && (count=read(pipeOut[0], buf, sizeof(char) * 1024))>0){
                 buf[count]='\0'; 
                 printf ("%s", buf);
                 if (strstr(buf, "msf >")!= 0){
                      write(pipeIn[1], "use exploit/windows/fileformat/adobe_pdf_embedded_exe_nojs \n\0 ", sizeof(char)*1024);
                     if (ccount++>=1){ printf ("encontrado"); b=false; }
             } }
          }
               close(pipeIn[1]); 
              close(pipeOut[0]);
           }}


It shows this:
Code:
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMM                MMMMMMMMMM
MMMN$                           vMMMM
MMMNl  MMMMM             MMMMM  JMMMM
MMMNl  MMMMMMMN       NMMMMMMM  JMMMM
MMMNl  MMMMMMMMMNmmmNMMMMMMMMM  JMMMM
MMMNI  MMMMMMMMMMMMMMMMMMMMMMM  jMMMM
MMMNI  MMMMMMMMMMMMMMMMMMMMMMM  jMMMM
MMMNI  MMMMM   MMMMMMM   MMMMM  jMMMM
MMMNI  MMMMM   MMMMMMM   MMMMM  jMMMM
MMMNI  MMMNM   MMMMMMM   MMMMM  jMMMM
MMMNI  WMMMM   MMMMMMM   MMMM#  JMMMM
MMMMR  ?MMNM             MMMMM .dMMMM
MMMMNm `?MMM             MMMM` dMMMMM
MMMMMMN  ?MM             MM?  NMMMMMN
MMMMMMMMNe                 JMMMMMNMMM
MMMMMMMMMMNm,            eMMMMMNMMNMM
MMMMNNMNMMMMMNx        MMMMMMNMMNMMNM
MMMMMMMMNMMNMMMMm+..+MMNMMNMNMMNMMNMM
        http://metasploit.pro


       =[ metasploit v4.7.2-1 [core:4.7 api:1.0]
+ -- --=[ 1211 exploits - 733 auxiliary - 202 post
+ -- --=[ 317 payloads - 30 encoders - 8 nops

stty: standard input : Inappropriate ioctl for device
stty: standard input : Inappropriate ioctl for device
stty: standard input : Inappropriate ioctl for device
stty: standard input : Inappropriate ioctl for device
stty: standard input : Inappropriate ioctl for device
stty: standard input : Inappropriate ioctl for device
stty: standard input : Inappropriate ioctl for device
^C

 

10 More Discussions You Might Find Interesting

1. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies

2. UNIX for Advanced & Expert Users

Interprocess communication using pipes and fork

I'm very worried. I have an assignment that is due in 3 weeks, and also tute exercises which I can't seem to understand and work out. Okay, the question: The parent process will convert the command arguments into integer values using atoi() and store them into an integer array which you will... (2 Replies)
Discussion started by: scmay
2 Replies

3. Programming

Understanding the purpose of dup/dup2

I'm having difficulty understanding the purposes of using dup/dup2 when involving forks. for example, if we call fork() once, that is, we are creating a child process. In what cases would we need to use dup or dup2 to duplicate the file descriptors for standard output and standard error? What... (1 Reply)
Discussion started by: Yifan_Guo
1 Replies

4. Programming

a issue with dup2

Hi, i have in one program such a pice of code ................ static int old_stderr_handle = -1; static int old_stdout_handle = -1; log_handle = open(log_file_name,O_CREAT|O_RDWR,932); old_stderr_handle = dup(STDERR_FILENO); if (dup2(log_handle,STDERR_FILENO) < 0) { //... (1 Reply)
Discussion started by: vovan
1 Replies

5. UNIX for Advanced & Expert Users

dup2 filedescriptor redirecting output

int redirect() { int fd,rc; fd = open("sample.DAT",O_CREAT | O_RDWR , 00777 ); rc = dup2(fd , 1 ) ; close (fd ); return 0; } I used the above to redirect all the cout statements to sample.DAT. process is redirecting the output and I had two questions 1. All stdout/cout statements... (2 Replies)
Discussion started by: satish@123
2 Replies

6. Programming

Implementation of dup2

Hi all,I'm reading <Advanced programming in the UNIX environment>,that book asked the reader to implement a function which has same functions with dup2 without calling fcntl.Could anyone give me a tip?Any help will be appreciated.:) (8 Replies)
Discussion started by: homeboy
8 Replies

7. Programming

C++ socket, fork & pipes

Hello, I'm stuck and this is a matter which I need to resolve quite fast (but I couldn't post in the "Emergency" section); the problem is this : I have created a chat program in which the client sends the sentence to the server and then the server should send it to all the clients connected,... (2 Replies)
Discussion started by: timmyyyyy
2 Replies

8. Programming

C, unix, pipes, fork, recursion

Hi, I will try to keep my post as compressed as my title was. I am writing on pseudo code on a recursive function that I want to read from the one-above function-run and then give the result to the function-run down below until a stop is triggered. Example: $ ls -la | grep x | sort In my... (2 Replies)
Discussion started by: tarasque
2 Replies

9. UNIX for Advanced & Expert Users

Dup2 - for file descriptor opened by a different process

is it possible to duplicate file descriptors(opened by a different process) with the help of dup or dup2. the two process do not share parent child relationship as well. (2 Replies)
Discussion started by: replytoshishir
2 Replies

10. Programming

C++ stuck with dup2 and pipe

What this code should do is: there are parent.cpp and child.cpp. Parent will send whatever is in the buffer to child and child will send back whatever received to parent. I do not know what I am doing wrong. I am confused what is missing in the parent and what else I should include into the child.... (1 Reply)
Discussion started by: ramono
1 Replies
X3270IF(1)						      General Commands Manual							X3270IF(1)

NAME
x3270if - command interface to x3270, c3270 and s3270 SYNOPSIS
x3270if [ -v ] action [ ( param [ ,... ] ) ] x3270if [ -v ] -s field x3270if -i DESCRIPTION
x3270if provides an interface between scripts and the 3270 emulators x3270, c3270, and s3270. x3270if performs one of three functions. In action mode (the default), it passes a single action and optional parameters to the emulator for execution; the result of the command is written to standard output. In status mode, it reports to standard output the value of one of the emulator's status fields. In iterative mode, it forms a continuous conduit between a script and the emulator. OPTIONS
-v Turns on verbose debug messages, showing the literal data that is passed between the emulator and x3270if. -s field Puts x3270if in status mode: it outputs the value of an emulator status field. Field is an integer in the range 0 through 11. The value 0 is a no-op and is used only to return exit status indicating the state of the emulator. The indices 1-11 and meanings of each field are documented on the x3270-script(1) manual page. -i Puts x3270if in iterative mode. Data from x3270if's standard input is copied to the emulator's script input; data from the emula- tor's script output is copied to x3270if's standard output. x3270if runs until it detects end-of-file on its standard input or on the output from the emulator. (This mode exists primarily to give expect(1) a process to run, on systems which do not support bidi- rectional pipes.) EXIT STATUS
In action mode, if the requested action succeeds, x3270if exits with status 0. If the action fails, x3270if exits with status 1. In iter- ative mode, x3270if exits with status 0 when it encounters end-of-file. If there is an operational error within x3270if itself, such as a command-line syntax error, missing environment variable, or an unexpectedly closed pipe, x3270if exits with status 2. ENVIRONMENT
x3270, c3270, and s3270 use a pair of pipes for communication with each child process. The values of the file descriptors for these pipes are encoded as text in two environment variables, which are required by x3270if: X3270OUTPUT Output from the emulator, input to the child process. X3270INPUT Input to the emulator, output from the child process. SEE ALSO
x3270(1), c3270(1), s3270(1), x3270-script(1) COPYRIGHT
Copyright 1999, 2000 by Paul Mattes. Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. 01 May 2002 X3270IF(1)
All times are GMT -4. The time now is 03:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy