-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSecurityHubFindingsToMSTeams.yaml
241 lines (227 loc) · 10 KB
/
SecurityHubFindingsToMSTeams.yaml
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
AWSTemplateFormatVersion: '2010-09-09'
Description: >-
Demonstrates how to connect SecurityHub to your Microsoft Teams channel. The template Installs a Lambda function that writes EventBridge Events to a Microsoft Teams incoming web hook. This relies on you creating
an *incoming web hook* in your Microsoft Teams account and simply passing the URL as a parameter to this template
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Microsoft Teams Configuration
Parameters:
- IncomingWebHookURL
ParameterLabels:
IncomingWebHookURL:
default: Microsoft Teams Incoming Web Hook URL
Parameters:
IncomingWebHookURL:
Default: https://CUSTOMER.webhook.office.com/webhookb2/12341234-abcd-1234-abcd-1234123412341234/IncomingWebhook/CODE/UUID
Description: Your unique Incoming Web Hook URL from Microsoft Teams service
Type: String
Resources:
SecurityHubToMSTeamsRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: /service-role/
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole
Policies: []
SecurityHubFindingsToMSTeams:
DependsOn: lambdafindingsToMSTeams
Type: AWS::Events::Rule
Properties:
Name: SecurityHubFindingsToMSTeams
Description: 'EventBridge Rule to enable SecurityHub Findings in Microsoft Teams'
State: ENABLED
EventPattern:
source:
- aws.securityhub
resources:
- !Join
- ':'
- - arn
- aws
- securityhub
- !Ref 'AWS::Region'
- !Ref 'AWS::AccountId'
- !Join
- /
- - action
- custom
- SendToMSTeams
Targets:
- Arn: !GetAtt 'lambdafindingsToMSTeams.Arn'
Id: SecurityHubToMSTeamsFunction
LambdaInvokePermission:
DependsOn:
- lambdafindingsToMSTeams
- SecurityHubFindingsToMSTeams
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
FunctionName: !GetAtt 'lambdafindingsToMSTeams.Arn'
SourceArn: !GetAtt 'SecurityHubFindingsToMSTeams.Arn'
lambdafindingsToMSTeams:
Metadata:
checkov:
skip:
- id: "CKV_AWS_115"
comment: "Example code - ReservedConcurrentExecutions may be considered in a Production environment to guarantee Lambda is launched"
- id: "CKV_AWS_116"
comment: "Example code - a Dead Letter Queue may be considered in a Production environment"
- id: "CKV_AWS_117"
comment: "Example code - Running a Lambda inside a VPC should be considered for a Production environemnt."
- id: "CKV_AWS_173"
comment: "Example code - Encrypting Lambda environment variables using KMS should be considered in Production environment."
cfn_nag:
rules_to_suppress:
- id: W89
reason: "Example code - Running a Lambda inside a VPC should be considered for a Production environemnt."
- id: W92
reason: "Example code - ReservedConcurrentExecutions may be considered in a Production environment to guarantee Lambda is launched."
Type: AWS::Lambda::Function
Properties:
Handler: index.handler
Role: !GetAtt 'SecurityHubToMSTeamsRole.Arn'
Code:
ZipFile: |
'use strict';
const AWS = require('aws-sdk');
const url = require('url');
const https = require('https');
const webHookUrl = process.env['webHookUrl'];
function postMessage(message, callback) {
const body = JSON.stringify(message);
const options = url.parse(webHookUrl);
options.method = 'POST';
options.headers = {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body),
};
const postReq = https.request(options, (res) => {
const chunks = [];
res.setEncoding('utf8');
res.on('data', (chunk) => chunks.push(chunk));
res.on('end', () => {
if (callback) {
callback({
body: chunks.join(''),
statusCode: res.statusCode,
statusMessage: res.statusMessage,
});
}
});
return res;
});
postReq.write(body);
postReq.end();
}
function processEvent(event, callback) {
const message = event;
const consoleUrl = `https://console.aws.amazon.com/securityhub`;
const finding = message.detail.findings[0].Types[0];
const findingDescription = message.detail.findings[0].Description;
const findingTime = message.detail.findings[0].UpdatedAt;
const account = message.detail.findings[0].AwsAccountId;
const region = message.detail.findings[0].Resources[0].Region;
const type = message.detail.findings[0].Resources[0].Type;
const messageId = message.detail.findings[0].Resources[0].Id;
const resource = message.detail.findings[0].Resources[0];
const recommendationText = message.detail.findings[0].Remediation.Recommendation.Text;
const recommendationUrl = message.detail.findings[0].Remediation.Recommendation.Url;
const title = message.detail.findings[0].Title;
var color = '#7CD197';
var severity = '';
if (1 <= message.detail.findings[0].Severity.Normalized && message.detail.findings[0].Severity.Normalized <= 39) {
severity = 'LOW';
color = '#879596';
} else if (40 <= message.detail.findings[0].Severity.Normalized && message.detail.findings[0].Severity.Normalized <= 69) {
severity = 'MEDIUM';
color = '#ed7211';
} else if (70 <= message.detail.findings[0].Severity.Normalized && message.detail.findings[0].Severity.Normalized <= 89) {
severity = 'HIGH';
color = '#ed7211';
} else if (90 <= message.detail.findings[0].Severity.Normalized && message.detail.findings[0].Severity.Normalized <= 100) {
severity = 'CRITICAL';
color = '#ff0209';
} else {
severity = 'INFORMATIONAL';
color = '#007cbc';
}
const sections= [{
"summary": finding + ` - ${consoleUrl}/home?region=` + `${region}#/findings?search=id%3D${messageId}`,
"activitySubtitle": `AWS SecurityHub finding in **${region}** for Acct: **${account}**`,
"activityTitle": `${title}`,
"activityImage": "https://raw.githubusercontent.com/aws-samples/aws-securityhub-findings-to-msteams/master/images/securityhub.png",
"text": `${findingDescription}`,
"facts": [{
"name": "Severity",
"value": `${severity}`,
}, {
"name": "Region",
"value": `${region}`,
}, {
"name": "Resource Type",
"value": `${type}`,
}, {
"name": "Resource Identifier",
"value": `***${messageId}***`,
}, {
"name": "Time Last Seen in Security Hub",
"value": `${findingTime}`,
}, {
"name": "Recommendation",
"value": `${recommendationText}`,
}, {
"name": "Recommendation URL",
"value": `${recommendationUrl}`,
}, {
"name": "Resource",
"value": "```" + JSON.stringify(resource, null, 2) + "```",
}],
"markdown": true,
"themeColor": color
}];
const teamsMessage = {
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"themeColor": color,
"summary": "SecurityHub Finding",
"sections": sections
}
postMessage(teamsMessage, (response) => {
if (response.statusCode < 400) {
console.info('Message posted successfully');
callback(null);
} else if (response.statusCode < 500) {
console.error(`Error posting message to Microsoft Teams API: ${response.statusCode} - ${response.statusMessage}`);
callback(null);
} else {
callback(`Server error when processing message: ${response.statusCode} - ${response.statusMessage}`);
}
});
}
exports.handler = (event, context, callback) => {
console.log("ENVIRONMENT VARIABLES\n" + JSON.stringify(process.env, null, 2))
console.info("EVENT\n" + JSON.stringify(event, null, 2))
processEvent(event, callback);
};
Environment:
Variables:
webHookUrl: !Ref 'IncomingWebHookURL'
Runtime: nodejs16.x
MemorySize: 128
Timeout: 10
Description: Lambda to push SecurityHub findings to Microsoft Teams
TracingConfig:
Mode: Active