-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFollowTcpStream.py
448 lines (384 loc) · 20 KB
/
FollowTcpStream.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# !/usr/bin/python
# -*- coding: utf-8 -*-
# FollowTcpStream - Comparable to Wireshark's functionality but including un-chunking and un-gzipping.
#
# Author: Christian Wojner
# Credits: Influenced by Brian Maloneys PCAP_tools (https://github.com/Beercow/ProcDOT-Plugins/tree/master/PCAP_tools)
#
# Dependencies:
# - TcpFlow 1.4
# Example: FollowTcpStream.py -i 10.0.2.15:1067 -p 80 -o "C:\Users\chrisu\AppData\Local\Temp\malware\FollowTcpStream.out" "data\test.pcap"
# Example: FollowTcpStream.py -i 10.0.2.15:1067 -p 80 -o "C:\Users\chrisu\AppData\Local\Temp\malware\FollowTcpStream.out" "C:\Users\chrisu\Documents\cert.at\Vorträge\LKOOE\Logs\Scripts.pcap"
import argparse
import os
import sys
import subprocess as sub
import zlib
import re
import binascii
# Cross-platform!
if (os.name == "nt"):
isWindows = True
isLinux = False
else:
isWindows = False
isLinux = True
gl_TestRunOutput = []
gl_TestRun = False
gl_OutFile = ""
gl_Verbose = False
gl_ChronologicalOrder = False
gl_NoMetaInformation = False
gl_NoSeparators = False
gl_NoHttpHeaders = False
# A management class for redirecting the output according to the arguments provided
class OutputWriter:
FirstWrite = True
FooterPending = False
def normal(self, text):
global gl_TestRunOutput
if (self.FirstWrite):
self.FirstWrite = False
gl_TestRunOutput = []
if (not gl_NoSeparators):
self.normal('\x01[FTS]> HEADER <[FTS]\x02\n\n')
if gl_TestRun:
gl_TestRunOutput.append(text)
gl_TestRunOutput.append("\n")
elif gl_OutFile == "":
print(text)
else:
if (isinstance(text, bytes)):
open(gl_OutFile, 'ab').write(text)
else:
open(gl_OutFile, 'at').write(text + "\n")
def verbose(self, text):
if (gl_Verbose):
self.normal(text)
def finish(self):
if (not self.FirstWrite):
if (not gl_NoSeparators):
self.normal('\n\n\x01[FTS]> FOOTER <[FTS]\x02')
class Flow:
Id = ""
Timestamp = ""
ConnectionNumber = ""
SrcIpAddress = ""
SrcPort = ""
DstIpAddress = ""
DstPort = ""
Data = ""
isHttpRequest = False
HttpRequestAction = ""
isHttpResponse = False
HttpRequestFlow = 0
HttpResponseFlow = 0
isHttp = False
HttpHeader = ""
isGzipped = False
isChunked = False
NextFlow = 0
OutputPending = True
# parse out tcp flow for IP
def parseFlow(pcapFile, ipAddress = b"", tcpPort = b""):
out = OutputWriter()
if (isWindows):
p = sub.Popen(['tcpflow', '-T %T--%A:%a-%B:%b--%N', '-cgD', '-r', pcapFile], stdout=sub.PIPE, stderr=sub.PIPE)
else:
p = sub.Popen(['tcpflow', '-T %T--%A:%a-%B:%b--%N', '-cJD', '-r', pcapFile], stdout=sub.PIPE, stderr=sub.PIPE)
stdout, stderr = p.communicate()
stdout = stdout.replace(b'\r\n', b'\n')
if ipAddress not in stdout:
out.normal("No tcp flows found for " + ipAddress.decode())
elif tcpPort not in stdout:
out.normal("No tcp flows found for " + tcpPort[1:].decode())
else:
flowsCacheRoot = 0
flowsCacheLast = 0
flows = re.findall(b'\x1b\[0;3[14]m\s*(\S+)\s\n(.*?)\x1b\[0m', stdout, re.DOTALL)
# flows = iter(flows)
out.verbose("Building flows ...")
out.verbose("")
flowBuilder = {}
for meta, hexdump in flows:
flow = Flow()
flow.Timestamp, \
flow.SrcIpAddress, \
flow.SrcPort, \
flow.DstIpAddress, \
flow.DstPort, \
flow.ConnectionNumber = re.search(b'^(.+?)--(.+?):(.+?)-(.+?):(.+?)--(.+?):', meta).groups()
flow.Id = flow.SrcIpAddress + b":" + flow.SrcPort + b"-" + flow.DstIpAddress + b":" + flow.DstPort
# Unhexdumpify data
hexdumprows = re.findall(b'^[0-9a-f]+: ((?:[0-9a-f]{4} |[0-9a-f]{2} )+).*?$', hexdump, re.MULTILINE)
data = b""
for hexdumprow in hexdumprows:
data = b''.join([data, hexdumprow])
data = data.replace(b" ", b"")
flow.Data = binascii.unhexlify(data)
flowId = flow.Id
flowIdInv = flow.DstIpAddress + b":" + flow.DstPort + b"-" + flow.SrcIpAddress + b":" + flow.SrcPort
if (re.match(b'^(?:GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH)\s.*', flow.Data)):
flow.isHttp = True
flow.isHttpRequest = True
flow.HttpRequestAction = flow.Data[:flow.Data.find(b"\r")]
if (flowId in flowBuilder):
flowBuilder.pop(flowId)
if (flowIdInv in flowBuilder):
flowBuilder.pop(flowIdInv)
if (re.match(b'^HTTP/.*', flow.Data)):
flow.isHttp = True
flow.isHttpResponse = True
if (flowIdInv in flowBuilder):
flow.HttpRequestFlow = flowBuilder[flowIdInv]
flowBuilder[flowIdInv].HttpResponseFlow = flow
if (flow.isHttp):
flow.HttpHeader, flow.Data = re.search(b'^(.*?)\r\n\r\n(.*)$', flow.Data, re.DOTALL).groups()
flow.HttpHeader = flow.HttpHeader.replace(b'\r\n', b'\n')
if (flow.isHttpResponse):
flow.isGzipped = re.search(b'^content-encoding:\s.*?gzip.*?$', flow.HttpHeader, re.IGNORECASE | re.MULTILINE) != None
flow.isChunked = re.search(b'^transfer-encoding:\s.*?chunked.*?$', flow.HttpHeader, re.IGNORECASE | re.MULTILINE) != None
if (flowIdInv in flowBuilder):
flowBuilder.pop(flowIdInv)
if (flowId in flowBuilder):
mainflow = flowBuilder[flowId]
mainflow.Data = b''.join([mainflow.Data, flow.Data])
out.verbose(" Appending " + str(len(flow.Data)) + " bytes to mainflow " + mainflow.Id.decode())
else:
flowBuilder[flowId] = flow
out.verbose(" Adding new mainflow " + flow.Id.decode() + " (" + str(len(flow.Data)) + " bytes)")
if (not flowsCacheRoot):
flowsCacheRoot = flow
flowsCacheLast = flow
else:
flowsCacheLast.NextFlow = flow
flowsCacheLast = flow
out.verbose("")
out.verbose("")
# Post-Processing flows ...
if (flowsCacheRoot):
out.verbose("Post-Processing flows ...")
out.verbose("")
flow = flowsCacheRoot
while (flow):
if (gl_Verbose):
out.verbose(" " + flow.Id.decode())
if (flow.isHttp and flow.isHttpRequest):
out.verbose(" isHttp (Request: " + flow.HttpRequestAction.decode() + ")")
if (flow.isHttp and flow.isHttpResponse):
out.verbose(" isHttp (Response)")
if (flow.isChunked):
out.verbose(" isChunked")
if (flow.isGzipped):
out.verbose(" isGzipped")
# Un-chunking ...
if (flow.isChunked):
# if (flow.Id == b"069.004.231.030:00080-192.168.001.010:49190"):
# print("Hallo")
# out.verbose(b"[" + flow.Data + b"]")
# out.verbose("")
chunkCounter = 0
chunkedData = flow.Data
flow.Data = b""
while (len(chunkedData)):
chunkCounter += 1
res = re.search(b'^([0-9a-fA-F]+)[^\r\n]*\r\n(.*)$', chunkedData, re.DOTALL)
if (res):
chunkSizeHex, chunkedData = res.groups()
chunkSizeHex = chunkSizeHex.decode()
chunkSize = int(chunkSizeHex, 16)
if (chunkSize):
out.verbose(" " + "Merging chunk #" + str(chunkCounter) + ": " + str(chunkSize) + " (" + chunkSizeHex + ") Bytes")
flow.Data = b''.join([flow.Data, chunkedData[:chunkSize]])
chunkedData = chunkedData[chunkSize:]
chunkedData = chunkedData[2:]
else:
chunkedData = ""
else:
out.verbose(" " + "Warning: Parsing error! (Note: Usually due to some special situation in the PCAP leading to interpretation mistakes in Tcpflow.)")
out.verbose("---- Content already merged ----")
out.verbose(b"[")
out.verbose(flow.Data)
out.verbose(b"]")
out.verbose("\n--------------------------------")
out.verbose("-------- Content pending -------")
out.verbose(b"[")
out.verbose(chunkedData)
out.verbose(b"]")
out.verbose("\n--------------------------------")
flow.Data = b''.join([flow.Data, chunkedData])
break
# Un-gzipping ...
if (flow.isGzipped):
try:
zlo = zlib.decompressobj(16 + zlib.MAX_WBITS)
sizeCompressed = len(flow.Data)
flow.Data = zlo.decompress(flow.Data)
sizeDecompressed = len(flow.Data)
out.verbose(" " + "Decompressing: " + str(sizeCompressed) + " bytes -> " + str(sizeDecompressed) + " bytes")
except Exception as exc:
out.verbose(" " + 'DECOMPRESSION ERROR: %s' % exc)
# Replacing non-printables ...
if (gl_Verbose):
c = flow.Data.count(b'.')
else:
c = 0
flow.Data = re.sub(b'[^!\"#\$%&\'\(\)\*\+,-\./0-9:;<=>\?@A-Z\[\]\^_`a-z\{\|\}\\\~\t\n\r ]', b'.', flow.Data)
out.verbose(" " + "Replacing non-printables: " + str(flow.Data.count(b'.') - c) + " occurrences")
# Jump to next flow ...
flow = flow.NextFlow
out.verbose("")
out.verbose("")
out.verbose("Effective output ...")
out.verbose("")
# Output ...
flow = flowsCacheRoot
while (flow):
if (ipAddress in flow.Id and tcpPort in flow.Id):
if (flow.OutputPending):
# HTTP request/response pair ...
if (flow.isHttp and flow.isHttpRequest and flow.HttpResponseFlow):
if (not gl_NoSeparators):
if (not gl_NoMetaInformation):
out.normal("\x01[FTS]> HTTP_PAIR_REQUEST: " + flow.SrcIpAddress.decode() + ":" + flow.SrcPort.decode() + " - " + flow.DstIpAddress.decode() + ":" + flow.DstPort.decode() + " <[FTS]\x02\n")
else:
out.normal("\x01[FTS]> HTTP_PAIR_REQUEST <[FTS]\x02\n")
out.normal(flow.HttpHeader.decode())
out.normal("")
if (flow.Data != b""):
out.normal(flow.Data.decode())
flow.OutputPending = False
if (not gl_ChronologicalOrder):
flowr = flow.HttpResponseFlow
if (not gl_NoSeparators):
if (not gl_NoMetaInformation):
out.normal("\x01[FTS]> HTTP_PAIR_RESPONSE: " + flowr.SrcIpAddress.decode() + ":" + flowr.SrcPort.decode() + " - " + flowr.DstIpAddress.decode() + ":" + flowr.DstPort.decode() + " <[FTS]\x02\n")
else:
out.normal("\x01[FTS]> HTTP_PAIR_RESPONSE <[FTS]\x02\n")
out.normal(flowr.HttpHeader.decode())
out.normal("")
out.normal(flowr.Data.decode())
flowr.OutputPending = False
# HTTP request without response ...
elif (flow.isHttp and flow.isHttpRequest and not flow.HttpResponseFlow):
if (not gl_NoSeparators):
if (not gl_NoMetaInformation):
out.normal("\x01[FTS]> HTTP_REQUEST: " + flow.SrcIpAddress.decode() + ":" + flow.SrcPort.decode() + " - " + flow.DstIpAddress.decode() + ":" + flow.DstPort.decode() + " <[FTS]\x02\n")
else:
out.normal("\x01[FTS]> HTTP_REQUEST <[FTS]\x02\n")
out.normal(flow.HttpHeader.decode())
out.normal("")
if (flow.Data != b""):
out.normal(flow.Data.decode())
flow.OutputPending = False
# HTTP response without request ...
elif (flow.isHttp and flow.isHttpResponse and not flow.HttpRequestFlow):
if (not gl_NoSeparators):
if (not gl_NoMetaInformation):
out.normal("\x01[FTS]> HTTP_RESPONSE: " + flow.SrcIpAddress.decode() + ":" + flow.SrcPort.decode() + " - " + flow.DstIpAddress.decode() + ":" + flow.DstPort.decode() + " <[FTS]\x02\n")
else:
out.normal("\x01[FTS]> HTTP_RESPONSE <[FTS]\x02\n")
out.normal(flow.HttpHeader.decode())
out.normal("")
if (flow.Data != b""):
out.normal(flow.Data.decode())
flow.OutputPending = False
# Any non-HTTP flow ...
elif (flow.isHttp and flow.isHttpResponse and not flow.HttpRequestFlow):
if (not gl_NoSeparators):
if (not gl_NoMetaInformation):
out.normal("\x01[FTS]> NON_HTTP: " + flow.SrcIpAddress.decode() + ":" + flow.SrcPort.decode() + " - " + flow.DstIpAddress.decode() + ":" + flow.DstPort.decode() + " <[FTS]\x02\n")
else:
out.normal("\x01[FTS]> NON_HTTP <[FTS]\x02\n")
out.normal(flow.Data.decode())
flow.OutputPending = False
flow = flow.NextFlow
out.finish()
# convert TCP port into tcpflow format
def parseTcpPort(tcpPort):
if (not re.match(r"0*?((?:0|[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))", tcpPort)):
e = str("[ERROR] Not a valid TCP port number!\n")
print(e)
sys.exit(2)
if tcpPort == "":
return ":" + tcpPort
else:
return ":" + ("0000" + tcpPort)[-5:]
# convert IP address into tcpflow format
def parseIpAddress(ipAddress):
if (not re.match(r"^(0*?(0|[1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.0*?(0|[1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.0*?(0|[1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.0*?(0|[1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(?::0*?((?:0|[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])))?)$", ipAddress)):
sys.stderr.normal("[ERROR] Not a valid IP address!\n")
sys.exit(1)
if ipAddress == "":
return ipAddress
else:
ipAddress, b1, b2, b3, b4, tcpPort = re.search(r"^(0*?(0|[1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.0*?(0|[1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.0*?(0|[1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.0*?(0|[1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(?::0*?((?:0|[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])))?)$", ipAddress).groups()
b1 = '00' + (b1)
b2 = '00' + (b2)
b3 = '00' + (b3)
b4 = '00' + (b4)
if (not tcpPort):
tcpPort = ""
else:
tcpPort = parseTcpPort(tcpPort)
ipAddress = b1[-3:] + '.' + b2[-3:] + '.' + b3[-3:] + '.' + b4[-3:] + tcpPort
return ipAddress
def checkTcpflowVersion():
try:
p = sub.Popen(['tcpflow', '-V'], stdout=sub.PIPE, stderr=sub.PIPE)
check = p.communicate()[0]
if b'tcpflow 1.' not in check.lower() and b'tcpflow 2.' not in check.lower():
e = str("[ERROR] Please download 1.0 or higer\nDownload: https://github.com/simsong/tcpflow/")
print(e)
sys.exit(3)
except Exception as e:
print("[ERROR] TCPflow missing. Please download 1.0 or higer.\nDownload: https://github.com/simsong/tcpflow/")
print("Exception: ", e)
sys.exit(4)
def main():
global gl_OutFile
global gl_Verbose
global gl_ChronologicalOrder
global gl_NoHttpHeaders
global gl_NoMetaInformation
global gl_NoSeparators
checkTcpflowVersion()
argparser = argparse.ArgumentParser()
argparser.add_argument("pcapfile", help="The PCAP file you want to process")
argparser.add_argument("-c", "--CHRONOLOGICAL_ORDER", action="store_true", help="Print in chronological order instead of keeping HTTP requests and responses together.")
argparser.add_argument("-H", "--NO_HTTP_HEADERS", action="store_true", help="Suppresses HTTP headers.")
argparser.add_argument("-i", "--IP_ADDRESS", help="Only include packets sent from or to this IP address (with an optional port separated by ':').")
argparser.add_argument("-M", "--NO_META_INFORMATION", action="store_true", help="Suppresses meta-information (i.e. IP-addresses, timestamps, ...).")
argparser.add_argument("-o", "--OUT_FILE", help="Use this file for output instead of stdout.")
argparser.add_argument("-p", "--TCP_PORT", help="Only include packets sent from or to this TCP port number.")
argparser.add_argument("-S", "--NO_SEPARATORS", action="store_true", help="Suppresses flow separators.")
argparser.add_argument("-v", "--VERBOSE", action="store_true", help="Output includes debug information.")
argparser.add_argument("-V", "--VERSION", action="store_true", help="Print version information.")
args = argparser.parse_args()
if (args.VERSION):
print("FollowTcpStream alpha\n")
else:
if args.CHRONOLOGICAL_ORDER:
gl_ChronologicalOrder = True
if args.NO_HTTP_HEADERS:
gl_NoHttpHeaders = True
ipAddress = b""
if args.IP_ADDRESS:
ipAddress = parseIpAddress(args.IP_ADDRESS).encode()
if args.NO_META_INFORMATION:
gl_NoMetaInformation = True
if args.OUT_FILE:
gl_OutFile = args.OUT_FILE
if (gl_OutFile != "" and os.path.isfile(gl_OutFile)):
os.remove(gl_OutFile)
tcpPort = b""
if args.TCP_PORT:
tcpPort = parseTcpPort(args.TCP_PORT).encode()
if args.NO_SEPARATORS:
gl_NoSeparators = True
if args.VERBOSE:
gl_Verbose = True
pcapFile = args.pcapfile
parseFlow(pcapFile, ipAddress, tcpPort)
if __name__ == '__main__':
main()