Skip to content

Instantly share code, notes, and snippets.

@summerwind
Last active June 22, 2016 01:57
Show Gist options
  • Save summerwind/ae3ce3fea644f6f1061d0e8362717b27 to your computer and use it in GitHub Desktop.
Save summerwind/ae3ce3fea644f6f1061d0e8362717b27 to your computer and use it in GitHub Desktop.
L3DSR Test Client

L3DSR Test Client

This code is tested on Ubuntu 16.04.

Setup

Install scapy.

$ sudo apt-get install scapy

Dropping RST for the VIP.

$ sudo iptables -A OUTPUT -p tcp --tcp-flags RST RST -d 172.18.18.251 -j DROP

Run

$ sudo python l3dsr.py

Output Example

This client outputs only the payload of the first packet.

$ sudo python l3dsr.py
Sending SYN packet...
Waiting SYN/ACK packet...
Sending HTTP Request...
Waiting HTTP Response...
==========
HTTP/1.1 200 OK
Server: nginx/1.10.0 (Ubuntu)
Date: Tue, 21 Jun 2016 13:39:13 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sun, 19 Jun 2016 04:00:37 GMT
Connection: close
ETag: "576618e5-264"
Accept-Ranges: bytes

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is succ
==========
Done
from scapy.all import *
client_addr = "172.18.18.201"
client_port = random.randrange(30000, 65535)
server_addr = "172.18.18.202"
server_port = 80
vip = "172.18.18.251"
dscp = 7
iface = "enp0s8"
payload = "GET / HTTP/1.0\r\n\r\n"
print "Sending SYN packet..."
ip = IP(src=client_addr, dst=server_addr, tos=(dscp << 2))
tcp = TCP(sport=client_port, dport=server_port, seq=0)
tcp.flags = "S"
syn = ip/tcp
send(syn, verbose=0)
print "Waiting SYN/ACK packet..."
pkts = sniff(filter=("tcp and port %d" % client_port), count=1, iface=iface)
#pkts[0].show()
if pkts[0].sprintf("%TCP.flags%") != "SA":
print "Error: packet flags - expect: SA, got: %s" % pkts[0].sprintf("%TCP.flags%")
exit(1)
print "Sending HTTP Request..."
tcp.seq += 1
tcp.ack = pkts[0].seq + 1
tcp.flags = "PA"
send(ip/tcp/payload, verbose=0)
print "Waiting HTTP Response..."
count = 0
while count < 10:
pkts=sniff(filter=("tcp and port %d" % client_port), count=1, iface=iface)
#pkts[0].show()
if pkts[0].load > 0:
print "=" * 10
print pkts[0].load
print "=" * 10
break
count += 1
if count == 10:
print "Error: timeout"
exit(1)
tcp.seq += len(payload)
tcp.ack = pkts[0].seq + len(pkts[0].load) + 1
tcp.flags = "A"
send(ip/tcp, verbose=0)
tcp.seq += 1
tcp.flags = "FA"
send(ip/tcp, verbose=0)
print "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment