This repository has been archived by the owner on May 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
lpvs-scan.pl
executable file
·412 lines (362 loc) · 11.7 KB
/
lpvs-scan.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/perl -w
# Copyright (c) 2012-2014 Lars Windolf <lars.windolf@gmx.de>
# Copyright (C) 2004-2010 John Peacock <jpeacock@cpan.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
use 5.010;
use strict;
use Term::ANSIColor;
use XML::LibXSLT;
use XML::LibXML;
use LWP::UserAgent;
use Getopt::Std;
################################################################################
# OS Configuration
################################################################################
my $apt_show_versions = 'apt-show-versions |grep "security upgradeable"';
my $aptitude = 'aptitude search "?and(~U,~Asecurity)"';
my %config = (
'os' => {
'Ubuntu' => {
'pkgtype' => 'deb',
'pkgsource' => 'description',
'feed' => 'http://www.ubuntu.com/usn/rss.xml',
'upgrades' => [('/usr/lib/update-notifier/apt-check -p', $apt_show_versions, $aptitude)],
'revsplit' => '(-|~|ubuntu)'
},
#'Debian' => {
# 'pkgtype' => 'deb',
# 'pkgsource' => 'link',
# 'feed' => 'http://www.debian.org/security/dsa-long',
# 'upgrades' => [('debsecan --format packages', $apt_show_versions, $aptitude)],
# 'revsplit' => '(-|~)'
#},
#'Redhat' => {
# 'pkgtype' => 'rpm',
# 'pkgsource' => 'link',
# 'feed' => 'https://rhn.redhat.com/rpc/recent-errata.pxt'
#},
'CentOS' => {
'pkglist' => 'rpm -qa',
'pkgtype' => 'rpm',
'pkgquery' => 'rpm -q',
'pkgsource' => 'description',
'feed' => 'https://bodhi.fedoraproject.org/rss/updates/?type=security'
# FIXME: define revsplit
}
},
'pkg' => {
'deb' => {
'list' => 'dpkg -l | grep "^ii"',
'query' => 'dpkg -p',
'querytoversion' => 'Version:\s+(\S+)'
},
'rpm' => {
'list' => 'rpm -qa',
'query' => 'rpm -q --queryformat="%{VERSION}-%{RELEASE}.%{ARCH}" ',
'querytoversion' => '^(.*)$'
}
}
);
my $verbose = 0;
my $silent = 0;
my $debug = 0;
my %opts;
getopts('hsvduV', \%opts);
if($opts{'h'}) {
print "\nUsage: $0 -hsvduV\n\n";
print " -h Print this help text.\n";
print " -s Silent mode. Only print warnings and errors.\n";
print " -v Verbose mode. Explains about skipped vulnerabilites.\n";
print " -d Debug mode. Explains about version comparisons.\n";
print " -u Check for security upgrades with OS specific check.\n";
print " -V Disable feed based checking.\n";
print "\n";
exit(0);
}
$silent = 1 if($opts{'s'});
$verbose = 1 if($opts{'v'});
$debug = 1 if($opts{'d'});
# Check for color support
unless(-t 1 and `tput colors` >= 8) {
$ENV{'ANSI_COLORS_DISABLED'} = 1;
}
################################################################################
# Startup Checks
################################################################################
# First try lsb_release (we expect this to exist on Ubuntu, but have a fallback for CentOS)
my $os = `lsb_release -is 2>/dev/null`;
chomp $os;
# CentOS fallback
if(-f "/etc/redhat-release") {
# /etc/redhat-release should have something like "CentOS release 5.x (xxx)
my $tmp = `cat /etc/redhat-release`;
if($tmp =~ /^CentOS\s+release\s+/) {
$os = "CentOS";
} else {
print STDERR "This Redhat-based distribution is not supported! Consider hacking $0 to add support.\n";
exit(1);
}
}
if($os eq "") {
print STDERR "Could not determine OS. Ensure 'lsb_release -s -i' works!\n";
exit(1);
}
unless(defined($config{'os'}->{$os})) {
print STDERR "Sorry '$os' is currently not supported! Consider hacking $0 to add support.\n";
exit(1);
}
# Select configuration
my $osConfig = $config{'os'}->{$os};
my $pkgConfig = $config{'pkg'}->{$osConfig->{'pkgtype'}};
my $packageList = `$pkgConfig->{'list'}`;
print scalar $packageList =~ tr/\n// unless($silent);
print " $os packages are installed.\n" unless($silent);
################################################################################
# Compare two versions
#
# $1 a safe version
# $2 the version to check if it is safe
#
# Returns
# -1 if first version is older
# 0 for identical or package with higher revision
# 1 if first version is newer
################################################################################
sub compare_versions {
my ($version1, $version2) = @_;
my $result = 0;
# Split everything
print "Compare $version1 <=> $version2... " if($debug);
if($version1 ne $version2) {
if($osConfig->{'pkgtype'} eq 'deb') {
`dpkg --compare-versions "$version1" eq "$version2"`;
if($? != 0) {
`dpkg --compare-versions "$version1" lt "$version2"`;
if($? == 0) {
$result = -1;
# We need to analyze revisions before deciding
# this case to distinguish cases like those:
#
# 1.2.0-3ubuntu1.3 < 1.2.0-3.ubuntu1.2 (MATCH)
# and
# 1.2.0-3ubuntu1.3 < 1.5.0-4.ubuntu1.0 (MISS)
#
# So we compare versions and revisions
if(defined($osConfig->{'revsplit'})) {
my @tmp1 = split /$osConfig->{'revsplit'}/, $version1;
my @tmp2 = split /$osConfig->{'revsplit'}/, $version2;
$result = 0 if($tmp1[0] eq $tmp2[0]);
print " revision detail: version #1: $tmp1[0] version #2: $tmp2[0] => " if($debug);
}
} else {
$result = 1;
}
} elsif($osConfig->{'pkgtype'} eq 'rpm') {
my $dir = dirname($0);
`$dir/rpm_vercmp.py "$version1" "$version2"`;
$result = $?;
} else {
die "No version comparison for this pkgtype yet.";
}
}
}
print $result . "\n" if($debug);
return $result;
}
unless(defined($opts{'V'})) {
################################################################################
# Fetch Advisory Feed
################################################################################
print "Downloading advisory feed '$osConfig->{feed}' ...\n" unless($silent);
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
my $response = $ua->get($osConfig->{'feed'});
unless ($response->is_success) {
die "Failed to fetch advisory feed! (".$response->status_line.")";
}
# XSLT for Feed Normalization
my $feed_xslt = <<EOT
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rss="http://purl.org/rss/1.0/"
exclude-result-prefixes="rss">
<!-- match RSS feeds -->
<xsl:template match="/rss/channel">
<feed>
<xsl:for-each select="item">
<xsl:copy>
<title><xsl:apply-templates select="title"/></title>
<link><xsl:apply-templates select="link"/></link>
<description><xsl:apply-templates select="description"/></description>
</xsl:copy>
</xsl:for-each>
</feed>
</xsl:template>
<xsl:template match="/rdf:RDF">
<feed>
<xsl:for-each select="rss:item">
<xsl:copy>
<title><xsl:apply-templates select="rss:title"/></title>
<link><xsl:apply-templates select="rss:link"/></link>
<description><xsl:apply-templates select="rss:description"/></description>
</xsl:copy>
</xsl:for-each>
</feed>
</xsl:template>
<!-- match Atom feeds -->
<!-- FIXME -->
</xsl:stylesheet>
EOT
;
# Normalize Feed
my $parser = XML::LibXML->new();
my $xslt = XML::LibXSLT->new();
my $source = $parser->parse_string($response->decoded_content);
my $style_doc = $parser->parse_string($feed_xslt);
my $stylesheet = $xslt->parse_stylesheet($style_doc);
my $results = $stylesheet->transform($source);
my $tmp = $stylesheet->output_string($results);
my $doc = $parser->parse_string($stylesheet->output_string($results));
################################################################################
# Process Advisories
################################################################################
foreach my $item ($doc->documentElement()->getChildrenByTagName("item")) {
my $title = @{$item->getChildrenByTagName("title")}[0]->textContent;
my $description = @{$item->getChildrenByTagName("description")}[0]->textContent;
chomp $title;
my $found = 0;
my $vulnerable = 0;
my %packages;
# Skip vulnerability if acknowledged
if($title =~ /^\s*([a-zA-Z0-9\-_\.]+):/) {
if(-f "$ENV{HOME}/.lpvs/$1") {
print color 'bold yellow';
print "$title (acknowledged)\n";
print color 'reset';
next;
}
}
# Determine packages affected by advisory
#
# a) from with description text
if($osConfig->{'pkgsource'} eq 'description') {
if($osConfig->{'pkgtype'} eq 'deb') {
# Ubuntu variant
my @tmp = ($description =~ m#href=.https://launchpad.net/ubuntu/\+source/[^>]*>([^<]+)</a>#g);
do {
my $package = shift(@tmp);
my $version = shift(@tmp);
${$packages{$package}} = {} unless(defined(${$packages{$package}}));
${$packages{$package}}->{$version} = 1;
} while(@tmp);
}
# in text RPM variant
if($osConfig->{'pkgtype'} eq 'rpm') {
my @tmp = ($description =~ m#[\s^](\S+\.rpm)[\s\$]#g);
foreach my $rpm (@tmp) {
if($rpm =~ /^(\w+)-([^-]+-[^-]+)\.rpm$/) {
my ($package, $version) = ($1, $2);
${$packages{$package}} = {} unless(defined(${$packages{$package}}));
${$packages{$package}}->{$version} = 1;
}
}
}
# b) from title
#} elsif($osConfig->{'pkgsource'} eq 'title') {
}
foreach my $package (keys %packages) {
my $installed = 0;
my $installedVersion;
# Generic install check
if($packageList =~ /$package/) {
if(`$pkgConfig->{query} $package 2>/dev/null` =~ /$pkgConfig->{'querytoversion'}/) {
# FIXME: RPM might return multiple installed package versions!
$installedVersion = $1;
$installed = 1;
}
}
# Check for vulnerable version
if($installed) {
$found = 1;
foreach my $version (keys %{${$packages{$package}}}) {
my $result = compare_versions($version, $installedVersion);
if($verbose) {
print color 'bold green' if($result == 0);
print color 'bold yellow' if($result == 1);
print color 'bold green' if($result == -1);
print color 'reset';
}
# Simple case: one of the versions fixing the issue
# is currently installed. We need no further checking
if($result == 0) {
$vulnerable = 0;
last;
}
# The current version is older than at least one of
# the suggested versions (old distro)
$vulnerable = 1 if($result > 0);
}
if($vulnerable == 1) {
print color 'bold red';
print "$title\n";
print " -> Vulnerable '$package' version $installedVersion installed!\n\n";
print color 'reset';
print " You should update to one the following versions:\n\n";
foreach my $version (keys %{${$packages{$package}}}) {
print " $version\n";
}
print "\n";
}
}
}
# When we find no packages
unless($silent) {
unless($found) {
if($verbose) {
print color 'yellow';
print "$title\n";
}
} else {
unless($vulnerable) {
print color 'bold green';
print "$title\n";
}
}
print color 'reset';
}
}
}
# Check for other uninstalled security upgrades (not listed by security feed)
if($opts{'u'}) {
if(defined($osConfig->{'upgrades'})) {
foreach(@{$osConfig->{'upgrades'}}) {
my $output = `$_ 2>&1`;
if($? eq 0) {
print color 'bold yellow';
# FIXME: Useful warning output
print "WARNING: '$_' reports additional available security upgrades:\n";
print $output . "\n";
print color 'reset';
last;
}
}
} else {
print "WARNING: Sorry, no upgrade check supported for this distro!\n";
}
}
print "Done.\n" unless($silent);