-
Notifications
You must be signed in to change notification settings - Fork 47
/
scanoptions.pl
206 lines (154 loc) · 5.28 KB
/
scanoptions.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
#!/usr/bin/perl -w
#@(#)$Revision: 1.11 $
# A Perl script, which demonstrates the capabilities of the QualysGuard
# API.
# With this script you can list or set the scan option profiles for
# your account.
# Indentation style: 1 tab = 4 spaces
use HTTP::Request;
use LWP::UserAgent;
require XML::Twig;
my $myname = "scanoptions";
my $request; # HTTP request handle
my $result; # HTTP response handle
my $server; # QualysGuard server's FQDN hostname
my $url; # API access URL
my $xml; # Twig object handle
# $server may be read from the shell environment, or defaults to
# qualysapi.qualys.com otherwise.
if ($ENV{QWSERV}) {
$server = $ENV{QWSERV};
} else {
$server = "qualysapi.qualys.com";
}
# Handlers and helper functions
sub error {
my ($xml, $element) = @_;
my $number = $element->att('number');
my $message;
# Some APIs return embedded "<SUMMARY>error summary text</SUMMARY>"
# elements, so detect and handle accordingly. NOTE: <SUMMARY>
# elements are usually included for reporting multiple errors with
# one error element.
if (!($message = $element->first_child_trimmed_text('SUMMARY'))) {
$message = $element->trimmed_text;
}
if ($number) {
printf STDERR "Request Status: FAILED\nError Number: %1d\nReason: %s\n", $number, $message;
} else {
printf STDERR "Request Status: FAILED\nReason: %s\n", $message;
}
exit 255;
}
sub generic_return {
my ($xml, $element) = @_;
my ($return, $status, $number, $message);
# This is a GENERIC_RETURN element. So, display the RETURN element,
# which gives the detailed status.
if ($return = $element->first_child('RETURN')) {
$status = $return->att('status');
$number = $return->att('number');
$message = $return->trimmed_text;
if ($number) {
printf STDERR "Request Status: %s\nError Number: %1d\nReason: %s\n", $status, $number, $message;
} else {
printf STDERR "Request Status: %s\nReason: %s\n", $status, $message;
}
} else {
# An XML recognition error; display the XML for the offending
# element.
printf STDERR "Unrecognized XML Element:\n%s\n", $element->print;
}
exit ($status eq "SUCCESS" ? 0 : 255);
}
sub loadbalancer {
my ($xml, $element) = @_;
printf " Load Balancer: %s\n", $element->att('value');
}
sub ports {
my ($xml, $element) = @_;
printf " Ports: %s%s\n", $element->att('range'), $element->trimmed_text ? sprintf " (%s)", $element->trimmed_text : "";
}
sub scandeadhosts {
my ($xml, $element) = @_;
printf "Scan Dead Hosts: %s\n", $element->att('value');
}
sub scanneroptions {
my ($xml, $element) = @_;
print "Scanner Options\n";
}
sub usage {
printf STDERR "usage: %s.pl username password {list|set [loadbalancer [ports [scandeadhosts]]]}\n", $myname;
print STDERR "\nloadbalancer={yes|no}\n";
print STDERR "\nports={default|full|<custom range>}\n\n";
print STDERR "<custom range> is a comma-seperated list of port numbers,\nor ranges of port numbers, separated by dashes (for example, 8000-8888).\n";
print STDERR "\nscandeadhosts={yes|no}\n";
printf STDERR "\nExample: > perl ./%s.pl login pass set no full yes\n\n",$myname;
exit 1;
}
# The Perl LWP package gives sufficient capabilities to connect to
# the QualysGuard API. To support the HTTP "Basic Authentication"
# scheme, it's necessary to subclass LWP::UserAgent and define a
# method called "get_basic_credentials", which will be called when
# the server challenges the script for authentication. This method
# returns the username and password, which simply are the second and
# third command line parameters.
# A subclass of LWP::UserAgent to handle HTTP Basic Authentication.
{
package authUserAgent;
@ISA = qw(LWP::UserAgent);
sub new {
my $self = LWP::UserAgent::new(@_);
$self;
}
sub get_basic_credentials {
return ($ARGV[0], $ARGV[1]);
}
}
# Check for at least username, password, and command
usage if ($#ARGV < 2);
if ($ARGV[2] eq "list") {
usage if ($#ARGV > 2);
$url = "https://$server/msp/scan_options.php";
} elsif ($ARGV[2] eq "set") {
usage if ($#ARGV > 5);
$url = "https://$server/msp/scan_options.php?";
$url .= "loadbalancer=$ARGV[3]&" if ($ARGV[3]);
$url .= "ports=$ARGV[4]&" if ($ARGV[4]);
$url .= "scandeadhosts=$ARGV[5]" if ($ARGV[5]);
} else {
usage;
}
# XML::Twig is a handy way to process an XML document. Here, we attach
# a handler, which is triggered whenever a tag is found
# in the XML document. We also attach an error() handler, which is
# triggered whenever Twig finds any errors. Note: The "comments"
# attribute is useful to recognize and return the error message
# text. Finally, the generic_return() handler covers the case where a
# <GENERIC_RETURN> element is encountered.
$xml = new XML::Twig(
TwigHandlers => {
ERROR => \&error,
GENERIC_RETURN => \&generic_return,
LOADBALANCER => \&loadbalancer,
PORTS => \&ports,
SCANDEADHOSTS => \&scandeadhosts,
},
comments => 'keep',
);
$xml->setStartTagHandlers({SCANNEROPTIONS => \&scanneroptions});
# Setup the request
$request = new HTTP::Request GET => $url;
# Create an instance of the authentication user agent
my $ua = authUserAgent->new;
# Make the request
$result = $ua->request($request);
# Check the result
if ($result->is_success) {
# Parse the XML
$xml->parse($result->content);
} else {
# An HTTP related error
printf STDERR "HTTP Error: %s\n", $result->status_line;
exit 1;
}