1

I have this script on my netcat server which asks for name and some other information:

*echo "Tell me your name"                                                    
 read $ln                                                                    
 echo "I got '$ln'"                                                          
 echo "Tell me something more"                                              
 while read ln; do                                                           
    echo "I got '$ln'"                                                       
    echo "Tell me something more"                                           
 done*

When a client connects to this server, I want the script to communicate with the client directly. On server end I do : while true; do nc -l -p port-no | ./My-script-file ; done

The while loop is just so that the server continues listening even after one client has closed the connection and nothing else. But somehow, I cannot get the queries to appear on client side.

On client side I do: nc server-ip port-no

I want the lines "Tell me your name", "I got..." and "Tell me something else" to appear on client screen and the input from client end to be fed into the script.

I have also tried options like --exec, -e and --sh-exec and the errors I am getting are something like

nc: invalid option -- '-'
usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl]
      [-m minttl] [-O length] [-P proxy_username] [-p source_port]
      [-q seconds] [-s source] [-T keyword] [-V rtable] [-W recvlimit] [-w timeout]
      [-X proxy_protocol] [-x proxy_address[:port]]       [destination] [port]
3

It seems Open-BSD netcat does not support -e or --exec commands which helps us to execute a file after a connection has been made. Since it is might cause the remote machine to run potentially harmful command to the connected machine, these commands are considered as dangerous.
As a work around I just uninstalled Open-BSD netcat and installed traditional-netcat server following the solution in the site https://stackoverflow.com/questions/10065993/how-to-switch-to-netcat-traditional-in-ubuntu. This gave me access to the -exec command. The command that I run on server is : nc -l -p port-number -e File-Script-to-execute

1

Ok, so here I've set up a small example:

#!/bin/bash

while read -p 'Tell me your name: ' ln;
  do 
    echo "I got $ln";
  done

So you save the script make it executable and run it as follows:

On the server

nc -l -p 4444 -k --sh-exec ./yourscript

Here -k does keep the connection open so no need for the loop you got there.

On the client

nc hostname 4444
foo bar

This should do the job.

  • I have tried using the --exec command before and it didn't work. I tried your command and this is what I got: root@kali:~/Desktop# nc -l -p 4444 -k --sh-exec ./Test-Script nc: invalid option -- '-' usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl] [-m minttl] [-O length] [-P proxy_username] [-p source_port] [-q seconds] [-s source] [-T keyword] [-V rtable] [-W recvlimit] [-w timeout] [-X proxy_protocol] [-x proxy_address[:port]] [destination] [port] root@kali:~/Desktop# – Shritama Sengupta Dec 21 '18 at 18:53
  • @ShritamaSengupta what version of netcat are you using? Does nc --help show you the option --sh-exec ? – Valentin Bajrami Dec 21 '18 at 18:56
  • I got this:nc -h OpenBSD netcat (Debian patchlevel 1.195-1) – Shritama Sengupta Dec 21 '18 at 19:00
  • @ShritamaSengupta your nc doesn't support the exec flag the. There is this answer here but it uses named pipes superuser.com/questions/691008/… – Valentin Bajrami Dec 21 '18 at 19:26
  • I just installed the traditional-netcat instead and accessed the -exec command. This seemed as the easier way to achieve my task. Thank you for your hint though. – Shritama Sengupta Dec 21 '18 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.