13

Is it possible to 'pipe' an instance of a console application through netcat, so netcat is listening for a new connection and redirects the stdin and stdout over the network connection.

8
  • yeah but it might have to be a one liner, which is fine, pipes are so. I don't know whether you can tell nc and pipe to an already running instance by pid. you can nc to an ip and pipe it to an nc that is listening and pipe that to an nc that is listening etc and pipe that to nc to an ip.
    – barlop
    Jun 14, 2013 at 19:02
  • I changed the title because what you mention sounds more like I/O redirection rather than the use of a pipe (which only deals with moving the output of one process to the input of another). Jun 14, 2013 at 19:40
  • @Breakthrough how does that differ from pipes? as he's talking about IO redirection and not talking about files, then how does it differ from piping?
    – barlop
    Jun 14, 2013 at 21:39
  • @barlop I/O redirection (< or >) is significantly different from piping (|), although you can sometimes achieve an equivalent operation with a mix of both. Again though, with I/O redirection, you can change the location of both streams; piping just redirects the standard output of one process to the standard input of another. Jun 14, 2013 at 21:43
  • @Breakthrough see what I asked you though. > and < is of course different from piping because < and > use files. But he's not talking of files. I asked you "how does that[IO redirection] differ from pipes? as he's talking about IO redirection and not talking about files, then how does it differ from piping? "
    – barlop
    Jun 14, 2013 at 21:45

1 Answer 1

16

Well, as documented in Wikipedia and netcat documentation, there is a -e option that causes it to spawn (execute) a program upon receipt of a connection, attaching the socket to stdin, stdout, and stderr of the process.  Example usage:

nc -l -pport_number-eprogram_name

Examples commonly show /bin/sh or bash being used as the program_name.  Use of this option is discouraged because it basically opens an anonymous, passwordless access portal into your machine.  Of course, this is mitigated by using a program with less power than the shell (one that doesn’t have the ability to escape to a shell), minimizing your use of it, and keeping it a secret.  Nonetheless, the original developers of netcat felt strongly enough that this option was a bad idea that they disabled it by default, and conditioned it under the “GAPING_SECURITY_HOLE” compilation option.  This is mentioned briefly in this NetCat Tutorial and other netcat documentation.

A Google search led me to discussions of this issue on other Stack Exchange sites: Stack Overflow and Server Fault.  Multiple contributors offered the following technique to do the same thing without using the -e option (i.e., in a version of netcat that has the -e option disabled):

On the server:
mkfifopipe_name
nc -l -pport_number<pipe_name  | program_name>pipe_name

On the client:
nc server_machine_name port_number

A couple of notes:

  • On some versions of netcat, -l implies -p, so you should say just -l and then the port number.
  • You might want to wrap your solution in a while true loop.
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .