Program 2: Scapy Packet Crafting and Firewall Testing (Source 2) This program uses a custom Python script with Scapy to craft and send ICMP, TCP, and UDP packets to a target Ubuntu VM configured with the UFW firewall. Phase System Steps and Commands 1. Network Setup Kali/Ubuntu Check the IP addresses using the ifconfig command. 2. Configure Firewall Ubuntu (Target) 1. Enable UFW: sudo ufw enable. 2. Add rules: sudo ufw deny 80/tcp (block web traffic) and sudo ufw allow 22/tcp (allow SSH). 3. Block ICMP echo requests (Pings): sudo iptables -I INPUT 1 -p icmp --icmp-type echo-request -j DROP. 3. Scapy Script Kali Linux Write the packet_lab.py script (provided below). #!/usr/bin/env python3 from scapy.all import * import time # Configuration TARGET_IP = "192.168.56.103" # Ubuntu VM DEST_PORT = 80 # HTTP port DEST_PORT2 = 22 # SSH port UDP_PORT = 53 # DNS port PACKET_DELAY = 1 # Delay between packets def banner(): print("\n========================================") print(" SCAPY PACKET-CRAFTING LAB SCRIPT") print("========================================\n") def send_icmp(): print("[*] Sending ICMP Echo Request...") packet = IP(dst=TARGET_IP) / ICMP() send(packet, verbose=0) print("[+] ICMP packet sent.\n") def send_tcp_syn(destination_port): print(f"[*] Sending TCP SYN to port {destination_port}...") packet = IP(dst=TARGET_IP) / TCP(dport=destination_port, flags="S") send(packet, verbose=0) print("[+] TCP SYN sent.\n") def send_tcp_null(destination_port): print("[*] Sending TCP NULL packet...") packet = IP(dst=TARGET_IP) / TCP(dport=destination_port, flags=0) send(packet, verbose=0) print("[+] NULL packet sent.\n") def send_tcp_fin(destination_port): print("[*] Sending TCP FIN packet...") packet = IP(dst=TARGET_IP) / TCP(dport=destination_port, flags="F") send(packet, verbose=0) print("[+] FIN sent.\n") def send_udp(): print(f"[*] Sending UDP to port {UDP_PORT}...") packet = IP(dst=TARGET_IP) / UDP(dport=UDP_PORT) / Raw(load="TestUDP") send(packet, verbose=0) print("[+] UDP sent.\n") def send_custom_payload(): print("[*] Sending TCP packet with custom payload...") packet = IP(dst=TARGET_IP) / TCP(dport=DEST_PORT, flags="PA") / Raw(load="HelloFromScapy") send(packet, verbose=0) print("[+] Payload packet sent.\n") def main(): banner() time.sleep(1) # Send packets step by step send_icmp() time.sleep(PACKET_DELAY) send_tcp_syn(DEST_PORT2) time.sleep(PACKET_DELAY) send_tcp_null(DEST_PORT2) time.sleep(PACKET_DELAY) send_tcp_fin(DEST_PORT2) time.sleep(PACKET_DELAY) send_udp() time.sleep(PACKET_DELAY) send_custom_payload() print("\n[+] Experiment Completed.") if __name__ == "__main__": main() 4. Execution & Capture Kali Linux 1. Start Wireshark, select the interface (e.g., eth0). 2. Run the script: sudo python3 packet_lab.py. 5. Observation & Cleanup Kali/Ubuntu 1. Check Wireshark output using filters like icmp, tcp.flags.syn==1 we will see TCP [SYN] Kali-> Unix(port 80).. then no RST, no Syn/Ack. , or tcp.port==22 In wireshark we will see TCP[SYN] kali -> Ubuntu Tcp[Syn, ACk] Ubuntu-> kali TCP [ACK] Kali-> Ubuntu. 2. Test connections manually (optional): ping , nc –v 80, nc –v 22, or ssh ubuntu@. 3. Remove the iptables rule: sudo iptables –D INPUT -p icmp --icmp-type echo-request -j DROP. Shortened Scapy Code (packet_lab.py) #!/usr/bin/env python3 from scapy.all import * # Configuration Variables TARGET_IP = "192.168.56.103" # Target Ubuntu VM IP (UPDATE THIS) DEST_PORT_BLOCKED = 80 # Denied port (from UFW rule) DEST_PORT_ALLOWED = 22 # Allowed port (SSH) UDP_PORT = 53 def main(): # 1. ICMP Echo Request (should be dropped by iptables rule) print("[*] Sending ICMP Echo Request...") send(IP(dst=TARGET_IP) / ICMP(), verbose=0) # 2. TCP SYN (to allowed port 22 - expected SYN/ACK response) print(f"[*] Sending TCP SYN to port {DEST_PORT_ALLOWED}...") send(IP(dst=TARGET_IP) / TCP(dport=DEST_PORT_ALLOWED, flags="S"), verbose=0) # 3. TCP NULL (to allowed port 22 - expected RST response if no service running, or dropped) print(f"[*] Sending TCP NULL packet (No flags)...") send(IP(dst=TARGET_IP) / TCP(dport=DEST_PORT_ALLOWED, flags=0), verbose=0) # 4. TCP FIN (to allowed port 22 - expected RST response if no service running, or dropped) print(f"[*] Sending TCP FIN packet...") send(IP(dst=TARGET_IP) / TCP(dport=DEST_PORT_ALLOWED, flags="F"), verbose=0) # 5. UDP Packet (to port 53 - may generate ICMP port unreachable if no listener) print(f"[*] Sending UDP to port {UDP_PORT}...") send(IP(dst=TARGET_IP) / UDP(dport=UDP_PORT) / Raw(load="TestUDP"), verbose=0) # 6. TCP Packet with Custom Payload (to blocked port 80 - expected RST or dropped) print("[*] Sending TCP packet with custom payload to blocked port...") send(IP(dst=TARGET_IP) / TCP(dport=DEST_PORT_BLOCKED, flags="PA") / Raw(load="HelloFromScapy"), verbose=0) print("\n[+] Experiment Completed.") if __name__ == "__main__": main()