blib/lib/Security/CVSS.pm

filter: Statementstmt 89.6% 113 / 126 Branchbran 75.0% 24 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 94.1% 16 / 17 Podpod 0.0% 0 / 6 totaltotal 84.5% 153 / 181 CC 28 SCAR 15.7
CC
28
Cov
87%
Vector
9.1
BaseScore
7.0
_ConvertToVectorValue
6.0
CRAP 4.8
↑ Summary
1 package Security::CVSS;
2  
3 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Module::Check_Args;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '0.3';
11  
12 1 our %BASE_PARAMS =
13 (
14 AccessVector => {Params => {'remote' => 1, 'local' => 0.7},
15 P2V => {'remote' => 'R', 'local' => 'L'}},
16  
17 AccessComplexity => {Params => {'low' => 1, 'high' => 0.8},
18 P2V => {'low' => 'L', 'high' => 'H'}},
19  
20 Authentication => {Params => {'required' => 0.6, 'not-required' => 1},
21 P2V => {'required' => 'R', 'not-required' => 'NR'}},
22  
23 ConfidentialityImpact => {Params => {'none' => 0, 'partial' => 0.7, 'complete' => 1},
24 P2V => {'none' => 'N', 'partial' => 'P', 'complete' => 'C'}},
25  
26 IntegrityImpact => {Params => {'none' => 0, 'partial' => 0.7, 'complete' => 1},
27 P2V => {'none' => 'N', 'partial' => 'P', 'complete' => 'C'}},
28  
29 AvailabilityImpact => {Params => {'none' => 0, 'partial' => 0.7, 'complete' => 1},
30 P2V => {'none' => 'N', 'partial' => 'P', 'complete' => 'C'}},
31  
32 ImpactBias => {Params => {'normal' => 1, 'confidentiality' => 1, 'integrity' => 1, 'availability' => 1},
33 P2V => {'normal' => 'N', 'confidentiality' => 'C', 'integrity' => 'I', 'availability' => 'A'}}
34 );
35  
36 1 _CreateV2P(\%BASE_PARAMS);
37  
38 1 our %TEMPORAL_PARAMS =
39 (
40 Exploitability => {Params => {'unproven' => 0.85, 'proof-of-concept' => 0.9, 'functional' => 0.95, 'high' => 1},
41 P2V => {'unproven' => 'U', 'proof-of-concept' => 'P', 'functional' => 'F', 'high' => 'H'}},
42  
43 RemediationLevel => {Params => {'official-fix' => 0.87, 'temporary-fix' => 0.9, 'workaround' => 0.95, 'unavailable' => 1},
44 P2V => {'official-fix' => 'O', 'temporary-fix' => 'T', 'workaround' => 'W', 'unavailable' => 'U'}},
45  
46 ReportConfidence => {Params => {'unconfirmed' => 0.9, 'uncorroborated' => 0.95, 'confirmed' => 1},
47 P2V => {'unconfirmed' => 'U', 'uncorroborated' => 'Uc', 'confirmed' => 'C'}}
48 );
49  
50 1 _CreateV2P(\%TEMPORAL_PARAMS);
51  
52 1 our %ENVIRONMENTAL_PARAMS =
53 (
54 CollateralDamagePotential => {Params => {'none' => 0, 'low' => 0.1, 'medium' => 0.3, 'high' => 0.5}},
55 TargetDistribution => {Params => {'none' => 0, 'low' => 0.25, 'medium' => 0.75, 'high' => 1}}
56 );
57  
58 1 our %ALL_PARAMS = (%BASE_PARAMS, %TEMPORAL_PARAMS, %ENVIRONMENTAL_PARAMS);
59  
60 # Create accessors for all parameters
61 1 foreach my $Accessor (keys %ALL_PARAMS)
62 {
63 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
64 12 *{"Security::CVSS::$Accessor"} = sub
65 66 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
66 66 exact_argcount(2);
67 66 my $self = shift;
68 66 $self->_ValidateParam($Accessor, @_);
69 12 };
70 }
71  
72 sub new
73 4 ▶{
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
74 4 range_argcount(1, 2);
75 4 my $class = shift;
76 4 my $Params = shift;
77  
78 4 my $self = bless({}, $class);
79  
80 4 ▶ if (defined($Params))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Params) 2 2 4
81 2 { $self->UpdateFromHash($Params); }
82  
83 4 return $self;
84 }
85  
86 # Create the Vector-to-Param hash from the P2V hash
87 sub _CreateV2P
88 2 ▶{
Subroutine _CreateV2P: called CC=2 SCAR=6.9
89 2 exact_argcount(1);
90 2 my $Params = shift;
91  
92 2 foreach my $Param (keys %$Params)
93 {
94 10 $Params->{$Param}->{V2P} = { map { $Params->{$Param}->{P2V}->{$_} => $_ } keys %{$Params->{$Param}->{P2V}} };
95 }
96 }
97  
98 sub _ValidateParam
99 66 ▶{
Subroutine _ValidateParam: called CC=3 SCAR=11.1
100 66 exact_argcount(3);
101 66 my $self = shift;
102 66 my $Param = shift;
103 66 my $Value = shift;
104  
105 # If vector value - convert to full value
106 66 ▶ if (exists($ALL_PARAMS{$Param}->{V2P}->{$Value}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Security::CVSS::ALL_PARAMS{$Param}{'V2P'}{$Value}) { } 35 31 66
107 35 { $Value = $ALL_PARAMS{$Param}->{V2P}->{$Value}; }
108 else
109 31 { $Value = lc($Value); }
110  
111 66 ▶ if (!grep(/^$Value$/i, keys %{$ALL_PARAMS{$Param}->{Params}}))
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^$Value$/i, keys %{$Security::CVSS::ALL_PARAMS{$Param}{"Params"};}) 0 66 66
112 0 { croak("Invalid value '$Value' for $Param"); }
113  
114 66 $self->{$Param} = $Value;
115 }
116  
117 sub _ConvertToVectorValue
118 0 ▶{
Subroutine _ConvertToVectorValue: not called CC=2 SCAR=17.9
119 0 my $Value = shift;
120 0 my @Words = split('-', $Value);
121  
122 0 my $VectorValue;
123 0 foreach my $Word (@Words)
124 0 { $VectorValue .= uc(substr($Word, 0, 1)); }
125 }
126  
127 # Sets up the object from a vector in the format at:
128 # http://nvd.nist.gov/cvss.cfm?vectorinfo
129 sub Vector
130 8 ▶{
Subroutine Vector: called CC=9 SCAR=22.1
Pod undocumented
131 8 range_argcount(1, 2);
132 8 my ($self, $Vector) = @_;
133  
134 8 ▶ if (defined($Vector))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Vector) { } 4 4 8
135 {
136 4 ▶ if ($Vector !~ m#^\(AV:([RL])/AC:([HL])/Au:(R|NR)/C:([NPC])/I:([NPC])/A:([NPC])/B:([NCIA])(/E:([UPFH])/RL:([OTWU])/RC:(U|Uc|C))?\)#)
Branch: 1 branch
BranchTrueFalseTotal
unless ($Vector =~ m[^\(AV:([RL])/AC:([HL])/Au:(R|NR)/C:([NPC])/I:([NPC])/A:([NPC])/B:([NCIA])(/E:([UPFH])/RL:([OTWU])/RC:(U|Uc|C))?\)]) 0 4 4
137 0 { croak('Invalid CVSS vector'); }
138  
139 4 my %Values =
140 (
141 AccessVector => $1,
142 AccessComplexity => $2,
143 Authentication => $3,
144 ConfidentialityImpact => $4,
145 IntegrityImpact => $5,
146 AvailabilityImpact => $6,
147 ImpactBias => $7
148 );
149  
150 4 ▶ if (defined($8))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $8) 2 2 4
151 {
152 # Has temporal portion
153 2 %Values =
154 (
155 %Values,
156 Exploitability => $9,
157 RemediationLevel => $10,
158 ReportConfidence => $11
159 );
160 }
161  
162 4 $self->UpdateFromHash(\%Values);
163 }
164 else
165 {
166 # Check all parameters exist
167 4 foreach my $Param (keys %BASE_PARAMS)
168 {
169 28 ▶ if (!defined($self->{$Param}))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{$Param}) 0 28 28
170 0 { croak("You must set '$Param' to output the CVSS vector"); }
171 }
172  
173 my $VectorValue = sub
174 34 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
175 34 return $ALL_PARAMS{$_[0]}->{P2V}->{$self->{$_[0]}};
176 4 };
177  
178 4 my $Vector = sprintf('AV:%s/AC:%s/Au:%s/C:%s/I:%s/A:%s/B:%s',
179 &$VectorValue('AccessVector'),
180 &$VectorValue('AccessComplexity'),
181 &$VectorValue('Authentication'),
182 &$VectorValue('ConfidentialityImpact'),
183 &$VectorValue('IntegrityImpact'),
184 &$VectorValue('AvailabilityImpact'),
185 &$VectorValue('ImpactBias'));
186  
187 4 my $Environmental = 1;
188 4 foreach my $Param (keys %TEMPORAL_PARAMS)
189 {
190 8 ▶ if (!defined($self->{$Param}))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{$Param}) 2 6 8
191 {
192 2 $Environmental = 0;
193 2 last;
194 }
195 }
196  
197 4 ▶ if ($Environmental)
Branch: 1 branch
BranchTrueFalseTotal
if ($Environmental) 2 2 4
198 {
199 2 $Vector .= sprintf('/E:%s/RL:%s/RC:%s',
200 &$VectorValue('Exploitability'),
201 &$VectorValue('RemediationLevel'),
202 &$VectorValue('ReportConfidence'));
203 }
204  
205 4 return "($Vector)";
206 }
207 }
208  
209 sub UpdateFromHash
210 7 ▶{
Subroutine UpdateFromHash: called CC=4 SCAR=15.2
Pod undocumented
211 7 exact_argcount(2);
212 7 my ($self, $Params) = @_;
213  
214 7 ▶ if (ref($Params) ne 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $Params ne "HASH") 0 7 7
215 0 { croak 'Parameter must be a hash reference'; }
216  
217 7 foreach my $Param (keys %$Params)
218 {
219 54 ▶ if (!exists($ALL_PARAMS{$Param}))
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Security::CVSS::ALL_PARAMS{$Param}) 0 54 54
220 0 { croak "$Param is not a valid parameter"; }
221  
222 54 $self->$Param($Params->{$Param});
223 }
224 }
225  
226 sub BaseScore
227 11 ▶{
Subroutine BaseScore: called CC=7 SCAR=19.5
Pod undocumented
228 11 exact_argcount(1);
229 11 my $self = shift;
230  
231 # Check all parameters exist
232 11 foreach my $Param (keys %BASE_PARAMS)
233 {
234 77 ▶ if (!defined($self->{$Param}))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{$Param}) 0 77 77
235 0 { croak("You must set '$Param' to calculate the Base CVSS score"); }
236 }
237  
238 11 my $Score = 10;
239 11 foreach my $Param ('AccessVector', 'AccessComplexity', 'Authentication')
240 {
241 33 $Score *= $BASE_PARAMS{$Param}->{Params}->{$self->{$Param}};
242 }
243  
244 # Calculate the impact portion of the score taking into account the weighting bias
245 11 my $ImpactScore = 0;
246 11 foreach my $ImpactType ('ConfidentialityImpact', 'IntegrityImpact', 'AvailabilityImpact')
247 {
248 33 my $Value = $BASE_PARAMS{$ImpactType}->{Params}->{$self->{$ImpactType}};
249  
250 33 ▶ if ($self->{ImpactBias} . 'impact' eq lc($ImpactType))
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'ImpactBias'} . 'impact' eq lc $ImpactType) { } 5 28 33
elsif ($self->{'ImpactBias'} eq 'normal') { } 18 10 28
251 5 { $Value *= 0.5; }
252 elsif ($self->{ImpactBias} eq 'normal')
253 18 { $Value *= 0.333; }
254 else
255 10 { $Value *= 0.25; }
256  
257 33 $ImpactScore += $Value;
258 }
259 11 $Score *= $ImpactScore;
260  
261 # Round to one sig fig
262 11 return sprintf('%.1f', $Score);
263 }
264  
265 sub TemporalScore
266 4 ▶{
Subroutine TemporalScore: called CC=4 SCAR=14.1
Pod undocumented
267 4 exact_argcount(1);
268 4 my $self = shift;
269  
270 # Check all parameters exist
271 4 foreach my $Param (keys %TEMPORAL_PARAMS)
272 {
273 12 ▶ if (!defined($self->{$Param}))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{$Param}) 0 12 12
274 0 { croak("You must set '$Param' to calculate the Temporal CVSS score"); }
275 }
276  
277 4 my $Score = $self->BaseScore();
278  
279 4 foreach my $Param (keys %TEMPORAL_PARAMS)
280 12 { $Score *= $TEMPORAL_PARAMS{$Param}->{Params}->{$self->{$Param}}; }
281  
282 # Round to one sig fig
283 4 return sprintf('%.1f', $Score);
284 }
285  
286 sub EnvironmentalScore
287 1 ▶{
Subroutine EnvironmentalScore: called CC=3 SCAR=11.2
Pod undocumented
288 1 exact_argcount(1);
289 1 my $self = shift;
290  
291 # Check all parameters exist
292 1 foreach my $Param (keys %ENVIRONMENTAL_PARAMS)
293 {
294 2 ▶ if (!defined($self->{$Param}))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{$Param}) 0 2 2
295 0 { croak("You must set '$Param' to calculate the Environmental CVSS score"); }
296 }
297  
298 1 my $TemporalScore = $self->TemporalScore;
299  
300 my $Score = ($TemporalScore + ((10 - $TemporalScore)
301 * $ENVIRONMENTAL_PARAMS{CollateralDamagePotential}->{Params}->{$self->{CollateralDamagePotential}}))
302 1 * $ENVIRONMENTAL_PARAMS{TargetDistribution}->{Params}->{$self->{TargetDistribution}};
303  
304 # Round to one sig fig
305 1 return sprintf('%.1f', $Score);
306 }
307  
308 1 1;
309 __END__
↑ Summary