-
Notifications
You must be signed in to change notification settings - Fork 689
/
monitoringMetrics.js
207 lines (179 loc) · 10.8 KB
/
monitoringMetrics.js
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
var async = require('async');
var helpers = require('../../../helpers/aws');
var filterPatterns = [
{
name: 'Unauthorized API Calls',
pattern: '{ ($.errorCode = *UnauthorizedOperation) || ($.errorCode = AccessDenied*) }'
},
{
name: 'Sign In Without MFA',
pattern: '{ ($.eventName = ConsoleLogin) && ($.additionalEventData.MFAUsed != Yes) }'
},
{
name: 'Root Account Usage',
pattern: '{ $.userIdentity.type = Root && $.userIdentity.invokedBy NOT EXISTS && $.eventType != AwsServiceEvent }'
},
{
name: 'IAM Policy Changes',
pattern: '{($.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)}'
},
{
name: 'CloudTrail Configuration Changes',
pattern: '{ ($.eventName = CreateTrail) || ($.eventName = UpdateTrail) || ($.eventName = DeleteTrail) || ($.eventName = StartLogging) || ($.eventName = StopLogging) }'
},
{
name: 'Sign In Failures',
pattern: '{ ($.eventName = ConsoleLogin) && ($.errorMessage = "Failed authentication") }'
},
{
name: 'Disabled CMKs',
pattern: '{($.eventSource = kms.amazonaws.com) && (($.eventName=DisableKey)||($.eventName=ScheduleKeyDeletion)) }'
},
{
name: 'S3 Policy Changes',
pattern: '{ ($.eventSource = s3.amazonaws.com) && (($.eventName = PutBucketAcl) || ($.eventName = PutBucketPolicy) || ($.eventName = PutBucketCors) || ($.eventName = PutBucketLifecycle) || ($.eventName = PutBucketReplication) || ($.eventName = DeleteBucketPolicy) || ($.eventName = DeleteBucketCors) || ($.eventName = DeleteBucketLifecycle) || ($.eventName = DeleteBucketReplication)) }'
},
{
name: 'ConfigService Changes',
pattern: '{($.eventSource = config.amazonaws.com) && (($.eventName=StopConfigurationRecorder)||($.eventName=DeleteDeliveryChannel)||($.eventName=PutDeliveryChannel)||($.eventName=PutConfigurationRecorder))}'
},
{
name: 'Security Group Changes',
pattern: '{ ($.eventName = AuthorizeSecurityGroupIngress) || ($.eventName = AuthorizeSecurityGroupEgress) || ($.eventName = RevokeSecurityGroupIngress) || ($.eventName = RevokeSecurityGroupEgress) || ($.eventName = CreateSecurityGroup) || ($.eventName = DeleteSecurityGroup)}'
},
{
name: 'Network ACL Changes',
pattern: '{ ($.eventName = CreateNetworkAcl) || ($.eventName = CreateNetworkAclEntry) || ($.eventName = DeleteNetworkAcl) || ($.eventName = DeleteNetworkAclEntry) || ($.eventName = ReplaceNetworkAclEntry) || ($.eventName = ReplaceNetworkAclAssociation) }'
},
{
name: 'Network Gateway Changes',
pattern: '{ ($.eventName = CreateCustomerGateway) || ($.eventName = DeleteCustomerGateway) || ($.eventName = AttachInternetGateway) || ($.eventName = CreateInternetGateway) || ($.eventName = DeleteInternetGateway) || ($.eventName = DetachInternetGateway) }'
},
{
name: 'Route Table Changes',
pattern: '{ ($.eventName = CreateRoute) || ($.eventName = CreateRouteTable) || ($.eventName = ReplaceRoute) || ($.eventName = ReplaceRouteTableAssociation) || ($.eventName = DeleteRouteTable) || ($.eventName = DeleteRoute) || ($.eventName = DisassociateRouteTable) }'
},
{
name: 'VPC Changes',
pattern: '{ ($.eventName = CreateVpc) || ($.eventName = DeleteVpc) || ($.eventName = ModifyVpcAttribute) || ($.eventName = AcceptVpcPeeringConnection) || ($.eventName = CreateVpcPeeringConnection) || ($.eventName = DeleteVpcPeeringConnection) || ($.eventName = RejectVpcPeeringConnection) || ($.eventName = AttachClassicLinkVpc) || ($.eventName = DetachClassicLinkVpc) || ($.eventName = DisableVpcClassicLink) || ($.eventName = EnableVpcClassicLink) }'
},
{
name: 'Organizations Changes',
pattern: '{ ($.eventSource = organizations.amazonaws.com) && (($.eventName = AcceptHandshake) || ($.eventName = AttachPolicy) || ($.eventName = CreateAccount) || ($.eventName = CreateOrganizationalUnit) || ($.eventName = CreatePolicy) || ($.eventName = DeclineHandshake) || ($.eventName = DeleteOrganization) || ($.eventName = DeleteOrganizationalUnit) || ($.eventName = DeletePolicy) || ($.eventName = DetachPolicy) || ($.eventName = DisablePolicyType) || ($.eventName = EnablePolicyType) || ($.eventName = InviteAccountToOrganization) || ($.eventName = LeaveOrganization) || ($.eventName = MoveAccount) || ($.eventName = RemoveAccountFromOrganization) || ($.eventName = UpdatePolicy) || ($.eventName = UpdateOrganizationalUnit)) }'
}
];
module.exports = {
title: 'CloudWatch Monitoring Metrics',
category: 'CloudWatchLogs',
domain: 'Compliance',
severity: 'Medium',
description: 'Ensures metric filters are setup for CloudWatch logs to detect security risks from CloudTrail.',
more_info: 'Sending CloudTrail logs to CloudWatch is only useful if metrics are setup to detect risky activity from those logs. There are numerous metrics that should be used. For the exact filter patterns, please see this plugin on GitHub: https://github.com/cloudsploit/scans/blob/master/plugins/aws/cloudwatchlogs/monitoringMetrics.js',
recommended_action: 'Enable metric filters to detect malicious activity in CloudTrail logs sent to CloudWatch.',
link: 'http://docs.aws.amazon.com/awscloudtrail/latest/userguide/send-cloudtrail-events-to-cloudwatch-logs.html',
apis: ['CloudTrail:describeTrails', 'CloudWatchLogs:describeMetricFilters'],
compliance: {
cis1: '3.0 Monitoring metrics are enabled'
},
realtime_triggers: ['cloudtrail:CreateTrail','cloudtrail:UpdateTrail','cloudtrail:DeleteTrail', 'cloudwatchlogs:PutMetricFilter','cloudwatchlogs:DeleteMetricFilter'],
run: function(cache, settings, callback) {
var results = [];
var source = {};
var regions = helpers.regions(settings);
async.each(regions.cloudtrail, function(region, rcb){
var describeTrails = helpers.addSource(cache, source,
['cloudtrail', 'describeTrails', region]);
if (!describeTrails) return rcb();
if (describeTrails.err || !describeTrails.data) {
helpers.addResult(results, 3,
`Unable to describe CloudTrail trails: ${helpers.addError(describeTrails)}`, region);
return rcb();
}
if (!describeTrails.data.length) {
helpers.addResult(results, 0,
'No CloudTrail trails found', region);
return rcb();
}
var trailsInRegion = [];
for (var t in describeTrails.data) {
if (describeTrails.data[t].HomeRegion &&
describeTrails.data[t].HomeRegion === region) {
trailsInRegion.push(describeTrails.data[t]);
}
}
if (!trailsInRegion.length) {
helpers.addResult(results, 0,
'No CloudTrail trails found in current home region', region);
return rcb();
}
var describeMetricFilters = helpers.addSource(cache, source,
['cloudwatchlogs', 'describeMetricFilters', region]);
if (!describeMetricFilters ||
describeMetricFilters.err || !describeMetricFilters.data) {
helpers.addResult(results, 3,
'Unable to query for CloudWatchLogs metric filters: ' + helpers.addError(describeMetricFilters), region);
return rcb();
}
if (!describeMetricFilters.data.length) {
helpers.addResult(results, 2,
'There are no CloudWatch metric filters in this region', region);
return rcb();
}
// Organize filters by log group name
var filters = {};
for (var f in describeMetricFilters.data) {
var filter = describeMetricFilters.data[f];
if (filter.logGroupName && filter.filterPattern) {
if (!filters[filter.logGroupName]) filters[filter.logGroupName] = [];
filters[filter.logGroupName].push(filter.filterPattern.replace(/\s+/g, '').toLowerCase());
}
}
async.each(trailsInRegion, function(trail, tcb){
if (!trail.CloudWatchLogsLogGroupArn) return tcb();
// CloudTrail stores the CloudWatch Log Group as a full ARN
// while CloudWatch Logs just stores the group name.
// Need to filter the name out for comparison.
var startPos = trail.CloudWatchLogsLogGroupArn.indexOf('log-group:') + 10;
var endPos = trail.CloudWatchLogsLogGroupArn.lastIndexOf(':');
var logGroupName = trail.CloudWatchLogsLogGroupArn.substring(startPos, endPos);
if (!filters[logGroupName]) {
helpers.addResult(results, 2,
'There are no CloudWatch metric filters for this trail', region,
trail.TrailARN);
return tcb();
}
var missing = [];
// If there is a filter setup, check for all strings.
for (var p in filterPatterns) {
var found = false;
var pattern = filterPatterns[p];
var patternSearch = pattern.pattern.replace(/\s+/g, '').toLowerCase();
for (var f in filters) {
var filter = filters[f];
if (filter.indexOf(patternSearch) > - 1) {
found = true;
break;
}
}
if (!found) {
missing.push(pattern.name);
}
}
if (missing.length) {
helpers.addResult(results, 2,
'Trail logs are missing filters for: ' + missing.join(', '), region,
trail.TrailARN);
} else {
helpers.addResult(results, 0,
'Trail logs have filter patterns for all required metrics', region,
trail.TrailARN);
}
tcb();
}, function(){
rcb();
});
}, function(){
callback(null, results, source);
});
}
};