-
Notifications
You must be signed in to change notification settings - Fork 11
/
unix-audit.py
executable file
·341 lines (266 loc) · 12.7 KB
/
unix-audit.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
#!/usr/bin/python3
import sys
import os
import re
wildcards = ["*", "any", "all"]
class Command:
def __init__(self, title: str):
self.title = title
self.tags = []
self.platform_tags = []
self.code_block = ""
def append(self, line: str):
self.code_block = self.code_block + line
def __str__(self):
return f"Title: {self.title}\nTags: {self.tags}\nPlatformTags: {self.platform_tags}\nCode: {self.code_block}"
class Commands:
def __init__(self):
self.commands = [] # list of Command objects
def add_or_get(self, title: str):
for command in self.commands:
if command.title == title:
return command
command = Command(title)
self.commands.append(command)
return command
def get_platforms(self):
platforms = set()
for command in self.commands:
for platform in command.platform_tags:
platforms.add(platform)
return platforms
def find_title(self, title: str):
for command in self.commands:
if command.title == title:
return command
return None
def find(self, platform: str, tags: list): # Lists of strings
found_commands_list = []
for command in self.commands:
applicable_platform = False
if command.platform_tags:
# check if platform is a wildcard
if platform in wildcards:
applicable_platform = True
# check if platform matches platform for command
elif platform in command.platform_tags:
applicable_platform = True
if applicable_platform:
applicable_tag = False
if command.tags:
# check if tag is a wildcard
if any([tag in wildcards for tag in tags]):
applicable_tag = True
# check if tag matches tag for command
elif any([tag in command.tags for tag in tags]):
applicable_tag = True
if applicable_tag:
found_commands_list.append(command)
found_commands = Commands()
found_commands.commands = found_commands_list
return found_commands
def get_tags(self):
tags = set()
for command in self.commands:
for tag in command.tags:
tags.add(tag)
return tags
def __str__(self):
return "\n".join([str(command) for command in self.commands])
def __iter__(self):
return iter(self.commands)
def get_script(self):
script = ""
for command in self.commands:
script = script + f"echo \"=== {command.title}\"===\n"
script = script + command.code_block + "\n"
return script
def count(self):
return len(self.commands)
def load_from_directory(self, database_dir: str):
# check if directory exists
if not os.path.isdir(database_dir):
print(f"[E] Database directory not found: {database_dir}. Run with no args for help.")
sys.exit(1)
#print(f"[I] Loading commands from database directory: {database_dir}")
# print number of .md files in directory
num_files = len([filename for filename in os.listdir(database_dir) if filename.endswith(".md")])
#print(f"[I] Found {num_files} .md files in database directory. Parsing...")
for filename in os.listdir(database_dir):
if filename.endswith(".md"):
#print(f"[I] Reading database file {filename}")
with open(os.path.join(database_dir, filename), 'r') as infile:
current_tags_dict = {}
current_platform_tags_dict = {}
section_title = {}
level = None
state = "outside_code_section"
for line in infile.readlines():
line = line.rstrip()
m = re.match(r'^(#+)\s*(.*?)\s*$', line)
if state == "outside_code_section" and m: # need to be careful to ignore shell commends in code blocks
level = len(m.group(1))
section_title[level] = m.group(2)
state = "outside_code_section"
continue
# check if line is like: PlatformTags: linux
m = re.match(r'^\s*PlatformTag:\s*(.*?)\s*$', line)
if m:
platform_tags_string = m.group(1)
current_platform_tags_dict[level] = [tag.strip() for tag in platform_tags_string.split(',')]
continue
# Tags: line
m = re.match(r'^\s*Tags:\s*(.*?)\s*$', line)
if m:
tags_string = m.group(1)
current_tags_dict[level] = [tag.strip() for tag in tags_string.split(',')]
continue
# code block boundary
m = re.match(r'^\s*```\s*$', line)
if m:
if state == "outside_code_section":
state = "in_code_section"
continue
if state == "in_code_section":
state = "outside_code_section"
continue
if state == "outside_code_section":
#print(f"[D] Ignoring: {line}")
continue
if state == "in_code_section":
# Save command from code block
current_title = None
current_tags_list = []
current_platform_tags_list = []
for l in range(1, level+1):
if l in current_tags_dict:
current_tags_list = current_tags_list + current_tags_dict[l]
if l in current_platform_tags_dict:
current_platform_tags_list = current_platform_tags_list + current_platform_tags_dict[l]
if current_title is None:
current_title = section_title[l]
else:
current_title = current_title + " > " + section_title[l]
command = self.add_or_get(current_title)
command.append(f"{line}\n")
command.tags = current_tags_list
command.platform_tags = current_platform_tags_list
continue
# Raise exception. Should never get here
raise Exception(f"Unexpected state: {state}")
#print(f"[I] Parsed {len(self.commands)} commands so far...")
def get_titles(self):
titles = set()
for command in self.commands:
titles.add(command.title)
return set(sorted(titles))
def usage():
print("Usage: unix-audit.py mode args")
print("")
print("Modes:")
print(" python3 unix-audit.py list <database-dir>")
print(" python3 unix-audit.py generate <database-dir> <platform-tag> <other-tag,other-tag,...>")
print(" python3 unix-audit.py compare <database-dir> <platform-tag1> <platform-tag2> <other-tag,other-tag,...>")
print("")
print("List mode - lists the platforms and tags available for other modes. Examples:")
print("")
print(" python3 unix-audit.py list ./checks-database/")
print("")
print("Generate mode - used to generate an audit script from md files. Examples:")
print("")
print(" python3 unix-audit.py generate ./checks-database/ linux all > audit-scripts/linux-audit.sh")
print(" python3 unix-audit.py generate ./checks-database/ aix all > audit-scripts/aix-audit.sh")
print(" python3 unix-audit.py generate ./checks-database/ solaris all > audit-scripts/solaris-audit.sh")
print("")
print("Compare mode - find differences in commands for 2 or more platforms. Examples:")
print("")
print(" python unix-audit.py compare ./checks-database/ all all > compare/comparison.md")
print(" python3 unix-audit.py compare ./checks-database/ linux,solaris > linux-solaris-compare.md")
print(" python3 unix-audit.py compare ./checks-database/ all authentication,logging > linux-solaris-compare.md")
print("")
if __name__ == "__main__":
# parse mode as first command line arg
if len(sys.argv) < 2:
print("[E] Missing mode. Specify 'generate', 'list', or 'compare' as first argument.")
usage()
sys.exit(1)
mode = sys.argv[1]
if mode not in ["generate", "list", "compare"]:
print("[E] Invalid mode. Specify 'generate', 'list', or 'compare' as first argument.")
usage()
sys.exit(1)
# parse database directory as second command line arg
if len(sys.argv) < 3:
print("[E] Missing database directory. Specify database directory as second argument.")
usage()
sys.exit(1)
database_dir = sys.argv[2]
available_commands = Commands()
available_commands.load_from_directory(database_dir)
if available_commands.count() == 0:
print(f"[E] No commands found in database directory: {database_dir}. Check md files are present and formatted correctly.")
usage()
sys.exit(1)
if mode == "list":
print(f"Available platforms: {', '.join(available_commands.get_platforms())}")
print(f"Available tags: {', '.join(available_commands.get_tags())}")
sys.exit(0)
# "generate" and "compare" modes take same arguments
# parse platform tag as third command line arg
if len(sys.argv) < 4:
print("[E] Missing platform tag. Specify platform tag as third argument.")
usage()
sys.exit(1)
platform_tags_string = sys.argv[3]
platform_tags = platform_tags_string.split(',')
# check if each supplied platform_tag is valid in database or is a wildcard
for platform_tag in platform_tags:
if platform_tag not in available_commands.get_platforms() and platform_tag not in wildcards:
print(f"[E] Invalid platform tag: {platform_tag}")
print(f"Available platforms: {', '.join(available_commands.get_platforms())}")
sys.exit(1)
# parse other tags as fourth command line arg
if len(sys.argv) < 5:
print("[E] Missing other tags. Specify other tags as fourth argument.")
usage()
sys.exit(1)
other_tags = sys.argv[4].split(',')
# check if each supplied other_tag is valid in database or is a wildcard
for tag in other_tags:
if tag not in available_commands.get_tags() and tag not in wildcards:
print(f"[E] Invalid tag: {tag}")
print(f"Available tags: {', '.join(available_commands.get_tags())}")
usage()
sys.exit(1)
if mode == "generate":
selected_commands = available_commands.find(platform_tags_string, other_tags)
# print(str(selected_commands))
print(selected_commands.get_script())
sys.exit(0)
if mode == "compare":
selected_commands = {}
titles = {}
if platform_tags_string in wildcards:
platform_tags = sorted(list(available_commands.get_platforms()))
platform_count = len(platform_tags)
for platform_tag in platform_tags:
selected_commands[platform_tag] = available_commands.find(platform_tag, other_tags)
titles[platform_tag] = selected_commands[platform_tag].get_titles()
# strip off first field delimited with >. This is the platform name.
titles[platform_tag] = set([">".join(title.split(">")[1:]).lstrip() for title in titles[platform_tag]])
union = set()
for platform_tag in platform_tags:
union = union.union(titles[platform_tag])
header_row = ["Check Title"] + platform_tags
print(f"| {' | '.join(header_row)} |")
print(f"| {' | '.join(['---' for i in range(platform_count + 1)])} |")
for title in sorted(union):
row = []
row.append(title)
for platform_tag in platform_tags:
if title in titles[platform_tag]:
row.append("Yes")
else:
row.append("No")
print(f"| {' | '.join(row)} |")
sys.exit(0)