Netcat in Python: Step 1: How Do We Begin?
Netcat in Python: Step 1: How Do We Begin?
Netcat in Python: Step 1: How Do We Begin?
Netcat in Python
by swarajdh
What is netcat? The manual page for netcat says the In essence, netcat allows you to connect to other
following: "the nc (or netcat) utility is used for just servers using the TCP or UDP protocol. TCP stands
about anything under the sun involving TCP, UDP, or for Transmission Control Protocol, and is connection
UNIX-domain sockets. It can open TCP connections, oriented. UDP stands for Universal Datagram
send UDP packets, listen on arbitrary TCP and UDP Protocol, and is connectionless. TCP is commonly
ports, do port scanning, and deal with both IPv4 and used for internet applications, while UDP is used for
IPv6. Unlike telnet(1), nc scripts nicely, and separates media streaming or VPNs.
error messages onto standard error instead of
sending them to standard output, as telnet(1) does
with some"
Above is how netcat is called. You can see that there are two arguments at the end called "destination" and "port."
The destination refers to a hostname or ip address of the server we are trying to connect to, while the port refers to
the port of the server we are trying to connect to.
Above is some beginning python code. As you can see, we want to process the arguments to the program
similarly to how the actual utility does. The hostname will be the first argument after the executable's name, while
the port will be the second argument after the executable's name in the command line.
Let's create a netcat function we can use. What we are basically doing here is creating a socket and connecting to
the server using the parameters given. For the netcat command, the current parameters are the hostname and port
of the server that we are attempting to connect to. The socket contains the parameters "socket.AF_INET" and
"socket.SOCK_STREAM" because we are defaulting to a TCP connection for this tutorial.
The above code (which is located below the code our request
from the previous section) simply allows us to run
multple netcat commands over a pseudo-open Line 36-45: We will read into the buffer until we read
connection. (In reality, each time you run a command, an empty line
it opens and then closes a new TCP connection, so it
doesn't truly emulate the behavior of netcat, we are Line 48: we simply call our netcat function with the
simply doing this for learning purposes). Lets break hostname, port, and newly created content (which is
this down line by line as well: properly encoded)
Line 31: We want to read commands indefinitely in Line 50: if the content of our buffer ever contains
order to maintain "interactiveness" "Connection: Close" (indicating we want to close the
connection), we simply break out of the loop
Line 32: This is our buffer that will store the content of
Step 6: Conclusion
At the end of this tutorial you should have a minimal working netcat implementation. I shall leave it as an exercise
to the user to implement features such as: