Skip to content

Commit 38d5ec6

Browse files
authored
updated logparse2.py
1 parent 345befe commit 38d5ec6

File tree

1 file changed

+53
-33
lines changed

1 file changed

+53
-33
lines changed

logparse2.py

+53-33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env python3
22

3+
# Written by Mike McPhee, Dec 2022.
4+
# 45Drives
35

46
import re
57
import os
@@ -8,35 +10,51 @@
810
import syslog
911
import subprocess
1012

11-
def processLog2(userdict: dict, obj: dict) -> list:
13+
def processLog2(userdict: dict, connectionActions: dict):
1214

1315
#obj = {}
1416
username = userdict["username"]
1517
localmachine = userdict["localmachine"]
1618
ipaddress = userdict["ipaddress"]
19+
1720

18-
if ipaddress not in obj:
19-
obj[ipaddress]={}
21+
if ipaddress not in connectionActions:
22+
connectionActions[ipaddress]={}
2023

2124

2225
# if username not in obj:
2326
# obj[username] = {}
2427

25-
if localmachine not in obj[ipaddress]:
26-
obj[ipaddress][localmachine] = {}
28+
if localmachine not in connectionActions[ipaddress]:
29+
connectionActions[ipaddress][localmachine] = {}
30+
31+
if username not in connectionActions[ipaddress][localmachine]:
32+
connectionActions[ipaddress][localmachine][username] = {}
33+
connectionActions[ipaddress][localmachine][username]["count"]= 0
34+
connectionActions[ipaddress][localmachine][username]["actions"]= []
35+
connectionActions[ipaddress][localmachine][username]["paths"]={}
2736

28-
if username not in obj[ipaddress][localmachine]:
29-
obj[ipaddress][localmachine][username] = {}
30-
obj[ipaddress][localmachine][username]["count"]= 0
31-
obj[ipaddress][localmachine][username]["actions"]= []
3237

33-
obj[ipaddress][localmachine][username]["count"]+=1
34-
obj[ipaddress][localmachine][username]["actions"].append({
38+
connectionActions[ipaddress][localmachine][username]["count"]+=1
39+
connectionActions[ipaddress][localmachine][username]["actions"].append({
3540
"sharename":userdict["sharename"],
36-
"action":userdict["action"],
41+
"action":userdict["action"].strip('\n'),
3742
"date":userdict["date"]
3843
})
39-
return obj
44+
45+
raw = userdict["action"].strip('\n').split('|')
46+
#print(raw)
47+
for path in raw:
48+
path = path.strip('\n')
49+
50+
#we only want filepaths beginning with "/"
51+
#if "/" in path:
52+
if path.startswith("/"):
53+
#print(path)
54+
if path not in connectionActions[ipaddress][localmachine][username]["paths"]:
55+
connectionActions[ipaddress][localmachine][username]["paths"][path]=0
56+
connectionActions[ipaddress][localmachine][username]["paths"][path]+=1
57+
4058

4159

4260

@@ -47,29 +65,30 @@ def main():
4765

4866
#try:
4967
linelist = []
50-
obj = {}
68+
connectionActions = {}
69+
70+
process = subprocess.Popen("cat /var/log/samba/smb_audit.log", stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8', shell=True)
71+
#process = subprocess.Popen("cat /var/log/samba/smb_audit.log | awk '{$1print $6, $8, $10, $12, $14, $16}'", stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8', shell=True)
5172

52-
process = subprocess.Popen("cat /var/log/samba/smb_audit.log | awk '{print $6, $8, $10, $12, $14, $16}'", stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8', shell=True)
5373
line = process.stdout.readline()
5474
#if line is not null, process it into a dict object
5575
while line:
5676
#print(line)
5777
#example output: IP:192.168.209.99 USER:user MACHINE:45dr-mmcphee SHARENAME:share DATE:2022/12/14 ACTION:|create_file|ok|0x80|file|open|/tank/samba/share
5878

59-
line = line.split(' ')
79+
line = line.split('???')
6080
entry = {"ipaddress":None, "username": None, "localmachine": None, "sharename": None, "date": None, "action": None}
6181

62-
entry["ipaddress"]=line[0].split(':')[1]
63-
entry["username"]=line[1].split(':')[1]
64-
entry["localmachine"]=line[2].split(':')[1]
65-
entry["sharename"]=line[3].split(':')[1]
66-
entry["date"]=line[4].split(':')[1]
67-
entry["action"]=line[5].split(':')[1]
68-
#entry["action"].append(line[5].split(':')[1])
69-
#filepaths = []
82+
entry["ipaddress"]=line[1]
83+
entry["username"]=line[2]
84+
entry["localmachine"]=line[3]
85+
entry["sharename"]=line[4]
86+
entry["date"]=line[5]
87+
entry["action"]=line[6]
88+
7089

7190
linelist.append(entry)
72-
#linelist.append(filepaths)
91+
#linelist.append(filepaths)#
7392

7493
line = process.stdout.readline()
7594

@@ -78,25 +97,26 @@ def main():
7897
for line in linelist:
7998
#print("processing log\n")
8099

81-
obj=processLog2(line, obj)
100+
processLog2(line, connectionActions)
82101

83-
#print("\n",json.dumps(obj, indent=4))
102+
#print("\n",json.dumps(connectionActions, indent=4))
84103
print("#HELP smb_audit_log_entry Number of times each username/machine/ip combination appears in the smb audit log")
85104
print("#TYPE smb_audit_log_entry counter")
86-
for ip in obj:
87-
machines = obj[ip]
105+
for ip in connectionActions:
106+
machines = connectionActions[ip]
88107
#print("\n",json.dumps(key, indent=4))
89108
#print("\n",ip )
90109
for machine in machines:
91110
users = machines[machine]
92111
for user in users:
93112
data = users[user]
113+
94114
print(f"smb_audit_log_entry{{ip={ip},machine={machine},user={user}}} {data['count']}")
115+
for key in data["paths"]:
116+
print(f"\t{key} {data['paths'][key]}")
117+
95118

96-
#break
97-
98-
#except KeyboardInterrupt:
99-
# break
100119

120+
101121
if __name__ == "__main__":
102122
main()

0 commit comments

Comments
 (0)