6. Extracting Files & Reconstructing Payloads
Extracting binary file artifacts from HTTP, FTP, and SMB streams
Network forensics often involves carving files directly from raw traffic captures (PCAPs). HTTP traffic typically sends files via the 'Content-Type: application/octet-stream' header, while FTP uses separate data channels. SMB (Server Message Block) is more complex, as it encapsulates file transfers within multi-part requests.
Using TShark is the most efficient way to isolate these streams. You can use the command 'tshark -r capture.pcap -q --export-objects "http,./extracted"' to automatically carve files identified in HTTP sessions. For SMB, you often need to filter by smb2.filename and reassemble the underlying byte buffers manually.
file or xxd after extraction to verify the header matches the expected file format.
tshark -r traffic.pcap -Y "http.content_type contains 'application/x-msdownload'" -T fields -e http.file_data > malware.bin
Decoding base64 payloads inside HTTP headers and DNS queries
Attackers often exfiltrate data or encode shellcode inside HTTP User-Agent headers or DNS TXT records to bypass signature-based detection. Base64 encoding is common because it converts binary data into a string format that fits into standard ASCII-based protocols.
In Python, decoding these payloads requires capturing the specific field, stripping any non-base64 characters, and passing the string to the base64 library. For DNS, you must first extract the subdomain label from the query before decoding.
import base64 encoded_data = "YmFkX3BheWxvYWRfdGV4dA==" decoded_data = base64.b64decode(encoded_data) print(f"Decoded payload: {decoded_data.decode('utf-8')}")
Reconstructing TCP reassembled data buffers programmatically
TCP is a stream-oriented protocol, meaning a single file transfer can be fragmented across dozens of TCP segments. To reconstruct the file, you must sort segments by their TCP sequence numbers and concatenate the payloads.
The following logic iterates through a capture, extracts data based on sequence numbers, and reconstructs the original buffer in memory.
tcp.analysis.retransmission to avoid duplicate data in your buffer.
import struct def reconstruct(segments): segments.sort(key=lambda x: x['seq']) buffer = b'' for seg in segments: buffer += seg['data'] return buffer # Simulated payload segments = [{'seq': 100, 'data': b'hello '}, {'seq': 106, 'data': b'world'}] print(reconstruct(segments))
Validating extracted file hashes (SHA-256) against threat databases
After carving a file, the most important step is verification. Generating a SHA-256 hash allows you to perform cross-checks against databases like VirusTotal or internal IOC (Indicator of Compromise) lists.
Python's hashlib library is the standard for generating these hashes. Once calculated, this hex digest is used as a lookup key for intelligence threat feeds.
import hashlib def get_hash(file_path): with open(file_path, 'rb') as f: return hashlib.sha256(f.read()).hexdigest() print(f"SHA-256: {get_hash('malware.bin') if 'malware.bin' in globals() else 'File not found'}")
Knowledge Check
Ready to test your understanding of 6. Extracting Files & Reconstructing Payloads?