Python for Cybersecurity 03 — Networking, Python in Kali, Sockets & TCP Client
Sharpen skills of Python in cybersecurity field.
This series of tutorials is mainly to share the notes taken while I was learning python for cybersecurity with the books — Black Hat Python.
There are some reference links in section of References below.
Catalogue
- Concepts of Networking
- Python in Kali
- Sockets
- TCP Client
Concepts of Networking
Difference between TCP& UDP | FreeCodeCamp
Python in Kali
Python on Kali Linux: A beginner’s guide | infoseccout.com
🔸Run the python file
Just follow the commands below. This example shows how to run a python file named test.
python test.py
🔸Install packages
For example, let’s install Nmap.
sudo apt install python3-nmap
Sockets
🔸Core Module
sockets
is the keysockets
andsocket API
are used to send message across a network
More stories of sockets | Real Python
🔸TCP
Transmission Control Protocol (TCP), which is
- Is reliable: Packets dropped in the network are detected and retransmitted by the sender.
- Has in-order data delivery: Data is read by your application in the order it was written by the sender.
🔸UDP
- Not reliable
- Data read by the receiver can be out-of-order from the sender’s writes
Build Client & Server
🔸TCP Client
# test.py
import socket
HOST = "www.google.com"
PORT = 80
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((HOST, PORT))
client.send(b"GET / HTTP/1.1\r\nHOST: google.com\r\n\r\n")
response = client.recv(4096)
print(response.decode())
client.close()
AF_INET
parameter indicates we’ll use a standard IPv4 address or hostnameAF_INET
parameter indicates this will be a TCP client- Connect client to the server
- Then we send it some data
- Receive some data back
- print out response