Class: Nexpose::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/nexpose.rb

Overview

Description

Object that represents a site, including the site configuration, scan history, and device listing.

Example

# Create a new Nexpose Connection on the default port and Login
nsc = Connection.new("10.1.40.10","nxadmin","password")
nsc.()

# Get an Existing Site
site_existing = Site.new(nsc,184)

# Create a New Site, add some hosts, and save it to the NSC
site = Site.new(nsc)
site.setSiteConfig("New Site", "New Site Created in the API")

# Add the hosts
site.site_config.addHost(HostName.new("localhost"))
site.site_config.addHost(IPRange.new("192.168.7.1","192.168.7.255"))
site.site_config.addHost(IPRange.new("10.1.20.30"))

status = site.saveSite()

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, site_id = -1)) ⇒ Site

Returns a new instance of Site.



963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
# File 'lib/nexpose.rb', line 963

def initialize(connection, site_id = -1)
	@error = false
	@connection = connection
	@site_id = site_id

	# If site_id > 0 then get SiteConfig
	if (@site_id.to_i > 0)
		# Create new SiteConfig object
		@site_config = SiteConfig.new()
		# Populate SiteConfig Obect with Data from the NSC
		@site_config.getSiteConfig(@connection,@site_id)
		@site_summary = SiteSummary.new(@site_id, @site_config.site_name, @site_config.description, @site_config.riskfactor)
		@site_scan_history = SiteScanHistory.new(@connection,@site_id)
		@site_device_listing = SiteDeviceListing.new(@connection,@site_id)

	else
		# Just in case user enters a number > -1 or = 0
		@site_id = -1

		@site_config = SiteConfig.new()
		setSiteConfig("New Site " + rand(999999999999).to_s,"")
		@site_summary = nil

	end

end

Instance Attribute Details

#connectionObject (readonly)

The NSC Connection associated with this object



946
947
948
# File 'lib/nexpose.rb', line 946

def connection
  @connection
end

#errorObject (readonly)

true if an error condition exists; false otherwise



938
939
940
# File 'lib/nexpose.rb', line 938

def error
  @error
end

#error_msgObject (readonly)

Error message string



940
941
942
# File 'lib/nexpose.rb', line 940

def error_msg
  @error_msg
end

#request_xmlObject (readonly)

The last XML request sent by this object



942
943
944
# File 'lib/nexpose.rb', line 942

def request_xml
  @request_xml
end

#response_xmlObject (readonly)

The last XML response received by this object



944
945
946
# File 'lib/nexpose.rb', line 944

def response_xml
  @response_xml
end

#site_configObject (readonly)

The configuration of this site SiteConfig Object



955
956
957
# File 'lib/nexpose.rb', line 955

def site_config
  @site_config
end

#site_device_listingObject (readonly)

The device listing for this site SiteDeviceListing Object



958
959
960
# File 'lib/nexpose.rb', line 958

def site_device_listing
  @site_device_listing
end

#site_idObject (readonly)

The Site ID site_id = -1 means create a new site. The NSC will assign a new site_id on SiteSave.



949
950
951
# File 'lib/nexpose.rb', line 949

def site_id
  @site_id
end

#site_scan_historyObject (readonly)

The scan history of this site SiteScanHistory Object



961
962
963
# File 'lib/nexpose.rb', line 961

def site_scan_history
  @site_scan_history
end

#site_summaryObject (readonly)

A summary overview of this site SiteSummary Object



952
953
954
# File 'lib/nexpose.rb', line 952

def site_summary
  @site_summary
end

Instance Method Details

#deleteSiteObject



1038
1039
1040
1041
# File 'lib/nexpose.rb', line 1038

def deleteSite()
	r = @connection.execute('<SiteDeleteRequest session-id="' + @connection.session_id.to_s + '" site-id="' + @site_id + '"/>')
	r.success
end

#getSiteXMLObject



1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
# File 'lib/nexpose.rb', line 1051

