-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambda.py
197 lines (188 loc) · 5.58 KB
/
lambda.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
from __future__ import print_function
import json
import urllib2
import boto3
from io import BytesIO
from gzip import GzipFile
print('Loading function')
s3 = boto3.client('s3')
# NOTE change these to configure slack integration
SLACK_HOOK = "https://hooks.slack.com/services/<asdf>/<asdf>/<asdf>" # change me
SLACK_CHANNEL = "general" # change me
SLACK_USER = "trails" # change me
SLACK_ICON = ":shield:"
ACCEPT = ["iam.amazonaws.com"]
WATCHLIST_OK = [
"DeactivateMFADevice",
"DeleteAccessKey",
"DeleteAccountAlias",
"DeleteAccountPasswordPolicy",
"DeleteGroup",
"DeleteGroupPolicy",
"DeleteInstanceProfile",
"DeleteLoginProfile",
"DeleteOpenIDConnectProvider",
"DeletePolicy",
"DeletePolicyVersion",
"DeleteRole",
"DeleteRolePolicy",
"DeleteSAMLProvider",
"DeleteServerCertificate",
"DeleteServiceSpecificCredential",
"DeleteSigningCertificate",
"DeleteSSHPublicKey",
"DeleteUser",
"DeleteUserPolicy",
"DeleteVirtualMFADevice",
"DetachGroupPolicy",
"DetachRolePolicy",
"DetachUserPolicy",
"RemoveClientIDFromOpenIDConnectProvider",
"RemoveRoleFromInstanceProfile",
"RemoveUserFromGroup"
]
WATCHLIST_WARN = [
"AddUserToGroup",
"AttachGroupPolicy",
"AttachRolePolicy",
"AttachUserPolicy",
"ChangePassword",
"CreateAccessKey",
"CreateAccountAlias",
"CreateGroup",
"CreateInstanceProfile",
"CreateLoginProfile",
"CreateOpenIDConnectProvider",
"CreatePolicy",
"CreatePolicyVersion",
"CreateRole",
"CreateSAMLProvider",
"CreateServiceLinkedRole",
"CreateServiceSpecificCredential",
"CreateUser",
"CreateVirtualMFADevice",
"PutGroupPolicy",
"PutRolePolicy",
"PutUserPolicy",
"UpdateAccessKey",
"UpdateAccountPasswordPolicy",
"UpdateAssumeRolePolicy",
"UpdateGroup",
"UpdateLoginProfile",
"UpdateOpenIDConnectProviderThumbprint",
"UpdateRoleDescription",
"UpdateSAMLProvider",
"UpdateServerCertificate",
"UpdateServiceSpecificCredential",
"UpdateSigningCertificate",
"UpdateSSHPublicKey",
"UpdateUser",
"UploadServerCertificate",
"UploadSigningCertificate",
"UploadSSHPublicKey"
]
WATCHLIST_IGNORE = [
"AddClientIDToOpenIDConnectProvider",
"AddRoleToInstanceProfile",
"EnableMFADevice",
"GenerateCredentialReport",
"GetAccessKeyLastUsed",
"GetAccountAuthorizationDetails",
"GetAccountPasswordPolicy",
"GetAccountSummary",
"GetContextKeysForCustomPolicy",
"GetContextKeysForPrincipalPolicy",
"GetCredentialReport",
"GetGroup",
"GetGroupPolicy",
"GetInstanceProfile",
"GetLoginProfile",
"GetOpenIDConnectProvider",
"GetPolicy",
"GetPolicyVersion",
"GetRole",
"GetRolePolicy",
"GetSAMLProvider",
"GetServerCertificate",
"GetSSHPublicKey",
"GetUser",
"GetUserPolicy",
"ListAccessKeys",
"ListAccountAliases",
"ListAttachedGroupPolicies",
"ListAttachedRolePolicies",
"ListAttachedUserPolicies",
"ListEntitiesForPolicy",
"ListGroupPolicies",
"ListGroups",
"ListGroupsForUser",
"ListInstanceProfiles",
"ListInstanceProfilesForRole",
"ListMFADevices",
"ListOpenIDConnectProviders",
"ListPolicies",
"ListPolicyVersions",
"ListRolePolicies",
"ListRoles",
"ListSAMLProviders",
"ListServerCertificates",
"ListServiceSpecificCredentials",
"ListSigningCertificates",
"ListSSHPublicKeys",
"ListUserPolicies",
"ListUsers",
"ListVirtualMFADevices",
"ResetServiceSpecificCredential",
"ResyncMFADevice",
"SetDefaultPolicyVersion",
"SimulateCustomPolicy",
"SimulatePrincipalPolicy"
]
WATCHLIST = WATCHLIST_OK + WATCHLIST_WARN
def lambda_handler(event, context):
message = event['Records'][0]['Sns']['Message']
print(message)
ev = json.loads(message)
bucket = ev["s3Bucket"]
for key in ev["s3ObjectKey"]:
print("getting " + key)
response = s3.get_object(Bucket=bucket, Key=key)
bytestream = BytesIO(response['Body'].read())
body = GzipFile(None, 'rb', fileobj=bytestream).read().decode('utf-8')
j = json.loads(body)
attachments = []
for record in j["Records"]:
if record["eventSource"] in ACCEPT:
if record["eventName"] not in WATCHLIST:
continue
print("found IAM change in log " + key)
attachment = {
"author_name": record["userIdentity"]["arn"],
"title": record["eventName"],
"fields": [
{"title": k, "value": v, "short": True}
for k, v
in record["requestParameters"].iteritems()
],
"color": "ok" if record["eventName"] in WATCHLIST_OK else "warning"
}
attachments.append(attachment)
if attachments:
if len(attachments) > 20:
print("warning! too many attachments")
data = {
"channel": SLACK_CHANNEL,
"username": SLACK_USER,
"icon_emoji": SLACK_ICON,
"attachments": attachments
}
print(json.dumps(data, indent=2))
headers = {"Content-Type": "application/json"}
payload = json.dumps(data)
req = urllib2.Request(SLACK_HOOK, payload, headers)
try:
urllib2.urlopen(req)
except urllib2.HTTPError as e:
print(e)
print(e.read())
return message