scapy custom source address shows failure

24 Views Asked by At

I have a python server running on port 8000 (the simple "python3 -m http.server") and I want to send a request with a custom ip address to it, so I wrote:

from scapy.all import IP, TCP, send
from scapy.layers.http import HTTP, HTTPRequest

# Set the source and destination IP addresses
src_ip = "127.0.0.1"  # Custom source IP address
dst_ip = "127.0.0.1"  # Destination IP (your server's IP)
dst_port = 8000  # Port your server is listening on

# Create the IP packet
ip_packet = IP(src=src_ip, dst=dst_ip)

# Create the TCP packet
tcp_packet = TCP(sport=12345, dport=dst_port, flags="S")  # SYN packet

# Send the SYN packet and receive the response
response = send(ip_packet / tcp_packet, verbose=False)

# Check if a response was received
if response:
    src_port = response[TCP].sport
else:
    print("No response received from the server.")
    exit(1)

# Create the HTTP GET request
get_request = HTTP() / HTTPRequest(
    method="GET",
    Path="/",
    Http_Version="HTTP/1.1",
    Host=dst_ip,
    headers={
        "User-Agent": "Mozilla/5.0",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    },
)

# Create the TCP packet with the HTTP GET request
tcp_packet = TCP(sport=src_port, dport=dst_port, flags="PA") / str(get_request)

# Send the HTTP GET request
send(ip_packet / tcp_packet, verbose=False)

however this exits with response "no response received from the server" and the server, indeed, does not show any received request too. Why could this be happening?

0

There are 0 best solutions below