-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript3.py
More file actions
executable file
·75 lines (67 loc) · 2.23 KB
/
Copy pathscript3.py
File metadata and controls
executable file
·75 lines (67 loc) · 2.23 KB
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
import os
import time
import mysql.connector
# List of important files to monitor
important_files = [
"/etc/passwd",
"/etc/shadow",
"/etc/hostname",
"/etc/sudoers",
"/etc/ssh/sshd_config"
]
# Initialize file modification times
file_last_modified = {file: os.path.getmtime(file) for file in important_files}
# Database connection details
DB_CONFIG = {
"host": "localhost",
"user": "root",
"password": "8520", # Replace with your MariaDB root password
"database": "hids",
"charset"="utf8mb4",
"collation"="utf8mb4_general_ci"
}
# Function to connect to MariaDB
def get_db_connection():
try:
connection = mysql.connector.connect(**DB_CONFIG)
return connection
except mysql.connector.Error as err:
print(f"Error connecting to MariaDB: {err}")
exit(1)
# Function to log the alert in the MariaDB database
def send_alert_to_db(message, file):
try:
conn = get_db_connection()
cursor = conn.cursor()
query = "INSERT INTO alerts (file, message) VALUES (%s, %s)"
cursor.execute(query, (file, message))
conn.commit()
cursor.close()
conn.close()
except mysql.connector.Error as err:
print(f"Error inserting data into MariaDB: {err}")
# Function to send an alert
def alert(message, file):
print(f"ALERT: {message}")
send_alert_to_db(message, file)
# Function to check file integrity
def check_file_integrity():
while True:
for file in important_files:
try:
# Check if the file's last modification time has changed
if os.path.getmtime(file) != file_last_modified[file]:
alert(f"File modified: {file}", file)
file_last_modified[file] = os.path.getmtime(file)
except FileNotFoundError:
alert(f"File missing: {file}", file)
# Reset the modification time for missing files to avoid errors
file_last_modified[file] = None
time.sleep(60)
if __name__ == "__main__":
# Start file integrity monitoring
try:
print("Starting file integrity monitoring...")
check_file_integrity()
except KeyboardInterrupt:
print("Monitoring stopped.")