-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
148 lines (127 loc) · 8.01 KB
/
main.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
import os
import re
import sys
import time
import logging
import pytz
from pyinotify import WatchManager, Notifier, EventsCodes, ProcessEvent
from termcolor import colored
from datetime import datetime
log_file_path = '/var/log/psono_ee/audit.log'
monitored_groups = []
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(message)s'
)
def get_event_type(log_line):
event_type_pattern = r'event=(\w+)'
event_type_match = re.search(event_type_pattern, log_line)
if event_type_match:
return event_type_match.group(1)
else:
return None
def format_date(input_date):
try:
input_datetime = datetime.strptime(input_date, "%Y-%m-%dT%H:%M:%S.%f")
paris_timezone = pytz.timezone('Europe/Paris')
input_datetime_paris = input_datetime.replace(tzinfo=pytz.utc).astimezone(paris_timezone)
formatted_date = input_datetime_paris.strftime("%d/%m/%Y %H:%M:%S")
return formatted_date
except ValueError:
return None
def parse_log_line(log_line):
patterns = [
(
r'^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+) logger=([^,]*), user_ip=(\d+\.\d+\.\d+\.\d+), req_method=(\w+), req_url=([^,]+), success=(\w+), hostname=([\w\d]+), status=(\w+), event=(\w+)$',
[]),
(
r'^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+) logger=([^,]*), user_ip=(\d+\.\d+\.\d+\.\d+), req_method=(\w+), req_url=([^,]+), success=(\w+), hostname=([\w\d]+), status=(\w+), event=(\w+), user=([^,]+), user_id=([\w\d-]+)$',
["user", "user_id"]),
(
r'^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+) logger=([^,]*), user_ip=(\d+\.\d+\.\d+\.\d+), req_method=(\w+), req_url=([^,]+), success=(\w+), hostname=([\w\d]+), status=(\w+), event=(\w+), user=([^,]+), user_id=([\w\d-]+), kwarg_secret_id=([\w\d-]+)$',
["user", "user_id", "kwarg_secret_id"]),
(
r'^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+) logger=([^,]*), user_ip=(\d+\.\d+\.\d+\.\d+), req_method=(\w+), req_url=([^,]+), success=(\w+), hostname=([\w\d]+), status=(\w+), event=(\w+), user=([^,]+), user_id=([\w\d-]+), kwarg_datastore_id=([\w\d-]+)$',
["user", "user_id", "kwarg_datastore_id"]),
(
r'^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+) logger=([^,]*), user_ip=(\d+\.\d+\.\d+\.\d+), req_method=(\w+), req_url=([^,]+), success=(\w+), hostname=([\w\d]+), status=(\w+), event=(\w+), user=([^,]+), user_id=([\w\d-]+), kwarg_link_share_id=([\w\d-]+)$',
["user", "user_id", "kwarg_link_share_id"]),
(
r'^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+) logger=([^,]*), user_ip=(\d+\.\d+\.\d+\.\d+), req_method=(\w+), req_url=([^,]+), success=(\w+), hostname=([\w\d]+), status=(\w+), event=(\w+), errors=([^,]+), user=([^,]+), user_id=([\w\d-]+)$',
["errors", "user", "user_id"]),
(
r'^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+) logger=([^,]*), user_ip=(\d+\.\d+\.\d+\.\d+), req_method=(\w+), req_url=([^,]+), success=(\w+), hostname=([\w\d]+), status=(\w+), event=(\w+), user=([^,]+), user_id=([\w\d-]+), kwarg_security_report_id=([\w\d-]+)$',
["user", "user_id", "kwarg_security_report_id"]),
(
r'^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+) logger=([^,]*), user_ip=(\d+\.\d+\.\d+\.\d+), req_method=(\w+), req_url=([^,]+), success=(\w+), hostname=([\w\d]+), status=(\w+), event=(\w+), user=([^,]+), user_id=([\w\d-]+), kwarg_token_id=([\w\d-]+)$',
["user", "user_id", "kwarg_token_id"]),
(
r'^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+) logger=([^,]*), user_ip=(\d+\.\d+\.\d+\.\d+), req_method=(\w+), req_url=([^,]+), success=(\w+), hostname=([\w\d]+), status=(\w+), event=(\w+), user=([^,]+), user_id=([\w\d-]+), kwarg_share_id=([\w\d-]+)$',
["user", "user_id", "kwarg_share_id"]),
(
r'^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+) logger=([^,]*), user_ip=(\d+\.\d+\.\d+\.\d+), req_method=(\w+), req_url=([^,]+), success=(\w+), hostname=([\w\d]+), status=(\w+), event=(\w+), user=([^,]+), user_id=([\w\d-]+), kwarg_administrated_user_id=([\w\d-]+)$',
["user", "user_id", "kwarg_administrated_user_id"]),
(
r'^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+) logger=([^,]*), user_ip=(\d+\.\d+\.\d+\.\d+), req_method=(\w+), req_url=([^,]+), success=(\w+), hostname=([\w\d]+), status=(\w+), event=(\w+), user=([^,]+), user_id=([\w\d-]+), kwarg_secret_link_id=([\w\d-]+)$',
["user", "user_id", "kwarg_secret_link_id"]),
(
r'^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+) logger=([^,]*), user_ip=(\d+\.\d+\.\d+\.\d+), req_method=(\w+), req_url=([^,]+), success=(\w+), hostname=([\w\d]+), status=(\w+), event=(\w+), user=([^,]+), user_id=([\w\d-]+), kwarg_group_id=([\w\d-]+)$',
["user", "user_id", "kwarg_group_id"]),
(
r'^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+) logger=([^,]*), user_ip=(\d+\.\d+\.\d+\.\d+), req_method=(\w+), req_url=([^,]+), success=(\w+), hostname=([\w\d]+), status=(\w+), event=(\w+), user=([^,]+), user_id=([\w\d-]+), kwarg_ldap_group_map_id=([\w\d-]+)$',
["user", "user_id", "kwarg_ldap_group_map_id"]),
(
r'^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+) logger=([^,]*), user_ip=(\d+\.\d+\.\d+\.\d+), req_method=(\w+), req_url=([^,]+), success=(\w+), hostname=([\w\d]+), status=(\w+), event=(\w+), user=([^,]+), user_id=([\w\d-]+), kwarg_membership_id=([\w\d-]+), kwarg_group_id=([\w\d-]+)$',
["user", "user_id", "kwarg_membership_id", "kwarg_group_id"]),
(
r'^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+) logger=([^,]*), user_ip=(\d+\.\d+\.\d+\.\d+), req_method=(\w+), req_url=([^,]+), success=(\w+), hostname=([\w\d]+), status=(\w+), event=(\w+), user=([^,]+), user_id=([\w\d-]+), kwarg_membership_id=([\w\d-]+), kwarg_group_id=([\w\d-]+), kwarg_group_name=([\w\d-]+), kwarg_membership_user_id=([\w\d-]+), kwarg_membership_user_name=([^,]+)$',
["user", "user_id", "kwarg_membership_id", "kwarg_group_id", "kwarg_group_name", "kwarg_membership_user_id",
"kwarg_membership_user_name"])
]
for pattern, extra_fields in patterns:
match = re.match(pattern, log_line)
if match:
groups = match.groups()
log_data = dict(
zip(["timestamp", "logger", "user_ip", "req_method", "req_url", "success", "hostname", "status",
"event"], groups))
for field in extra_fields:
log_data[field] = groups[len(log_data)]
log_data['timestamp'] = format_date(log_data['timestamp'])
return log_data
print(colored(f"Could not parse log line: {log_line}", 'red'))
return None
# TODO: if group id is in monitored groups then send mail
def monitor_group(log_data):
return
class EventHandler(ProcessEvent):
def __init__(self):
self.log_file_position = os.path.getsize(log_file_path)
def process_IN_MODIFY(self, event):
if event.pathname == log_file_path:
with open(log_file_path, 'r') as log_file:
log_file.seek(self.log_file_position)
new_lines = log_file.readlines()
for line in new_lines:
print(colored("Line Detected : {}".format(get_event_type(line)), 'yellow'))
log_data = parse_log_line(line.strip())
if log_data:
for key, value in log_data.items():
print(colored(f"{key}: {value}", 'cyan'))
print('=' * 60 + '\n')
self.log_file_position = log_file.tell()
def main():
print(colored("Psono Logging Audit Tool", 'green'))
wm = WatchManager()
mask = EventsCodes.FLAG_COLLECTIONS['OP_FLAGS']['IN_MODIFY']
notifier = Notifier(wm, EventHandler())
wm.add_watch(log_file_path, mask)
try:
while True:
notifier.process_events()
if notifier.check_events():
notifier.read_events()
except KeyboardInterrupt:
notifier.stop()
if __name__ == "__main__":
main()