TCP and UDP clients with Python
Created On 26. Jan 2020
Updated: 2022-05-01 23:29:39.970344000 +0000
Created By: acidghost
Hacking with ready-to-go tools is cool, but just acting big with all predefined automation without knowing how things work won't bring us far.
In a vocabulary of every security elite, at least some knowledge of some scripting is required. Python is a kind offender for most newbies.
Below we are building a simple client that will try to connect to google and get a response.
Be aware this might look simple and sharp, but there is a lot wrong with how we are formulating it. It is fine, for our first go we don't need to complicate things.
import socket target_host = www.google.com target_port = 80 create a socket object use socket.SOCK_DGRAM instead for UDP client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) connect the client client.connect((target_host,target_port)) send some data use sendto() instead for UDP client.send("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n") receive some data response = client.recv(4096) print response
Check below more on sockets which will be more heavily covering later.
A socket can be created by making a call to the class method
socket() in the socket module. This will return a socket in the domain specified.
The parameters to the method are as follows.
Python supports three address families:
AF_INET: Used for IP version 4 or IPv4 Internet addressing.
AF_INET6: Used for IPv6 Internet addressing.
AF_UNIX: Used for UNIX domain sockets (UDS).
Socket type: Usually, socket type can be either
SOCK_DGRAM for User Datagram Protocol (UDP) or
SOCK_STREAM for Transmission Control Protocol (TCP).
SOCK_RAW is used to create raw sockets.
Protocol: Generally left at the default value. Default value is 0.
The socket module has the following class methods:
socket.socket(family, type): Create and return a new socket object
socket.getfqdn(name): Convert a string IP address to a fully qualified domain name
socket.gethostbyname(hostname): Resolve a hostname to an IP address Instance methods require a socket instance returned from socket.
The socket module has the following instance methods:
sock.bind( (address, port) ): Bind the socket to the address and port
sock.accept(): Return a client socket with peer address information
sock.listen(backlog): Place the socket into the listening state
sock.connect( (address, port) ): Connect the socket to the defined host and port
sock.recv( bufferLength[, flags] ): Receive data from the socket, up to
buflen (maximum bytes to receive)
sock.recvfrom( bufferLength[, flags] ): Receive data from the socket, up to
buflen bytes, also returning the remote host and port from which the data came
sock.send( data[, flags] ): Send data through the socket
sock.sendall( data[, flags] ): Send data through the socket, and continues to send data until either all data has been sent or an error occurred
sock.close(): Close the socket
sock.getsockopt( lvl, optname ): Get the value for the specified socket option
sock.setsockopt( lvl, optname, val ): Set the value for the specified socket option
Remember this and you will become a python socket sorcerer in no time