-
Notifications
You must be signed in to change notification settings - Fork 2
/
logging.template
executable file
·394 lines (394 loc) · 16.5 KB
/
logging.template
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
AWSTemplateFormatVersion: 2010-09-09
Description: 'Initializes global resources and logging/monitoring capabilities (qs-1nb14cqc5)'
Metadata:
Stack:
Value: 0
VersionDate:
Value: 20160510
Identifier:
Value: template-logging
Input:
Description: CloudTrail bucket name
Output:
Description: Outputs ID of all deployed resources
Parameters:
pNotifyEmail:
Description: Notification email for security events
Type: String
Default: ''
pSupportsGlacier:
Description: Determines hether this region supports Glacier (passed in from Main template)
Type: String
Default: true
Conditions:
IsGovCloud: !Equals [ us-gov-west-1, !Ref 'AWS::Region' ]
SupportsGlacier: !Equals [ true, !Ref pSupportsGlacier ]
Resources:
rSecurityAlarmTopic:
Type: AWS::SNS::Topic
Properties:
Subscription:
- Endpoint: !Ref pNotifyEmail
Protocol: email
rArchiveLogsBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
Properties:
AccessControl: LogDeliveryWrite
LifecycleConfiguration:
Rules:
- Id: Transition90daysRetain7yrs
Status: Enabled
ExpirationInDays: 2555
Transition:
TransitionInDays: 90
StorageClass: !If [ SupportsGlacier, GLACIER, STANDARD_IA ]
VersioningConfiguration:
Status: Enabled
rArchiveLogsBucketPolicy:
Type: AWS::S3::BucketPolicy
DependsOn: rArchiveLogsBucket
Properties:
Bucket: !Ref rArchiveLogsBucket
PolicyDocument:
Statement:
- Sid: Enforce HTTPS Connections
Action: s3:*
Effect: Deny
Principal: '*'
Resource: !Sub
- arn:${Partition}:s3:::${rArchiveLogsBucket}/*
- { Partition: !If [ IsGovCloud, aws-us-gov, aws ] }
Condition:
Bool:
aws:SecureTransport: false
- Sid: Restrict Delete* Actions
Action: s3:Delete*
Effect: Deny
Principal: '*'
Resource: !Sub
- arn:${Partition}:s3:::${rArchiveLogsBucket}/*
- { Partition: !If [ IsGovCloud, aws-us-gov, aws ] }
- Sid: DenyUnEncryptedObjectUploads
Effect: Deny
Principal: '*'
Action: s3:PutObject
Resource: !Sub
- arn:${Partition}:s3:::${rArchiveLogsBucket}/*
- { Partition: !If [ IsGovCloud, aws-us-gov, aws ] }
Condition:
StringNotEquals:
s3:x-amz-server-side-encryption: AES256
rCloudTrailBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
Properties:
AccessControl: Private
VersioningConfiguration:
Status: Enabled
LoggingConfiguration:
DestinationBucketName: !Ref rArchiveLogsBucket
LogFilePrefix: cloudtraillogs
rCloudTrailS3Policy:
Type: AWS::S3::BucketPolicy
DependsOn: rCloudTrailBucket
Properties:
Bucket: !Ref rCloudTrailBucket
PolicyDocument:
Statement:
- Sid: AWSCloudTrailAclCheck20150319
Effect: Allow
Principal:
Service: cloudtrail.amazonaws.com
Action: s3:GetBucketAcl
Resource: !Sub
- arn:${Partition}:s3:::${rCloudTrailBucket}
- { Partition: !If [ IsGovCloud, aws-us-gov, aws ] }
- Sid: AWSCloudTrailWrite20150319
Effect: Allow
Principal:
Service: cloudtrail.amazonaws.com
Action: s3:PutObject
Resource: !Sub
- arn:${Partition}:s3:::${rCloudTrailBucket}/*
- { Partition: !If [ IsGovCloud, aws-us-gov, aws ] }
Condition:
StringEquals:
s3:x-amz-acl: bucket-owner-full-control
- Sid: Enforce HTTPS Connections
Action: s3:*
Effect: Deny
Principal: '*'
Resource: !Sub
- arn:${Partition}:s3:::${rCloudTrailBucket}/*
- { Partition: !If [ IsGovCloud, aws-us-gov, aws ] }
Condition:
Bool:
aws:SecureTransport: false
- Sid: Restrict Delete* Actions
Action: s3:Delete*
Effect: Deny
Principal: '*'
Resource: !Sub
- arn:${Partition}:s3:::${rCloudTrailBucket}/*
- { Partition: !If [ IsGovCloud, aws-us-gov, aws ] }
- Sid: DenyUnEncryptedObjectUploads
Effect: Deny
Principal: '*'
Action: s3:PutObject
Resource: !Sub
- arn:${Partition}:s3:::${rCloudTrailBucket}/*
- { Partition: !If [ IsGovCloud, aws-us-gov, aws ] }
Condition:
StringNotEquals:
s3:x-amz-server-side-encryption: AES256
rCloudTrailLoggingLocal:
Type: AWS::CloudTrail::Trail
DependsOn: rCloudTrailS3Policy
Properties:
S3BucketName: !Ref rCloudTrailBucket
IsLogging: true
EnableLogFileValidation: true
IncludeGlobalServiceEvents: true
CloudWatchLogsLogGroupArn: !GetAtt [ rCloudTrailLogGroup, Arn ]
CloudWatchLogsRoleArn: !GetAtt [ rCloudWatchLogsRole, Arn ]
rCloudWatchLogsRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- cloudtrail.amazonaws.com
Action:
- sts:AssumeRole
Path: /
Policies:
- PolicyName: cloudwatchlogsrole
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: AWSCloudTrailCreateLogStream20141101
Effect: Allow
Action:
- logs:CreateLogStream
Resource: !Sub
- arn:${Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:${rCloudTrailLogGroup}:log-stream:*
- { Partition: !If [ IsGovCloud, aws-us-gov, aws ] }
- Sid: AWSCloudTrailPutLogEvents20141101
Effect: Allow
Action:
- logs:PutLogEvents
Resource: !Sub
- arn:${Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:${rCloudTrailLogGroup}:log-stream:*
- { Partition: !If [ IsGovCloud, aws-us-gov, aws ] }
rCloudTrailRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Path: /
Policies:
- PolicyName: cloudtrail-limited-actions
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- s3:ListBucket
Resource: !Sub
- arn:${Partition}:s3:::${rCloudTrailBucket}
- { Partition: !If [ IsGovCloud, aws-us-gov, aws ] }
- Effect: Allow
Action:
- s3:PutObject
- s3:GetObject
Resource: !Sub
- arn:${Partition}:s3:::${rCloudTrailBucket}/*
- { Partition: !If [ IsGovCloud, aws-us-gov, aws ] }
rCloudTrailProfile:
Type: AWS::IAM::InstanceProfile
DependsOn: rCloudTrailRole
Properties:
Path: /
Roles:
- !Ref rCloudTrailRole
rCloudTrailLogGroup:
Type: AWS::Logs::LogGroup
Properties:
RetentionInDays: 90
rIAMPolicyChangesMetricFilter:
Type: AWS::Logs::MetricFilter
Properties:
LogGroupName: !Ref rCloudTrailLogGroup
FilterPattern: '{($.eventName=DeleteGroupPolicy)||($.eventName=DeleteRolePolicy)||($.eventName=DeleteUserPolicy)||($.eventName=PutGroupPolicy)||($.eventName=PutRolePolicy)||($.eventName=PutUserPolicy)||($.eventName=CreatePolicy)||($.eventName=DeletePolicy)||($.eventName=CreatePolicyVersion)||($.eventName=DeletePolicyVersion)||($.eventName=AttachRolePolicy)||($.eventName=DetachRolePolicy)||($.eventName=AttachUserPolicy)||($.eventName=DetachUserPolicy)||($.eventName=AttachGroupPolicy)||($.eventName=DetachGroupPolicy)}'
MetricTransformations:
- MetricNamespace: CloudTrailMetrics
MetricName: IAMPolicyEventCount
MetricValue: 1
rNetworkAclChangesMetricFilter:
Type: AWS::Logs::MetricFilter
Properties:
LogGroupName: !Ref rCloudTrailLogGroup
FilterPattern: '{ ($.eventName = CreateNetworkAcl) || ($.eventName = CreateNetworkAclEntry) || ($.eventName = DeleteNetworkAcl) || ($.eventName = DeleteNetworkAclEntry) || ($.eventName = ReplaceNetworkAclEntry) || ($.eventName = ReplaceNetworkAclAssociation) }'
MetricTransformations:
- MetricNamespace: CloudTrailMetrics
MetricName: NetworkAclEventCount
MetricValue: 1
rNetworkAclChangesAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmDescription: Alarms when an API call is made to create, update or delete a Network ACL.
AlarmActions:
- !Ref rSecurityAlarmTopic
MetricName: NetworkAclEventCount
Namespace: CloudTrailMetrics
ComparisonOperator: GreaterThanOrEqualToThreshold
EvaluationPeriods: 1
Period: 300
Statistic: Sum
Threshold: 1
rSecurityGroupChangesMetricFilter:
Type: AWS::Logs::MetricFilter
Properties:
LogGroupName: !Ref rCloudTrailLogGroup
FilterPattern: '{ ($.eventName = AuthorizeSecurityGroupIngress) || ($.eventName = AuthorizeSecurityGroupEgress) || ($.eventName = RevokeSecurityGroupIngress) || ($.eventName = RevokeSecurityGroupEgress) || ($.eventName = CreateSecurityGroup) || ($.eventName = DeleteSecurityGroup) }'
MetricTransformations:
- MetricNamespace: CloudTrailMetrics
MetricName: SecurityGroupEventCount
MetricValue: 1
rSecurityGroupChangesAlarm:
Type: AWS::CloudWatch::Alarm
DependsOn: rNetworkAclChangesAlarm
Properties:
AlarmDescription: Alarms when an API call is made to create, update or delete a Security Group.
AlarmActions:
- !Ref rSecurityAlarmTopic
MetricName: SecurityGroupEventCount
Namespace: CloudTrailMetrics
ComparisonOperator: GreaterThanOrEqualToThreshold
EvaluationPeriods: 1
Period: 300
Statistic: Sum
Threshold: 1
rIAMRootActivity:
Type: AWS::Logs::MetricFilter
Properties:
LogGroupName: !Ref rCloudTrailLogGroup
FilterPattern: '{ $.userIdentity.type = "Root" && $.userIdentity.invokedBy NOT EXISTS && $.eventType != "AwsServiceEvent" }'
MetricTransformations:
- MetricNamespace: CloudTrailMetrics
MetricName: RootUserPolicyEventCount
MetricValue: 1
rRootActivityAlarm:
Type: AWS::CloudWatch::Alarm
DependsOn: rSecurityGroupChangesAlarm
Properties:
AlarmDescription: Root user activity detected!
AlarmActions:
- !Ref rSecurityAlarmTopic
MetricName: RootUserPolicyEventCount
Namespace: CloudTrailMetrics
ComparisonOperator: GreaterThanOrEqualToThreshold
EvaluationPeriods: 1
Period: 300
Statistic: Sum
Threshold: 1
rUnauthorizedAttempts:
Type: AWS::Logs::MetricFilter
Properties:
LogGroupName: !Ref rCloudTrailLogGroup
FilterPattern: '{($.errorCode=AccessDenied)||($.errorCode=UnauthorizedOperation)}'
MetricTransformations:
- MetricNamespace: CloudTrailMetrics
MetricName: UnauthorizedAttemptCount
MetricValue: 1
rUnauthorizedAttemptAlarm:
Type: AWS::CloudWatch::Alarm
DependsOn: rRootActivityAlarm
Properties:
AlarmDescription: Multiple unauthorized actions or logins attempted!
AlarmActions:
- !Ref rSecurityAlarmTopic
MetricName: UnauthorizedAttemptCount
Namespace: CloudTrailMetrics
ComparisonOperator: GreaterThanOrEqualToThreshold
EvaluationPeriods: 1
Period: 300
Statistic: Sum
Threshold: 5
rIAMPolicyChangesAlarm:
Type: AWS::CloudWatch::Alarm
DependsOn: rUnauthorizedAttemptAlarm
Properties:
AlarmDescription: IAM Configuration changes detected!
AlarmActions:
- !Ref rSecurityAlarmTopic
MetricName: IAMPolicyEventCount
Namespace: CloudTrailMetrics
ComparisonOperator: GreaterThanOrEqualToThreshold
EvaluationPeriods: 1
Period: 300
Statistic: Sum
Threshold: 1
rIAMCreateAccessKeyAlarm:
Type: AWS::CloudWatch::Alarm
DependsOn: rIAMPolicyChangesAlarm
Properties:
AlarmDescription: 'Warning: New IAM access key was created. Please be sure this action was neccessary.'
AlarmActions:
- !Ref rSecurityAlarmTopic
MetricName: NewAccessKeyCreated
Namespace: CloudTrailMetrics
ComparisonOperator: GreaterThanOrEqualToThreshold
EvaluationPeriods: 1
Period: 300
Statistic: Sum
Threshold: 1
rIAMCreateAccessKey:
Type: AWS::Logs::MetricFilter
DependsOn: rIAMCreateAccessKeyAlarm
Properties:
LogGroupName: !Ref rCloudTrailLogGroup
FilterPattern: '{($.eventName=CreateAccessKey)}'
MetricTransformations:
- MetricNamespace: CloudTrailMetrics
MetricName: NewAccessKeyCreated
MetricValue: 1
rCloudTrailChangeAlarm:
Type: AWS::CloudWatch::Alarm
DependsOn: rIAMCreateAccessKeyAlarm
Properties:
AlarmDescription: 'Warning: Changes to CloudTrail log configuration detected in this account'
AlarmActions:
- !Ref rSecurityAlarmTopic
MetricName: CloudTrailChangeCount
Namespace: CloudTrailMetrics
ComparisonOperator: GreaterThanOrEqualToThreshold
EvaluationPeriods: 1
Period: 300
Statistic: Sum
Threshold: 1
rCloudTrailChange:
Type: AWS::Logs::MetricFilter
Properties:
LogGroupName: !Ref rCloudTrailLogGroup
FilterPattern: '{($.eventSource = cloudtrail.amazonaws.com) && (($.eventName != Describe*) && ($.eventName != Get*) && ($.eventName != Lookup*) && ($.eventName != Lookup*))}'
MetricTransformations:
- MetricNamespace: CloudTrailMetrics
MetricName: CloudTrailChangeCount
MetricValue: 1
Outputs:
rArchiveLogsBucket:
Value: !Ref rArchiveLogsBucket
rSecurityAlarmTopic:
Value: !Ref rSecurityAlarmTopic
rCloudTrailLogGroup:
Value: !Ref rCloudTrailLogGroup