Table of Contents

Lab 08 - Network Security

Objectives

Preparation

Please spawn a virtual machine on openstack.

You need to prepare the playground:

echo "<h1>Hello, I am "<insert your 31337 name here>" </h1>" | sudo tee /var/www/html/index.html
# you may also append a slogan (choose one or make your own):
echo '<h3>All your base are belong to us!</h3>' | sudo tee -a /var/www/html/index.html
echo '<h3>We will rock you!</h3>' | sudo tee -a /var/www/html/index.html
echo '<h3>Am talent și nu mă las!</h3>' | sudo tee -a /var/www/html/index.html
sudo passwd student
# set an unbreakable password, but make sure you won't forget it until the end of the lab!
# let the h4x0rs in (enables the account):
sudo usermod -e -1 -U hacker

Leave hacker's password unchanged!

No trolling / spamming yet, please!

Tasks

[30p] 1. Port Scanning

Read here about port scanning techniques.

For the following exercises, you should be working on an OpenStack VM, scanning for the VMs of your colleagues.

[10p] Task A: Network scan

[10p] Task B: TCP / UDP port scans

[10p] Task C: OS / Version scans

[30p] 2. Iptables

Iptables is an interface to the Netfilter firewall that is built into the Linux kernel. It provides an administrator with an interface to add, remove, and modify packet rules.

Here's a iptables cheeatsheet to help you get started.

[5p] Intro: Best practices

Here are some best practices when writing firewall rules (read them):

Try to not get locked out of ssh-ing your virtual machine! Double-check the rule before you add it to a iptables chain (always ask yourself: does it match a broad range of packets / will it filter my SSH traffic from fep)!

[10p] Task B: Server Firewall

If you want to turn off wall messages being displayed on your tty, check out man mesg command!

But beware when nesting shells! (e.g., from student to root using su). In this case, you must exit them all and issue mesg on the first one (the tty spawned by ssh!).

[5p] Task C: Workstation Firewall

Workstation Firewall Script

Workstation Firewall Script

# Remove any existing rules from all chains
iptables --flush
 
# Allow traffic on the loopback interface
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
 
# Allow SSH traffic
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
 
# Accept any related or established connections
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
 
# Set the default policy to drop
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
 
# Outbound DNS lookups
iptables -A OUTPUT -o ens3 -p udp -m udp --dport 53 -j ACCEPT
 
# Allow outbound SSH
iptables -A OUTPUT -o ens3 -p tcp -m tcp --dport 22 -m state --state NEW -j ACCEPT
 
# Outbound PING requests
iptables -A OUTPUT -o ens3 -p icmp -j ACCEPT
 
# Outbound Network Time Protocol (NTP) requests
iptables -A OUTPUT -o ens3 -p udp --dport 123 --sport 123 -j ACCEPT
 
# Outbound HTTP and HTTPS
iptables -A OUTPUT -i ens3 -p tcp -m tcp --dport 80 -m state --state NEW -j ACCEPT
iptables -A OUTPUT -i ens3 -p tcp -m tcp --dport 443 -m state --state NEW -j ACCEPT

[10p] Task D: DNS blocking

[0p] Task E: Port knocking (bonus)

[30p] 3. Man in the Middle

The ARP protocol is widely used for translating L3 (IP) addresses into L2 (data-link) addresses.

Unfortunately, it suffers from a critical security vulnerability: due to its unauthenticated nature, a destination machine has no way of determining whether a ARP reply is valid, so an attacker can forge ARP packets and tell a victim PC to associate the router's IP address to the attacker's MAC address, such that it will send all traffic through the victim's machine.

[20p] Task A: ARP Cache Poisoning

# turn on IP Forwarding -- for the attacker (inherited from host)
# containers don't have permission for this and we don't want to bother with capabilities
sudo sysctl -w net.ipv4.ip_forward=1
 
# Open a "Victim" terminal (on your VM):
docker run --rm -ti --entrypoint /bin/bash --name victim ubuntu:22.04
# Open an "Attacker" terminal (also on the same VM):
docker run --rm -ti --entrypoint /bin/bash --name attacker --sysctl net.ipv4.ip_forward=1 ubuntu:22.04
 
# we need two terminals for the attacker (for the tcpdump later)
# so... in a third terminal, spawn a Docker exec shell:
docker exec -ti attacker /bin/bash
apt update && apt install -y iproute2 iputils-ping netcat-openbsd
ip a sh
# install prerequisites for this task
apt update && apt install -y dsniff tcpdump iproute2 iputils-ping
 
# start poisoning the host's ARP cache
arpspoof -i <INTERFACE> -t <VICTIM_IP> <GATEWAY_IP> -r

[10p] Task B: Test Implementation

tcpdump udp port 53 -nvvX
# check ARP table (your gateway's MAC should be the attacker's)
ip nei sh
# ping your favorite website
ping my.secretwebsite.com
# Unfortunately, IP forwarding inside container doesn't work :(

The tcpdump capture on the Attacker terminal should show the intercepted DNS requests ;)

[10p] 4. Feedback

Please take a minute to fill in the feedback form for this lab.