Skip to content

Commit 25e242d

Browse files
committed
Fix sometimes not working the summary in json files
1 parent cef71d3 commit 25e242d

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

zeek-summarizer.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,44 @@
2626
log_types = ['conn', 'dns', 'http', 'ssl', 'smb_mapping'] # Added smb_mapping
2727
log_files = defaultdict(list)
2828
for log_type in log_types:
29-
patterns = [f"{log_type}.log", f"{log_type}.*.log", f"{log_type}.*.log.gz"]
29+
patterns = [
30+
f"{log_type}.log",
31+
f"{log_type}.log.gz",
32+
f"{log_type}.*.log",
33+
f"{log_type}.*.log.gz",
34+
]
35+
found = set()
3036
for pattern in patterns:
31-
log_files[log_type] += glob.glob(os.path.join(args.directory, pattern))
37+
found.update(glob.glob(os.path.join(args.directory, pattern)))
38+
log_files[log_type] = sorted(found)
3239

3340
# Read files with TSV header support
3441
def read_lines(filepath):
3542
open_func = gzip.open if filepath.endswith('.gz') else open
3643
mode = 'rt' if filepath.endswith('.gz') else 'r'
3744
fields = []
45+
is_json = None
3846
try:
3947
with open_func(filepath, mode, errors='replace') as f:
40-
for line in f:
41-
if line.startswith('#fields'):
42-
fields = line.strip().split('\t')[1:]
43-
elif not line.startswith('#') and fields:
44-
parts = line.strip().split('\t')
48+
for raw_line in f:
49+
line = raw_line.strip()
50+
if not line:
51+
continue
52+
if line.startswith('#'):
53+
if line.startswith('#fields'):
54+
fields = line.split('\t')[1:]
55+
continue
56+
if is_json is None:
57+
is_json = line.startswith('{')
58+
if is_json:
59+
try:
60+
yield json.loads(raw_line)
61+
except json.JSONDecodeError:
62+
continue
63+
else:
64+
if not fields:
65+
continue
66+
parts = raw_line.rstrip('\n').split('\t')
4567
if len(parts) != len(fields):
4668
continue
4769
yield dict(zip(fields, parts))
@@ -389,4 +411,3 @@ def filter_local_set(ipset):
389411
legacy_ports_dst = sections['dst_ports_as_dst'].most_common(5)
390412
if legacy_ports_dst:
391413
console.print(" 🛡️ Dst Ports (as destination, top 5): " + ', '.join(f"{k} ({v})" for k, v in legacy_ports_dst))
392-

0 commit comments

Comments
 (0)