Class: Nexpose::Connection
- Inherits:
-
Object
- Object
- Nexpose::Connection
- Includes:
- NexposeAPI, XMLUtils
- Defined in:
- lib/nexpose/connection.rb
Overview
Description
Object that represents a connection to a Nexpose Security Console.
Examples
# Create a new Nexpose::Connection on the default port
nsc = Connection.new('10.1.40.10', 'nxadmin', 'password')
# Create a new Nexpose::Connection from a URI or "URI" String
nsc = Connection.from_uri('https://10.1.40.10:3780', 'nxadmin', 'password')
# Login to NSC and Establish a Session ID
nsc.login
# Check Session ID
if nsc.session_id
puts 'Login Successful'
else
puts 'Login Failure'
end
# Logout
logout_success = nsc.logout
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
The hostname or IP Address of the NSC.
-
#password ⇒ Object
readonly
The password used to login to the NSC.
-
#port ⇒ Object
readonly
The port of the NSC (default is 3780).
-
#request_xml ⇒ Object
readonly
The last XML request sent by this object, useful for debugging.
-
#response_xml ⇒ Object
readonly
The last XML response received by this object, useful for debugging.
-
#session_id ⇒ Object
readonly
Session ID of this connection.
-
#url ⇒ Object
readonly
The URL for communication.
-
#username ⇒ Object
readonly
The username used to login to the NSC.
Class Method Summary collapse
-
.from_uri(uri, user, pass, silo_id = nil) ⇒ Object
A constructor to load a Connection object from a URI.
Instance Method Summary collapse
-
#download(url, file_name = nil) ⇒ Object
Download a specific URL, typically a report.
-
#execute(xml, version = '1.1') ⇒ Object
Execute an API request.
-
#initialize(ip, user, pass, port = 3780, silo_id = nil) ⇒ Connection
constructor
A constructor for Connection.
-
#login ⇒ Object
Establish a new connection and Session ID.
-
#logout ⇒ Object
Logout of the current connection.
Methods included from NexposeAPI
#_append_asset!, #_scan_ad_hoc, #console_command, #create_multi_tenant_user, #create_silo, #create_silo_profile, #delete_asset_group, #delete_device, #delete_engine, #delete_mtu, #delete_report, #delete_report_config, #delete_report_template, #delete_scan_template, #delete_shared_credential, #delete_silo, #delete_silo_profile, #delete_site, #delete_ticket, #delete_tickets, #delete_user, #delete_vuln_exception, #engine_activity, #filter, #find_device_by_address, #find_vuln_check, #generate_report, #get_user_id, #last_report, #last_scan, #list_asset_groups, #list_device_vulns, #list_engine_pools, #list_engines, #list_mtu, #list_report_templates, #list_reports, #list_scan_templates, #list_shared_credentials, #list_silo_profiles, #list_silos, #list_site_devices, #list_sites, #list_tickets, #list_users, #list_vuln_exceptions, #list_vulns, #pause_scan, #recall_vuln_exception, #report_history, #restart, #resubmit_vuln_exception, #resume_scan, #role_delete, #role_listing, #scan_activity, #scan_asset, #scan_assets, #scan_device, #scan_devices, #scan_ips, #scan_site, #scan_statistics, #scan_status, #search, #send_log, #site_scan_history, #start_update, #stop_scan, #system_information, #vuln_details
Methods included from XMLUtils
Constructor Details
#initialize(ip, user, pass, port = 3780, silo_id = nil) ⇒ Connection
A constructor for Connection
55 56 57 58 59 60 61 62 63 |
# File 'lib/nexpose/connection.rb', line 55 def initialize(ip, user, pass, port = 3780, silo_id = nil) @host = ip @port = port @username = user @password = pass @silo_id = silo_id @session_id = nil @url = "https://#{@host}:#{@port}/api/API_VERSION/xml" end |
Instance Attribute Details
#host ⇒ Object (readonly)
The hostname or IP Address of the NSC
33 34 35 |
# File 'lib/nexpose/connection.rb', line 33 def host @host end |
#password ⇒ Object (readonly)
The password used to login to the NSC
39 40 41 |
# File 'lib/nexpose/connection.rb', line 39 def password @password end |
#port ⇒ Object (readonly)
The port of the NSC (default is 3780)
35 36 37 |
# File 'lib/nexpose/connection.rb', line 35 def port @port end |
#request_xml ⇒ Object (readonly)
The last XML request sent by this object, useful for debugging.
44 45 46 |
# File 'lib/nexpose/connection.rb', line 44 def request_xml @request_xml end |
#response_xml ⇒ Object (readonly)
The last XML response received by this object, useful for debugging.
46 47 48 |
# File 'lib/nexpose/connection.rb', line 46 def response_xml @response_xml end |
#session_id ⇒ Object (readonly)
Session ID of this connection
31 32 33 |
# File 'lib/nexpose/connection.rb', line 31 def session_id @session_id end |
#url ⇒ Object (readonly)
The URL for communication
41 42 43 |
# File 'lib/nexpose/connection.rb', line 41 def url @url end |
#username ⇒ Object (readonly)
The username used to login to the NSC
37 38 39 |
# File 'lib/nexpose/connection.rb', line 37 def username @username end |
Class Method Details
.from_uri(uri, user, pass, silo_id = nil) ⇒ Object
A constructor to load a Connection object from a URI
49 50 51 52 |
# File 'lib/nexpose/connection.rb', line 49 def self.from_uri(uri, user, pass, silo_id = nil) uri = URI.parse(uri) new(uri.host, user, pass, uri.port, silo_id) end |
Instance Method Details
#download(url, file_name = nil) ⇒ Object
Download a specific URL, typically a report. Include an optional file_name parameter to write the output to a file.
Note: XML and HTML reports have charts not downloaded by this method.
Would need to do something more sophisticated to grab
all the associated image files.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/nexpose/connection.rb', line 102 def download(url, file_name = nil) return nil if url.nil? or url.empty? uri = URI.parse(url) http = Net::HTTP.new(@host, @port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE # XXX: security issue headers = {'Cookie' => "nexposeCCSessionID=#{@session_id}"} resp = http.get(uri.to_s, headers) if file_name File.open(file_name, 'w') { |file| file.write(resp.body) } else resp.body end end |
#execute(xml, version = '1.1') ⇒ Object
Execute an API request
88 89 90 91 92 93 94 |
# File 'lib/nexpose/connection.rb', line 88 def execute(xml, version = '1.1') @request_xml = xml.to_s @api_version = version response = APIRequest.execute(@url, @request_xml, @api_version) @response_xml = response.raw_response_data response end |
#login ⇒ Object
Establish a new connection and Session ID
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/nexpose/connection.rb', line 66 def login begin login_hash = {'sync-id' => 0, 'password' => @password, 'user-id' => @username} login_hash['silo-id'] = @silo_id if @silo_id r = execute(make_xml('LoginRequest', login_hash)) if r.success @session_id = r.sid true end rescue APIError raise AuthenticationFailed.new(r) end end |