forked from HariSekhon/Nagios-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_aws_users_mfa_enabled.py
executable file
·134 lines (113 loc) · 4 KB
/
check_aws_users_mfa_enabled.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
#!/usr/bin/env python3
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: 2020-01-08 11:35:39 +0000 (Wed, 08 Jan 2020)
#
# https://github.com/HariSekhon/Nagios-Plugins
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn
# and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/HariSekhon
#
"""
Nagios Plugin to detect AWS IAM users without MFA enabled
Auto excludes users without passwords
Generates an IAM credential report, then parses it to determine the time since each user's password
and access keys were last used, using the most recent timestamps among the password and access keys
one as the last used age of the account
Requires iam:GenerateCredentialReport on resource: *
Uses the Boto python library, read here for the list of ways to configure your AWS credentials:
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html
See also DevOps Python Tools and DevOps Bash Tools repos which have more similar AWS tools
- https://github.com/HariSekhon/DevOps-Python-tools
- https://github.com/HariSekhon/DevOps-Bash-tools
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import csv
import os
import sys
import time
import traceback
from io import StringIO
import boto3
from botocore.exceptions import ClientError
srcdir = os.path.abspath(os.path.dirname(__file__))
libdir = os.path.join(srcdir, 'pylib')
sys.path.append(libdir)
try:
# pylint: disable=wrong-import-position
from harisekhon.utils import log
from harisekhon import NagiosPlugin
except ImportError:
print(traceback.format_exc(), end='')
sys.exit(4)
__author__ = 'Hari Sekhon'
__version__ = '0.2.0'
class AWSUsersMFA(NagiosPlugin):
def __init__(self):
# Python 2.x
super(AWSUsersMFA, self).__init__()
# Python 3.x
# super().__init__()
self.days = None
self.now = None
self.msg = 'AWSUsersMFA msg not defined'
self.ok()
def process_args(self):
self.no_args()
def run(self):
iam = boto3.client('iam')
log.info('generating credentials report')
while True:
result = iam.generate_credential_report()
log.debug('%s', result)
if result['State'] == 'COMPLETE':
log.info('credentials report generated')
break
log.info('waiting for credentials report')
time.sleep(1)
try:
result = iam.get_credential_report()
except ClientError as _:
raise
csv_content = result['Content']
log.debug('%s', csv_content)
filehandle = StringIO(unicode(csv_content))
filehandle.seek(0)
csvreader = csv.reader(filehandle)
headers = next(csvreader)
assert headers[0] == 'user'
assert headers[3] == 'password_enabled'
assert headers[7] == 'mfa_active'
user_count = 0
users_without_mfa_count = 0
for row in csvreader:
if not self.check_user_mfa(row):
users_without_mfa_count += 1
user_count += 1
if users_without_mfa_count:
self.warning()
self.msg = 'AWS users with passwords without MFA enabled = {} out of {} users'\
.format(users_without_mfa_count, user_count)
self.msg += ' | num_users_without_mfa={} num_users={}'.format(users_without_mfa_count, user_count)
@staticmethod
def check_user_mfa(row):
log.debug('processing user: %s', row)
user = row[0]
password_enabled = row[3]
mfa = row[7]
log.debug('user: %s, password enabled: %s, mfa enabled: %s', user, password_enabled, mfa)
if mfa == 'true':
return True
elif password_enabled == 'false':
return True
return False
if __name__ == '__main__':
AWSUsersMFA().main()