-
Notifications
You must be signed in to change notification settings - Fork 0
/
event-to-slack.cf.json
207 lines (207 loc) · 10.8 KB
/
event-to-slack.cf.json
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
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Connects SecurityHub to your Slack channel. The template Installs a Lambda function that writes CW Events to a Slack incoming web hook. This relies on you creating an *incoming web hook* in your slack account and simply passing the URL as a parameter to this template",
"Parameters": {
"IncomingWebHookURL": {
"Default": "https://hooks.slack.com/services/XXXXXX/YYYYY/REPLACE_WITH_YOURS",
"Description": "Your unique Incoming Web Hook URL from slack service",
"Type": "String"
},
"SecurityHubEventArn": {
"Default": "*",
"Description": "ARN of Event to allow lambda to publish",
"Type": "String"
}
},
"Resources": {
"SecurityHubToSlackRole": {
"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": []
}
},
"LambdaInvokePermission": {
"DependsOn": [
"LambdaFindingsToSlack"
],
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"Principal": "events.amazonaws.com",
"FunctionName": {
"Fn::GetAtt": [
"LambdaFindingsToSlack",
"Arn"
]
},
"SourceArn": {
"Ref": "SecurityHubEventArn"
}
}
},
"LambdaFindingsToSlack": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"SecurityHubToSlackRole",
"Arn"
]
},
"Code": {
"ZipFile": {
"Fn::Join": [
"",
[
"'use strict';\n",
"const AWS = require('aws-sdk');\n",
"const url = require('url');\n",
"const https = require('https');\n",
"\n",
"const webHookUrl = process.env['webHookUrl'];\n",
"\n",
"function postMessage(message, callback) {\n",
"const body = JSON.stringify(message);\n",
"const options = url.parse(webHookUrl);\n",
"options.method = 'POST';\n",
"options.headers = {\n",
"'Content-Type': 'application/json',\n",
"'Content-Length': Buffer.byteLength(body),\n",
"};\n",
"\n",
"const postReq = https.request(options, (res) => {\n",
"const chunks = [];\n",
"res.setEncoding('utf8');\n",
"res.on('data', (chunk) => chunks.push(chunk));\n",
"res.on('end', () => {\n",
"if (callback) {\n",
"callback({\n",
"body: chunks.join(''),\n",
"statusCode: res.statusCode,\n",
"statusMessage: res.statusMessage,\n",
"});\n",
"}\n",
"});\n",
"return res;\n",
"});\n",
"\n",
"postReq.write(body);\n",
"postReq.end();\n",
"}\n",
"\n",
"function processEvent(event, callback) {\n",
"const message = event;\n",
"const consoleUrl = `https://console.aws.amazon.com/securityhub`;\n",
"const finding = message.detail.findings[0].Types[0];\n",
"const findingDescription = message.detail.findings[0].Description;\n",
"const findingTime = message.detail.findings[0].UpdatedAt;\n",
"const findingTimeEpoch = Math.floor(new Date(findingTime) / 1000);\n",
"const account = message.detail.findings[0].AwsAccountId;\n",
"const region = message.detail.findings[0].Resources[0].Region;\n",
"const type = message.detail.findings[0].Resources[0].Type;\n",
"const messageId = message.detail.findings[0].Resources[0].Id;\n",
"const lastSeen = `<!date^${findingTimeEpoch}^{date} at {time} | ${findingTime}>`;\n",
"var color = '#7CD197';\n",
"var severity = '';\n",
"\n",
"if (1 <= message.detail.findings[0].Severity.Normalized && message.detail.findings[0].Severity.Normalized <= 39) {severity = 'LOW'; color ='#879596';}\n",
"else if (40 <= message.detail.findings[0].Severity.Normalized && message.detail.findings[0].Severity.Normalized <= 69) {severity = 'MEDIUM'; color = '#ed7211';}\n",
"else if (70 <= message.detail.findings[0].Severity.Normalized && message.detail.findings[0].Severity.Normalized <= 89) {severity = 'HIGH'; color = '#ed7211';}\n",
"else if (90 <= message.detail.findings[0].Severity.Normalized && message.detail.findings[0].Severity.Normalized <= 100) {severity = 'CRITICAL'; color = '#ff0209';}\n",
"else {severity = 'INFORMATIONAL'; color = '#007cbc';}\n",
"\n",
"const attachment = [{\n",
"\"fallback\": finding + ` - ${consoleUrl}/home?region=` + `${region}#/findings?search=id%3D${messageId}`,\n",
"\"pretext\": `*AWS SecurityHub finding in ${region} for Acct: ${account}*`,\n",
"\"title\": `${finding}`,\n",
"\"title_link\": `${consoleUrl}/home?region=${region}#/research`,\n",
"\n",
"\"text\": `${findingDescription}`,\n",
"\"fields\": [\n",
"{\"title\": \"Severity\",\"value\": `${severity}`, \"short\": true},\n",
"{\"title\": \"Region\",\"value\": `${region}`,\"short\": true},\n",
"{\"title\": \"Resource Type\",\"value\": `${type}`,\"short\": true},\n",
"{\"title\": \"Last Seen\",\"value\": `${lastSeen}`, \"short\": true}\n",
"],\n",
"\"mrkdwn_in\": [\"pretext\"],\n",
"\"color\": color\n",
"}];\n",
"\n",
"const slackMessage = {\n",
"text : '',\n",
"attachments : attachment,\n",
"username: 'SecurityHub',\n",
"'mrkdwn': true,\n",
"icon_url: 'https://raw.githubusercontent.com/aws-samples/amazon-securityhub-to-slack/master/images/gd_logo.png'\n",
"};\n",
"\n",
"postMessage(slackMessage, (response) => {\n",
"if (response.statusCode < 400) {\n",
"console.info('Message posted successfully');\n",
"callback(null);\n",
"} else if (response.statusCode < 500) {\n",
"console.error(`Error posting message to Slack API: ${response.statusCode} - ${response.statusMessage}`);\n",
"callback(null);\n",
"} else {\n",
"callback(`Server error when processing message: ${response.statusCode} - ${response.statusMessage}`);\n",
"}\n",
"});\n",
"}\n",
"exports.handler = (event, context, callback) => {\n",
" processEvent(event, callback);\n",
"};\n"
]
]
}
},
"Environment": {
"Variables": {
"webHookUrl": {
"Ref": "IncomingWebHookURL"
}
}
},
"Runtime": "nodejs12.x",
"MemorySize": 128,
"Timeout": 10,
"Description": "Lambda to push SecurityHub findings to Slack",
"TracingConfig": {
"Mode": "Active"
}
}
}
},
"Outputs": {
"LambdaFindingsToSlackArn": {
"Description": "Lambda Function ARN",
"Value": {
"Fn::GetAtt": [
"LambdaFindingsToSlack",
"Arn"
]
}
}
}
}