|
| 1 | +# scapy.contrib.description = TCP Convergence Layer version 4 (TCPCLv4) |
| 2 | +# scapy.contrib.status = loads |
| 3 | + |
| 4 | +# These classes support unit testing of the TCPCL scapy layer (scapy.contrib.dtn.tcpcl) and illustrate how the protocol messages may be used to emulate a TCPCL session. |
| 5 | + |
| 6 | +from scapy.all import Raw, raw, TCP, Packet, bind_layers, split_layers |
| 7 | +import scapy.contrib.dtn.tcpcl as TCPCL |
| 8 | +from typing import List |
| 9 | + |
| 10 | + |
| 11 | +class Session: |
| 12 | + """ |
| 13 | + TCPCL messages are conventionally, but not necessarily, sent on port 4556. Since this cannot be relied upon, especially on a localhost session, the best way to bind TCP packets to TCPCL message is to track the state of a TCPCL session. Once Contact Headers are successfuly exchanged, TCP packets can be assumed to carry payloads of TCPCL messages until the session ends. |
| 14 | + """ |
| 15 | + def __init__(self): |
| 16 | + self.contact_init = False |
| 17 | + self.contact_ack = False |
| 18 | + self.is_active = False |
| 19 | + self.term_begun = False |
| 20 | + self.sport = 0 |
| 21 | + self.dport = 0 |
| 22 | + |
| 23 | + @staticmethod |
| 24 | + def bind_messages(sport, dport): |
| 25 | + bind_layers(TCP, TCPCL.MsgHeader, sport=sport, dport=dport) |
| 26 | + bind_layers(TCP, TCPCL.MsgHeader, sport=dport, dport=sport) |
| 27 | + |
| 28 | + @staticmethod |
| 29 | + def split_messages(sport, dport): |
| 30 | + split_layers(TCP, TCPCL.MsgHeader, sport=sport, dport=dport) |
| 31 | + split_layers(TCP, TCPCL.MsgHeader, sport=dport, dport=sport) |
| 32 | + |
| 33 | + def activate(self): |
| 34 | + if not (self.contact_init and self.contact_ack): |
| 35 | + raise Exception("tried to activate a session before initialization and acknowledgement") |
| 36 | + |
| 37 | + self.is_active = self.contact_init and self.contact_ack |
| 38 | + |
| 39 | + Session.bind_messages(self.sport, self.dport) |
| 40 | + |
| 41 | + def terminate(self): |
| 42 | + if not (self.contact_init and self.contact_ack): |
| 43 | + raise Exception("tried to terminate a session while none was active") |
| 44 | + |
| 45 | + self.is_active = self.contact_ack = self.contact_init = False |
| 46 | + |
| 47 | + Session.split_messages(self.sport, self.dport) |
| 48 | + |
| 49 | + def init_contact(self, sport, dport): |
| 50 | + self.contact_init=True |
| 51 | + self.sport = sport |
| 52 | + self.dport = dport |
| 53 | + |
| 54 | + def init_timeout(self): |
| 55 | + self.contact_init=False |
| 56 | + |
| 57 | + def proc_ack(self): |
| 58 | + self.contact_ack=True |
| 59 | + self.activate() |
| 60 | + |
| 61 | + def proc_term(self): |
| 62 | + self.term_begun = True |
| 63 | + |
| 64 | + def proc_term_ack(self): |
| 65 | + self.terminate() |
| 66 | + |
| 67 | +class TestTcpcl(): |
| 68 | + |
| 69 | + @staticmethod |
| 70 | + def check_pkt(pkt: Packet, options: List[Packet]): |
| 71 | + """Asserts that pkt is equal to one of the packets in options (according to the raw representation)""" |
| 72 | + for opt in options: |
| 73 | + assert raw(pkt) in list(map(raw, options)), "Failed to build a properly formatted TCPCL message" |
| 74 | + |
| 75 | + @staticmethod |
| 76 | + def make_prn(): |
| 77 | + """Define a function for processing packets that closes over a new Session. |
| 78 | + Return it for use in Scapy.sniff.""" |
| 79 | + |
| 80 | + sess = Session() |
| 81 | + |
| 82 | + def process(pkt): |
| 83 | + # Manage session initialization |
| 84 | + if not sess.is_active: |
| 85 | + try: # try to find a Contact Header |
| 86 | + pay = pkt[Raw].load |
| 87 | + contact = TCPCL.ContactHeader(pay) # should raise unhandled error if |
| 88 | + # the TCP payload does not fit ContactHeader |
| 89 | + # replace pkt's raw payload with a ContactHeader formatted payload |
| 90 | + pkt[TCP].remove_payload() |
| 91 | + pkt = pkt / contact |
| 92 | + |
| 93 | + # process ContactHeader |
| 94 | + if sess.contact_init: # session aready initialized, Header is an ack |
| 95 | + sess.proc_ack() |
| 96 | + print("BEGIN TCPCL SESSION") |
| 97 | + else: |
| 98 | + sess.init_contact(pkt[TCP].sport, pkt[TCP].dport) |
| 99 | + except IndexError: # no TCP payload to process |
| 100 | + pass |
| 101 | + else: # currently in an active session |
| 102 | + if TCPCL.SessTerm in pkt: |
| 103 | + # process SessTerm |
| 104 | + if sess.term_begun: |
| 105 | + sess.proc_term_ack() |
| 106 | + print("END TCPCL SESSION") |
| 107 | + else: |
| 108 | + sess.proc_term() |
| 109 | + |
| 110 | + return pkt # end of process |
| 111 | + |
| 112 | + return process # end of make_prn |
0 commit comments