-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathitgc.py
381 lines (372 loc) · 15.5 KB
/
itgc.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
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/python3
from csv import DictWriter, DictReader
from time import time
from argparse import ArgumentParser
from configparser import ConfigParser
from logging import getLogger, basicConfig, INFO
from lib.coreutils import mail_send, get_credentials, ssh_test
from lib import itgcbin
def main():
"""Doing the thing."""
# Setting up logging.
log = getLogger('ITGC_Audit')
basicConfig(
format='%(asctime)s %(name)s %(levelname)s: %(message)s',
datefmt='%m/%d/%Y %H:%M:%S',
level=INFO,
filename='itgc_audit.log'
)
# Setting up an argument parser.
a_parse = ArgumentParser(description='SOX security reviews')
a_parse.add_argument('os', type=str, help='Linux, AIX, Oracle or MySQL')
args = a_parse.parse_args()
# Setting up the results file.
results_write = open('audit_results.csv', 'w')
fields = ['host_name', 'admin_exceptions', 'orphans']
results = DictWriter(results_write, fieldnames=fields)
results.writeheader()
# Setting mail configuration.
config = ConfigParser()
config.read('test.cnf')
mail_info = {
'sender': str(), 'recipients': str(),
'subject': str(), 'server': str(),
'body': str()
}
mail_info['sender'] = config['mail']['sender']
mail_info['recipients'] = config['mail']['recipient']
mail_info['server'] = config['mail']['server']
# Getting audit info.
ossec_server = config['main']['ossec']
if args.os == 'Linux':
LinuxAudit = itgcbin.UnixHostAudit('Linux')
# Variable initialization
monitored_groups = config['linux']['admin_groups'].split(',')
exclusions = config['linux']['exclusions'].split(',')
known_admins = []
admin_file = open(
config['linux']['known_admins'], 'r', encoding='ascii'
)
for admin_group in admin_file:
known_admins.append(admin_group)
linux_host_list = LinuxAudit.get_hosts(ossec_server)
ad_users = LinuxAudit.get_ad_users()
alive_int = len(linux_host_list.get('active_hosts'))
dead_int = len(linux_host_list.get('dead_hosts'))
total_int = alive_int + dead_int
log.info('Beginning Linux ITGC Audit.')
start = time()
# Running the audit for Linux.
for host in linux_host_list.get('active_hosts'):
users = LinuxAudit.get_users(host)
admin_groups = LinuxAudit.get_groups(host, monitored_groups)
if len(users) < 1:
orphans = ['Unable to retrieve users.']
else:
orphans = str(LinuxAudit.get_audit_ex(
users, ad_users, exclusions
))
bad_admins = LinuxAudit.get_admin_ex(
known_admins, admin_groups
)
results.writerow(
{'host_name': host, 'admin_exceptions': bad_admins,
'orphans': orphans}
)
results_write.close()
# Parsing the results of the audit.
results_read = open('audit_results.csv', 'r', newline='')
r_reader = DictReader(results_read)
msg_body = '%d hosts were successfully audited out of %d hosts\n\n' % (
alive_int, total_int
)
for row in r_reader:
msg_body = msg_body + (
'*' * 64 + '\n' +
'%s results:\n' % row['host_name']
)
msg_body = msg_body + 'Accounts without AD account: '
for orphan in list(row['orphans']):
msg_body = msg_body + orphan
msg_body = msg_body + '\n'
msg_body = msg_body + 'Admin Exceptions: '
for exception in list(row['admin_exceptions']):
msg_body = msg_body + exception
msg_body = msg_body + '\n\n'
msg_body = msg_body + (
'*' * 64 + '\n' +
'Active Hosts: %s\n' % (linux_host_list.get('active_hosts')) +
'*' * 64 + '\n' +
'Unreachlable Hosts: %s\n' % (linux_host_list.get('dead_hosts'))
)
end = time()
diff = round(end - start, 2)
msg_body = msg_body + (
'Audit execution time: %d seconds\n' % diff
)
mail_info['body'] = msg_body
mail_info['subject'] = 'SOX Monthly Linux Security Review Report'
# Emailing a report with the audit findings.
mail_send(mail_info)
results_read.close()
log.info('Linux audit completed in %d seconds', diff)
if args.os == 'AIX':
AIXAudit = itgcbin.UnixHostAudit('AIX')
# Variable initilization
monitored_groups = config['aix']['admin_groups'].split(',')
exclusions = config['aix']['exclusions'].split(',')
known_admins = []
admin_file = open(
config['aix']['known_admins'], 'r', encoding='ascii'
)
for admin_group in admin_file:
known_admins.append(admin_group)
aix_known_hosts = config['aix']['known_hosts'].split(',')
aix_host_list = AIXAudit.get_hosts(ossec_server)
for aix_host in aix_known_hosts:
if (aix_host not in aix_host_list['active_hosts'] and
ssh_test(aix_host)):
aix_host_list['active_hosts'].append(aix_host)
else:
aix_host_list['dead_hosts'].append(aix_host)
ad_users = AIXAudit.get_ad_users()
alive_int = len(aix_host_list.get('active_hosts'))
dead_int = len(aix_host_list.get('dead_hosts'))
total_int = alive_int + dead_int
log.info('Beginning AIX ITGC audit.')
start = time()
# Running the audit for AIX.
for host in aix_host_list.get('active_hosts'):
users = AIXAudit.get_users(host)
admin_groups = AIXAudit.get_groups(host, monitored_groups)
if len(users) < 1:
orphans = ['Unable to retrieve users.']
else:
orphans = str(AIXAudit.get_audit_ex(
users, ad_users, exclusions
))
bad_admins = AIXAudit.get_admin_ex(
known_admins, admin_groups
)
results.writerow(
{'host_name': host, 'admin_exceptions': bad_admins,
'orphans': orphans}
)
results_write.close()
# Parsing the results of the audit.
results_read = open('audit_results.csv', 'r', newline='')
r_reader = DictReader(results_read)
msg_body = '%d hosts were audited out of %d hosts\n\n' % (
alive_int, total_int
)
for row in r_reader:
msg_body = msg_body + (
'*' * 64 + '\n' +
'%s results:\n' % row['host_name']
)
msg_body = msg_body + 'Accounts without AD account: '
for orphan in list(row['orphans']):
msg_body = msg_body + orphan
msg_body = msg_body + '\n'
msg_body = msg_body + 'Admin Exceptions: '
for exception in list(row['admin_exceptions']):
msg_body = msg_body + exception
msg_body = msg_body + '\n\n'
msg_body = msg_body + (
'*' * 64 + '\n' +
'Active Hosts: %s\n' % (aix_host_list.get('active_hosts')) +
'*' * 64 + '\n' +
'Unreachlable Hosts: %s\n' % (aix_host_list.get('dead_hosts'))
)
end = time()
diff = round(end - start, 2)
msg_body = msg_body + (
'Audit execution time: %d seconds\n' % diff
)
mail_info['body'] = msg_body
mail_info['subject'] = 'SOX Monthly AIX Security Review Report'
# Emailing a report with the audit findings.
mail_send(mail_info)
results_read.close()
log.info('AIX ITGC audit complete in %d seconds', diff)
if args.os == 'Oracle':
# Setting up the results file.
results_write = open('audit_results.csv', 'w')
fields = ['db_name', 'dba_exceptions', 'orphans',
'schema_prof', 'default_prof']
results = DictWriter(results_write, fieldnames=fields)
results.writeheader()
# Object instantiation
db_audit = itgcbin.OracleDBAudit()
# Variable initialization
db_audit.db_user = config['oracle']['db_user']
scss_dict = {
'api_key': config['oracle']['scss_api'],
'otp': config['oracle']['scss_otp'],
'userid': config['oracle']['scss_user'],
'url': config['oracle']['scss_url']
}
tns_file = '/opt/oracle/instantclient_11_2/network/admin/tnsnames.ora'
env = config['oracle']['environment']
db_pass = get_credentials(scss_dict)
db_hosts = db_audit.get_db_list(tns_file, db_pass, env)
ad_users = db_audit.get_ad_users()
# Creating a list of DBs applicable to the environment.
alive_int = len(db_hosts['active_dbs'])
dead_int = len(db_hosts['dead_dbs'])
total_int = alive_int + dead_int
# Running the audit.
log.info('Beginning Oracle ITGC audit.')
start = time()
for db in db_hosts['active_dbs']:
db_usernames = []
db_admins = []
user_info = db_audit.get_db_users(db_pass, db)
for entry in user_info:
if (entry['profile'] != 'SCHEMA_PROF' and
entry['profile'] != 'DEFAULT'):
db_usernames.append(entry['username'])
# Checking for users past term.
audit_ex = db_audit.get_audit_ex(
db_usernames, ad_users, config['oracle']['exclusions']
)
# Checking for misconfigured profiles.
bad_profiles = db_audit.get_bad_profiles(user_info)
granted_roles = db_audit.get_db_granted_roles(db_pass, db)
for role in granted_roles:
if (role['granted_role'] == 'DBA'):
db_admins.append(role['username'])
# Checking for DBA exceptions.
dba_exception = db_audit.get_admin_ex(
config['oracle']['known_admins'], db_admins
)
# Writing to the results file.
results.writerow(
{'db_name': db,
'dba_exceptions': dba_exception,
'orphans': audit_ex,
'schema_prof': bad_profiles['schema_prof'],
'default_prof': bad_profiles['default_prof']}
)
results_write.close()
# Parsing the results of the audit.
results_read = open('audit_results.csv', 'r', newline='')
r_reader = DictReader(results_read)
msg_body = '%d hosts were successfully audited out of %d hosts\n\n' % (
alive_int, total_int
)
for row in r_reader:
msg_body = msg_body + (
'*' * 64 + '\n' +
'%s results:\n' % row['db_name']
)
msg_body = msg_body + 'Accounts without AD account: '
for orphan in list(row['orphans']):
msg_body = msg_body + orphan
msg_body = msg_body + '\n'
msg_body = msg_body + 'Admin Exceptions: '
for exception in list(row['dba_exceptions']):
msg_body = msg_body + exception
msg_body = msg_body + '\n'
msg_body = msg_body + 'Human users with Schema Profile: '
msg_body = msg_body + row['schema_prof']
msg_body = msg_body + '\n'
msg_body = msg_body + 'Users with Default Profile: '
msg_body = msg_body + row['default_prof']
msg_body = msg_body + '\n\n'
msg_body = msg_body + (
'*' * 64 + '\n' +
'Active DBs: %s\n' % (db_audit.host_list.get('active_dbs')) +
'*' * 64 + '\n' +
'Unreachable DBs: %s\n' % (db_audit.host_list.get('dead_dbs'))
)
end = time()
diff = round(end - start, 2)
msg_body = msg_body + (
'Audit execution time: %d seconds\n' % diff
)
mail_info['body'] = msg_body
mail_info['subject'] = 'SOX Monthly Oracle DB Security Review Report'
# Emailing a report with the audit findings.
mail_send(mail_info)
results_read.close()
log.info('Oracle ITGC audit complete in %d seconds', diff)
if args.os == 'MySQL':
# Setting up the results file.
results_write = open('audit_results.csv', 'w')
fields = ['db_name', 'orphans', 'bad_admins']
results = DictWriter(results_write, fieldnames=fields)
results.writeheader()
# Object instantiation
mysql_audit = itgcbin.MySQLAudit()
# Variable initialization
mysql_audit.db_user = config['mysql']['db_user']
scss_dict = {
'api_key': config['mysql']['scss_api'],
'otp': config['mysql']['scss_otp'],
'userid': config['mysql']['scss_user'],
'url': config['mysql']['scss_url']
}
db_pass = get_credentials(scss_dict)
mysql_hosts = config['mysql']['hosts'].split(',')
known_admins = config['mysql']['admins'].split(',')
ad_users = mysql_audit.get_ad_users()
# Running the audit.
log.info('Beginning MySQL audit.')
start = time()
for db in mysql_hosts:
# Getting MySQL DB users and grants.
mysql_users = mysql_audit.get_mysql_users(db, db_pass)
mysql_grants = mysql_audit.get_mysql_grants(
db, mysql_users, db_pass
)
# Checking to see if the MySQL DB user has a corresponding
# AD account.
mysql_audit_ex = mysql_audit.get_audit_ex(
mysql_users, ad_users, config['oracle']['exclusions']
)
# Checking the list of users with All Priv and other admin
# roles and comparing to a list of known admins.
mysql_allpriv_ex = mysql_audit.get_mysql_allpriv_ex(
mysql_grants, known_admins
)
# Writing to the results file.
results.writerow({
'db_name': db,
'orphans': mysql_audit_ex,
'bad_admins': mysql_allpriv_ex
})
# Closing the file to free it to be read later.
results_write.close()
# Parsing the results of the audit.
results_read = open('audit_results.csv', 'r', newline='')
r_reader = DictReader(results_read)
msg_body = 'MySQL User Audit \n'
for row in r_reader:
msg_body = msg_body + (
'*' * 64 + '\n' +
'%s results:\n' % row['db_name']
)
msg_body = msg_body + 'Accounts without AD account:\n'
for orphan in list(row['orphans']):
msg_body = msg_body + orphan
msg_body = msg_body + '\n'
msg_body = msg_body + 'Unapproved Privileged Grants:\n'
for bad_admin in row['bad_admins'].strip('[]').split(','):
for grant in bad_admin:
msg_body = msg_body + grant
msg_body = msg_body + '\n'
msg_body = msg_body + '\n'
end = time()
elapsed = round(end - start, 2)
msg_body = msg_body + (
'Audit execution time: %d seconds\n' % elapsed
)
mail_info['body'] = msg_body
mail_info['subject'] = 'Monthly MySQL User Security Review Report'
# Emailing a report with the audit findings.
mail_send(mail_info)
results_read.close()
log.info('MySQL audit complete in %d seconds', elapsed)
if __name__ == '__main__':
main()