1
1
#!/usr/bin/env python3
2
2
3
+ # Written by Mike McPhee, Dec 2022.
4
+ # 45Drives
3
5
4
6
import re
5
7
import os
8
10
import syslog
9
11
import subprocess
10
12
11
- def processLog2 (userdict : dict , obj : dict ) -> list :
13
+ def processLog2 (userdict : dict , connectionActions : dict ):
12
14
13
15
#obj = {}
14
16
username = userdict ["username" ]
15
17
localmachine = userdict ["localmachine" ]
16
18
ipaddress = userdict ["ipaddress" ]
19
+
17
20
18
- if ipaddress not in obj :
19
- obj [ipaddress ]= {}
21
+ if ipaddress not in connectionActions :
22
+ connectionActions [ipaddress ]= {}
20
23
21
24
22
25
# if username not in obj:
23
26
# obj[username] = {}
24
27
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" ]= {}
27
36
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" ]= []
32
37
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 ({
35
40
"sharename" :userdict ["sharename" ],
36
- "action" :userdict ["action" ],
41
+ "action" :userdict ["action" ]. strip ( ' \n ' ) ,
37
42
"date" :userdict ["date" ]
38
43
})
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
+
40
58
41
59
42
60
@@ -47,29 +65,30 @@ def main():
47
65
48
66
#try:
49
67
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)
51
72
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 )
53
73
line = process .stdout .readline ()
54
74
#if line is not null, process it into a dict object
55
75
while line :
56
76
#print(line)
57
77
#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
58
78
59
- line = line .split (' ' )
79
+ line = line .split ('??? ' )
60
80
entry = {"ipaddress" :None , "username" : None , "localmachine" : None , "sharename" : None , "date" : None , "action" : None }
61
81
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
+
70
89
71
90
linelist .append (entry )
72
- #linelist.append(filepaths)
91
+ #linelist.append(filepaths)#
73
92
74
93
line = process .stdout .readline ()
75
94
@@ -78,25 +97,26 @@ def main():
78
97
for line in linelist :
79
98
#print("processing log\n")
80
99
81
- obj = processLog2 (line , obj )
100
+ processLog2 (line , connectionActions )
82
101
83
- #print("\n",json.dumps(obj , indent=4))
102
+ #print("\n",json.dumps(connectionActions , indent=4))
84
103
print ("#HELP smb_audit_log_entry Number of times each username/machine/ip combination appears in the smb audit log" )
85
104
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 ]
88
107
#print("\n",json.dumps(key, indent=4))
89
108
#print("\n",ip )
90
109
for machine in machines :
91
110
users = machines [machine ]
92
111
for user in users :
93
112
data = users [user ]
113
+
94
114
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
+
95
118
96
- #break
97
-
98
- #except KeyboardInterrupt:
99
- # break
100
119
120
+
101
121
if __name__ == "__main__" :
102
122
main ()
0 commit comments