-
Notifications
You must be signed in to change notification settings - Fork 93
/
statement.py
958 lines (841 loc) · 37.6 KB
/
statement.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
import jsoncfg
import re
from . import (
iam_definition,
is_arn_match,
expand_action,
UnknownActionException,
UnknownPrefixException,
)
from .finding import Finding
from .misc import make_list
def is_condition_key_match(document_key, str):
""" Given a documented condition key and one from a policy, determine if they match
Examples:
- s3:prefix and s3:prefix obviously match
- s3:ExistingObjectTag/<key> and s3:ExistingObjectTag/backup match
"""
# Normalize both
document_key = document_key.lower()
str = str.lower()
# Check if the document key has a pattern match in it
if "$" in document_key:
# Some services use a format like license-manager:ResourceTag/${TagKey}
if str.startswith(document_key.split("$")[0]):
return True
elif "<" in document_key:
# Some services use a format like s3:ExistingObjectTag/<key>
if str.startswith(document_key.split("<")[0]):
return True
elif "tag-key" in document_key:
# Some services use a format like secretsmanager:ResourceTag/tag-key
if str.startswith(document_key.split("tag-key")[0]):
return True
# Just return whether they match exactly
return document_key == str
def get_privilege_info(service, action):
"""
Given a service, like "s3"
and an action, like "ListBucket"
return the info from the docs about that action, along with some of the info from the docs
"""
for service_info in iam_definition:
if service_info["prefix"] == service:
for privilege_info in service_info["privileges"]:
if privilege_info["privilege"] == action:
privilege_info["service_resources"] = service_info["resources"]
privilege_info["service_conditions"] = service_info["conditions"]
return privilege_info
raise UnknownActionException("Unknown action {}:{}".format(service, action))
def get_arn_format(resource_type, service_resources):
"""
Search through the service_resources for the given resource_type.
Example, a resource_type of "object*", given the S3 resources,
should return "arn:.*?:s3:::.*?/.*?"
Raises an exception if the resource_type cannot be found.
"""
# Get rid of the asterisk
resource_type = resource_type.replace("*", "")
# Search through the resource service resources for this resource type
for resource_definition in service_resources:
if resource_type == resource_definition["resource"]:
# The resource["arn"] looks like "arn:${Partition}:s3:::${BucketName}/${ObjectName}"
# We need it to look like "arn:.*?:s3:::.*?/.*?" for matching
# This does a minimal (non-greedy) match
arn_format = re.sub(r"\$\{.*?\}", "*", resource_definition["arn"])
# Get the compiled regex
return arn_format
raise Exception(
"Could not find the resource type {} in the service definition. {}".format(
resource_type, service_resources
)
)
def is_valid_region(str):
region_regex = re.compile("^([a-z]{2}|us-gov)-[a-z]+-[0-9]$")
if str == "" or str == "*" or region_regex.match(str):
return True
return False
def is_valid_account_id(str):
# TODO I may want to check for common place holder values for account ids,
# such as 000000000000 and 123456789012
account_id_regex = re.compile("^[0-9]{12}$")
if str == "" or str == "*" or account_id_regex.match(str):
return True
return False
OPERATORS = {
# String
"StringEquals": "String",
"StringNotEquals": "String",
"StringEqualsIgnoreCase": "String",
"StringNotEqualsIgnoreCase": "String",
"StringLike": "String",
"StringNotLike": "String",
# Number
"NumericEquals": "Number",
"NumericNotEquals": "Number",
"NumericLessThan": "Number",
"NumericLessThanEquals": "Number",
"NumericGreaterThan": "Number",
"NumericGreaterThanEquals": "Number",
# Date
"DateEquals": "Date",
"DateNotEquals": "Date",
"DateLessThan": "Date",
"DateLessThanEquals": "Date",
"DateGreaterThan": "Date",
"DateGreaterThanEquals": "Date",
# Bool
"Bool": "Bool",
"Null": "Bool",
# Binary
"BinaryEquals": "Binary",
# Ip
"IpAddress": "Ip",
"NotIpAddress": "Ip",
# Arn
"ArnEquals": "Arn",
"ArnLike": "Arn",
"ArnNotEquals": "Arn",
"ArnNotLike": "Arn",
}
GLOBAL_CONDITION_KEYS = {
"aws:CalledVia": "String",
"aws:CalledViaFirst": "String",
"aws:CalledViaLast": "String",
"aws:CurrentTime": "Date",
"aws:EpochTime": "Date", # This needs to accept Date or Numeric
"aws:MultiFactorAuthAge": "Numeric",
"aws:MultiFactorAuthPresent": "Bool",
"aws:PrincipalOrgID": "String",
"aws:PrincipalArn": "Arn",
"aws:RequestedRegion": "String",
"aws:SecureTransport": "Bool",
"aws:UserAgent": "String",
# Keys Available for Some Services
"aws:PrincipalTag/*": "String",
"aws:PrincipalType": "String",
"aws:Referer": "String",
"aws:RequestTag/*": "String",
"aws:ResourceTag/*": "String",
"aws:SourceAccount": "String",
"aws:SourceArn": "Arn",
"aws:SourceIp": "Ip",
"aws:SourceVpc": "String",
"aws:SourceVpce": "String",
"aws:TagKeys": "String",
"aws:TokenIssueTime": "Date",
"aws:userid": "String",
"aws:username": "String",
"aws:VpcSourceIp": "Ip",
}
def get_global_key_type(str):
"""
Given a global key, return it's type, or None if not found.
Examples:
"aws:CurrentTime" -> "Date"
"aws:currenttime" -> "Date"
"test" -> None
"""
str = str.lower()
for key in GLOBAL_CONDITION_KEYS:
if "*" in key:
if str.startswith(key.split("*")[0].lower()):
return GLOBAL_CONDITION_KEYS[key]
elif str == key.lower():
return GLOBAL_CONDITION_KEYS[key]
return None
def is_value_in_correct_format_for_type(type_needed, values):
"""
Given a documented type needed such as "Arn", return True if all values match.
For example, if you have a condition of:
"Condition": {"DateGreaterThan" :{"aws:CurrentTime" : "2019-07-16T12:00:00Z"}}
Then this function would end up being called with:
- type_needed: Date
- values: ["2019-07-16T12:00:00Z"]
This would return True.
"""
type_needed = translate_documentation_types(type_needed)
regex_patterns = {
"Arn": "^arn:.*:.*:.*:.*:.*$",
# Binary is a base64 encoded value, like "QmluYXJ5VmFsdWVJbkJhc2U2NA=="
"Binary": "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$",
"Bool": "^(true)|(false)$",
"Date": "^\d{4}-\d{2}-\d{2}T\d\d:\d\d:\d\dZ$",
# Ip is either IPv4 or IPv6 (ex. 203.0.113.0/24 or 2001:DB8:1234:5678::/64)
# and may not have a range specified (ex. /32)
"Ip": "^(\d+.\d+.\d+.\d+(/\d+)?)|\d*:\d*:\d*:\d*:\d*:\d*(/\d+)?$",
"Number": "^\d+$",
"String": ".*", # Strings can be anything
}
for value in values:
# Convert the value to a string, which is need for Bool
# The json file should contain true or false, but due to being converted to a Python structure
# this will be capitalized as True or False.
if isinstance(value, bool):
value = "{}".format(value).lower()
if type_needed not in regex_patterns:
raise Exception("Unknown type: {}".format(type_needed))
regex = re.compile(regex_patterns[type_needed])
if not regex.match(value):
return False
return True
def translate_documentation_types(str):
"""
The docs use different type names, so this standardizes them.
Example: The condition secretsmanager:RecoveryWindowInDays is listed as using a "Long"
So return "Number"
"""
if str in ["Arn", "ARN"]:
return "Arn"
elif str in ["Bool", "Boolean"]:
return "Bool"
elif str in ["Date"]:
return "Date"
elif str in ["Long", "Numeric"]:
return "Number"
elif str in ["String", "string", "ArrayOfString"]:
return "String"
elif str in ["Ip"]:
return "Ip"
else:
raise Exception("Unknown data format: {}".format(str))
class Statement:
findings = []
resource_star = {}
effect_allow = True
stmt = None
sid = None
policy_id = None
_is_valid = True
def set_policy_identifier(self, policy_id):
self.policy_id = policy_id
def __init__(self, stmt, analyze=True):
self.findings = []
self.resource_star = {}
self.stmt = stmt
if analyze:
if not self.analyze_statement():
# Statement is malformed
self._is_valid = False
return
def __str__(self):
return json.dumps(self.stmt, indent=2)
@property
def is_valid(self):
return self._is_valid
def in_actions(self, privilege_prefix, privilege_name):
"""
Given "s3" "GetObject", determine if the privilege is in this statement.
This could happen either because the Action is ["s3:GetObject"] or ["s3:*", "ec2:*"]
or because the action is not in the NotAction. For example, if we have an Allow on NotAction "ec2:*",
then this, with "s3" "GetObject" returns True.
"""
if "Action" in self.stmt:
for action in make_list(self.stmt["Action"]):
if action.value == "*" or action.value == "*:*":
return True
expanded_actions = expand_action(action.value, raise_exceptions=False)
for action_struct in expanded_actions:
if (
action_struct["service"] == privilege_prefix
and action_struct["action"] == privilege_name
):
return True
return False
# Else, we're dealing with a NotAction
for action in make_list(self.stmt["NotAction"]):
if action == "*" or action == "*:*":
# I don't think it makes sense to have a "NotAction" of "*", but I'm including this check anyway.
return False
for action_struct in expand_action(action.value, raise_exceptions=False):
if (
action_struct["service"] == privilege_prefix
and action_struct["action"] == privilege_name
):
return False
return True
def get_resources_for_privilege(self, privilege_prefix, privilege_name):
"""
If this privilege is allowed or denied by this statement, return the relevant resources.
Else return None.
For example, if the statement has Actions 's3:*', and resources
["arn:aws:s3:::bucket", "arn:aws:s3:::bucket/*"]
and the privilege given is 's3:PutBucketPolicy' this should return ["arn:aws:s3:::bucket"],
as the other resource is only applicable to object related privileges.
If the privilege given is 's3:ListAllMyBuckets' this should return None, as that privilege does not
apply to these resources.
"""
if not self.in_actions(privilege_prefix, privilege_name):
# Given privilege is unrelated to this statement
return []
if "NotResource" in self.stmt:
# TODO How should I handle NotResource?
return ["*"]
affected_resources = []
privilege_info = get_privilege_info(privilege_prefix, privilege_name)
# Iterate through the resources defined in the action definition
for resource_type in privilege_info["resource_types"]:
resource_type = resource_type["resource_type"]
# Only check the required resources which have a "*" at the end
if "*" not in resource_type:
continue
arn_format = get_arn_format(
resource_type, privilege_info["service_resources"]
)
# At least one resource has to match the action's required resources
for resource in make_list(self.stmt["Resource"]):
if is_arn_match(resource_type, arn_format, resource.value):
affected_resources.append(resource.value)
elif resource.value == "*":
affected_resources.append(resource.value)
# Ensure we match on "*"
for resource in make_list(self.stmt["Resource"]):
if resource.value == "*":
affected_resources.append(resource.value)
return affected_resources
def add_finding(self, finding, detail=None, location={}):
"""
Add finding to the class.
finding: String specifiying the problem
location: Dictionary with information about where this problem is. Often set to:
{"location": "string"}
"""
if "jsoncfg.config_classes.ConfigJSONObject" in str(type(location)):
node_location = jsoncfg.node_location(location)
location = {"line": node_location.line, "column": node_location.column}
elif "jsoncfg.config_classes.ConfigJSONScalar" in str(type(location)):
node_location = jsoncfg.node_location(location)
location = {"string": location.value}
location["line"] = node_location.line
location["column"] = node_location.column
elif "tuple" in str(type(location)):
node_location = jsoncfg.node_location(location[1])
location = {"string": location[0]}
location["line"] = node_location.line
location["column"] = node_location.column
elif "jsoncfg.config_classes.ConfigJSONScalar" in str(
type(location.get("string", ""))
):
node_location = jsoncfg.node_location(location["string"])
location["line"] = node_location.line
location["column"] = node_location.column
location["string"] = location["string"].value
elif "tuple" in str(type(location.get("string", ""))):
node_location = jsoncfg.node_location(location["string"][1])
location["line"] = node_location.line
location["column"] = node_location.column
location["string"] = location["string"][0]
elif "jsoncfg.config_classes" in str(type(location.get("string", ""))):
location["string"] = location["string"][0]
self.findings.append(Finding(finding, detail, location))
def _check_principal(self, principal_element):
"""
Checks that the Principal (or NotPrincipal) element conforms to expectations
"""
for principal in make_list(principal_element):
if jsoncfg.node_is_scalar(principal):
if principal.value == "*":
continue
else:
self.add_finding("UNKNOWN_PRINCIPAL", location=principal)
continue
# We have a ConfigJSONObject
for json_object in principal:
key = json_object[0]
if key == "AWS":
for aws_principal in make_list(json_object[1]):
text = aws_principal.value
account_id_regex = re.compile("^\d{12}$")
arn_regex = re.compile("^arn:[-a-z\*]*:iam::(\d{12}|):.*$")
if text == "*":
pass
elif account_id_regex.match(text):
pass
elif arn_regex.match(text):
pass
else:
self.add_finding(
"UNKNOWN_PRINCIPAL", location=principal, detail=text
)
elif key == "Federated":
for federation in make_list(json_object[1]):
federation = federation.value
saml_regex = re.compile(
"^arn:[-a-z\*]*:iam::\d{12}:saml-provider/.*$"
)
if federation in [
"cognito-identity.amazonaws.com",
"www.amazon.com",
"graph.facebook.com",
"accounts.google.com",
]:
pass
elif saml_regex.match(federation):
pass
else:
self.add_finding(
"UNKNOWN_FEDERATION_SOURCE",
location=principal,
detail=federation,
)
elif key == "Service":
# This should be something like apigateway.amazonaws.com
# I don't know what all the restrictions could be though.
pass
else:
self.add_finding("UNKNOWN_PRINCIPAL", location=principal)
return True
def _check_condition(self, operator, condition_block, expanded_actions):
"""
operator is something like "StringLike"
condition_block is something like {"s3:prefix":["home/${aws:username}/*"]}
"""
operator_type_requirement = None
for documented_operator in OPERATORS:
op = documented_operator.lower()
if operator.lower() in [
op,
op + "ifexists",
"forallvalues:" + op,
"foranyvalue:" + op,
"forallvalues:" + op + "ifexists",
"foranyvalue:" + op + "ifexists",
]:
operator_type_requirement = OPERATORS[documented_operator]
break
if operator_type_requirement is None:
self.add_finding(
"UNKNOWN_OPERATOR",
detail=operator,
location=condition_block,
)
if operator_type_requirement == "Bool":
# Get the value that is being compared against
for c in condition_block:
value = str(c[1].value).lower()
if value != "true" and value != "false":
self.add_finding(
"MISMATCHED_TYPE_OPERATION_TO_NULL", location=condition_block
)
return False
for block in condition_block:
key = block[0]
values = []
for v in make_list(block[1]):
values.append(v.value)
# Check for known bad pattern
if operator.lower() == "bool":
if key.lower() == "aws:MultiFactorAuthPresent".lower() and "false" in values:
self.add_finding(
"BAD_PATTERN_FOR_MFA",
detail='The condition {"Bool": {"aws:MultiFactorAuthPresent":"false"}} is bad because aws:MultiFactorAuthPresent may not exist so it does not enforce MFA. You likely want to use a Deny with BoolIfExists.',
location=condition_block,
)
elif operator.lower() == "null":
if key.lower == "aws:MultiFactorAuthPresent".lower() and "false" in values:
self.add_finding(
"BAD_PATTERN_FOR_MFA",
detail='The condition {"Null": {"aws:MultiFactorAuthPresent":"false"}} is bad because aws:MultiFactorAuthPresent it does not enforce MFA, and only checks if the value exists. You likely want to use an Allow with {"Bool": {"aws:MultiFactorAuthPresent":"true"}}.',
location=condition_block,
)
if operator.lower() in ["null"]:
# The following condition is valid:
# "Condition": { "Null": { "aws:MultiFactorAuthAge": true }
# If we check further we'll get a MISMATCHED_TYPE finding due to
# aws:MultiFactorAuthAge being checked against a bool value instead of a date
continue
# The key here from the example is "s3:prefix"
condition_type = get_global_key_type(key)
if condition_type:
# This is a global key, like aws:CurrentTime
# Check if the values match the type (ex. must all be Date values)
if not is_value_in_correct_format_for_type(
condition_type, values
):
self.add_finding(
"MISMATCHED_TYPE",
detail="Type mismatch: {} requires a value of type {} but given {}".format(
key, condition_type, values
),
location=condition_block,
)
else:
# See if this is a service specific key
for action_struct in expanded_actions:
privilege_info = get_privilege_info(
action_struct["service"], action_struct["action"]
)
# Ensure the condition_key exists
match = None
for resource_type in privilege_info["resource_types"]:
for condition_key in resource_type["condition_keys"]:
if is_condition_key_match(condition_key, key):
match = condition_key
if match is None:
self.add_finding(
"UNKNOWN_CONDITION_FOR_ACTION",
detail="Unknown condition {} for action {}:{}".format(
key, action_struct["service"], action_struct["action"]
),
location=condition_block,
)
continue
condition_type = None
for condition in privilege_info["service_conditions"]:
if condition["condition"] == match:
condition_type = condition["type"]
if condition_type is None:
raise Exception(
"Action condition not found in service definition for {}".format(
match
)
)
if not is_value_in_correct_format_for_type(
condition_type, values
):
self.add_finding(
"MISMATCHED_TYPE",
detail="Type mismatch: {} requires a value of type {} but given {}".format(
key, condition_type, values
),
location=condition_block,
)
if condition_type is not None:
# if operator_type_requirement.lower() == 'string' and condition_type.lower() = 'arn':
# # Ignore these.
# pass
documenation_condition_type = translate_documentation_types(
condition_type
)
if operator_type_requirement != documenation_condition_type:
if (
operator_type_requirement == "String"
and documenation_condition_type == "Arn"
):
self.add_finding(
"MISMATCHED_TYPE_BUT_USABLE",
detail="Type mismatch: {} requires a value of type {} but given {}".format(
operator,
operator_type_requirement,
translate_documentation_types(condition_type),
),
location=condition_block,
)
else:
self.add_finding(
"MISMATCHED_TYPE",
detail="Type mismatch: {} requires a value of type {} but given {}".format(
operator,
operator_type_requirement,
translate_documentation_types(condition_type),
),
location=condition_block,
)
return
def analyze_statement(self):
"""
Given a statement, look for problems and extract out the parts.
If it is malformed, return False
"""
actions = []
resources = []
conditions = []
# Check no unknown elements exist
for element in self.stmt:
if element[0] not in [
"Effect",
"Sid",
"Principal",
"NotPrincipal",
"Action",
"NotAction",
"Resource",
"NotResource",
"Condition",
]:
self.add_finding(
"MALFORMED",
detail="Statement contains an unknown element",
location=element,
)
return False
# Track Sid for better location reporting
if "Sid" in self.stmt:
self.sid = self.stmt["Sid"].value
# Check Principal if it exists. This only applicable to resource policies. Also applicable to
# IAM role trust policies, but those don't have Resource elements, so that would break other things
# if we tried to check those.
if "Principal" in self.stmt and "NotPrincipal" in self.stmt:
self.add_finding(
"MALFORMED",
detail="Statement contains both Principal and NotPrincipal",
location=self.stmt,
)
return False
if "Principal" in self.stmt:
self._check_principal(self.stmt["Principal"])
elif "NotPrincipal" in self.stmt:
self._check_principal(self.stmt["NotPrincipal"])
# Check Effect
if "Effect" not in self.stmt:
self.add_finding(
"MALFORMED",
detail="Statement does not contain an Effect element",
location=self.stmt,
)
return False
effect = self.stmt["Effect"]
if effect.value not in ["Allow", "Deny"]:
self.add_finding(
"MALFORMED",
detail="Unknown Effect used. Effect must be either Allow or Deny",
location=effect,
)
return False
if effect.value == "Allow":
self.effect_allow = True
else:
self.effect_allow = False
# Check Sid
if "Sid" in self.stmt and not re.fullmatch(
"[0-9A-Za-z]*", self.stmt["Sid"].value
):
# The grammar is defined at https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_grammar.html
self.add_finding("INVALID_SID", location={"string": self.stmt["Sid"]})
return False
# Check Action
if "Action" in self.stmt and "NotAction" in self.stmt:
self.add_finding(
"MALFORMED",
detail="Statement contains both Action and NotAction",
location=self.stmt,
)
return False
if "Action" in self.stmt:
actions = make_list(self.stmt["Action"])
elif "NotAction" in self.stmt:
actions = make_list(self.stmt["NotAction"])
else:
self.add_finding(
"MALFORMED",
detail="Statement contains neither Action nor NotAction",
location=self.stmt,
)
return False
# Check Resource exists and save the list of resources for later
if "Resource" in self.stmt and "NotResource" in self.stmt:
self.add_finding(
"MALFORMED",
detail="Statement contains both Resource and NotResource",
location=self.stmt,
)
return False
if "Resource" in self.stmt:
resources = make_list(self.stmt["Resource"])
elif "NotResource" in self.stmt:
resources = make_list(self.stmt["NotResource"])
else:
self.add_finding(
"MALFORMED",
detail="Statement contains neither Resource nor NotResource",
location=self.stmt,
)
return False
# Check if a Condition element exists and if so save them for later
if "Condition" in self.stmt:
conditions = make_list(self.stmt["Condition"])
if len(conditions) > 1:
self.add_finding(
"MALFORMED",
detail="Condition formatted incorrectly",
location=self.stmt,
)
return False
# Expand the actions from s3:Get* to s3:GetObject and others
expanded_actions = []
for action in actions:
# Handle special case where all actions are allowed
if action.value == "*" or action.value == "*:*":
# TODO Should ensure the resource is "*" with this action
continue
try:
# Given an action such as "s3:List*", return all the possible values it could have
expanded_actions.extend(expand_action(action.value))
except UnknownActionException as e:
self.add_finding(
"UNKNOWN_ACTION", detail=str(e), location=action,
)
return False
except UnknownPrefixException as e:
self.add_finding("UNKNOWN_PREFIX", detail=str(e), location=action)
return False
except Exception as e:
self.add_finding("EXCEPTION", detail=str(e), location=action)
return False
# Check the resources are correct formatted correctly
has_malformed_resource = False
for resource in resources:
if resource.value == "*":
continue
try:
parts = resource.value.split(":")
except AttributeError:
has_malformed_resource = True
self.add_finding(
"INVALID_ARN",
detail=(
"Must be a string. Maybe you're trying to analyze a "
"CloudFormation template which is outside of Parliament's "
"scope."
),
location=resource,
)
continue
if len(parts) < 6:
has_malformed_resource = True
self.add_finding(
"INVALID_ARN", detail="Does not have 6 parts", location=resource,
)
continue
elif parts[0] != "arn":
has_malformed_resource = True
self.add_finding(
"INVALID_ARN", detail="Does not start with arn:", location=resource,
)
continue
elif parts[1] not in ["aws", "aws-cn", "aws-us-gov", "aws-iso", "*", ""]:
has_malformed_resource = True
self.add_finding(
"INVALID_ARN", detail="Unexpected partition", location=resource,
)
continue
# The service is in parts[2]
elif not is_valid_region(parts[3]):
has_malformed_resource = True
self.add_finding(
"INVALID_ARN",
detail="Region expected to be of form like us-east-1",
location=resource,
)
elif not is_valid_account_id(parts[4]):
has_malformed_resource = True
self.add_finding(
"INVALID_ARN",
detail="Account expected to be of form like 123456789012",
location=resource,
)
# TODO I should check for the use of valid variables in the resource, such as ${aws:username}
# See https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_variables.html
# TODO Note, there are also service specific variables, such s3:prefix and sns:Endpoint
# These are case-insensitive
# Before checking the resource more, ensure we don't have malformed resources.
# Also, looking for mismatches of Actions and Resources has different rules when a
# Deny is involved. For example, AWSEC2SpotServiceRolePolicy has a Deny on ec2:RunInstances
# but only lists a resource of "arn:aws:ec2:*:*:instance/*" instead of all the required
# resources.
# https://github.com/SummitRoute/aws_managed_policies/blob/4b71905a9042e66b22bc3d2b9cb1378b1e1d239e/policies/AWSEC2SpotServiceRolePolicy#L21
if not has_malformed_resource and self.effect_allow:
actions_without_matching_resources = []
# Ensure the required resources for each action exist
# Iterate through each action
for action_struct in expanded_actions:
privilege_info = get_privilege_info(
action_struct["service"], action_struct["action"]
)
# If the privilege requires a resource of "*", ensure it has it.
if len(privilege_info["resource_types"]) == 0 or (
len(privilege_info["resource_types"]) == 1
and privilege_info["resource_types"][0]["resource_type"] == ""
):
match_found = False
for resource in resources:
if resource.value == "*":
match_found = True
if not match_found:
actions_without_matching_resources.append(
{
"action": "{}:{}".format(
action_struct["service"], action_struct["action"]
),
"required_format": "*",
}
)
# Iterate through the resources defined in the action definition
for resource_type in privilege_info["resource_types"]:
resource_type = resource_type["resource_type"]
# Only check the required resources which have a "*" at the end
if "*" not in resource_type:
continue
arn_format = get_arn_format(
resource_type, privilege_info["service_resources"]
)
# At least one resource has to match the action's required resources
match_found = False
for resource in resources:
if resource.value == "*":
# expansion leads to duplication actions
action_key = "{}:{}".format(
action_struct["service"], action_struct["action"]
)
self.resource_star.setdefault(action_key, 0)
self.resource_star[action_key] += 1
match_found = True
continue
if is_arn_match(resource_type, arn_format, resource.value):
match_found = True
continue
if not match_found:
actions_without_matching_resources.append(
{
"action": "{}:{}".format(
action_struct["service"], action_struct["action"]
),
"required_format": arn_format,
}
)
if actions_without_matching_resources:
# We have location info for each action via the variable `actions` which is a ConfigJSONArray, but
# because we can only list one location, we'll just use the location of the statement
# because `actions_without_matching_resources` contains the name of each action and the required format.
self.add_finding(
"RESOURCE_MISMATCH",
detail=actions_without_matching_resources,
location=self.stmt
)
# If conditions exist, it will be an element, which was previously made into a list
if len(conditions) == 1:
# Iterate through each condition, of something like:
# - "StringLike": {"s3:prefix":["home/${aws:username}/*"]}
# - "DateGreaterThan" :{"aws:CurrentTime":"2019-07-16T12:00:00Z"}
for condition in conditions[0]:
# The operator is the first element (ex. `StringLike`) and the condition_block follows it
operator = condition[0]
condition_block = condition[1]
self._check_condition(operator, condition_block, expanded_actions)
# add the resource_star finding last
# after all offending actions from the expanded list have been identified
if self.resource_star:
self.add_finding(
"RESOURCE_STAR", detail=sorted(self.resource_star), location=self.stmt
)
return not has_malformed_resource