TCP and UDP Servers: The First Step in My Cybersecurity Project Journey
For my journey into cybersecurity projects, I started by diving into the foundational concepts of network communication: TCP (Transmission Control Protocol) and UDP (User Datagram Protocol). These protocols form the backbone of networking, and I'm excited to share my experience of setting up TCP and UDP servers as the first step in building my cybersecurity sandbox.
TCP and UDP
As I began tinkering with TCP and UDP servers, I further realized their significance in understanding how data flows across networks. TCP, with its reliable and ordered delivery, felt like having a structured conversation, while UDP's lightweight and unordered approach was more like shooting off quick messages. This diversity allows me to simulate various cyber threats and defenses, to enhance my cybersecurity skills.
Setting Up TCP
TCP Server Example in Python:
#!/usr/bin/python3
import socket
# Create socket object
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# AF_INET is IPv4
# SOCK_STREAM is TCP
# Bind socket to host and port
host = '127.0.0.1' # localhost
port = 8010 # Any available port
server_socket.bind((host, port))
# Listen for incoming connections
server_socket.listen(5) # Allows up to 5 connections
print("Server is listening on port ", port)
while True:
# Accept connections from client
client_socket, addr = server_socket.accept()
print(addr, " connected to the server")
# Receive data from client
data = client_socket.recv(1024).decode('utf-8')
print("Received: ", data)
# Respond to client
response = "Hello from server!"
client_socket.send(response.encode('utf-8'))
# Close client socket
client_socket.close()
This TCP server sets up a socket, binds it to a specific address and port, listens for incoming connections, and accepts them. It then receives messages from the connected client, prints them, and sends the client a response.
TCP Client Example in Python:
#!/usr/bin/python3
import socket
# Socket object
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# AF_INET for IPv4
# SOCK_STREAM for TCP
# connect to server
host = '127.0.0.1' # localhost
port = 8010 # same port as server
client_socket.connect((host, port))
while True:
# Input message from user
message = input('Send message to server (type "quit" to exit): ')
# Send message to server
client_socket.send(message.encode('utf-8'))
#check if user wants to quit
if message.lower() == 'quit' or message.lower == 'q':
print("Closing connection...")
break
# Receive server response
response = client_socket.recv(1024).decode('utf-8')
print("Received: ", response)
client_socket.close()
This TCP client sets up a socket and connects it to the server. It then continuously prompts the user for messages, sends them to the server, receives responses, and prints them.
TCP Server and Client Code
Setting Up UDP
UDP Server Example in Python:
#!/usr/bin/python3
import socket
# Create socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# AF_INET means use IPv4
# SOCK_DGRAM means use UDP
# Bind socket to host and port
host = '127.0.0.1' # localhost
port = 8010 # any available port
server_socket.bind((host, port))
print("Server is listening on port", port)
while True:
# Receive data from client
data, addr = server_socket.recvfrom(1024)
print("Received: ", data.decode('utf-8'))
# Respond to client
response = "Hello from server!"
server_socket.sendto(response.encode('utf-8'), addr)
This UDP server sets up a socket, binds it to a specific address and port, and then waits for messages from clients. When a message is received, the server prints it and sends a response back to the client.
UDP Client Example in Python:
#!/usr/bin/python3
import socket
# Create socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Define address and port
server_address = ('127.0.0.1', 8010)
while True:
# Get message from user
message = input("Enter message to server ('quit' to exit): ")
# Send message to server
client_socket.sendto(message.encode('utf-8'), server_address)
# Check for quit
if message.lower() == 'quit' or message.lower() == 'q':
print("Closing connection...")
break
# Receive response from server
response, addr = client_socket.recvfrom(1024)
print("Received: ", response.decode('utf-8'))
This UDP client sets up a socket and continuously prompts the user for messages. It sends each message to the server and prints the response received from the server.
UDP Server and Client Code
Moving Forward
This exploration into TCP and UDP servers is just the beginning of my cybersecurity project journey. From here, I plan to expand my sandbox, delve into chat servers, encryption, and various security tools to learn about both attacks and defensive techniques.
Conclusion
TCP and UDP servers are serving as my foundation for understanding network communication in cybersecurity. Through hands-on experimentation with TCP and UDP servers, I'm gaining invaluable insights into how data moves across networks and laying the groundwork for future cybersecurity projects.