-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfim.py
More file actions
186 lines (139 loc) · 6.01 KB
/
fim.py
File metadata and controls
186 lines (139 loc) · 6.01 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import os # To walk through directories and get file paths
import hashlib # To perform SHA-265 hashing
import json # To save and load the baseline file
import argparse
import logging
import configparser
import fnmatch
from datetime import datetime
BUFFER_SIZE = 65536 # Read files in 64kb chunks
BASELINE_FILE = "baseline.json"
def setup_logging():
logger = logging.getLogger('FIM')
logger.setLevel(logging.INFO)
file_handler = logging.FileHandler('fim.log')
file_formatter = logging.Formatter(
'%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(file_formatter)
console_handler = logging.StreamHandler()
console_formatter = logging.Formatter('%(message)s')
console_handler.setFormatter(console_formatter)
logger.addHandler(file_handler)
logger.addHandler(console_handler)
return logger
logger = setup_logging()
def load_config():
config = configparser.ConfigParser()
exclusions = {'dirs': [], 'files': []}
if os.path.exists('config.ini'):
config.read('config.ini')
if 'Exclusions' in config:
exclusions['dirs'] = config['Exclusions'].get(
'exclude_dirs', '').split(',')
exclusions['files'] = config['Exclusions'].get(
'exclude_files', '').split(',')
exclusions['dirs'] = [p.strip()
for p in exclusions['dirs'] if p.strip()]
exclusions['files'] = [p.strip()
for p in exclusions['files'] if p.strip()]
logger.info("Loaded exclusion config: %s", exclusions)
return exclusions
def get_file_hash(filepath):
sha256_hash = hashlib.sha256()
try:
with open(filepath, "rb") as f:
while True:
data = f.read(BUFFER_SIZE) # Read file in chunks
if not data:
break
sha256_hash.update(data)
except IOError:
return None
return sha256_hash.hexdigest()
def is_excluded(path, exclusions):
for pattern in exclusions['dirs']:
if fnmatch.fnmatch(path, pattern) or fnmatch.fnmatch(os.path.basename(path), pattern):
return True
for pattern in exclusions['files']:
if fnmatch.fnmatch(os.path.basename(path), pattern):
return True
return False
def create_basline(directory, exclusions):
baseline = {}
print(f"[*] Creating baseline for directory: {directory}")
for dirpath, dirnames, filenames in os.walk(directory):
dirnames[:] = [d for d in dirnames if not is_excluded(
os.path.join(dirpath, d), {'dirs': exclusions['dirs'], 'files': []})]
for filename in filenames:
filepath = os.path.join(dirpath, filename)
if is_excluded(filepath, exclusions):
continue
file_hash = get_file_hash(filepath)
if file_hash:
relative_path = os.path.relpath(filepath, directory)
baseline[relative_path] = file_hash
with open(BASELINE_FILE, "w") as f:
json.dump(baseline, f, indent=4)
print(f"[+] Baseline created successfully! {len(baseline)} files scanned.")
def check_integrity(directory, exclusions):
try:
with open(BASELINE_FILE, "r") as f:
baseline = json.load(f)
except FileNotFoundError:
print(
f"[-] Baseline file '{BASELINE_FILE}' not found. Please create one using 'init'.")
return
print(f"[*] Checking integrity for directory: {directory}")
current_state = {}
for dirpath, dirnames, filenames in os.walk(directory):
dirnames[:] = [d for d in dirnames if not is_excluded(
os.path.join(dirpath, d), {'dirs': exclusions['dirs'], 'files': []})]
for filename in filenames:
filepath = os.path.join(dirpath, filename)
if is_excluded(filepath, exclusions):
continue
file_hash = get_file_hash(filepath)
if file_hash:
relative_path = os.path.relpath(filepath, directory)
current_state[relative_path] = file_hash
baseline_files = set(baseline.keys())
current_files = set(current_state.keys())
new_files = current_files - baseline_files
deleted_files = baseline_files - current_files
modified_files = set()
for file in baseline_files.intersection(current_files):
if baseline[file] != current_state[file]:
modified_files.add(file)
report_header = f"Integrity Check Report ({datetime.now()})"
logger.info("\n" + "-"*len(report_header) + "\n" +
report_header + "\n" + "-"*len(report_header))
if not new_files and not deleted_files and not modified_files:
logger.info("Everything is OK. No changes detected.")
else:
if new_files:
logger.warning("New files detected (%d):", len(new_files))
for file in new_files:
logger.warning(" - %s", file)
if deleted_files:
logger.warning("Deleted files detected (%d):", len(deleted_files))
for file in deleted_files:
logger.warning(" - %s", file)
if modified_files:
logger.warning("Modified files detected (%d):",
len(modified_files))
for file in modified_files:
logger.warning(" - %s", file)
logger.info("-" * len(report_header))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="File Integrity Monitor")
parser.add_argument("mode", choices=[
"init", "check"], help="The mode to run the script in: 'init' or 'check'")
parser.add_argument("directory", help="The directory to monitor")
args = parser.parse_args()
exclusions = load_config()
if not os.path.isdir(args.directory):
print(f"[!] Error: Directory '{args.directory}' not found.")
elif args.mode == "init":
create_basline(args.directory, exclusions)
elif args.mode == "check":
check_integrity(args.directory, exclusions)