def getSiteXML()

	xml = '<Site id="' + "#{@site_config.site_id}" + '" name="' + "#{@site_config.site_name}" + '" description="' + "#{@site_config.description}" + '" riskfactor="' + "#{@site_config.riskfactor}" + '">'

	xml << ' <Hosts>'
	@site_config.hosts.each do |h|
		xml << h.to_xml if h.respond_to? :to_xml
	end
	xml << '</Hosts>'

	xml << '<Credentials>'
	@site_config.credentials.each do |c|
		xml << c.to_xml if c.respond_to? :to_xml
	end
	xml << ' </Credentials>'

	xml << ' <Alerting>'
	@site_config.alerts.each do |a|
		xml << a.to_xml if a.respond_to? :to_xml
	end
	xml << ' </Alerting>'

	xml << ' <ScanConfig configID="' + "#{@site_config.scanConfig.configID}" + '" name="' + "#{@site_config.scanConfig.name}" + '" templateID="' + "#{@site_config.scanConfig.templateID}" + '" configVersion="' + "#{@site_config.scanConfig.configVersion}" + '">'

	xml << ' <Schedules>'
	@site_config.scanConfig.schedules.each do |s|
		xml << ' <Schedule enabled="' + s.enabled + '" type="' + s.type + '" interval="' + s.interval + '" start="' + s.start + '"/>'
	end
	xml << ' </Schedules>'

	xml << ' <ScanTriggers>'
	@site_config.scanConfig.scanTriggers.each do |s|

		if (s.class.to_s == "Nexpose::AutoUpdate")
			xml << ' <autoUpdate enabled="' + s.enabled + '" incremental="' + s.incremental + '"/>'
		end
	end

	xml << ' </ScanTriggers>'

	xml << ' </ScanConfig>'

	xml << ' </Site>'

	return xml
end

#printSiteObject



1044
1045
1046
1047
1048
1049
# File 'lib/nexpose.rb', line 1044

def printSite()
	puts "Site ID: " + @site_summary.id
	puts "Site Name: " + @site_summary.site_name
	puts "Site Description: " + @site_summary.description
	puts "Site Risk Factor: " + @site_summary.riskfactor
end

#saveSiteObject

Saves this site in the NSC



1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
# File 'lib/nexpose.rb', line 1025

def saveSite()
	r = @connection.execute('<SiteSaveRequest session-id="' + @connection.session_id + '">' + getSiteXML() + ' </SiteSaveRequest>')
	if (r.success)
		@site_id =  r.attributes['site-id']
		@site_config._set_site_id(@site_id)
		@site_config.scanConfig._set_configID(@site_id)
		@site_config.scanConfig._set_name(@site_id)
		return true
	else
		return false
	end
end

#scanSiteObject

Initiates a scan of this site. If successful returns scan_id and engine_id in an associative array. Returns false if scan is unsuccessful.



1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
# File 'lib/nexpose.rb', line 1010

def scanSite()
	r = @connection.execute('<SiteScanRequest session-id="' + "#{@connection.session_id}" + '" site-id="' + "#{@site_id}" + '"/>')
	if(r.success)
		res = {}
		r.res.elements.each('//Scan/') do |s|
			res[:scan_id]   = s.attributes['scan-id']
			res[:engine_id] = s.attributes['engine-id']
		end
		return res
	else
		return false
	end
end

#setSiteConfig(site_name, description, riskfactor = 1) ⇒ Object

Creates a new site configuration



997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
# File 'lib/nexpose.rb', line 997

def setSiteConfig(site_name, description, riskfactor = 1)
	setSiteSummary(site_name,description,riskfactor)
	@site_config = SiteConfig.new()
	@site_config._set_site_id(-1)
	@site_config._set_site_name(site_name)
	@site_config._set_description(description)
	@site_config._set_riskfactor(riskfactor)
	@site_config._set_scanConfig(ScanConfig.new(-1,"tmp","full-audit"))
	@site_config._set_connection(@connection)

end

#setSiteSummary(site_name, description, riskfactor = 1) ⇒ Object

Creates a new site summary



991
992
993
994
# File 'lib/nexpose.rb', line 991

def setSiteSummary(site_name, description, riskfactor = 1)
	@site_summary = SiteSummary.new(-1,site_name,description,riskfactor)

end