-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathcna-assignment-info-to-json.pl
executable file
·242 lines (206 loc) · 6.14 KB
/
cna-assignment-info-to-json.pl
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
#!/usr/bin/perl -w
#
# Convert CNA assignment information in CSV or flat file format to JSON.
#
# Copyright (C) The MITRE Corporation.
######################################################################
use strict;
use Getopt::Long;
use JSON::XS;
use Text::CSV_XS;
######################################################################
# Initialize variables.
$| = 1;
my $cve_pat = qr/CVE-[12]\d{3}-\d{4,}/;
my $data_version = "4.0";
my @supported_data_versions = (
$data_version,
);
# Set the assigner and vendor, then remove the "die" line below.
die "\$assigner and \$vendor variables need to be configured!";
my $assigner = '*** unspecified ***'; # e-mail address for the security PoC.
my $vendor = '*** unspecified ***'; # vendor name.
######################################################################
# Process commandline arguments.
my %options = (
'spec' => \$data_version,
);
Getopt::Long::Configure('bundling');
GetOptions(
\%options,
"help|h|?!",
"files|f!",
"spec|s=s",
"vendor|v=s",
) or $options{help} = 1;
$0 =~ s/^.+\///;
if ($options{help} or @ARGV > 0)
{
warn "\n" .
"Usage: $0 [options]\n" .
"\n" .
"Converts CVE assignment information from STDIN to JSON.\n" .
"\n" .
"Options :\n" .
" -?, -h, --help Display this help and exit.\n" .
" -f, --files Write output to individual files instead of to STDOUT.\n" .
" -s, --spec <spec> Output JSON that conforms to specified specification (defaults to $data_version).\n" .
" -v, --vendor <vendor> Use <vendor> as the specified vendor instead of '$vendor'.\n";
exit 1;
}
$vendor = $options{vendor} if (exists $options{vendor});
######################################################################
# Process input.
my $json = new JSON::XS;
my @data;
my $csv = Text::CSV_XS->new({ binary => 1, allow_whitespace => 1 });
my $contents = do { local $/; <> };
my @lines = split(/\n/, $contents);
my $input_fmt = ($lines[0] =~ /^\s*\[CVEID\]/ ? "multi-line" : "csv");
my $l = 0;
while (@lines)
{
my $line = shift @lines;
$l++;
next unless $line =~ /\S/;
my($id, $product, $version, $problem_type, @urls, $description);
if ($input_fmt eq "csv")
{
unless ($csv->parse($line))
{
warn "Failed to parse line $l - " . $csv->error_input() . "\n";
next;
}
my @fields = $csv->fields();
unless (@fields == 6)
{
warn "Unexpected number of fields in line $l!\n";
next;
}
$id = $fields[0];
$product = $fields[1];
$version = $fields[2];
$problem_type = $fields[3];
push(@urls, split(/\s+/, $fields[4]));
$description = $fields[5];
}
else
{
die "*** Invalid content in line $l ($line)! ***\n" unless ($line =~ /^\s*\[CVEID\]\s*:\s*(.+?)\s*$/);
$id = $1;
$line = shift @lines or die "*** Incomplete entry for $id at line $l! ***\n";
$l++;
die "*** Invalid content in line $l ($line)! ***\n" unless ($line =~ /^\s*\[PRODUCT\]\s*:\s*(.+?)\s*$/);
$product = $1;
$line = shift @lines or die "*** Incomplete entry for $id at line $l! ***\n";
$l++;
die "*** Invalid content in line $l ($line)! ***\n" unless ($line =~ /^\s*\[VERSION\]\s*:\s*(.+?)\s*$/);
$version = $1;
$line = shift @lines or die "*** Incomplete entry for $id at line $l! ***\n";
$l++;
die "*** Invalid content in line $l ($line)! ***\n" unless ($line =~ /^\s*\[PROBLEMTYPE\]\s*:\s*(.+?)\s*$/);
$problem_type = $1;
$line = shift @lines or die "*** Incomplete entry for $id at line $l! ***\n";
$l++;
die "*** Invalid content in line $l ($line)! ***\n" unless ($line =~ /^\s*\[REFERENCES\]\s*:\s*(.+?)\s*$/);
@urls = split(/\s+/, $1);
$line = shift @lines or die "*** Incomplete entry for $id at line $l! ***\n";
$l++;
die "*** Invalid content in line $l ($line)! ***\n" unless ($line =~ /^\s*\[DESCRIPTION\]\s*:\s*(.+?)\s*$/);
$description = $1;
}
$id =~ s/^\s+|\s+$//g;
$product =~ s/^\s+|\s+$//g;
$version =~ s/^\s+|\s+$//g;
$problem_type =~ s/^\s+|\s+$//g;
$description =~ s/^\s+|\s+$//g;
unless ($id =~ /^$cve_pat$/)
{
warn "ignored - '$id' is not a valid CVE id.\n";
next;
}
my $datum;
if ($data_version == "4.0")
{
$datum->{data_type} = "CVE";
$datum->{data_format} = "MITRE";
$datum->{data_version} = $data_version;
$datum->{CVE_data_meta} = {
'ID' => $id,
};
$datum->{problemtype} = {
"problemtype_data" => [
{
"description" => [
{
"lang" => "eng",
"value" => "$problem_type",
},
],
},
],
};
my @version_objs;
foreach my $v (split /\s*;\s*/, $version)
{
$v =~ s/^\s*and\s+(\S)/$1/;
$v =~ s/^\s+|\s+$//g;
push(@version_objs, { "version_value" => $v });
}
$datum->{affects} = {
"vendor" => {
"vendor_data" => [
{
"vendor_name" => "$vendor",
"product" => {
"product_data" => [
{
"product_name" => "$product",
"version" => {
"version_data" => [
@version_objs
],
},
},
],
},
},
],
},
};
$datum->{CVE_data_meta}->{ASSIGNER} = $assigner;
$datum->{CVE_data_meta}->{STATE} = "PUBLIC";
$datum->{description} = {
'description_data' => [
{
"value" => $description,
"lang" => "eng",
}
],
};
foreach my $url (@urls)
{
my $ref_obj = {
"url" => $url,
};
push(@{$datum->{references}->{reference_data}}, $ref_obj);
}
}
if ($options{files})
{
my $file = $id . ".json";
my $fh;
unless (open $fh, ">:encoding(UTF-8)", "$file")
{
warn "Failed to write to '$file' - $!\n";
next;
}
print $fh $json->canonical(1)->pretty(1)->encode($datum);
close($fh);
}
else
{
push(@data, $datum);
}
}
print $json->canonical(1)->pretty(1)->encode(\@data) unless $options{files};