30 Mins

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.

Security Operator Note: Always check the file signatures (magic bytes) using file or xxd after extraction to verify the header matches the expected file format.
Text
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.

Security Operator Note: DNS queries are case-insensitive, but base64 is case-sensitive. Ensure your decoder handles padding characters ('=') which are often stripped in DNS tunneling.
Python 3
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.

Security Operator Note: Ignore Retransmission segments in Wireshark/TShark by using the filter tcp.analysis.retransmission to avoid duplicate data in your buffer.
Python 3
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.

Security Operator Note: Always hash the file as soon as it hits your disk to maintain a clean chain of custody during incident response.
Python 3
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?

Back