|
| 1 | +import sys |
| 2 | +import binascii |
| 3 | +from threading import Thread |
| 4 | +import time |
| 5 | +import socket |
| 6 | + |
| 7 | +# Create a pcap from a htp test file |
| 8 | +# Launches a server on port 8080 |
| 9 | +# Launches a client in another thread that connects to it |
| 10 | +# Both client and server read the htp test file |
| 11 | +# And they send and receive data as described (without analysing it) |
| 12 | +# So, you need to capture traffic on port 8080 while running the script |
| 13 | + |
| 14 | +def removeOneEOL(s): |
| 15 | + r = s |
| 16 | + if r[-1] == '\n': |
| 17 | + r = r[:-1] |
| 18 | + if r[-1] == '\r': |
| 19 | + r = r[:-1] |
| 20 | + return r |
| 21 | + |
| 22 | + |
| 23 | +class ServerThread(Thread): |
| 24 | + |
| 25 | + def __init__(self, filename): |
| 26 | + Thread.__init__(self) |
| 27 | + self.filename = filename |
| 28 | + |
| 29 | + def run(self): |
| 30 | + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 31 | + s.bind(("127.0.0.1", 8080)) |
| 32 | + s.listen(1) |
| 33 | + conn, addr = s.accept() |
| 34 | + f = open(self.filename) |
| 35 | + state = 0 |
| 36 | + sending = "" |
| 37 | + receiving = "" |
| 38 | + |
| 39 | + for l in f.readlines(): |
| 40 | + # change of state |
| 41 | + if l.rstrip() == "<<<" or l.rstrip() == ">>>": |
| 42 | + # Receiving or sending buffer |
| 43 | + if state == 1: |
| 44 | + conn.send(removeOneEOL(sending)) |
| 45 | + print "server sent", len(removeOneEOL(sending)) |
| 46 | + elif state == 2: |
| 47 | + data = conn.recv(len(removeOneEOL(receiving))) |
| 48 | + print "server recvd", len(data) |
| 49 | + if l.rstrip() == "<<<": |
| 50 | + state = 1 |
| 51 | + sending = "" |
| 52 | + elif l.rstrip() == ">>>": |
| 53 | + state = 2 |
| 54 | + receiving = "" |
| 55 | + else: |
| 56 | + if state == 1: |
| 57 | + sending += l |
| 58 | + elif state == 2: |
| 59 | + receiving += l |
| 60 | + |
| 61 | + # Receiving or sending last buffer |
| 62 | + if state == 1: |
| 63 | + conn.send(sending) |
| 64 | + print "server sent", len(sending) |
| 65 | + elif state == 2: |
| 66 | + data = conn.recv(len(receiving)) |
| 67 | + print "server recvd", len(data) |
| 68 | + conn.close() |
| 69 | + s.close() |
| 70 | + f.close() |
| 71 | + |
| 72 | + |
| 73 | +class ClientThread(Thread): |
| 74 | + |
| 75 | + def __init__(self, filename): |
| 76 | + Thread.__init__(self) |
| 77 | + self.filename = filename |
| 78 | + |
| 79 | + def run(self): |
| 80 | + time.sleep(1) |
| 81 | + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 82 | + s.connect(("127.0.0.1", 8080)) |
| 83 | + f = open(self.filename) |
| 84 | + state = 0 |
| 85 | + sending = "" |
| 86 | + receiving = "" |
| 87 | + |
| 88 | + for l in f.readlines(): |
| 89 | + if l.rstrip() == "<<<" or l.rstrip() == ">>>": |
| 90 | + if state == 1: |
| 91 | + s.send(removeOneEOL(sending)) |
| 92 | + print "client sent", len(removeOneEOL(sending)) |
| 93 | + elif state == 2: |
| 94 | + data = s.recv(len(removeOneEOL(receiving))) |
| 95 | + print "client recvd", len(data) |
| 96 | + if l.rstrip() == "<<<": |
| 97 | + state = 2 |
| 98 | + receiving = "" |
| 99 | + elif l.rstrip() == ">>>": |
| 100 | + state = 1 |
| 101 | + sending = "" |
| 102 | + else: |
| 103 | + if state == 1: |
| 104 | + sending += l |
| 105 | + elif state == 2: |
| 106 | + receiving += l |
| 107 | + |
| 108 | + if state == 1: |
| 109 | + s.send(sending) |
| 110 | + print "client sent", len(sending) |
| 111 | + elif state == 2: |
| 112 | + data = s.recv(len(receiving)) |
| 113 | + print "client recvd", len(data) |
| 114 | + s.close() |
| 115 | + f.close() |
| 116 | + |
| 117 | +t1 = ServerThread(sys.argv[1]) |
| 118 | +t2 = ClientThread(sys.argv[1]) |
| 119 | + |
| 120 | +# Launch threads |
| 121 | +t1.start() |
| 122 | +t2.start() |
| 123 | + |
| 124 | +# Wait for threads to finish |
| 125 | +t1.join() |
| 126 | +t2.join() |
0 commit comments