forked from arcuricm/Hands-on-Security-Hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OriginalChallenge.yml
1146 lines (1080 loc) · 37.1 KB
/
OriginalChallenge.yml
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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Resources:
DataBucket:
Type: AWS::S3::Bucket
Properties:
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: aws:kms
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
UpdateReplacePolicy: Delete
DeletionPolicy: Delete
Metadata:
aws:cdk:path: ResourcesStack/DataBucket/Resource
DataSecret:
Type: AWS::SecretsManager::Secret
Properties:
GenerateSecretString:
GenerateStringKey: password
SecretStringTemplate: '{"username": "user"}'
UpdateReplacePolicy: Delete
DeletionPolicy: Delete
Metadata:
aws:cdk:path: ResourcesStack/DataSecret/Resource
DataTable:
Type: AWS::DynamoDB::Table
Properties:
KeySchema:
- AttributeName: id
KeyType: HASH
AttributeDefinitions:
- AttributeName: id
AttributeType: S
BillingMode: PAY_PER_REQUEST
SSESpecification:
SSEEnabled: true
UpdateReplacePolicy: Delete
DeletionPolicy: Delete
Metadata:
aws:cdk:path: ResourcesStack/DataTable/Resource
WriteDataFunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: lambda.amazonaws.com
Version: '2012-10-17'
ManagedPolicyArns:
- Fn::Join:
- ''
- - 'arn:'
- Ref: AWS::Partition
- :iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Metadata:
aws:cdk:path: ResourcesStack/WriteDataFunctionRole/Resource
WriteDataFunctionRoleDefaultPolicy8547BA9E:
Type: AWS::IAM::Policy
Properties:
PolicyDocument:
Statement:
- Action:
- s3:PutObject
- s3:DeleteObject
Effect: Allow
Resource:
Fn::Join:
- ''
- - Fn::GetAtt:
- DataBucket
- Arn
- /data-object
- Action:
- ssm:PutParameter
- ssm:DeleteParameter
Effect: Allow
Resource:
Fn::Join:
- ''
- - 'arn:'
- Ref: AWS::Partition
- ':ssm:'
- Ref: AWS::Region
- ':'
- Ref: AWS::AccountId
- :parameter/data-parameter
- Action:
- dynamodb:PutItem
- dynamodb:DeleteItem
Effect: Allow
Resource:
Fn::GetAtt:
- DataTable
- Arn
Version: '2012-10-17'
PolicyName: WriteDataFunctionRoleDefaultPolicy8547BA9E
Roles:
- Ref: WriteDataFunctionRole
Metadata:
aws:cdk:path: ResourcesStack/WriteDataFunctionRole/DefaultPolicy/Resource
WriteDataFunction:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: |2+
import json
import boto3
import uuid
import cfnresponse
ssm = boto3.client('ssm')
s3 = boto3.client('s3')
ddb = boto3.client('dynamodb')
def on_event(event, context):
print(json.dumps(event))
request_type = event['RequestType']
try:
if request_type == 'Create': return on_create(event, context)
if request_type == 'Update': return on_update(event, context)
if request_type == 'Delete': return on_delete(event, context)
except Exception as e:
print(e)
cfnresponse.send(event, context, cfnresponse.FAILED, {})
raise e
raise Exception("Invalid request type: %s" % request_type)
def on_create(event, context):
props = event["ResourceProperties"]
print("create new resource with props %s" % props)
ssm.put_parameter(
Name=props['param_name'],
Value=uuid.uuid4().hex,
Type='SecureString',
Overwrite=True
)
s3.put_object(
Bucket=props['s3_bucket_name'],
Key=props['s3_object_key'],
Body=bytes(uuid.uuid4().hex, 'utf-8')
)
ddb.put_item(
TableName=props['ddb_table_name'],
Item={
"id": {
"S": props['ddb_item_key']
},
"value": {
"S": uuid.uuid4().hex
}
}
)
cfnresponse.send(event, context, cfnresponse.SUCCESS, {
"s3_object_key" : props['s3_object_key']
})
return 'Created'
def on_update(event, context):
print("update resource - doing nothing")
cfnresponse.send(event, context, cfnresponse.SUCCESS, {})
return 'Updated'
def on_delete(event, context):
props = event["ResourceProperties"]
print("deleting resources with props %s" % props)
try:
ssm.delete_parameter(
Name=props['param_name']
)
except Exception as e:
print(e)
try:
s3.delete_object(
Bucket=props['s3_bucket_name'],
Key=props['s3_object_key']
)
except Exception as e:
print(e)
try:
ddb.delete_item(
TableName=props['ddb_table_name'],
Key={
"id": {
"S": props['ddb_item_key']
}
}
)
except Exception as e:
print(e)
cfnresponse.send(event, context, cfnresponse.SUCCESS, {})
return 'Deleted'
Role:
Fn::GetAtt:
- WriteDataFunctionRole
- Arn
Handler: index.on_event
Runtime: python3.9
Timeout: 300
DependsOn:
- WriteDataFunctionRoleDefaultPolicy8547BA9E
- WriteDataFunctionRole
Metadata:
aws:cdk:path: ResourcesStack/WriteDataFunction/Resource
WriteData:
Type: AWS::CloudFormation::CustomResource
Properties:
ServiceToken:
Fn::GetAtt:
- WriteDataFunction
- Arn
param_name: data-parameter
s3_bucket_name:
Ref: DataBucket
s3_object_key: data-object
ddb_table_name:
Ref: DataTable
ddb_item_key: data-item
UpdateReplacePolicy: Delete
DeletionPolicy: Delete
Metadata:
aws:cdk:path: ResourcesStack/WriteData/Default
DataRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
AWS:
Fn::Join:
- ''
- - 'arn:'
- Ref: AWS::Partition
- ':iam::'
- Ref: AWS::AccountId
- :root
Version: '2012-10-17'
Metadata:
aws:cdk:path: ResourcesStack/DataRole/Resource
DataRoleDefaultPolicy0486B57A:
Type: AWS::IAM::Policy
Properties:
PolicyDocument:
Statement:
- Action:
- secretsmanager:GetSecretValue
- secretsmanager:DescribeSecret
Effect: Allow
Resource:
Ref: DataSecret
- Action:
- s3:GetObject*
- s3:GetBucket*
- s3:List*
Effect: Allow
Resource:
- Fn::GetAtt:
- DataBucket
- Arn
- Fn::Join:
- ''
- - Fn::GetAtt:
- DataBucket
- Arn
- /*
- Action:
- dynamodb:BatchGetItem
- dynamodb:GetRecords
- dynamodb:GetShardIterator
- dynamodb:Query
- dynamodb:GetItem
- dynamodb:Scan
- dynamodb:ConditionCheckItem
- dynamodb:DescribeTable
Effect: Allow
Resource:
- Fn::GetAtt:
- DataTable
- Arn
- Ref: AWS::NoValue
- Action:
- s3:GetParameter
- s3:GetParameters
Effect: Allow
Resource:
Fn::Join:
- ''
- - 'arn:'
- Ref: AWS::Partition
- ':ssm:'
- Ref: AWS::Region
- ':'
- Ref: AWS::AccountId
- :parameter/data-parameter
Version: '2012-10-17'
PolicyName: DataRoleDefaultPolicy0486B57A
Roles:
- Ref: DataRole
Metadata:
aws:cdk:path: ResourcesStack/DataRole/DefaultPolicy/Resource
DataEventsBucket:
Type: AWS::S3::Bucket
Properties:
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: aws:kms
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
UpdateReplacePolicy: Retain
DeletionPolicy: Retain
Metadata:
aws:cdk:path: ResourcesStack/DataEventsBucket/Resource
DataEventsBucketPolicy23490203:
Type: AWS::S3::BucketPolicy
Properties:
Bucket:
Ref: DataEventsBucket
PolicyDocument:
Statement:
- Action: s3:GetBucketAcl
Effect: Allow
Principal:
Service: cloudtrail.amazonaws.com
Resource:
Fn::GetAtt:
- DataEventsBucket
- Arn
- Action: s3:PutObject
Condition:
StringEquals:
aws:SourceArn:
Fn::Join:
- ''
- - 'arn:'
- Ref: AWS::Partition
- ':cloudtrail:'
- Ref: AWS::Region
- ':'
- Ref: AWS::AccountId
- :trail/data-events-trail
s3:x-amz-acl: bucket-owner-full-control
Effect: Allow
Principal:
Service: cloudtrail.amazonaws.com
Resource:
Fn::Join:
- ''
- - Fn::GetAtt:
- DataEventsBucket
- Arn
- /AWSLogs/
- Ref: AWS::AccountId
- /*
Version: '2012-10-17'
Metadata:
aws:cdk:path: ResourcesStack/DataEventsBucket/Policy/Resource
DataEventsTrail:
Type: AWS::CloudTrail::Trail
Properties:
IsLogging: true
S3BucketName:
Ref: DataEventsBucket
EventSelectors:
- DataResources:
- Type: AWS::S3::Object
Values:
- Fn::Join:
- ''
- - Fn::GetAtt:
- DataBucket
- Arn
- /
- Type: AWS::DynamoDB::Table
Values:
- Fn::GetAtt:
- DataTable
- Arn
IncludeManagementEvents: false
IncludeGlobalServiceEvents: false
TrailName: data-events-trail
Metadata:
aws:cdk:path: ResourcesStack/DataEventsTrail
DataFunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: lambda.amazonaws.com
Version: '2012-10-17'
ManagedPolicyArns:
- Fn::Join:
- ''
- - 'arn:'
- Ref: AWS::Partition
- :iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Metadata:
aws:cdk:path: ResourcesStack/DataFunctionRole/Resource
DataFunctionRoleDefaultPolicyF2A93448:
Type: AWS::IAM::Policy
Properties:
PolicyDocument:
Statement:
- Action: securityhub:BatchImportFindings
Effect: Allow
Resource:
Fn::Join:
- ''
- - 'arn:'
- Ref: AWS::Partition
- ':securityhub:'
- Ref: AWS::Region
- ':'
- Ref: AWS::AccountId
- :product/
- Ref: AWS::AccountId
- /default
Version: '2012-10-17'
PolicyName: DataFunctionRoleDefaultPolicyF2A93448
Roles:
- Ref: DataFunctionRole
Metadata:
aws:cdk:path: ResourcesStack/DataFunctionRole/DefaultPolicy/Resource
DataFunction:
Type: AWS::Lambda::Function
Properties:
Role:
Fn::GetAtt:
- DataFunctionRole
- Arn
Environment:
Variables:
MATCH_ROLE_ARN:
Fn::GetAtt:
- DataRole
- Arn
Handler: index.lambda_handler
Runtime: python3.9
Code:
ZipFile: |
import boto3
import json
import re
import traceback
import os
FINDING_SEVERITY_LABEL='HIGH'
FINDING_TYPE="Unusual Behaviors"
COMPANY_NAME="Custom"
PRODUCT_NAME="DecoyDetector"
ACCESS_DENIED_MESSAGE_REGEX = re.compile('User: (.*) is not authorized to perform: sts:AssumeRole on resource: (.*)')
IPV4_REGEX = re.compile('^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$')
MATCH_ROLE_ARN = os.environ.get('MATCH_ROLE_ARN')
USER_IDENTITY_TO_ASFF = {
'accessKeyId': 'AccessKeyId',
'accountId': 'AccountId',
'principalId': 'PrincipalId',
'type': 'PrincipalType',
'userName': 'PrincipalName'
}
ATTRIBUTES_TO_ASFF = {
'creationDate': 'CreationDate',
'mfaAuthenticated': 'MfaAuthenticated'
}
SESSION_ISSUER_TO_ASFF = {
'accountId': 'AccountId',
'arn': 'Arn',
'principalId': 'PrincipalId',
'type': 'Type',
'userName': 'UserName'
}
session = boto3.session.Session()
sts = session.client('sts')
caller_id = sts.get_caller_identity()
my_arn = caller_id['Arn']
my_account = caller_id['Account']
my_region = session.region_name
my_partition = session.get_partition_for_region(session.region_name)
sh = session.client('securityhub')
def get_resource_arn(resource_list, type):
for r in resource_list:
if(r['type'] == type):
if('ARN' in r):
return r['ARN']
elif('ARNPrefix' in r):
return r['ARNPrefix']
return None
def map_keys(source, mapping):
dest = {}
for source_key, source_value in source.items():
if(source_key in mapping):
dest_key = mapping[source_key]
dest[dest_key] = source_value
return dest
def add_user_identity(resources, event, detail):
user_identity = detail['userIdentity']
# append information about the Access Key:
access_key_resource = {
'Id': user_identity['accessKeyId'],
'Type': 'AwsIamAccessKey',
'ResourceRole': 'Actor',
'Partition': my_partition,
'Region': event['region'],
'Details': {}
}
aws_iam_access_key = map_keys(user_identity, USER_IDENTITY_TO_ASFF)
if('sessionContext' in user_identity):
session_context = {}
if('attributes' in user_identity['sessionContext']):
attr = map_keys(user_identity['sessionContext']['attributes'], ATTRIBUTES_TO_ASFF)
if(len(attr) != 0):
# Change type of 'MfaAuthenticated' to boolean
if('MfaAuthenticated' in attr):
if(attr['MfaAuthenticated'] == 'true'):
attr['MfaAuthenticated'] = True
else:
attr['MfaAuthenticated'] = False
session_context['Attributes'] = attr
if('sessionIssuer' in user_identity['sessionContext']):
si = map_keys(user_identity['sessionContext']['sessionIssuer'], SESSION_ISSUER_TO_ASFF)
if(len(si) != 0):
session_context['SessionIssuer'] = si
if(len(session_context) != 0):
aws_iam_access_key['SessionContext'] = session_context
if(len(aws_iam_access_key) != 0):
access_key_resource['Details']['AwsIamAccessKey'] = aws_iam_access_key
resources.append(access_key_resource)
# Now add either an AwsIamUser or AwsIamRole depending on the type of identity
type = user_identity['type']
if(type == 'AssumedRole'):
role = {
'Id': user_identity['sessionContext']['sessionIssuer']['arn'],
'Type': 'AwsIamRole',
'ResourceRole': 'Actor',
'Partition': my_partition,
'Region': event['region'],
}
resources.append(role)
elif(type == 'IAMUser'):
user = {
'Id': user_identity['arn'],
'Type': 'AwsIamUser',
'ResourceRole': 'Actor',
'Partition': my_partition,
'Region': event['region'],
}
resources.append(user)
def add_s3_fields(finding, event, detail):
add_fields = {
"Title": f"Secret S3 bucket accessed. Regular employees know not to access this resource. {detail['requestParameters']['bucketName']}",
"Description": f"Secret S3 bucket {detail['requestParameters']['bucketName']} was accessed by {detail['userIdentity']['arn']}. This S3 bucket has been provisioned to monitor and generate security events when accessed and can be an indicator of unintended or unauthorized access to your AWS Account.",
'Resources': [
{
'Id': get_resource_arn(detail['resources'], 'AWS::S3::Bucket'),
'Type': 'AwsS3Bucket',
'ResourceRole': 'Target',
'Partition': my_partition,
'Region': event['region']
},
{
'Id': get_resource_arn(detail['resources'], 'AWS::S3::Object'),
'Type': 'AwsS3Object',
'ResourceRole': 'Target',
'Partition': my_partition,
'Region': event['region']
}
]
}
finding.update(add_fields)
def add_dynamodb_fields(finding, event, detail):
table_arn = get_resource_arn(detail['resources'], 'AWS::DynamoDB::Table')
add_fields = {
"Title": f"Secret DynamoDB table accessed. Regular employees know not to access this resource. {table_arn}",
"Description": f"Secret DynamoDB table {table_arn} was accessed by {detail['userIdentity']['arn']}. This DynamoDB table has been provisioned to monitor and generate security events when accessed and can be an indicator of unintended or unauthorized access to your AWS Account.",
'Resources': [
{
'Id': table_arn,
'Type': 'AwsDynamoDbTable',
'ResourceRole': 'Target',
'Partition': my_partition,
'Region': event['region']
}
]
}
finding.update(add_fields)
def add_sts_fields(finding, event, detail):
if('errorCode' in detail and detail['errorCode'] == 'AccessDenied'):
# Handle STS AccessDenied errors by parsing out IAM Role ARN from errorMessage
error_message = detail['errorMessage']
result = ACCESS_DENIED_MESSAGE_REGEX.fullmatch(error_message)
if(result):
principal_arn = result.group(1)
target_role_arn = result.group(2)
if(target_role_arn != MATCH_ROLE_ARN):
raise Exception(f"STS AssumeRole AccessDenied: target role {target_role_arn} does not match {MATCH_ROLE_ARN}")
else:
raise Exception('STS AssumeRole AccessDenied: errorMessage did not match expected pattern')
else:
principal_arn = detail['userIdentity']['arn']
target_role_arn = detail['requestParameters']['roleArn']
add_fields = {
"Title": f"Suspicious activity detected accessing private decoy IAM role {target_role_arn}",
"Description": f"Private decoy IAM role {target_role_arn} was accessed by {principal_arn}. This IAM role has been provisioned to monitor and generate security events when accessed and can be an indicator of unintended or unauthorized access to your AWS Account.",
'Resources': [
{
'Id': target_role_arn,
'Type': 'AwsIamRole',
'ResourceRole': 'Target',
'Partition': my_partition,
'Region': event['region']
}
]
}
finding.update(add_fields)
def add_kms_fields(finding, event, detail):
if(detail['requestParameters'] == None):
# this is most likely a KMS AccessDenied error, there's not much to add here
raise Exception(f"Missing requestParameter details for KMS event")
enc_context = detail['requestParameters']['encryptionContext']
if('PARAMETER_ARN' in enc_context):
# Decryption of SSM Parameter
add_fields = {
"GeneratorId": 'ssm.amazonaws.com',
"Title": f"Suspicious activity detected accessing private decoy Systems Manager parameter {enc_context['PARAMETER_ARN']}",
"Description": f"Private decoy Systems Manager parameter {enc_context['PARAMETER_ARN']} was accessed by {detail['userIdentity']['arn']}. This Systems Manager parameter has been provisioned to monitor and generate security events when accessed and can be an indicator of unintended or unauthorized access to your AWS Account.",
'Resources': [
{
'Id': enc_context['PARAMETER_ARN'],
'Type': 'Other',
'ResourceRole': 'Target',
'Partition': my_partition,
'Region': event['region']
}
]
}
elif('SecretARN' in enc_context):
# Decryption of Secret
add_fields = {
"GeneratorId": 'secretsmanager.amazonaws.com',
"Title": f"Suspicious activity detected accessing private decoy secret {enc_context['SecretARN']}",
"Description": f"Private decoy secret {enc_context['SecretARN']} was accessed by {detail['userIdentity']['arn']}. This secret has been provisioned to monitor and generate security events when accessed and can be an indicator of unintended or unauthorized access to your AWS Account.",
'Resources': [
{
'Id': enc_context['SecretARN'],
'Type': 'AwsSecretsManagerSecret',
'ResourceRole': 'Target',
'Partition': my_partition,
'Region': event['region']
}
]
}
else:
raise Exception(f"Unexpected encryption context: {enc_context}")
# add the KMS Key resource
key = {
'Id': get_resource_arn(detail['resources'], 'AWS::KMS::Key'),
'Type': 'AwsKmsKey',
'ResourceRole': 'Target',
'Partition': my_partition,
'Region': event['region']
}
add_fields['Resources'].append(key)
finding.update(add_fields)
def map_finding(event):
detail = event['detail']
# Add common fields for all events
finding = {
"SchemaVersion": "2018-10-08",
"Id": detail['eventID'],
"ProductArn": f"arn:aws:securityhub:{my_region}:{my_account}:product/{my_account}/default",
"GeneratorId": detail['eventSource'],
"AwsAccountId": event['account'],
"CreatedAt": detail['eventTime'],
"UpdatedAt": detail['eventTime'],
"CompanyName": COMPANY_NAME,
"ProductName": PRODUCT_NAME,
"FindingProviderFields": {
"Severity": {
"Label": FINDING_SEVERITY_LABEL
},
"Types": [ FINDING_TYPE ]
},
"Action": {
"ActionType": "AWS_API_CALL",
"AwsApiCallAction": {
"Api": detail['eventName'],
"CallerType": 'remoteIp',
"ServiceName": detail['eventSource'],
"RemoteIpDetails": {
"IpAddressV4": detail['sourceIPAddress']
}
}
}
}
# remove IpAddressV4 if it is not an actual IPv4
match = IPV4_REGEX.fullmatch(finding['Action']['AwsApiCallAction']['RemoteIpDetails']['IpAddressV4'])
if(match == None):
# Not an IPv4: this is likely an internal AWS API call: remove RemoteIpDetails
del finding['Action']['AwsApiCallAction']['RemoteIpDetails']
# Add additional custom product-specific fields:
custom_fields = {}
if('errorCode' in detail):
custom_fields = {
'apiResult': 'ERROR',
'errorCode': detail['errorCode'],
'errorMessage': detail['errorMessage']
}
else:
custom_fields = {
'apiResult': 'SUCCESS',
}
# Add other fields from CloudTrail Event:
custom_fields['userAgent'] = detail['userAgent']
custom_fields['requestID'] = detail['requestID']
product_fields = {}
for k, v in custom_fields.items():
pf_key = f"{COMPANY_NAME}/{PRODUCT_NAME}/{k}"
product_fields[pf_key] = v
finding['ProductFields'] = product_fields
source = event['source']
if(source == 'aws.s3'):
add_s3_fields(finding, event, detail)
elif(source == 'aws.sts'):
add_sts_fields(finding, event, detail)
elif(source == 'aws.kms'):
# KMS events are for SecretsManager and Systems Manager
add_kms_fields(finding, event, detail)
elif(source == 'aws.dynamodb'):
add_dynamodb_fields(finding, event, detail)
else:
# An event source we don't recognize - this shoudn't happen so we log and ignore the event
raise Exception(f"Unexpected event source {source}")
add_user_identity(finding['Resources'], event, detail)
return finding
def import_event(event):
try:
finding = map_finding(event)
except Exception as e:
print('Exception mapping event:', e, ': ignoring and not sending to Security Hub')
traceback.print_exc()
return
try:
print('Mapped ASFF finding:')
print(json.dumps(finding))
result = sh.batch_import_findings(Findings=[finding])
if(result['FailedCount'] != 0):
failed = result['FailedFindings'][0]
print(f"Failed to import finding id {failed['Id']}: ErrorCode: {failed['ErrorCode']}, ErrorMessage: {failed['ErrorMessage']}")
else:
print(f"Successfully imported finding id {finding['Id']}")
except Exception as e:
print('Exception calling Security Hub: ', e)
traceback.print_exc()
return
def lambda_handler(event, context):
print("Received EventBridge event:")
print(json.dumps(event))
import_event(event)
DependsOn:
- DataFunctionRoleDefaultPolicyF2A93448
- DataFunctionRole
- WriteData
Metadata:
aws:cdk:path: ResourcesStack/DataFunction/Resource
aws:asset:path: asset.ae8b074293c2b283aaecb1cab0308580c1d5068fba43f032539df55f6c03fb9e
aws:asset:is-bundled: false
aws:asset:property: Code
DDBRule:
Type: AWS::Events::Rule
Properties:
EventPattern:
detail:
eventName:
- BatchExecuteStatement
- BatchGetItem
- BatchWriteItem
- DeleteItem
- ExecuteStatement
- ExecuteTransaction
- GetItem
- PutItem
- Query
- Scan
- TransactGetItems
- TransactWriteItems
- UpdateItem
resources:
ARN:
- Fn::GetAtt:
- DataTable
- Arn
type:
- AWS::DynamoDB::Table
detail-type:
- AWS API Call via CloudTrail
source:
- aws.dynamodb
State: ENABLED
Targets:
- Arn:
Fn::GetAtt:
- DataFunction
- Arn
Id: Target0
Metadata:
aws:cdk:path: ResourcesStack/DDBRule/Resource
DDBRuleAllowEventRuleResourcesStackDataFunction7E871F45ED9E05E7:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName:
Fn::GetAtt:
- DataFunction
- Arn
Principal: events.amazonaws.com
SourceArn:
Fn::GetAtt:
- DDBRule
- Arn
Metadata:
aws:cdk:path: ResourcesStack/DDBRule/AllowEventRuleResourcesStackDataFunction7E871F45
S3Rule:
Type: AWS::Events::Rule
Properties:
EventPattern:
detail:
eventName:
- HeadObject
- GetObject
- GetObjectAcl
- GetObjectAttributes
- GetObjectLegalHold
- GetObjectLockConfiguration
- GetObjectRetention
- GetObjectTagging
- GetObjectTorrent
- PutObject
- PutObjectAcl
- PutObjectLegalHold
- PutObjectLockConfiguration
- PutObjectRetention
- PutObjectTagging
- SelectObjectContent
- DeleteObject
- DeleteObjects
- DeleteObjectTagging
resources:
ARN:
- Fn::GetAtt:
- DataBucket
- Arn
type:
- AWS::S3::Bucket
detail-type:
- AWS API Call via CloudTrail
source:
- aws.s3
State: ENABLED
Targets:
- Arn:
Fn::GetAtt:
- DataFunction
- Arn
Id: Target0
Metadata:
aws:cdk:path: ResourcesStack/S3Rule/Resource
S3RuleAllowEventRuleResourcesStackDataFunction7E871F459737E5BC:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName:
Fn::GetAtt:
- DataFunction
- Arn
Principal: events.amazonaws.com
SourceArn:
Fn::GetAtt:
- S3Rule
- Arn
Metadata:
aws:cdk:path: ResourcesStack/S3Rule/AllowEventRuleResourcesStackDataFunction7E871F45
STSRule:
Type: AWS::Events::Rule
Properties:
EventPattern:
detail:
eventName:
- AssumeRole
resources:
ARN:
- Fn::GetAtt:
- DataRole
- Arn
type:
- AWS::IAM::Role
detail-type:
- AWS API Call via CloudTrail
source:
- aws.sts
State: ENABLED
Targets:
- Arn:
Fn::GetAtt:
- DataFunction
- Arn
Id: Target0
Metadata:
aws:cdk:path: ResourcesStack/STSRule/Resource
STSRuleAllowEventRuleResourcesStackDataFunction7E871F45D5755E7F:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName:
Fn::GetAtt:
- DataFunction
- Arn
Principal: events.amazonaws.com
SourceArn:
Fn::GetAtt:
- STSRule
- Arn
Metadata:
aws:cdk:path: ResourcesStack/STSRule/AllowEventRuleResourcesStackDataFunction7E871F45
STSAccessDeniedRule:
Type: AWS::Events::Rule
Properties:
EventPattern:
detail: