-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathasff2hdf.ts
More file actions
296 lines (266 loc) · 9.93 KB
/
Copy pathasff2hdf.ts
File metadata and controls
296 lines (266 loc) · 9.93 KB
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
import fs from 'fs';
import {
type AwsSecurityFindingFilters,
type DescribeStandardsControlsCommandOutput,
type GetEnabledStandardsCommandOutput,
SecurityHub,
type SecurityHubClientConfig,
type StandardsControl,
type StandardsSubscription,
} from '@aws-sdk/client-securityhub';
import { Flags } from '@oclif/core';
import { ASFFResults as Mapper, INPUT_TYPES } from '@mitre/hdf-converters';
import { NodeHttpHandler } from '@smithy/node-http-handler';
import https from 'https';
import _ from 'lodash';
import { checkInput, checkSuffix, resolveSafeChild, safeFilename } from '../../utils/global';
import { createWinstonLogger } from '../../utils/logging';
import { BaseCommand } from '../../utils/oclif/base_command';
// Should be no more than 100
const API_MAX_RESULTS = 100;
export default class ASFF2HDF extends BaseCommand<typeof ASFF2HDF> {
static readonly usage
= '<%= command.id %> -o <hdf-output-folder> [--interactive] [-L info|warn|debug|verbose]'
+ ' [-i <asff-json> | -a | -r <region> | -I | -C <certificate> | -t <target>...] [-H <additional-input-files>...]';
static readonly description
= 'Translate a AWS Security Finding Format JSON into a Heimdall Data Format JSON file(s)';
static readonly examples = [
{
description: '\u001B[93mUsing ASFF JSON file\u001B[0m',
command: '<%= config.bin %> <%= command.id %> -i asff-findings.json -o output-folder-name',
},
{
description: '\u001B[93mUsing ASFF JSON file with additional input files\u001B[0m',
command: '<%= config.bin %> <%= command.id %> -i asff-findings.json --securityHub standard-1.json standard-2.json -o output-folder-name',
},
{
description: '\u001B[93mUsing AWS to pull ASFF JSON findings\u001B[0m',
command: '<%= config.bin %> <%= command.id %> --aws -o out -r us-west-2 --target rhel7',
},
];
static readonly flags = {
input: Flags.string({
char: 'i',
required: false,
description: '\u001B[31m(required if not using AWS)\u001B[34m Input ASFF JSON file',
exclusive: ['aws', 'region', 'insecure', 'certificate', 'target'],
}),
aws: Flags.boolean({
char: 'a',
required: false,
description: 'Pull findings from AWS Security Hub',
exclusive: ['input'],
dependsOn: ['region'],
}),
region: Flags.string({
char: 'r',
required: false,
description: 'Security Hub region to pull findings from',
exclusive: ['input'],
}),
insecure: Flags.boolean({
char: 'I',
required: false,
default: false,
description: 'Disable SSL verification, this is insecure.',
exclusive: ['input', 'certificate'],
}),
securityHub: Flags.string({
char: 'H',
required: false,
multiple: true,
description:
'Additional input files to provide context that an ASFF file needs such as the CIS AWS Foundations or AWS Foundational Security Best Practices documents (in ASFF compliant JSON form)',
}),
output: Flags.string({
char: 'o',
required: true,
description: 'Output HDF JSON folder',
}),
certificate: Flags.string({
char: 'C',
required: false,
description: 'Trusted signing certificate file',
exclusive: ['input', 'insecure'],
}),
target: Flags.string({
char: 't',
required: false,
multiple: true,
description:
'Target ID(s) to pull from Security Hub (maximum 10), leave blank for non-HDF findings',
exclusive: ['input'],
}),
};
async run() {
const { flags } = await this.parse(ASFF2HDF);
const logger = createWinstonLogger({ module: 'asff2hdf', level: flags.logLevel });
let securityHub;
// Check if output folder already exists
if (fs.existsSync(flags.output)) {
throw new Error(`Output folder ${flags.output} already exists`);
}
const findings: string[] = [];
// If we've been passed an input file
if (flags.input) {
const data = fs.readFileSync(flags.input, 'utf8');
// Attempt to convert to one finding per line
try {
const convertedJson = JSON.parse(data);
if (Array.isArray(convertedJson)) {
findings.push(
...convertedJson.map(finding => JSON.stringify(finding)),
);
} else if ('Findings' in convertedJson) {
findings.push(
...convertedJson.Findings.map((finding: Record<string, unknown>) =>
JSON.stringify(finding),
),
);
} else if ('Controls' in convertedJson) {
throw new Error(
'Invalid ASFF findings format - a standards standards was passed to --input instead of --securityHub',
);
} else {
checkInput(
{ data: data, filename: flags.input }, // skipcq: JS-0240
INPUT_TYPES.ASFF,
'AWS Security Finding Format JSON',
);
}
} catch (error) {
const splitLines = data.split('\n');
if (splitLines.length === 0) {
logger.error('Invalid ASFF findings format - no lines found');
throw error;
}
try {
findings.push(
...splitLines.map(finding => JSON.stringify(JSON.parse(finding))),
);
} catch (error) {
logger.error('Invalid ASFF findings format - unable to parse JSON');
throw error;
}
}
// If we've been passed any Security Standards JSONs
if (flags.securityHub) {
securityHub = flags.securityHub.map((file: string) =>
fs.readFileSync(file, 'utf8'),
);
}
} else if (flags.aws) {
// Flag to pull findings from AWS Security Hub
const clientOptions: SecurityHubClientConfig = {
region: flags.region,
requestHandler: new NodeHttpHandler({
httpsAgent: new https.Agent({
// Disable HTTPS verification if requested
rejectUnauthorized: !flags.insecure,
// Pass an SSL certificate to trust
ca: flags.certificate
? fs.readFileSync(flags.certificate, 'utf8')
: undefined,
}),
}),
};
// Create our SecurityHub client
const client = new SecurityHub(clientOptions);
// Pagination
let nextToken;
let first = true;
let filters: AwsSecurityFindingFilters = {};
// Filter by target name
if (flags.target) {
filters = {
Id: flags.target.map((target: string) => {
return { Value: target, Comparison: 'PREFIX' };
}),
};
}
logger.info('Starting collection of Findings');
const queryParams: Record<string, unknown> = {
Filters: filters,
MaxResults: API_MAX_RESULTS,
};
// Get findings
while (first || nextToken !== undefined) {
first = false;
logger.debug(`Querying for NextToken: ${nextToken}`);
_.set(queryParams, 'NextToken', nextToken);
const getFindingsResult = await client.getFindings(queryParams);
logger.debug(
`Received: ${getFindingsResult.Findings?.length} findings`,
);
if (getFindingsResult.Findings) {
findings.push(
...getFindingsResult.Findings.map(finding =>
JSON.stringify(finding),
),
);
}
nextToken = getFindingsResult.NextToken;
}
nextToken = undefined;
first = true;
logger.info('Starting collection of enabled security standards');
const enabledStandards: StandardsSubscription[] = [];
// Get active security standards subscriptions (enabled standards)
while (first || nextToken !== undefined) {
first = false;
logger.debug(`Querying for NextToken: ${nextToken}`);
// type system seems to think that this call / the result is from the callback variant of the function instead of the promise based one and throwing fits
const getEnabledStandardsResult: GetEnabledStandardsCommandOutput
= (await client.getEnabledStandards({
NextToken: nextToken,
})) as unknown as GetEnabledStandardsCommandOutput;
logger.debug(
`Received: ${getEnabledStandardsResult.StandardsSubscriptions?.length} standards`,
);
if (getEnabledStandardsResult.StandardsSubscriptions) {
enabledStandards.push(
...getEnabledStandardsResult.StandardsSubscriptions,
);
}
nextToken = getEnabledStandardsResult.NextToken;
}
securityHub = [];
// Describe the controls to give context to the mapper
for (const standard of enabledStandards) {
nextToken = undefined;
first = true;
const standardsControls: StandardsControl[] = [];
while (first || nextToken !== undefined) {
first = false;
logger.debug(`Querying for NextToken: ${nextToken}`);
const getEnabledStandardsResult: DescribeStandardsControlsCommandOutput
= await client.describeStandardsControls({
StandardsSubscriptionArn: standard.StandardsSubscriptionArn,
NextToken: nextToken || '',
});
logger.info(
`Received: ${getEnabledStandardsResult.Controls?.length} Controls`,
);
if (getEnabledStandardsResult.Controls) {
standardsControls.push(...getEnabledStandardsResult.Controls);
}
nextToken = getEnabledStandardsResult.NextToken;
}
securityHub.push(JSON.stringify({ Controls: standardsControls }));
}
} else {
throw new Error(
'Please select an input file or --aws to pull findings from AWS',
);
}
const converter = new Mapper(findings.join('\n'), securityHub);
const results = converter.toHdf();
fs.mkdirSync(flags.output);
_.forOwn(results, (result, filename) => {
fs.writeFileSync(
resolveSafeChild(flags.output, safeFilename(checkSuffix(filename))),
JSON.stringify(result, null, 2),
);
});
}
}