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.



1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
# File 'lib/nexpose.rb', line 1440

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



1423
1424
1425
# File 'lib/nexpose.rb', line 1423

def connection
  @connection
end

#errorObject (readonly)

true if an error condition exists; false otherwise



1415
1416
1417
# File 'lib/nexpose.rb', line 1415

def error
  @error
end

#error_msgObject (readonly)

Error message string



1417
1418
1419
# File 'lib/nexpose.rb', line 1417

def error_msg
  @error_msg
end

#request_xmlObject (readonly)

The last XML request sent by this object



1419
1420
1421
# File 'lib/nexpose.rb', line 1419

def request_xml
  @request_xml
end

#response_xmlObject (readonly)

The last XML response received by this object



1421
1422
1423
# File 'lib/nexpose.rb', line 1421

def response_xml
  @response_xml
end

#site_configObject (readonly)

SiteConfig Object



1432
1433
1434
# File 'lib/nexpose.rb', line 1432

def site_config
  @site_config
end

#site_device_listingObject (readonly)

SiteDeviceListing Object



1435
1436
1437
# File 'lib/nexpose.rb', line 1435

def site_device_listing
  @site_device_listing
end

#site_idObject (readonly)

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



1426
1427
1428
# File 'lib/nexpose.rb', line 1426

def site_id
  @site_id
end

#site_scan_historyObject (readonly)

SiteScanHistory Object



1438
1439
1440
# File 'lib/nexpose.rb', line 1438

def site_scan_history
  @site_scan_history
end

#site_summaryObject (readonly)

SiteSummary Object



1429
1430
1431
# File 'lib/nexpose.rb', line 1429

def site_summary
  @site_summary
end

Instance Method Details

#deleteSiteObject



1515
1516
1517
1518
# File 'lib/nexpose.rb', line 1515

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

#getSiteXMLObject



1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
# File 'lib/nexpose.rb', line 1528

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



1521
1522
1523
1524
1525
1526
# File 'lib/nexpose.rb', line 1521

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



1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
# File 'lib/nexpose.rb', line 1502

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.



1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
# File 'lib/nexpose.rb', line 1487

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



1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
# File 'lib/nexpose.rb', line 1474

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



1468
1469
1470
1471
# File 'lib/nexpose.rb', line 1468

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

end