-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathtest_policy_finding.py
More file actions
331 lines (314 loc) · 25.9 KB
/
Copy pathtest_policy_finding.py
File metadata and controls
331 lines (314 loc) · 25.9 KB
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
import unittest
import json
from cloudsplaining.output.policy_finding import PolicyFinding
from cloudsplaining.scan.policy_document import PolicyDocument
from cloudsplaining.shared.exclusions import Exclusions
class TestPolicyFinding(unittest.TestCase):
def test_policy_finding_for_data_exfiltration(self):
test_policy = {
"Version": "2012-10-17",
"Statement": [{"Effect": "Allow", "Action": ["s3:GetObject"], "Resource": "*"}],
}
policy_document = PolicyDocument(test_policy)
# (1) If the user is a member of an excluded group, return True
exclusions_cfg = dict(users=["obama"], groups=["exclude-group"], roles=["MyRole"], policies=["exclude-policy"])
exclusions = Exclusions(exclusions_cfg)
policy_finding = PolicyFinding(policy_document, exclusions)
results = policy_finding.results
expected_results = {
"ServicesAffected": ["s3"],
"PrivilegeEscalation": {
"severity": "high",
"description": '<p>These policies allow a combination of IAM actions that allow a principal with these permissions to escalate their privileges - for example, by creating an access key for another IAM user, or modifying their own permissions. This research was pioneered by Spencer Gietzen at Rhino Security Labs. Remediation guidance can be found <a href="https://rhinosecuritylabs.com/aws/aws-privilege-escalation-methods-mitigation/">here</a>.</p>',
"findings": [],
},
"ResourceExposure": {
"severity": "high",
"description": '<p>Resource Exposure actions allow modification of Permissions to <a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_identity-vs-resource.html">resource-based policies</a> or otherwise can expose AWS resources to the public via similar actions that can lead to resource exposure - for example, the ability to modify <a href="https://docs.aws.amazon.com/ram/latest/userguide/what-is.html">AWS Resource Access Manager</a>.</p>',
"findings": [],
},
"DataExfiltration": {
"severity": "medium",
"description": '<div style="text-align:left"><p>Policies with Data Exfiltration potential allow certain read-only IAM actions without resource constraints, such as <code>s3:GetObject</code>, <code>ssm:GetParameter*</code>, or <code>secretsmanager:GetSecretValue</code>. <br> <ul> <li>Unrestricted <code>s3:GetObject</code> permissions has a long history of customer data leaks.</li> <li><code>ssm:GetParameter*</code> and <code>secretsmanager:GetSecretValue</code> are both used to access secrets.</li> <li><code>rds:CopyDBSnapshot</code> and <code>rds:CreateDBSnapshot</code> can be used to exfiltrate RDS database contents.</li> </ul></p></div>',
"findings": ["s3:GetObject"],
},
"ServiceWildcard": {
"severity": "medium",
"description": '<p>"Service Wildcard" is the unofficial way of referring to IAM policy statements that grant access to ALL actions under a service - like s3:*. Prioritizing the remediation of policies with this characteristic can help to efficiently reduce the total count of issues in the Cloudsplaining report.</p>',
"findings": [],
},
"CredentialsExposure": {
"severity": "high",
"description": "<p>Credentials Exposure actions return credentials as part of the API response , such as ecr:GetAuthorizationToken, iam:UpdateAccessKey, and others. The full list is maintained here: https://gist.github.com/kmcquade/33860a617e651104d243c324ddf7992a</p>",
"findings": [],
},
"InfrastructureModification": {"severity": "low", "description": "", "findings": []},
}
# print(json.dumps(results, indent=4))
self.assertDictEqual(results, expected_results)
def test_policy_finding_for_resource_exposure(self):
test_policy = {
"Version": "2012-10-17",
"Statement": [{"Effect": "Allow", "Action": ["s3:PutObjectAcl"], "Resource": "*"}],
}
policy_document = PolicyDocument(test_policy)
exclusions_cfg = dict()
exclusions = Exclusions(exclusions_cfg)
policy_finding = PolicyFinding(policy_document, exclusions)
results = policy_finding.results
expected_results = {
"ServicesAffected": ["s3"],
"PrivilegeEscalation": {
"severity": "high",
"description": '<p>These policies allow a combination of IAM actions that allow a principal with these permissions to escalate their privileges - for example, by creating an access key for another IAM user, or modifying their own permissions. This research was pioneered by Spencer Gietzen at Rhino Security Labs. Remediation guidance can be found <a href="https://rhinosecuritylabs.com/aws/aws-privilege-escalation-methods-mitigation/">here</a>.</p>',
"findings": [],
},
"ResourceExposure": {
"severity": "high",
"description": '<p>Resource Exposure actions allow modification of Permissions to <a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_identity-vs-resource.html">resource-based policies</a> or otherwise can expose AWS resources to the public via similar actions that can lead to resource exposure - for example, the ability to modify <a href="https://docs.aws.amazon.com/ram/latest/userguide/what-is.html">AWS Resource Access Manager</a>.</p>',
"findings": ["s3:PutObjectAcl"],
},
"DataExfiltration": {
"severity": "medium",
"description": '<div style="text-align:left"><p>Policies with Data Exfiltration potential allow certain read-only IAM actions without resource constraints, such as <code>s3:GetObject</code>, <code>ssm:GetParameter*</code>, or <code>secretsmanager:GetSecretValue</code>. <br> <ul> <li>Unrestricted <code>s3:GetObject</code> permissions has a long history of customer data leaks.</li> <li><code>ssm:GetParameter*</code> and <code>secretsmanager:GetSecretValue</code> are both used to access secrets.</li> <li><code>rds:CopyDBSnapshot</code> and <code>rds:CreateDBSnapshot</code> can be used to exfiltrate RDS database contents.</li> </ul></p></div>',
"findings": [],
},
"ServiceWildcard": {
"severity": "medium",
"description": '<p>"Service Wildcard" is the unofficial way of referring to IAM policy statements that grant access to ALL actions under a service - like s3:*. Prioritizing the remediation of policies with this characteristic can help to efficiently reduce the total count of issues in the Cloudsplaining report.</p>',
"findings": [],
},
"CredentialsExposure": {
"severity": "high",
"description": "<p>Credentials Exposure actions return credentials as part of the API response , such as ecr:GetAuthorizationToken, iam:UpdateAccessKey, and others. The full list is maintained here: https://gist.github.com/kmcquade/33860a617e651104d243c324ddf7992a</p>",
"findings": [],
},
"InfrastructureModification": {"severity": "low", "description": "", "findings": ["s3:PutObjectAcl"]},
}
# print(json.dumps(results, indent=4))
self.assertDictEqual(results, expected_results)
def test_policy_finding_for_privilege_escalation(self):
test_policy = {
"Version": "2012-10-17",
"Statement": [{"Effect": "Allow", "Action": ["iam:CreatePolicyVersion"], "Resource": "*"}],
}
policy_document = PolicyDocument(test_policy)
exclusions_cfg = dict()
exclusions = Exclusions(exclusions_cfg)
policy_finding = PolicyFinding(policy_document, exclusions)
results = policy_finding.results
expected_results = {
"ServicesAffected": ["iam"],
"PrivilegeEscalation": {
"severity": "high",
"description": '<p>These policies allow a combination of IAM actions that allow a principal with these permissions to escalate their privileges - for example, by creating an access key for another IAM user, or modifying their own permissions. This research was pioneered by Spencer Gietzen at Rhino Security Labs. Remediation guidance can be found <a href="https://rhinosecuritylabs.com/aws/aws-privilege-escalation-methods-mitigation/">here</a>.</p>',
"findings": [
{
"type": "CreateNewPolicyVersion",
"actions": ["iam:createpolicyversion"],
}
],
},
"ResourceExposure": {
"severity": "high",
"description": '<p>Resource Exposure actions allow modification of Permissions to <a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_identity-vs-resource.html">resource-based policies</a> or otherwise can expose AWS resources to the public via similar actions that can lead to resource exposure - for example, the ability to modify <a href="https://docs.aws.amazon.com/ram/latest/userguide/what-is.html">AWS Resource Access Manager</a>.</p>',
"findings": ["iam:CreatePolicyVersion"],
},
"DataExfiltration": {
"severity": "medium",
"description": '<div style="text-align:left"><p>Policies with Data Exfiltration potential allow certain read-only IAM actions without resource constraints, such as <code>s3:GetObject</code>, <code>ssm:GetParameter*</code>, or <code>secretsmanager:GetSecretValue</code>. <br> <ul> <li>Unrestricted <code>s3:GetObject</code> permissions has a long history of customer data leaks.</li> <li><code>ssm:GetParameter*</code> and <code>secretsmanager:GetSecretValue</code> are both used to access secrets.</li> <li><code>rds:CopyDBSnapshot</code> and <code>rds:CreateDBSnapshot</code> can be used to exfiltrate RDS database contents.</li> </ul></p></div>',
"findings": [],
},
"ServiceWildcard": {
"severity": "medium",
"description": '<p>"Service Wildcard" is the unofficial way of referring to IAM policy statements that grant access to ALL actions under a service - like s3:*. Prioritizing the remediation of policies with this characteristic can help to efficiently reduce the total count of issues in the Cloudsplaining report.</p>',
"findings": [],
},
"CredentialsExposure": {
"severity": "high",
"description": "<p>Credentials Exposure actions return credentials as part of the API response , such as ecr:GetAuthorizationToken, iam:UpdateAccessKey, and others. The full list is maintained here: https://gist.github.com/kmcquade/33860a617e651104d243c324ddf7992a</p>",
"findings": [],
},
"InfrastructureModification": {
"severity": "low",
"description": "",
"findings": ["iam:CreatePolicyVersion"],
},
}
# print(json.dumps(results, indent=4))
self.assertDictEqual(results, expected_results)
def test_finding_actions_excluded(self):
test_policy = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
# "s3:GetObject",
"logs:CreateLogStream",
"logs:PutLogEvents",
],
"Resource": "*",
}
],
}
policy_document = PolicyDocument(test_policy)
# (1) EXCLUDE actions
exclusions_cfg = {"exclude-actions": ["logs:CreateLogStream", "logs:PutLogEvents"]}
exclusions = Exclusions(exclusions_cfg)
policy_finding = PolicyFinding(policy_document, exclusions)
results = policy_finding.results
expected_results = {
"ServicesAffected": [],
"PrivilegeEscalation": {
"severity": "high",
"description": '<p>These policies allow a combination of IAM actions that allow a principal with these permissions to escalate their privileges - for example, by creating an access key for another IAM user, or modifying their own permissions. This research was pioneered by Spencer Gietzen at Rhino Security Labs. Remediation guidance can be found <a href="https://rhinosecuritylabs.com/aws/aws-privilege-escalation-methods-mitigation/">here</a>.</p>',
"findings": [],
},
"ResourceExposure": {
"severity": "high",
"description": '<p>Resource Exposure actions allow modification of Permissions to <a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_identity-vs-resource.html">resource-based policies</a> or otherwise can expose AWS resources to the public via similar actions that can lead to resource exposure - for example, the ability to modify <a href="https://docs.aws.amazon.com/ram/latest/userguide/what-is.html">AWS Resource Access Manager</a>.</p>',
"findings": [],
},
"DataExfiltration": {
"severity": "medium",
"description": '<div style="text-align:left"><p>Policies with Data Exfiltration potential allow certain read-only IAM actions without resource constraints, such as <code>s3:GetObject</code>, <code>ssm:GetParameter*</code>, or <code>secretsmanager:GetSecretValue</code>. <br> <ul> <li>Unrestricted <code>s3:GetObject</code> permissions has a long history of customer data leaks.</li> <li><code>ssm:GetParameter*</code> and <code>secretsmanager:GetSecretValue</code> are both used to access secrets.</li> <li><code>rds:CopyDBSnapshot</code> and <code>rds:CreateDBSnapshot</code> can be used to exfiltrate RDS database contents.</li> </ul></p></div>',
"findings": [],
},
"ServiceWildcard": {
"severity": "medium",
"description": '<p>"Service Wildcard" is the unofficial way of referring to IAM policy statements that grant access to ALL actions under a service - like s3:*. Prioritizing the remediation of policies with this characteristic can help to efficiently reduce the total count of issues in the Cloudsplaining report.</p>',
"findings": [],
},
"CredentialsExposure": {
"severity": "high",
"description": "<p>Credentials Exposure actions return credentials as part of the API response , such as ecr:GetAuthorizationToken, iam:UpdateAccessKey, and others. The full list is maintained here: https://gist.github.com/kmcquade/33860a617e651104d243c324ddf7992a</p>",
"findings": [],
},
"InfrastructureModification": {"severity": "low", "description": "", "findings": []},
}
# print(json.dumps(results, indent=4))
self.assertDictEqual(results, expected_results)
# (2) When they are not excluded, make sure they show up in results
exclusions_cfg = {}
exclusions = Exclusions(exclusions_cfg)
policy_finding = PolicyFinding(policy_document, exclusions)
results = policy_finding.results
expected_results = {
"ServicesAffected": ["logs"],
"PrivilegeEscalation": {
"severity": "high",
"description": '<p>These policies allow a combination of IAM actions that allow a principal with these permissions to escalate their privileges - for example, by creating an access key for another IAM user, or modifying their own permissions. This research was pioneered by Spencer Gietzen at Rhino Security Labs. Remediation guidance can be found <a href="https://rhinosecuritylabs.com/aws/aws-privilege-escalation-methods-mitigation/">here</a>.</p>',
"findings": [],
},
"ResourceExposure": {
"severity": "high",
"description": '<p>Resource Exposure actions allow modification of Permissions to <a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_identity-vs-resource.html">resource-based policies</a> or otherwise can expose AWS resources to the public via similar actions that can lead to resource exposure - for example, the ability to modify <a href="https://docs.aws.amazon.com/ram/latest/userguide/what-is.html">AWS Resource Access Manager</a>.</p>',
"findings": [],
},
"DataExfiltration": {
"severity": "medium",
"description": '<div style="text-align:left"><p>Policies with Data Exfiltration potential allow certain read-only IAM actions without resource constraints, such as <code>s3:GetObject</code>, <code>ssm:GetParameter*</code>, or <code>secretsmanager:GetSecretValue</code>. <br> <ul> <li>Unrestricted <code>s3:GetObject</code> permissions has a long history of customer data leaks.</li> <li><code>ssm:GetParameter*</code> and <code>secretsmanager:GetSecretValue</code> are both used to access secrets.</li> <li><code>rds:CopyDBSnapshot</code> and <code>rds:CreateDBSnapshot</code> can be used to exfiltrate RDS database contents.</li> </ul></p></div>',
"findings": [],
},
"ServiceWildcard": {
"severity": "medium",
"description": '<p>"Service Wildcard" is the unofficial way of referring to IAM policy statements that grant access to ALL actions under a service - like s3:*. Prioritizing the remediation of policies with this characteristic can help to efficiently reduce the total count of issues in the Cloudsplaining report.</p>',
"findings": [],
},
"CredentialsExposure": {
"severity": "high",
"description": "<p>Credentials Exposure actions return credentials as part of the API response , such as ecr:GetAuthorizationToken, iam:UpdateAccessKey, and others. The full list is maintained here: https://gist.github.com/kmcquade/33860a617e651104d243c324ddf7992a</p>",
"findings": [],
},
"InfrastructureModification": {
"severity": "low",
"description": "",
"findings": ["logs:CreateLogStream", "logs:PutLogEvents"],
},
}
# print(json.dumps(results, indent=4))
self.assertDictEqual(results, expected_results)
def test_data_exfiltration_respects_include_actions(self):
"""GH issue #624: `include-actions` in the exclusions config should surface actions in the
DataExfiltration finding too, not just be silently ignored by that category."""
test_policy = {
"Version": "2012-10-17",
"Statement": [{"Effect": "Allow", "Action": ["s3:ListBucket"], "Resource": "*"}],
}
policy_document = PolicyDocument(test_policy)
exclusions_cfg = {"include-actions": ["s3:ListBucket"]}
exclusions = Exclusions(exclusions_cfg)
policy_finding = PolicyFinding(policy_document, exclusions)
results = policy_finding.results
expected_results = {
"ServicesAffected": ["s3"],
"PrivilegeEscalation": {
"severity": "high",
"description": '<p>These policies allow a combination of IAM actions that allow a principal with these permissions to escalate their privileges - for example, by creating an access key for another IAM user, or modifying their own permissions. This research was pioneered by Spencer Gietzen at Rhino Security Labs. Remediation guidance can be found <a href="https://rhinosecuritylabs.com/aws/aws-privilege-escalation-methods-mitigation/">here</a>.</p>',
"findings": [],
},
"ResourceExposure": {
"severity": "high",
"description": '<p>Resource Exposure actions allow modification of Permissions to <a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_identity-vs-resource.html">resource-based policies</a> or otherwise can expose AWS resources to the public via similar actions that can lead to resource exposure - for example, the ability to modify <a href="https://docs.aws.amazon.com/ram/latest/userguide/what-is.html">AWS Resource Access Manager</a>.</p>',
"findings": [],
},
"DataExfiltration": {
"severity": "medium",
"description": '<div style="text-align:left"><p>Policies with Data Exfiltration potential allow certain read-only IAM actions without resource constraints, such as <code>s3:GetObject</code>, <code>ssm:GetParameter*</code>, or <code>secretsmanager:GetSecretValue</code>. <br> <ul> <li>Unrestricted <code>s3:GetObject</code> permissions has a long history of customer data leaks.</li> <li><code>ssm:GetParameter*</code> and <code>secretsmanager:GetSecretValue</code> are both used to access secrets.</li> <li><code>rds:CopyDBSnapshot</code> and <code>rds:CreateDBSnapshot</code> can be used to exfiltrate RDS database contents.</li> </ul></p></div>',
"findings": ["s3:ListBucket"],
},
"ServiceWildcard": {
"severity": "medium",
"description": '<p>"Service Wildcard" is the unofficial way of referring to IAM policy statements that grant access to ALL actions under a service - like s3:*. Prioritizing the remediation of policies with this characteristic can help to efficiently reduce the total count of issues in the Cloudsplaining report.</p>',
"findings": [],
},
"CredentialsExposure": {
"severity": "high",
"description": "<p>Credentials Exposure actions return credentials as part of the API response , such as ecr:GetAuthorizationToken, iam:UpdateAccessKey, and others. The full list is maintained here: https://gist.github.com/kmcquade/33860a617e651104d243c324ddf7992a</p>",
"findings": [],
},
# s3:ListBucket also shows up here: this is pre-existing, unrelated `include-actions`
# behavior (statement_detail.py forces any `include-actions` entry into the modify-actions
# check regardless of its access level), not something introduced by this fix.
"InfrastructureModification": {"severity": "low", "description": "", "findings": ["s3:ListBucket"]},
}
# print(json.dumps(results, indent=4))
self.assertDictEqual(results, expected_results)
# (2) Without `include-actions`, the same policy must NOT surface s3:ListBucket as a
# Data Exfiltration finding, since it is not in the default READ_ONLY_DATA_EXFILTRATION_ACTIONS list.
exclusions_cfg = {}
exclusions = Exclusions(exclusions_cfg)
policy_finding = PolicyFinding(policy_document, exclusions)
results = policy_finding.results
expected_results = {
"ServicesAffected": [],
"PrivilegeEscalation": {
"severity": "high",
"description": '<p>These policies allow a combination of IAM actions that allow a principal with these permissions to escalate their privileges - for example, by creating an access key for another IAM user, or modifying their own permissions. This research was pioneered by Spencer Gietzen at Rhino Security Labs. Remediation guidance can be found <a href="https://rhinosecuritylabs.com/aws/aws-privilege-escalation-methods-mitigation/">here</a>.</p>',
"findings": [],
},
"ResourceExposure": {
"severity": "high",
"description": '<p>Resource Exposure actions allow modification of Permissions to <a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_identity-vs-resource.html">resource-based policies</a> or otherwise can expose AWS resources to the public via similar actions that can lead to resource exposure - for example, the ability to modify <a href="https://docs.aws.amazon.com/ram/latest/userguide/what-is.html">AWS Resource Access Manager</a>.</p>',
"findings": [],
},
"DataExfiltration": {
"severity": "medium",
"description": '<div style="text-align:left"><p>Policies with Data Exfiltration potential allow certain read-only IAM actions without resource constraints, such as <code>s3:GetObject</code>, <code>ssm:GetParameter*</code>, or <code>secretsmanager:GetSecretValue</code>. <br> <ul> <li>Unrestricted <code>s3:GetObject</code> permissions has a long history of customer data leaks.</li> <li><code>ssm:GetParameter*</code> and <code>secretsmanager:GetSecretValue</code> are both used to access secrets.</li> <li><code>rds:CopyDBSnapshot</code> and <code>rds:CreateDBSnapshot</code> can be used to exfiltrate RDS database contents.</li> </ul></p></div>',
"findings": [],
},
"ServiceWildcard": {
"severity": "medium",
"description": '<p>"Service Wildcard" is the unofficial way of referring to IAM policy statements that grant access to ALL actions under a service - like s3:*. Prioritizing the remediation of policies with this characteristic can help to efficiently reduce the total count of issues in the Cloudsplaining report.</p>',
"findings": [],
},
"CredentialsExposure": {
"severity": "high",
"description": "<p>Credentials Exposure actions return credentials as part of the API response , such as ecr:GetAuthorizationToken, iam:UpdateAccessKey, and others. The full list is maintained here: https://gist.github.com/kmcquade/33860a617e651104d243c324ddf7992a</p>",
"findings": [],
},
"InfrastructureModification": {"severity": "low", "description": "", "findings": []},
}
# print(json.dumps(results, indent=4))
self.assertDictEqual(results, expected_results)