Python for Cybersecurity 03 — Networking, Python in Kali, Sockets & TCP Client

Yen
2 min readSep 2, 2023

--

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
Just the default desktop on Kali Linux. But I totally agree with it.

Prerequisite

Concepts of Networking

Difference between TCP& UDP | FreeCodeCamp

source image from wikipedia

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 key
  • sockets andsocket APIare 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

User Datagram Protocol (UDP)

  • Not reliable
  • Data read by the receiver can be out-of-order from the sender’s writes

— Real Python

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_INETparameter indicates we’ll use a standard IPv4 address or hostname
  • AF_INETparameter 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

--

--

Yen

Programming is a mindset. Cybersecurity is the process. Focus on Python & Rust currently. More about me | https://msha.ke/monles