-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathcfn-local.yml
337 lines (322 loc) · 12.7 KB
/
cfn-local.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
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Description: aws-security-survival-kit alerting - local events - https://zoph.io
###########################################################
Parameters:
###########################################################
AlarmRecipient:
Type: String
AllowedPattern: "^[\\x20-\\x45]?[\\w-\\+]+(\\.[\\w]+)*@[\\w-]+(\\.[\\w]+)*(\\.[a-z]{2,})$"
ConstraintDescription: Email address required.
Description: Alarm recipient email address
CTLogGroupName:
Type: String
Description: CloudTrail LogGroup
Project:
Type: String
Description: Project Name
Region:
Type: String
Description: AWS Region
###############################################################################
###########################################################
Resources:
###########################################################
Dashboard:
Type: AWS::CloudWatch::Dashboard
Properties:
DashboardName: !Sub "AWS-Security-Survival-Kit-Dashboard-${AWS::Region}"
DashboardBody: !Sub |
{"widgets": [{ "height": 12, "width": 24, "y": 0, "x": 0, "type": "log", "properties": { "query": "SOURCE '${CTLogGroupName}' | fields eventTime, eventName, eventSource, recipientAccountId as Account, userIdentity.sessionContext.sessionIssuer.userName as Principal, userIdentity.invokedBy as InvokedBy, errorCode\n\n\n\n | filter errorCode like /(Client.UnauthorizedOperation|AccessDenied)/", "region": "${Region}", "title": "Latest AccessDenied from CT", "view": "table" } }, { "height": 6, "width": 24, "y": 12, "x": 0, "type": "log", "properties": { "query": "SOURCE '${CTLogGroupName}' | fields eventTime, eventName, eventSource, userIdentity.sessionContext.sessionIssuer.userName as Principal, userIdentity.invokedBy as InvokedBy, responseElements.instancesSet.items.0.metadataOptions.httpTokens as IMDSv2\n\n | filter eventName like \"RunInstances\" and IMDSv2 like \"optional\"", "region": "${Region}", "title": "IMDSv1 RunInstances", "view": "table" } } ] }
EventRuleCTChange:
Type: "AWS::Events::Rule"
Properties:
Name: !Sub "${Project}-detect-cloudtrail-changes"
Description: !Sub "[${Project}] Monitor change on CloudTrail setup"
State: "ENABLED"
Targets:
- Arn:
Ref: CtAlertingTopic
Id: CtAlertingTopic
InputTransformer:
InputPathsMap:
EventId: "$.id"
EventTime: "$.detail.eventTime"
EventName: "$.detail.eventName"
EventAccountId: "$.account"
EventSourceIp: "$.detail.sourceIPAddress"
EventRegion: "$.region"
EventBlame: "$.detail.userIdentity.principalId"
EventBlameDetails: "$.detail.userIdentity.sessionContext.sessionIssuer.userName"
# Doc: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-events-rule-inputtransformer.html
InputTemplate: |
{
"🆔 EventId": <EventId>,
"🕑 EventTime": <EventTime>,
"👉 EventName": <EventName>,
"🧾 EventAccountId": <EventAccountId>,
"🌐 EventSourceIp": <EventSourceIp>,
"🌏 EventRegion": <EventRegion>,
"👨🏭 EventBlame": <EventBlame>,
"📃 EventBlameDetails": <EventBlameDetails>
}
EventPattern:
detail-type:
- "AWS API Call via CloudTrail"
detail:
eventSource:
- "cloudtrail.amazonaws.com"
eventName:
- "StopLogging"
- "DeleteTrail"
- "UpdateTrail"
EventRuleExfilDisk:
Type: "AWS::Events::Rule"
Properties:
Name: !Sub "${Project}-detect-disk-exfiltration"
Description: !Sub "[${Project}] Detect exfiltration on EBS Snapshot or AMI (sharing)"
State: "ENABLED"
Targets:
- Arn:
Ref: CtAlertingTopic
Id: CtAlertingTopic
InputTransformer:
InputPathsMap:
EventId: "$.id"
EventTime: "$.detail.eventTime"
EventName: "$.detail.eventName"
EventAccountId: "$.account"
EventSourceIp: "$.detail.sourceIPAddress"
EventRegion: "$.region"
EventBlame: "$.detail.userIdentity.principalId"
EventBlameDetails: "$.detail.userIdentity.sessionContext.sessionIssuer.userName"
InputTemplate: |
{
"🆔 EventId": <EventId>,
"🕑 EventTime": <EventTime>,
"👉 EventName": <EventName>,
"🧾 EventAccountId": <EventAccountId>,
"🌐 EventSourceIp": <EventSourceIp>,
"🌏 EventRegion": <EventRegion>,
"👨🏭 EventBlame": <EventBlame>,
"📃 EventBlameDetails": <EventBlameDetails>
}
EventPattern:
source:
- aws.ec2
detail-type:
- "AWS API Call via CloudTrail"
detail:
eventSource:
- "ec2.amazonaws.com"
eventName:
- "ModifySnapshotAttribute"
- "ModifyImageAttribute"
- "SharedSnapshotCopyInitiated"
- "SharedSnapshotVolumeCreated"
EventRuleStsWhoAmI:
Type: "AWS::Events::Rule"
Properties:
Name: !Sub "${Project}-detect-sts-whoami"
Description: !Sub "[${Project}] Detect STS get-caller-identity"
State: "ENABLED"
Targets:
- Arn:
Ref: CtAlertingTopic
Id: CtAlertingTopic
InputTransformer:
InputPathsMap:
EventId: "$.id"
EventTime: "$.detail.eventTime"
EventName: "$.detail.eventName"
EventAccountId: "$.account"
EventSourceIp: "$.detail.sourceIPAddress"
EventRegion: "$.region"
EventBlame: "$.detail.userIdentity.principalId"
EventBlameDetails: "$.detail.userIdentity.sessionContext.sessionIssuer.userName"
InputTemplate: |
{
"🆔 EventId": <EventId>,
"🕑 EventTime": <EventTime>,
"👉 EventName": <EventName>,
"🧾 EventAccountId": <EventAccountId>,
"🌐 EventSourceIp": <EventSourceIp>,
"🌏 EventRegion": <EventRegion>,
"👨🏭 EventBlame": <EventBlame>,
"📃 EventBlameDetails": <EventBlameDetails>
}
EventPattern:
source:
- aws.sts
detail-type:
- "AWS API Call via CloudTrail"
detail:
eventSource:
- "sts.amazonaws.com"
eventName:
- "GetCallerIdentity"
EventRuleCloudShellGetFileDownloadUrls:
# Can detect data exfil through CloudShell (ie: scrape and download credentials stored in Secrets Manager)
Type: "AWS::Events::Rule"
Properties:
Name: !Sub "${Project}-detect-cloudshell-getfiledownloadurls"
Description: !Sub "[${Project}] Detect CloudShell GetFileDownloadUrls"
State: "ENABLED_WITH_ALL_CLOUDTRAIL_MANAGEMENT_EVENTS"
Targets:
- Arn:
Ref: CtAlertingTopic
Id: CtAlertingTopic
InputTransformer:
InputPathsMap:
EventId: "$.id"
EventTime: "$.detail.eventTime"
EventName: "$.detail.eventName"
EventAccountId: "$.account"
EventSourceIp: "$.detail.sourceIPAddress"
EventRegion: "$.region"
EventBlame: "$.detail.userIdentity.principalId"
FilePath: "$.detail.requestParameters.FileDownloadPath"
EventBlameDetails: "$.detail.userIdentity.sessionContext.sessionIssuer.userName"
InputTemplate: |
{
"🆔 EventId": <EventId>,
"🕑 EventTime": <EventTime>,
"👉 EventName": <EventName>,
"🧾 EventAccountId": <EventAccountId>,
"🌐 EventSourceIp": <EventSourceIp>,
"🌏 EventRegion": <EventRegion>,
"📂 FilePath": <FilePath>,
"👨🏭 EventBlame": <EventBlame>,
"📃 EventBlameDetails": <EventBlameDetails>
}
EventPattern:
source:
- aws.cloudshell
detail-type:
- "AWS API Call via CloudTrail"
detail:
eventSource:
- "cloudshell.amazonaws.com"
eventName:
- "GetFileDownloadUrls"
CloudWatchLogsMetricsFilterAccessDenied:
Type: AWS::Logs::MetricFilter
Properties:
FilterPattern: '{ ($.errorCode = "*UnauthorizedOperation") || ($.errorCode = "AccessDenied*") }'
LogGroupName: !Ref CTLogGroupName
MetricTransformations:
- MetricValue: "1"
MetricNamespace: "CloudTrailMetrics"
MetricName: "UnauthorizedAttemptCount"
DefaultValue: 0
AlarmAccessDenied:
Type: "AWS::CloudWatch::Alarm"
Properties:
ActionsEnabled: true
AlarmName: !Sub "Unauthorized API Call [${Project}]"
AlarmActions:
- !Ref CtAlertingTopic
AlarmDescription: >
Alarm on Unauthorized API Call
ComparisonOperator: GreaterThanOrEqualToThreshold
EvaluationPeriods: 2
MetricName: "UnauthorizedAttemptCount"
Namespace: "CloudTrailMetrics"
Period: 3600
Statistic: Sum
Threshold: 1
TreatMissingData: "notBreaching"
AlarmConsoleFailedAuth:
Type: "AWS::CloudWatch::Alarm"
Properties:
AlarmName: !Sub "Failed Console Login [${Project}]"
AlarmDescription: !Sub "Failed Console Login [${Project}]"
MetricName: "ConsoleLoginFailures"
Namespace: "CloudTrailMetrics"
Statistic: "Sum"
Period: "300"
EvaluationPeriods: "1"
Threshold: "1"
ComparisonOperator: "GreaterThanOrEqualToThreshold"
AlarmActions:
- !Ref CtAlertingTopic
TreatMissingData: "notBreaching"
CloudWatchLogsMetricsFilterConsoleFailedAuth:
Type: "AWS::Logs::MetricFilter"
Properties:
LogGroupName: !Ref CTLogGroupName
FilterPattern: '{ ($.eventName = ConsoleLogin) && ($.errorMessage = "Failed authentication") }'
MetricTransformations:
- MetricValue: "1"
MetricNamespace: "CloudTrailMetrics"
MetricName: "ConsoleLoginFailures"
AlarmIMDSv1RunInstance:
Type: "AWS::CloudWatch::Alarm"
Properties:
AlarmName: !Sub "IMDSv1 RunInstance Detected [${Project}]"
AlarmDescription: !Sub "IMDSv1 RunInstance Detected [${Project}]"
MetricName: "IMDSv1RunInstance"
Namespace: "CloudTrailMetrics"
Statistic: "Sum"
Period: "300"
EvaluationPeriods: "1"
Threshold: "1"
ComparisonOperator: "GreaterThanOrEqualToThreshold"
AlarmActions:
- !Ref CtAlertingTopic
TreatMissingData: "notBreaching"
CloudWatchLogsMetricsFilterIMDSv1:
Type: "AWS::Logs::MetricFilter"
Properties:
LogGroupName: !Ref CTLogGroupName
FilterPattern: '{ ($.eventName = RunInstances) && ($.responseElements.instancesSet.items[0].metadataOptions.httpTokens = "optional") }'
MetricTransformations:
- MetricValue: "1"
MetricNamespace: "CloudTrailMetrics"
MetricName: "IMDSv1RunInstance"
CtAlertingTopic:
# https://aws.amazon.com/premiumsupport/knowledge-center/cloudwatch-receive-sns-for-alarm-trigger/
Type: "AWS::SNS::Topic"
Properties:
DisplayName: !Sub "[${AWS::AccountId}] Security Alarm"
TopicName: !Sub "${Project}-alarm-topic-${AWS::Region}"
Subscription:
- Protocol: email
Endpoint: !Ref AlarmRecipient
Tags:
- Key: "Project"
Value: !Ref Project
SnsTopicPolicy:
Type: "AWS::SNS::TopicPolicy"
Properties:
PolicyDocument:
Statement:
- Sid: "__default_statement_ID"
Effect: "Allow"
Principal:
AWS: "*"
Action:
- "sns:GetTopicAttributes"
- "sns:SetTopicAttributes"
- "sns:AddPermission"
- "sns:RemovePermission"
- "sns:DeleteTopic"
- "sns:Subscribe"
- "sns:ListSubscriptionsByTopic"
- "sns:Publish"
- "sns:Receive"
Resource:
Ref: CtAlertingTopic
Condition:
StringEquals:
AWS:SourceOwner:
Ref: "AWS::AccountId"
- Sid: "CWEvents"
Effect: "Allow"
Principal:
Service: "events.amazonaws.com"
Action: "sns:Publish"
Resource:
Ref: CtAlertingTopic
Topics:
- Ref: CtAlertingTopic