Testing

From Network Security Wiki


Server with Latency

Source: github.com

#!/usr/bin/env python
import argparse
import time
from http.server import HTTPServer, BaseHTTPRequestHandler

class S(BaseHTTPRequestHandler):
    def _set_headers(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()

    def _html(self, message):
        """This just generates an HTML document that includes `message`
        in the body. Override, or re-write this do do more interesting stuff.
        """
        content = f"<html><body><h1>{message}</h1></body></html>"
        return content.encode("utf8")  # NOTE: must return a bytes object!

    def do_GET(self):
        time.sleep(5)                     # this will delay the "200 OK" message => App response time
        self._set_headers()
        #time.sleep(5)                    # this will delay the Body => Data transfer time
        self.wfile.write(self._html("hi!"))

    def do_HEAD(self):
        self._set_headers()

    def do_POST(self):
        # Doesn't do anything with posted data
        self._set_headers()
        self.wfile.write(self._html("POST!"))


def run(server_class=HTTPServer, handler_class=S, addr="localhost", port=8000):
    server_address = (addr, port)
    httpd = server_class(server_address, handler_class)

    print(f"Starting httpd server on {addr}:{port}")
    httpd.serve_forever()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Run a simple HTTP server")
    parser.add_argument(
        "-l",
        "--listen",
        default="localhost",
        help="Specify the IP address on which the server listens",
    )
    parser.add_argument(
        "-p",
        "--port",
        type=int,
        default=8000,
        help="Specify the port on which the server listens",
    )
    args = parser.parse_args()
    run(addr=args.listen, port=args.port)

Running the Server:

sudo python3 dummy-web-server.py -l 10.140.196.7 -p 8080    # ==> Default port 8000

Generating traffic:

curl http://10.140.196.7:8080                        # GET
curl -I http://10.140.196.7:8080                     # HEAD
curl -d "foo=bar&bin=baz" http://10.140.196.7:8080   # POST

Scapy Web Server

Source: akaljed.wordpress.com, lost-and-found-narihiro.blogspot.com

Disable the Linux Kernel's RST-ACK as it believes that no port is open, has no knowledge of what Scapy is doing:

sudo iptables -A OUTPUT -p tcp --tcp-flags RST RST --sport 8000 -j DROP
#!/usr/bin/env python
from scapy.all import *
import os

# a[0] sniff one packet ( syn packet ) from 10.140.196.6
a=sniff(count=1,filter="tcp and port 8000 and host 10.140.196.6")

# prepare for a[1] : web -> client syn , ack
Sport_num=a[0].sport
Seq_num1=a[0].seq  # for a[1]
Ack_num1=a[0].seq+1 # for a[1]

# a[1] build IP layer
ip=IP(src="10.140.196.7",dst="10.140.196.6")

# a[1] build TCP layer ( syn,ack : web -> client )
tcp_synack1=TCP(sport=8000, dport=Sport_num,flags="SA", seq=Seq_num1, ack=Ack_num1, options=[('MSS', 1460), ('SAckOK', ''), ('NOP', None), ('WScale', 7)])

# a[1] : SYN ACK packet (sr1) to client and put client's response into answer2
answer2=sr1(ip/tcp_synack1)

# a[3] recieve the HTTP GET ( client -> web )
GEThttp = sniff(filter="tcp and port 8000 and host 10.140.196.6",count=1,prn=lambda x:x.sprintf("{IP:%IP.src%: %TCP.dport%}"))

# a[4] ack , seq number ( web -> client )
Ack_num4=Ack_num1+len(GEThttp[0].load)
Seq_num4=Seq_num1+1

# a[4] build TCP layer
tcp_ack4=TCP(sport=8000, dport=Sport_num, flags="AP", seq=Seq_num4, ack=Ack_num4, options=[('MSS', 1460), ('SAckOK', ''), ('NOP', None), ('WScale', 7)])

# a[5] send ack to client
sr1(ip/tcp_ack4)

# a[6] build HTTP response ( web -> client )
html1='HTTP/1.1 200 OK\r\nDate: Sun, 13 Oct 2019 13:41:53 GMT\r\nServer: Apache/2.2.22 (Ubuntu)\r\nLast-Modified: Mon, 10 Dec 2012 06:18:23 GMT\r\nETag: "1062407-6-4d079884fb4f2"\r\nAccept-Ranges: bytes\r\nContent-Length: 6\r\nVary: Accept-Encoding\r\nKeep-Alive: timeout=5, max=100\r\nConnection: Keep-Alive\r\nContent-Type: text/html\r\n\r\nhello\n'

# a[5] build TCP layer
tcp5=TCP(sport=8000, dport=Sport_num, flags="PA", seq=Seq_num4, ack=Ack_num4, options=[('MSS', 1460), ('SAckOK', ''), ('NOP', None), ('WScale', 7)])

# a[5] send HTTP response ( web -> client )
answer6=sr1(ip/tcp5/html1)

# close connection ( web -> client )
tcp6=TCP(sport=8000, dport=Sport_num, flags="FA",seq=Seq_num4, ack=Ack_num4,options=[('MSS', 1460), ('SAckOK', ''), ('NOP', None), ('WScale', 7)])

# close connection ( web -> client )
send(ip/tcp5)



References





{{#widget:DISQUS |id=networkm |uniqid=Testing |url=https://aman.awiki.org/wiki/Testing }}