1
---
2
name: protocol-reverse-engineering
3
description: Master network protocol reverse engineering including packet analysis, protocol dissection, and custom protocol documentation. Use when analyzing network traffic, understanding proprietary protocols, or debugging network communication.
4
---
5
6
# Protocol Reverse Engineering
7
8
Comprehensive techniques for capturing, analyzing, and documenting network protocols for security research, interoperability, and debugging.
9
10
## Traffic Capture
11
12
### Wireshark Capture
13
14
```bash
15
# Capture on specific interface
16
wireshark -i eth0 -k
17
18
# Capture with filter
19
wireshark -i eth0 -k -f "port 443"
20
21
# Capture to file
22
tshark -i eth0 -w capture.pcap
23
24
# Ring buffer capture (rotate files)
25
tshark -i eth0 -b filesize:100000 -b files:10 -w capture.pcap
26
```
27
28
### tcpdump Capture
29
30
```bash
31
# Basic capture
32
tcpdump -i eth0 -w capture.pcap
33
34
# With filter
35
tcpdump -i eth0 port 8080 -w capture.pcap
36
37
# Capture specific bytes
38
tcpdump -i eth0 -s 0 -w capture.pcap # Full packet
39
40
# Real-time display
41
tcpdump -i eth0 -X port 80
42
```
43
44
### Man-in-the-Middle Capture
45
46
```bash
47
# mitmproxy for HTTP/HTTPS
48
mitmproxy --mode transparent -p 8080
49
50
# SSL/TLS interception
51
mitmproxy --mode transparent --ssl-insecure
52
53
# Dump to file
54
mitmdump -w traffic.mitm
55
56
# Burp Suite
57
# Configure browser proxy to 127.0.0.1:8080
58
```
59
60
## Protocol Analysis
61
62
### Wireshark Analysis
63
64
```
65
# Display filters
66
tcp.port == 8080
67
http.request.method == "POST"
68
ip.addr == 192.168.1.1
69
tcp.flags.syn == 1 && tcp.flags.ack == 0
70
frame contains "password"
71
72
# Following streams
73
Right-click > Follow > TCP Stream
74
Right-click > Follow > HTTP Stream
75
76
# Export objects
77
File > Export Objects > HTTP
78
79
# Decryption
80
Edit > Preferences > Protocols > TLS
81
- (Pre)-Master-Secret log filename
82
- RSA keys list
83
```
84
85
### tshark Analysis
86
87
```bash
88
# Extract specific fields
89
tshark -r capture.pcap -T fields -e ip.src -e ip.dst -e tcp.port
90
91
# Statistics
92
tshark -r capture.pcap -q -z conv,tcp
93
tshark -r capture.pcap -q -z endpoints,ip
94
95
# Filter and extract
96
tshark -r capture.pcap -Y "http" -T json > http_traffic.json
97
98
# Protocol hierarchy
99
tshark -r capture.pcap -q -z io,phs
100
```
101
102
### Scapy for Custom Analysis
103
104
```python
105
from scapy.all import *
106
107
# Read pcap
108
packets = rdpcap("capture.pcap")
109
110
# Analyze packets
111
for pkt in packets:
112
if pkt.haslayer(TCP):
113
print(f"Src: {pkt[IP].src}:{pkt[TCP].sport}")
114
print(f"Dst: {pkt[IP].dst}:{pkt[TCP].dport}")
115
if pkt.haslayer(Raw):
116
print(f"Data: {pkt[Raw].load[:50]}")
117
118
# Filter packets
119
http_packets = [p for p in packets if p.haslayer(TCP)
120
and (p[TCP].sport == 80 or p[TCP].dport == 80)]
121
122
# Create custom packets
123
pkt = IP(dst="target")/TCP(dport=80)/Raw(load="GET / HTTP/1.1\r\n")
124
send(pkt)
125
```
126
127
## Protocol Identification
128
129
### Common Protocol Signatures
130
131
```
132
HTTP - "HTTP/1." or "GET " or "POST " at start
133
TLS/SSL - 0x16 0x03 (record layer)
134
DNS - UDP port 53, specific header format
135
SMB - 0xFF 0x53 0x4D 0x42 ("SMB" signature)
136
SSH - "SSH-2.0" banner
137
FTP - "220 " response, "USER " command
138
SMTP - "220 " banner, "EHLO" command
139
MySQL - 0x00 length prefix, protocol version
140
PostgreSQL - 0x00 0x00 0x00 startup length
141
Redis - "*" RESP array prefix
142
MongoDB - BSON documents with specific header
143
```
144
145
### Protocol Header Patterns
146
147
```
148
+--------+--------+--------+--------+
149
| Magic number / Signature |
150
+--------+--------+--------+--------+
151
| Version | Flags |
152
+--------+--------+--------+--------+
153
| Length | Message Type |
154
+--------+--------+--------+--------+
155
| Sequence Number / Session ID |
156
+--------+--------+--------+--------+
157
| Payload... |
158
+--------+--------+--------+--------+
159
```
160
161
Before binary protocol or encryption analysis, read [parsers, structure identification, and TLS techniques](references/binary-encryption-analysis.md).
162
163
Before documenting a protocol or writing a dissector, read [the specification template and Lua example](references/protocol-documentation.md).
164
165
## Active Testing
166
167
### Fuzzing with Boofuzz
168
169
```python
170
from boofuzz import *
171
172
def main():
173
session = Session(
174
target=Target(
175
connection=TCPSocketConnection("target", 8888)
176
)
177
)
178
179
# Define protocol structure
180
s_initialize("HELLO")
181
s_static(b"\x50\x52\x4f\x54") # Magic
182
s_word(1, name="version") # Version
183
s_word(0x01, name="type") # Type (HELLO)
184
s_size("payload", length=4) # Length field
185
s_block_start("payload")
186
s_dword(0x12345678, name="client_id")
187
s_word(0, name="flags")
188
s_block_end()
189
190
session.connect(s_get("HELLO"))
191
session.fuzz()
192
193
if __name__ == "__main__":
194
main()
195
```
196
197
### Replay and Modification
198
199
```python
200
from scapy.all import *
201
202
# Replay captured traffic
203
packets = rdpcap("capture.pcap")
204
for pkt in packets:
205
if pkt.haslayer(TCP) and pkt[TCP].dport == 8888:
206
send(pkt)
207
208
# Modify and replay
209
for pkt in packets:
210
if pkt.haslayer(Raw):
211
# Modify payload
212
original = pkt[Raw].load
213
modified = original.replace(b"client", b"CLIENT")
214
pkt[Raw].load = modified
215
# Recalculate checksums
216
del pkt[IP].chksum
217
del pkt[TCP].chksum
218
send(pkt)
219
```
220
221
## Best Practices
222
223
### Analysis Workflow
224
225
1. **Capture traffic**: Multiple sessions, different scenarios
226
2. **Identify boundaries**: Message start/end markers
227
3. **Map structure**: Fixed header, variable payload
228
4. **Identify fields**: Compare multiple samples
229
5. **Document format**: Create specification
230
6. **Validate understanding**: Implement parser/generator
231
7. **Test edge cases**: Fuzzing, boundary conditions
232
233
### Common Patterns to Look For
234
235
- Magic numbers/signatures at message start
236
- Version fields for compatibility
237
- Length fields (often before variable data)
238
- Type/opcode fields for message identification
239
- Sequence numbers for ordering
240
- Checksums/CRCs for integrity
241
- Timestamps for timing
242
- Session/connection identifiers
243