-
Notifications
You must be signed in to change notification settings - Fork 2
/
harden-dragonflybsd.py
370 lines (323 loc) · 16.4 KB
/
harden-dragonflybsd.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env python
"""
Harden DragonFly BSD system perms, settings.
Set and reset rc, sysctl, login, confs; set file perms, run shell commands
No arguments, only uses settings.ini file in the same directory.
Example: sendmail_enable = "NONE"
"""
__author__ = "Elias Christopher Griffin"
__url__ = "https://www.quadhelion.engineering"
__license__ = "QHELP-OME-NC-ND-NAI"
__copyright__ = "https://www.quadhelion.engineering/QHELP-OME-NC-ND-NAI.html"
__version__ = "3.0.1"
__date__ = "09/17/2023"
__email__ = "elias@quadhelion.engineering"
__status__ = "Production"
from pathlib import Path
from datetime import datetime
import os, re, subprocess, syslog, configparser, shutil, sys
_date = datetime.now()
date_time = _date.strftime("%m/%d/%Y, %H:%M")
config = configparser.ConfigParser()
config.read('settings.ini')
harden_freebsd_log = Path("/var/log/harden-dragonflybsd.log")
rc_conf = Path("/etc/rc.conf")
sysctl_conf = Path("/etc/sysctl.conf")
loader_conf = Path("/boot/loader.conf")
login_conf = Path("/etc/login.conf")
cron_access = Path("/var/cron/allow")
at_access = Path("/var/at/at.allow")
backup_suffix = ".original"
rc_backup = rc_conf.with_name(rc_conf.name + backup_suffix)
sysctl_backup = sysctl_conf.with_name(sysctl_conf.name + backup_suffix)
loader_backup = loader_conf.with_name(loader_conf.name + backup_suffix)
login_backup = login_conf.with_name(login_conf.name + backup_suffix)
def exception_handler(func):
def intake(*args, **kwargs):
try:
func(*args, **kwargs)
except PermissionError as e:
print(f"\n******************\033[38;5;1m Permissions \033[0;0m************************\n")
print(f"Insufficient permissions {e}")
print(f"*******************************************************\n")
except OSError as e:
print(f"\n********************\033[38;5;1m Locked \033[0;0m**************************\n")
print(f"Perhaps file is busy, locked, process blocked, or raced:\n")
print(f"{e}")
print(f"*******************************************************\n")
return intake
# Write to either "syslog" (/var/log/messages) or "script" (/var/log/harden-dragonflybsd.log)
@exception_handler
def writeLog(log_type, content):
harden_freebsd_logwriter = open(harden_freebsd_log, "a")
syslog.openlog("LOG_INFO")
if log_type == "script":
harden_freebsd_logwriter.writelines(content + os.linesep)
elif log_type == "syslog":
syslog.syslog(1, content)
else:
print(f"*******************************************************\n")
print(f"\033[38;5;63m LOG: \033[0;0m {content}")
print(f"*******************************************************\n")
# Make *.original backups of all files only once
if config['SCRIPT']['first_run'] == "True":
try:
harden_freebsd_log.touch()
cron_access.touch()
at_access.touch()
rc_backup.write_bytes(rc_conf.read_bytes())
sysctl_backup.write_bytes(sysctl_conf.read_bytes())
loader_backup.write_bytes(loader_conf.read_bytes())
login_backup.write_bytes(login_conf.read_bytes())
except FileNotFoundError as e:
error_path = Path(e.filename)
print(f"\n********************\033[38;5;1m File Not Found \033[0;0m*******************")
print(f"Filename: {error_path.parts}")
print("*******************************************************\n")
except PermissionError as e:
print(f"\n******************\033[38;5;1m Permissions \033[0;0m************************\n")
print(f"Insufficient permissions {e}")
print(f"*******************************************************\n")
except OSError as e:
print(f"\n********************\033[38;5;1m Locked \033[0;0m**************************\n")
print(f"Perhaps file is busy, locked, process blocked, or raced:\n")
print(f"{e}")
print(f"*******************************************************\n")
else:
writeLog("syslog", "System file backups complete")
with open('settings.ini', 'w') as configfile:
config.set('SCRIPT', 'first_run', 'False')
config.write(configfile)
print(f"\n*********************\033[38;5;76m Success \033[0;0m*************************")
print(f"\033[38;5;75mCreated: \033[0;0m")
print(f" {cron_access.name}, {at_access.name} \n")
print(f"\033[38;5;75mBackups Made: \033[0;0m")
print(f" {rc_backup.name}, {sysctl_backup.name}")
print(f" {login_backup.name}, {loader_backup.name} \n")
print(f"*******************************************************\n")
# Read the system file content
try:
rc_content = rc_conf.read_text(encoding="utf-8")
sysctl_content = sysctl_conf.read_text(encoding="utf-8")
login_content = login_conf.read_text(encoding="utf-8")
loader_content = loader_conf.read_text(encoding="utf-8")
except FileNotFoundError as e:
error_path = Path(e.filename)
writeLog("script", "Error finding file " + error_path)
print(f"\n*******************\033[38;5;1m File Not Found \033[0;0m********************")
print(f"Filename: {error_path.name}")
print(f"Directories used: {error_path.parts}\n")
print("*******************************************************\n")
except PermissionError as e:
print(f"\n******************\033[38;5;1m Permissions \033[0;0m************************\n")
print(f"Permission to read/append {e}")
print(f"{os.stat(rc_conf)}{os.linesep}")
print(f"{os.stat(sysctl_conf)}{os.linesep}")
print(f"{os.stat(loader_conf)}{os.linesep}")
print(f"{os.stat(cron_access)}{os.linesep}")
print(f"{os.stat(at_access)}{os.linesep}")
print(f"{os.stat(login_conf)}{os.linesep}")
print(f"*******************************************************\n")
else:
print(
f"{os.linesep}*********************\033[38;5;75m Running \033[0;0m*************************{os.linesep}"
f"Loaded system files for read/append\n"
f"*******************************************************{os.linesep}"
)
finally:
writeLog("syslog", "Hardening in progress")
print(f"\n********************\033[38;5;75m Info Panel \033[0;0m***********************")
print(f"Executing {__file__}")
print(f"Executing {date_time}")
print(f"*******************************************************\n")
# Main working class dealing with rc.conf and sysctl.conf
class Conf:
def __init__(self, file, setting, flag):
self.file = file
self.setting = setting
self.flag = flag
# Changes the flag from whatever it is currently to flag in settings.ini
def setConf(self):
try:
with open(self.file, 'r+', encoding="us-ascii") as file_content:
lines = file_content.readlines()
for i, line in enumerate(lines):
if line.startswith(self.setting):
lines[i] = self.setting + "=" + self.flag + os.linesep
file_content.seek(0)
for line in lines:
file_content.write(line)
file_content.truncate()
writeLog("script", self.setting + " was set to " + self.flag)
except OSError as e:
print(f"\n********************\033[38;5;1m Locked \033[0;0m**************************\n")
print(f"Perhaps file is busy, locked, process blocked, or raced:\n")
print(f"{e}")
print(f"*******************************************************\n")
else:
print(f"\033[38;5;208m {self.setting} \033[0;0m changed to\033[38;5;208m {self.flag}\033[0;0m in\033[38;5;75m {self.file}\033[0;0m ")
# Appends at the end of a file a directive that was not present previously
def addConf(self):
try:
with open(self.file, 'a') as file_content:
file_content.write(self.setting + "=" + self.flag + os.linesep)
writeLog("script", self.setting + "=" + self.flag + " added")
except OSError as e:
print(f"\n********************\033[38;5;1m Locked \033[0;0m**************************\n")
print(f"Perhaps file is busy, locked, process blocked, or raced:\n")
print(f"{e}")
print(f"*******************************************************\n")
else:
print(f"\033[38;5;63m {self.setting} \033[0;0m added to \033[38;5;75m{self.file}\033[0;0m ")
# Checks to see if the directive is already in the conf, returns True if present.
def checkConf(self) -> bool:
self.found = False
try:
with open(self.file, 'r') as file_content:
lines = file_content.readlines()
for i, line in enumerate(lines):
if line.startswith(self.setting):
self.found = True
else:
pass
return self.found
except OSError as e:
print(f"\n********************\033[38;5;1m Locked \033[0;0m**************************\n")
print(f"Perhaps file is busy, locked, process blocked, or raced:\n")
print(f"{e}")
print(f"*******************************************************\n")
# Checks proper flag and equality syntax in rc.conf and sysctl.conf with first-boot 13.2 directives
# May not work with advanced flags added later
def verifyConf(self):
global conf_directives
conf_directives = []
sysctl_conf_verify = re.compile(r'[^\"]') # No quotes
loader_rc_conf_verify = re.compile(r'^[\"].+[$\"]') # Pair of quotes
try:
with open(self.file, 'r+') as file_content:
lines = file_content.readlines()
for i, line in enumerate(lines):
partitioned_line = line.partition("=")
if line.isspace():
pass
elif line.startswith("#"):
pass
elif partitioned_line[1] != "=":
print(f"\n*******************************************************")
print(f"Error at {lines[i]}: No equality operator. Restored original.")
print(f"*******************************************************\n")
writeLog("script", "No equality operator at line " + lines[i].rstrip() + " in " + self.file.name)
self.restoreConf()
sys.exit()
elif self.file == rc_conf and re.match(loader_rc_conf_verify, partitioned_line[2]) == None:
print(f"\n*******************************************************")
print(f"Error: {self.flag} not allowed in {lines[i]} in {self.file}. Restored original.")
print(f"*******************************************************\n")
writeLog("script", "Quote matching error at line " + lines[i].rstrip())
self.restoreConf()
sys.exit()
elif self.file == loader_conf and re.match(loader_rc_conf_verify, partitioned_line[2]) == None:
print(f"\n*******************************************************")
print(f"Error: {self.flag} not allowed in {lines[i]} in {self.file}. Restored original.")
print(f"*******************************************************\n")
writeLog("script", "Quote matching error at line " + lines[i].rstrip())
self.restoreConf()
sys.exit()
elif self.file == sysctl_conf and re.match(sysctl_conf_verify, partitioned_line[2]) == None:
print(f"\n*******************************************************")
print(f"Error: {self.flag} not allowed in {lines[i].rstrip()} in {self.file}. Restored original.")
print(f"*******************************************************\n")
writeLog("script", "Quote in sysctl.conf at line " + lines[i])
self.restoreConf()
sys.exit()
else:
conf_directives.append(line.rstrip())
except OSError as e:
print(f"\n********************\033[38;5;1m Locked \033[0;0m**************************\n")
print(f"Perhaps file is busy, locked, process blocked, or raced:\n")
print(f"{e}")
print(f"*******************************************************\n")
# If syntax verification fails, restore *.originals to prevent boot failure
# If in single user read-only mode use commands:
# zfs set readonly=false zroot
# zfs mount -a
def restoreConf(self):
try:
if self.file == rc_conf:
shutil.copy(rc_backup, rc_conf)
elif self.file == sysctl_conf:
shutil.copy(sysctl_backup, sysctl_conf)
elif self.file == loader_conf:
shutil.copy(loader_backup, loader_conf)
except FileNotFoundError as e:
error_path = Path(e.filename)
print(f"\n*******************\033[38;5;1m File Not Found \033[0;0m********************")
print(f"Filename: {error_path.name}")
print(f"Directories used: {error_path.parts}\n")
print("*******************************************************\n")
else:
print(f"\n*********************\033[38;5;76m Success \033[0;0m*************************")
print(f"Files restored")
print("*******************************************************\n")
# Hardcoded sections as only t e contain flags we can dynamically set and re-set.
# Loops through all directives and sets each
class SetOpts:
def __init__(self, section):
self.section = section
if self.section == "STARTUP":
file = rc_conf
elif self.section == "SYSTEM":
file = sysctl_conf
elif self.section == "KERNEL":
file = loader_conf
else:
pass
for opt in config[self.section]:
value = config[self.section][opt]
conf_runner = Conf(file, opt, value)
setting_present = conf_runner.checkConf()
if setting_present:
conf_runner.setConf()
conf_runner.verifyConf()
else:
conf_runner.addConf()
conf_runner.verifyConf()
# Run shell commands for named section. Will error if sent setting.ini sections that have no shell commands.
def shellCommand(section):
try:
for opt in config[section]:
value = config[section][opt]
command_result = subprocess.run([value], shell=True, timeout=0.7)
except subprocess.CalledProcessError as e:
syslog.syslog(syslog.LOG_ERR, "Failure: Shell Command")
print(f"\n*********************\033[38;5;1m Shell Error \033[0;0m*********************")
print(f"Command {e.args[1]} failed")
print(f"Terminated by {command_result.returncode}")
print(f"{command_result.stderr}")
print("*******************************************************\n")
except OSError as e:
print(f"\n********************\033[38;5;1m Locked \033[0;0m**************************\n")
print(f"Perhaps file is busy, locked, process blocked, or raced:\n")
print(f"{e}")
print(f"*******************************************************\n")
else:
print(f"\n*********************\033[38;5;76m Success \033[0;0m*************************")
print(f"\033[38;5;208mShell Errors: \033[0;0m {command_result.stdout}")
print(f"*******************************************************\n")
# Set chmod for added convienence of the adminstrator with error handling and logging
@exception_handler
def setChmod(file, setting):
os.fchmod(file, setting)
writeLog("script", file + "was set to " + setting )
# Main
writeLog("script", date_time)
SetOpts("STARTUP")
SetOpts("SYSTEM")
SetOpts("KERNEL")
shellCommand("FILESEC")
shellCommand("USERSEC")
# Write succesfull completion to console and syslog
writeLog("script", "************ SUCCESS ************")
writeLog("script", "All files and directives validate")
writeLog("script", "*********************************")
writeLog("syslog", "SUCCESS: Hardening completed")