File: CVSS.pm

package info (click to toggle)
libcvss-perl 1.14-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 380 kB
  • sloc: perl: 4,409; makefile: 2
file content (208 lines) | stat: -rw-r--r-- 4,185 bytes parent folder | download
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
package App::CVSS;

use feature ':5.10';
use strict;
use warnings;
use utf8;

use Getopt::Long qw(GetOptionsFromArray :config gnu_compat);
use Pod::Usage   qw(pod2usage);
use Carp         ();
use JSON::PP     ();
use Data::Dumper ();

use CVSS ();

our $VERSION = $CVSS::VERSION;

my %options = (format => 'json');

sub _print { print(($_[0] || '') . (defined $options{null} ? "\0" : "\n")) }

sub cli_error {
    my ($error) = @_;
    $error =~ s/ at .* line \d+.*//;
    print STDERR "ERROR: $error\n";
}

sub run {

    my ($class, @args) = @_;

    GetOptionsFromArray(
        \@args, \%options, qw(
            help|h
            man
            v

            vector-string=s

            severity
            score

            base-score
            base-severity

            temporal-score
            temporal-severity

            environmental-score
            environmental-severity

            exploitability-score
            impact-score
            modified-impact-score

            null|0
            format=s

            json
            xml
        )
    ) or pod2usage(-verbose => 0);

    pod2usage(-exitstatus => 0, -verbose => 2) if defined $options{man};
    pod2usage(-exitstatus => 0, -verbose => 0) if defined $options{help};

    if (defined $options{v}) {

        (my $progname = $0) =~ s/.*\///;

        say <<"VERSION";
$progname version $VERSION

Copyright 2023-2024, Giuseppe Di Terlizzi <gdt\@cpan.org>

This program is part of the "CVSS" distribution and is free software;
you can redistribute it and/or modify it under the same terms as Perl itself.

Complete documentation for $progname can be found using 'man $progname'
or on the internet at <https://metacpan.org/dist/CVSS>.
VERSION

        return 0;

    }

    my ($vector_string) = @args;

    pod2usage(-verbose => 1) if !$vector_string;

    $options{format} = 'json' if defined $options{json};
    $options{format} = 'xml'  if defined $options{xml};

    $options{'base-severity'} = 1 if defined $options{severity};
    $options{'base-score'}    = 1 if defined $options{score};

    my $cvss = eval { CVSS->from_vector_string($vector_string) };

    if ($@) {
        cli_error($@);
        return 1;
    }

    if ($options{'base-severity'}) {
        _print $cvss->base_severity;
        return 0;
    }

    if ($options{'base-score'}) {
        _print $cvss->base_score;
        return 0;
    }

    if ($cvss->version <= 3.1) {

        if ($options{'environmental-score'}) {
            _print $cvss->environmental_score;
            return 0;
        }

        if ($options{'environmental-severity'}) {
            _print $cvss->environmental_severity;
            return 0;
        }

        if ($options{'temporal-score'}) {
            _print $cvss->temporal_score;
            return 0;
        }

        if ($options{'temporal-severity'}) {
            _print $cvss->temporal_severity;
            return 0;
        }

        if ($options{'impact-score'}) {
            _print $cvss->impact_score;
            return 0;
        }

        if ($options{'exploitability-score'}) {
            _print $cvss->exploitability_score;
            return 0;
        }

        if ($options{'modified-impact-score'}) {
            _print $cvss->modified_impact_score;
            return 0;
        }

    }

    if ($options{format} eq 'json') {
        print JSON::PP->new->canonical->pretty(1)->convert_blessed(1)->encode($cvss);
        return 0;
    }

    if ($options{format} eq 'xml') {
        print $cvss->to_xml;
        return 0;
    }

}

1;

__END__

=encoding utf-8

=head1 NAME

App::CVSS - CVSS Command Line Interface

=head1 SYNOPSIS

    use App::CVSS qw(run);

    run(\@ARGV);

=head1 DESCRIPTION

App::CVSS "Command Line Interface" helper module for C<cvss(1)>.

=over

=item App::CVSS->run(@args)

Execute the command

=item cli_error($error)

Clean error

=back

=head1 AUTHOR

L<Giuseppe Di Terlizzi|https://metacpan.org/author/gdt>

=head1 COPYRIGHT AND LICENSE

Copyright © 2023-2024 L<Giuseppe Di Terlizzi|https://metacpan.org/author/gdt>

You may use and distribute this module according to the same terms
that Perl is distributed under.

=cut