This is a classic use of netcat
. But this is unix.SE
so my answer will be completely in unix.
Note: netcat
has different names on different distros:
netcat
: alias to nc
on some distros
nc
: GNU netcat on linux or BSD netcat on *BSD
ncat
: Nmap netcat, consistent on most systems
Options between different versions of netcat
vary, I'll point out where different version may behave differently. Moreover, I strongly recommend installing the nmap version of netcat (ncat
) since its command line options are consistent across different systems.
I'll be using ncat
as the netcat name thorough the answer.
TCP
To use TCP to control a machine through netcat
you have two options: using a named pipe (which works with all versions of netcat) and using -e
(which only exists in the linux version, or, more exactly, -e
on *BSD does something completely different).
On the server side you need to perform either:
mkfifo pinkie
ncat -kl 0.0.0.0 4096 <pinkie | /bin/sh >pinkie
Where: 0.0.0.0
is the placeholder for "all interfaces", use a specific IP to limit it to a specific interface; -l
is listen and -k
keep open (to not terminate after a single connection).
Another option (on linux/ncat) is to use:
ncat -kl 0.0.0.0 4096 -e /bin/sh
To achieve the same result.
On the client side you can use your app or simply perform:
ncat <server ip> 4096
And you are in control of the shell on the server, and can send commands.
UDP
UDP is similar but has some limitations. You cannot use -k
for the UDP protocol without -e
, therefore you need to use the linux/ncat to achieve a reusable socket.
On the server side you do:
ncat -ukl 0.0.0.0 4096 -e /bin/sh
And on the client side (or from your app):
ncat -u <server ip> 4096
And once again you have a working shell.