Skip to content

Commit c79fda6

Browse files
committed
Added print_communication_matrix_as_csv function.
1 parent 9240557 commit c79fda6

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

fg_log_parser.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
Options:
88
-b --countbytes Count bytes for each communication quartet
99
-h --help Show this message
10-
-v --verbose activate verbose messages
10+
-v --verbose Activate verbose messages
1111
--version Shows version information
1212
-n --noipcheck Do not check if src and dst ip are present
13+
-c --csv Print matrix in csv format (default is netsted format)
1314
1415
Log Format Options (case sensitive):
1516
--srcipfield=<srcipfield> Src ip address field [default: srcip]
1617
--dstipfield=<dstipfield> Dst ip address field [default: dstip]
1718
--dstportfield=<dstportfield> Dst port field [default: dstport]
1819
--protofield=<protofield> Protocol field [default: proto]
1920
21+
2022
If countbytes options is set you may have to specify:
2123
--sentbytesfield=<sentbytesfield> Field for sent bytes [default: sentbyte]
2224
--rcvdbytesfield=<rcvdbytesfield> Field for rcvd bytes [default: rcvdbyte]
@@ -259,6 +261,37 @@ def print_communication_matrix(matrix, indent=0):
259261
print ' ' * (indent+1) + str(value)
260262
return None
261263

264+
def print_communication_matrix_as_csv(matrix, countbytes=False):
265+
"""
266+
Prints communication matrix in csv format.
267+
268+
Example:
269+
>>> matrix = {'192.168.1.1': {'8.8.8.8': {'53': {'UDP': {'count': 1}}}}}
270+
>>> print_communication_matrix_as_csv(matrix)
271+
srcip;dstip;dport;proto;count;sentbytes;rcvdbytes
272+
192.168.1.1;8.8.8.8;53;UDP;1
273+
274+
Example 2 (option countbytes set):
275+
>>> matrix = {'192.168.1.1': {'8.8.8.8': {'53': {'UDP': {'count': 1, 'sentbytes': 10, 'rcvdbytes': 10}}}}}
276+
>>> print_communication_matrix_as_csv(matrix, countbytes=True)
277+
srcip;dstip;dport;proto;count;sentbytes;rcvdbytes
278+
192.168.1.1;8.8.8.8;53;UDP;1;10;10
279+
280+
"""
281+
# Header
282+
print "srcip;dstip;dport;proto;count;sentbytes;rcvdbytes"
283+
for srcip in matrix.keys():
284+
for dstip in matrix.get(srcip):
285+
for dport in matrix[srcip][dstip].keys():
286+
for proto in matrix[srcip][dstip].get(dport):
287+
count = matrix[srcip][dstip][dport][proto].get("count")
288+
if countbytes:
289+
rcvdbytes = matrix[srcip][dstip][dport][proto].get("rcvdbytes")
290+
sentbytes = matrix[srcip][dstip][dport][proto].get("sentbytes")
291+
print "%s;%s;%s;%s;%s;%s;%s" % (srcip, dstip, dport, proto, count, rcvdbytes, sentbytes)
292+
else:
293+
print "%s;%s;%s;%s;%s" % (srcip, dstip, dport, proto, count)
294+
262295

263296
def main():
264297
"""
@@ -273,6 +306,7 @@ def main():
273306
countbytes = arguments['--countbytes']
274307
verbose = arguments['--verbose']
275308
noipcheck = arguments['--noipcheck']
309+
csv = arguments['--csv']
276310

277311
# define logfile format
278312
# note: default values are set in the docopt string, see __doc__
@@ -301,7 +335,10 @@ def main():
301335
# parse log
302336
log.info("Reading firewall log...")
303337
matrix = get_communication_matrix(logfile, logformat, countbytes, noipcheck)
304-
print_communication_matrix(matrix)
338+
if csv:
339+
print_communication_matrix_as_csv(matrix)
340+
else:
341+
print_communication_matrix(matrix)
305342
return 0
306343

307344
if __name__ == "__main__":

0 commit comments

Comments
 (0)