forked from Qualys/community
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlastScanInfo.pl
More file actions
executable file
·302 lines (227 loc) · 7.7 KB
/
lastScanInfo.pl
File metadata and controls
executable file
·302 lines (227 loc) · 7.7 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
297
298
299
300
301
302
#!/usr/bin/perl
=head1 NAME
lastScanInfo.pl
=head1 SYNOPSIS
This script uses the QualysGuard API to get the last scan time and scanner for the specified IP.
=head1 DESCRIPTION
See usage() defined at the bottom of this script.
=head1 EXAMPLES
lastScanInfo.pl --user=foo --password=bar --serverurl=https://qualysapi.qualys.com --ip=10.1.1.1
=cut
#---------------------------------------
#
# Use clauses
#
#---------------------------------------
use strict;
use LWP::UserAgent;
#use LWP::Debug qw(+); # uncomment this to see LWP debug messages
use HTTP::Request;
use HTTP::Response;
use HTTP::Cookies;
use HTTP::Request::Common;
use Getopt::Long;
use File::Basename;
use File::Temp qw/ tempfile tempdir /;
use XML::Twig;
use POSIX qw(strftime);
use Date::Parse;
use URI::Escape;
#---------------------------------------
#
# Globals
#
#---------------------------------------
my $appname = basename($0);
my $version = '1.0.0';
my ($username, $password, $server_url, $help, $proxy, $proxy_username, $proxy_password, $debug, $outputFile, $ip, $usefile, $scanRef, $retStr);
my $logFile= basename($0, '.pl').".log";
my $DAY = (24 * 60 *60);
my $xmlFile = '';
# Get a decent temp file to use
(undef, $xmlFile) = tempfile("sepXXXXXX", SUFFIX => ".xml", OPEN => 0);
my %hostInfo = (TOTAL => 0, SEPINSTALLED => 0, SEPRUNNING => 0, CURRENT => 0, OUTDATED => 0, UNKNOWN => 0, NOSEP => 0 );
my %vulnInfo;
my $validDate;
# Get command line options
GetOptions('username=s' => \$username,
'password=s' => \$password,
'proxy=s' => \$proxy,
'proxy_username=s' => \$proxy_username,
'proxy_password=s' => \$proxy_password,
'serverurl=s' => \$server_url,
'verbose' => \$debug,
'help' => \$help,
'outfile=s' => \$outputFile,
'ip=s' => \$ip,
'usefile=s' => \$usefile);
# Does the user want help?
usage() if ($help);
# Make sure we have all the arguments.
my $msg = '';
my $errStr = 'ERROR - Missing argument';
$msg .= 'username,' unless ($username);
$msg .= 'password,' unless ($password);
$msg .= 'serverurl,' unless ($server_url);
$msg .= 'ip,' unless ($ip);
# Get rid of a trailing comma for neatness
chop($msg);
# Are we supposed to use a file?
$xmlFile = $usefile if ($usefile);
# Make message plural or not
$errStr .= 's' if ($msg =~ /,/);
usage("$errStr: $msg") if ($msg);
# Use default output file if not specified
$outputFile = basename($0, '.pl').".csv" unless ($outputFile);
# Make sure we can write to the output file
usage("ERROR - cannot write to output file $outputFile") unless ((-w $outputFile) || !(-e $outputFile)) ;
# URI-escape the IP
$retStr = "Asset $ip";
$ip = uri_escape($ip);
# Configure the user agent
$ENV{'HTTPS_PROXY'} = $proxy if ($proxy);
$ENV{'HTTPS_PROXY_USERNAME'} = $proxy_username if ($proxy_username);
$ENV{'HTTPS_PROXY_PASSWORD'} = $proxy_password if ($proxy_password);
$ENV{HTTPS_PKCS12_FILE} = '';
$ENV{HTTPS_PKCS12_PASSWORD} = '';
my $agent_string = $appname .'$Revision: '.$version.' $';
my $ua = LWP::UserAgent->new('agent' => $agent_string,
'requests_redirectable' => [],
'timeout' => 900);
$ua->default_header('X-Requested-With' => $agent_string);
# Open the log
open(LOGFILE, ">>$logFile");
# Emit starting timestamp
&logPrint('INFO',"$appname starting up...");
#---------------------------------------
#
# Main script starts here
#
#---------------------------------------
# Get the asset data
&getData('ip');
# Process the data
&processIPData();
# Get scan infomation
&getData('scan');
# Find Scanners
&getScannerList();
print "$retStr\n";
# Ending timestamp
&logPrint('INFO',"$appname finished.");
# Close the log
close(LOGFILE);
# Done!
&cleanUp(0);
# Indicate which command line arguments are supported and/or required
sub usage {
my $msg = shift;
$msg = "$appname $version" unless $msg;
print <<EOF;
$msg
$appname [arguments]
Required Arguments:
--username=SOMEUSER QualysGuard username
--password=SOMEPASS Password for username
--serverurl=https://SOMEURL Platform server url for launching reports
--ip=w.x.y.z The IP to get information for
Optional Arguments:
--proxy=http://SOMEURL HTTPS proxy URL
--proxy_username=SOMEUSER HTTPS proxy USERNAME
--proxy_password=SOMEPASS HTTPS proxy PASSWORD
--verbose Outputs log information to STDOUT
--outfile=SOMEFILE A file to output to; if not specified, will default to $appname.csv
--help Provide usage information (what you are reading)
$appname will get the last scan time and scanner for the specified IP.
Example:
./lastScanInfo.pl --username=foo --password=bar --ip=10.1.1.1 --serverurl=https://qualysapi.qualys.com
Asset 10.1.1.1 was scanned during scan/1315938346.36085 with scanners is_foo_az1 (Scanner 5.17.57-1, Vulnerability Signatures 1.28.210-2)
EOF
&cleanUp(1);
}
# Routine to call asset_data_report.php
sub getData
{
my $type = shift;
# Skip this if we already have the file
return if (-e "$type-$xmlFile");
my $url = "$server_url/msp/";
if ($type eq 'ip') {
$url .= "scan_report_list.php?target=$ip&last=yes";
} else {
$url .= "scan_report.php?ref=$scanRef";
}
my $req = HTTP::Request->new(GET => $url);
$req->authorization_basic($username, $password);
my $res = $ua->request($req);
&logPrint('INFO', "Getting data from API at: $url");
if (! $res->is_success){
my $error = $res->status_line;
&logPrint('ERROR', "Failed to fetch data with error: $error");
&cleanUp(1);
}
# Save the results to a file
open(MYFILE, ">$type-$xmlFile");
binmode(MYFILE);
print MYFILE $res->content;
close(MYFILE);
}
# Routine to process the Data
sub processIPData
{
# Let's create a new twig
my $twig= new XML::Twig( twig_handlers => { SCAN_REPORT => \&getReport } );
# Parse the twig
$twig->parsefile( "ip-$xmlFile" );
}
# Routine to get scan reference
sub getReport
{
# Passed in the vuln information
my ($twig, $scan) = @_;
# Get the scan ref
$scanRef = $scan->{'att'}->{'ref'};
&logPrint('INFO',"Ref: $scanRef\n");
$retStr .= " was scanned during $scanRef";
$twig->purge;
}
# Routine to process the Data
sub getScannerList
{
# Let's create a new twig
my $twig= new XML::Twig( twig_handlers => { KEY => \&getScanners } );
# Parse the twig
$twig->parsefile( "scan-$xmlFile" );
}
# Routine to get scaners
sub getScanners
{
# Passed in the vuln information
my ($twig, $hosts) = @_;
return unless ($hosts->{'att'}->{'value'} eq 'SCAN_HOST');
&logPrint('INFO',"Scanner: ".$hosts->first_child_text);
$retStr .= " with scanner(s) ".$hosts->first_child_text;
$twig->purge;
}
# Routine to print to the log
sub logPrint
{
my ($sevStr, $msg) = @_;
my $timestamp = POSIX::strftime("%m/%d %H:%M:%S", localtime());
my $entry = "$timestamp|$sevStr|$msg\n";
# Send the entry to the logfile if we have a good file number
print LOGFILE $entry if (fileno(LOGFILE));
# Print to STDOUT if we have the --debug flag, or if we don't have a good file number
print $entry if ($debug || (!fileno(LOGFILE)));
}
# Routine to clean up when exiting
sub cleanUp
{
my $exitCode = shift;
# Close the log file
close(LOGFILE);
# Delete the temporary file
unlink("ip-$xmlFile") unless ($usefile);
unlink("scan-$xmlFile") unless ($usefile);
exit($exitCode);
}