Milestone 1: Custom TCP Handshake Inspector
Building a Raw TCP Segment Parser
Scenario: You are assigned to the VoidX Incident Response team. A compromised node in the subnet has been leaking raw traffic captures. Your mission is to develop a Python script that ingests a hexadecimal byte stream representing a TCP segment, extracts the header fields, and validates the TCP control flags.
Mission Objective: Implement a robust parser using the Python struct module to handle binary data unpacking. Your script must account for the 20-byte base TCP header, specifically mapping the Source/Destination ports, Sequence Number, Acknowledgment Number, Data Offset, and the Flag fields.
Goals & Requirements: The script must output the parsed sequence number as an integer and display the state of SYN, ACK, and FIN flags in a human-readable format. Ensure you handle the byte-ordering correctly using network byte order (big-endian).
Practical Hints: Use 'struct.unpack("!HHLLBBHHH", data)' to deconstruct the first 20 bytes. Remember that the flags are packed into the 14th byte of the TCP header; use bitwise AND operations (e.g., flag_byte & 0x02 for SYN) to isolate specific bits.
# Target Payload: # mock_pkt = b'\x00\x50\x01\xbb\x00\x00\x00\x64\x00\x00\x00\x00\x50\x02\x72\x10\x00\x00\x00\x00' # Required Output: 'Seq: 100, ACK: False, SYN: True'
Verifying TCP Flag States
Scenario: The security SOC requires verification of handshake integrity. You must extend your tool to flag potential scanning activity, specifically checking for half-open connections or illegal flag combinations like SYN+FIN.
Mission Objective: Create a conditional logic block that evaluates the parsed flags against a whitelist of valid handshake states. Alert if a segment exhibits anomalies such as simultaneous SYN and FIN.
Goals & Requirements: Return a status report string indicating if the captured segment corresponds to a valid SYN, SYN-ACK, or ACK packet, or if it is flagged as Malicious/Malformed.
Practical Hints: Refer to the TCP Flag mask: FIN=0x01, SYN=0x02, RST=0x04, PSH=0x08, ACK=0x10, URG=0x20. Use a simple lookup dictionary to correlate these masks to their descriptive labels.
# Target Flag Mask Input: 0x12 (SYN + ACK) # Required Output: "Flags found: ['SYN', 'ACK']"
Objectives & Tasks
Review the tasks below and follow the interactive instructions to complete this module.
Knowledge Check
Ready to test your understanding of Milestone 1: Custom TCP Handshake Inspector?