nexpose_pxgrid 0.1.2-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.travis.yml +4 -0
  4. data/Gemfile +4 -0
  5. data/LICENCE.APACHE +144 -0
  6. data/LICENSE.MIT +21 -0
  7. data/README.md +49 -0
  8. data/RadiusSimulator.jar +0 -0
  9. data/Rakefile +31 -0
  10. data/bin/logback.xml +17 -0
  11. data/bin/pxnx.rb +34 -0
  12. data/bin/pxnx_daemon.rb +105 -0
  13. data/keystore.jks +0 -0
  14. data/lib/java_libs/FastInfoset-1.2.12.jar +0 -0
  15. data/lib/java_libs/commons-cli-20040117.000000.jar +0 -0
  16. data/lib/java_libs/commons-codec-1.7.jar +0 -0
  17. data/lib/java_libs/commons-io-2.4.jar +0 -0
  18. data/lib/java_libs/commons-lang-2.6.jar +0 -0
  19. data/lib/java_libs/cxf-api-2.7.3.jar +0 -0
  20. data/lib/java_libs/cxf-rt-bindings-xml-2.7.3.jar +0 -0
  21. data/lib/java_libs/cxf-rt-core-2.7.3.jar +0 -0
  22. data/lib/java_libs/cxf-rt-frontend-jaxrs-2.7.3.jar +0 -0
  23. data/lib/java_libs/cxf-rt-transports-http-2.7.3.jar +0 -0
  24. data/lib/java_libs/geronimo-javamail_1.4_spec-1.7.1.jar +0 -0
  25. data/lib/java_libs/istack-commons-runtime-2.14.jar +0 -0
  26. data/lib/java_libs/javax.ws.rs-api-2.0-m10.jar +0 -0
  27. data/lib/java_libs/jaxb-api-2.2.7.jar +0 -0
  28. data/lib/java_libs/jaxb-core-2.2.7-b57.jar +0 -0
  29. data/lib/java_libs/jaxb-impl-2.2.7-b57.jar +0 -0
  30. data/lib/java_libs/log4j-1.2.17.jar +0 -0
  31. data/lib/java_libs/log4j-rolling-appender-20131024-2017.jar +0 -0
  32. data/lib/java_libs/logback-classic-1.1.2.jar +0 -0
  33. data/lib/java_libs/logback-core-1.1.2.jar +0 -0
  34. data/lib/java_libs/pxgrid-core-model-1.0.0.10.jar +0 -0
  35. data/lib/java_libs/pxgrid-eps-client-stub-1.0.0.10.jar +0 -0
  36. data/lib/java_libs/pxgrid-eps-model-1.0.0.10.jar +0 -0
  37. data/lib/java_libs/pxgrid-gc-admin-model-1.0.0.10.jar +0 -0
  38. data/lib/java_libs/pxgrid-gcl-1.0.0.10.jar +0 -0
  39. data/lib/java_libs/pxgrid-identity-client-stub-1.0.0.10.jar +0 -0
  40. data/lib/java_libs/pxgrid-identity-model-1.0.0.10.jar +0 -0
  41. data/lib/java_libs/pxgrid-ise-model-1.0.0.10.jar +0 -0
  42. data/lib/java_libs/pxgrid-isemetadata-client-stub-1.0.0.10.jar +0 -0
  43. data/lib/java_libs/pxgrid-isemetadata-model-1.0.0.10.jar +0 -0
  44. data/lib/java_libs/pxgrid-net-model-1.0.0.10.jar +0 -0
  45. data/lib/java_libs/pxgrid-netcapture-model-1.0.0.10.jar +0 -0
  46. data/lib/java_libs/pxgrid-tdanalysis-model-1.0.0.10.jar +0 -0
  47. data/lib/java_libs/slf4j-api-1.7.2.jar +0 -0
  48. data/lib/java_libs/smack-3.2.1.jar +0 -0
  49. data/lib/java_libs/smackx-3.2.1.jar +0 -0
  50. data/lib/java_libs/stax-api-1.0-2.jar +0 -0
  51. data/lib/java_libs/stax2-api-3.1.1.jar +0 -0
  52. data/lib/java_libs/woodstox-core-asl-4.1.4.jar +0 -0
  53. data/lib/java_libs/wsdl4j-1.6.2.jar +0 -0
  54. data/lib/java_libs/xmlschema-core-2.0.3.jar +0 -0
  55. data/lib/pxnx_jruby.rb +70 -0
  56. data/lib/pxnx_jruby/config/pxnx.config +40 -0
  57. data/lib/pxnx_jruby/connection.rb +84 -0
  58. data/lib/pxnx_jruby/connection_manager.rb +44 -0
  59. data/lib/pxnx_jruby/eps_broker.rb +49 -0
  60. data/lib/pxnx_jruby/nexpose_connection.rb +62 -0
  61. data/lib/pxnx_jruby/nx_logger.rb +166 -0
  62. data/lib/pxnx_jruby/session_directory_notification.rb +26 -0
  63. data/lib/pxnx_jruby/spoon/README +7 -0
  64. data/lib/pxnx_jruby/spoon/pxnx_daemon_unix.rb +181 -0
  65. data/lib/pxnx_jruby/spoon/pxnx_daemon_windows.rb +50 -0
  66. data/lib/pxnx_jruby/spoon/pxnx_runner.rb +7 -0
  67. data/lib/pxnx_jruby/version.rb +5 -0
  68. data/pxnx.gemspec +28 -0
  69. data/releases/pxnx-0.1.0-java.gem +0 -0
  70. data/truststore.jks +0 -0
  71. metadata +220 -0
data/keystore.jks ADDED
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
data/lib/pxnx_jruby.rb ADDED
@@ -0,0 +1,70 @@
1
+ #
2
+ # The Nexpose pxGrid Node implementation.
3
+ #
4
+ =begin
5
+
6
+ Copyright (C) 2015, Rapid7 LLC
7
+ All rights reserved.
8
+
9
+ Redistribution and use in source and binary forms, with or without modification,
10
+ are permitted provided that the following conditions are met:
11
+
12
+ * Redistributions of source code must retain the above copyright notice,
13
+ this list of conditions and the following disclaimer.
14
+
15
+ * Redistributions in binary form must reproduce the above copyright notice,
16
+ this list of conditions and the following disclaimer in the documentation
17
+ and/or other materials provided with the distribution.
18
+
19
+ * Neither the name of Rapid7 LLC nor the names of its contributors
20
+ may be used to endorse or promote products derived from this software
21
+ without specific prior written permission.
22
+
23
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
27
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
+
34
+ =end
35
+
36
+ #
37
+ # WARNING! This code makes an SSL connection to the Nexpose server, but does NOT
38
+ # verify the certificate at this time. This can be a security issue if
39
+ # an attacker is able to man-in-the-middle the connection between the
40
+ # Metasploit console and the Nexpose server. In the common case of
41
+ # running Nexpose and Metasploit on the same host, this is a low risk.
42
+ #
43
+
44
+ #
45
+ # WARNING! This code is still rough and going through substantive changes. While
46
+ # you can build tools using this library today, keep in mind that
47
+ # method names and parameters may change in the future.
48
+ #
49
+ require 'java'
50
+ Dir[File.dirname(__FILE__) + '/java_libs/*.jar'].each { |jar| require jar }
51
+ require 'pxnx_jruby/version'
52
+ require 'pxnx_jruby/connection'
53
+ require 'pxnx_jruby/session_directory_notification'
54
+ require 'pxnx_jruby/nexpose_connection'
55
+ require 'pxnx_jruby/eps_broker'
56
+ require 'pxnx_jruby/connection_manager'
57
+
58
+ # Implements Cisco pxGrid Node to scan IPs connecting to the network.
59
+ module PxnxJruby
60
+ # Create a connection to the pxGrid Controller.
61
+ def self.create_connection(connection_parameters = {})
62
+ Connection.new(connection_parameters)
63
+ end
64
+
65
+ # Generates a random 10.x.x.x IP Address for simulation/testing.
66
+ def self.random_ip
67
+ ip = IPAddr.new(10 * 2**24 + rand(2**24), Socket::AF_INET)
68
+ ip.to_s
69
+ end
70
+ end
@@ -0,0 +1,40 @@
1
+ ---
2
+ # This configuration file defines all the particular options necessary to support the Cisco pxGrid Nexpose Integration.
3
+ # Fields marked (M) are mandatory.
4
+ #
5
+ # (M) Batch mode delay.
6
+ # Timeframe in seconds for which to wait before scanning connected devices with the 's' for seconds. Default 60s is considered "Realtime".
7
+ :batch_mode_delay: 60s
8
+ # (M) Enables logging to the log directory.
9
+ :logging_enabled: true
10
+ # (M) Sets the log level threshold for output.
11
+ :log_level: info
12
+ # (M) Number of connections. The number of simultaneous connections we can maintain to Nexpose for scanning.
13
+ :nexpose_connection_max: 3
14
+ # Nexpose options.
15
+ :nexpose_data:
16
+ # (M) Nexpose console hostname.
17
+ :nxconsole: 127.0.0.1
18
+ # (M) Nexpose username.
19
+ :nxuser: nxadmin
20
+ # (M) Scan engine to use. A value of '-1' indicates use of the default engine.
21
+ :engine: -1
22
+ # (M) Risk score threshold used to quarantine an asset. If an asset has a risk score equal to or larger than this value it will be quarantined.
23
+ :riskscore: 20
24
+ # (M) Time to wait before cancelling a scan and continuing.
25
+ :scan_timeout: 3600
26
+ # Cisco pxGrid options. Note all of these options can be set through environment variables of the same capitalised name e.g. ISE_URL instead of :ise_url
27
+ :pxg_data:
28
+ # Cisco Identity Services Engine hostname/ip.
29
+ # :ise_url: 127.0.0.7
30
+ # Secondary Cisco Identity Services Engine hostname/ip.
31
+ # :secondary_ise_url: 127.0.0.8
32
+ # Cisco Identity Services Engine username.
33
+ # :ise_username: admin
34
+ # Cisco Identity Services Engine password.
35
+ # :ise_password: Nxadmin1!
36
+ # Keystore passphrase (Keystore located at pxnx/keystore.jks)
37
+ # :keystore_password: nxadmin
38
+ # Truststore passphrase (Truststore located at pxnx/truststore.jks)
39
+ # :truststore_password: nxadmin
40
+
@@ -0,0 +1,84 @@
1
+ module PxnxJruby
2
+ # This class handles to the pxGrid Controller.
3
+ class Connection
4
+ require 'java'
5
+ require 'jruby/core_ext'
6
+ java_import org.slf4j.Logger
7
+ java_import org.slf4j.LoggerFactory
8
+ attr_accessor :config, :recon, :grid
9
+
10
+ # Initializes all the needed variables for connecting to Cisco.
11
+ #
12
+ # === Arguments
13
+ # * +Options+ - A Hash with information about the Cisco connection.
14
+ #
15
+ # === Options
16
+ # * +:ise_url+ - The URL for ISE. If several it also takes an Array of IPs
17
+ # * +:ise_username+ - The username for ISE.
18
+ # * +:ise_password+ - The password for ISE. We highly recommend setting this as an environment variable.
19
+ # * +:keystore_password+ - The password for your keystore. We highly recommend setting this as an environment variable.
20
+ # * +:truststore_password+ - The password for your truststore. We highly recommend setting this as an environment variable.
21
+ #
22
+ # * *Returns* :
23
+ # - @config: A TLSConfiguration type object with all the configurations.
24
+ # ---
25
+ # TODO: This method ABC is too high (http://c2.com/cgi/wiki?AbcMetric)
26
+ # ---
27
+ def initialize(options = {})
28
+ @log = LoggerFactory.getLogger(Connection.become_java!)
29
+ @config = Java::com.cisco.pxgrid.TLSConfiguration.new
30
+
31
+ @options = options
32
+ file_path = File.expand_path(File.dirname(__FILE__))
33
+ hosts = [get_option(:ise_url), get_option(:secondary_ise_url)]
34
+
35
+ @config.setHosts(hosts.select { |h| !h.nil? })
36
+ @config.setUserName(get_option(:ise_username))
37
+ @config.setPassword(get_option(:ise_password))
38
+ @config.setKeystorePath("#{file_path}/../../keystore.jks")
39
+ @config.setKeystorePassphrase(get_option(:keystore_password))
40
+ @config.setTruststorePath("#{file_path}/../../truststore.jks")
41
+ @config.setTruststorePassphrase(get_option(:truststore_password))
42
+ @log.info("Listening for events, using username <#{@config.getUserName}>")
43
+ @config
44
+ end
45
+
46
+ # Returns a configuration option, prioritising environment variables.
47
+ # * *Returns* :
48
+ # - the configuration option value
49
+ def get_option(option)
50
+ value = ENV[option.to_s.upcase]
51
+ value ||= (@options[:pxg_data] || {})[option] unless @options.nil?
52
+ @log.info('No configuration value found for #{option}') if value.nil?
53
+ value
54
+ end
55
+
56
+ # Connects to Cisco ISE.
57
+ # * *Returns* :
58
+ # - true if connection succeeds, false if there was a failure.
59
+ # ---
60
+ # TODO: This method ABC is too high (http://c2.com/cgi/wiki?AbcMetric)
61
+ # ---
62
+ def connect
63
+ @grid = Java.com.cisco.pxgrid.GridConnection.new(@config)
64
+ @recon = Java.com.cisco.pxgrid.ReconnectionManager.new(@grid)
65
+ @recon.setRetryMillisecond(2000)
66
+ @log.info('Connecting...')
67
+ begin
68
+ @recon.start
69
+ rescue => e
70
+ @log.error("Could not connect: #{e.backtrace}")
71
+ return false
72
+ end
73
+ @log.info('Connection complete!')
74
+ true
75
+ end
76
+
77
+ # Disconnects from Cisco ISE.
78
+ def disconnect
79
+ @log.info('Disconnecting...')
80
+ @recon.stop unless @recon.nil?
81
+ @log.info('Disconnection complete!')
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,44 @@
1
+ module PxnxJruby
2
+ # This class managers the concurrency calls for pxGrid and Nexpose.
3
+ class ConnectionManager
4
+ require 'singleton'
5
+ include Singleton
6
+ require 'rufus-scheduler'
7
+ require 'thread_safe'
8
+ require 'java'
9
+ require 'jruby/core_ext'
10
+ java_import org.slf4j.Logger
11
+ java_import org.slf4j.LoggerFactory
12
+ java_import java.util.concurrent.Executors
13
+
14
+ def initialize
15
+ @log = LoggerFactory.getLogger(ConnectionManager.become_java!)
16
+ @join_list = ThreadSafe::Array.new
17
+ @connection_pool = nil
18
+ @scheduler = nil
19
+ end
20
+
21
+ # TODO: This method ABC is too high (http://c2.com/cgi/wiki?AbcMetric)
22
+ def setup(config_options = {})
23
+ # We can only have a certain number of connections to Nexpose. Generate a pool of connections for realtime or batched scans.
24
+ @connection_pool = Executors.newFixedThreadPool(config_options[:nexpose_connection_max])
25
+ # Schedule our "realtime" or batched tasks
26
+ @scheduler = Rufus::Scheduler.new
27
+ @scheduler.every config_options[:batch_mode_delay] do
28
+ begin
29
+ @log.debug("Scheduler executed. Number of queued connections for scanning is <#{@join_list.size}>.")
30
+ @connection_pool.submit(PxnxJruby::NexposeConnection.new(@join_list.clone, config_options)) unless @join_list.empty?
31
+ @join_list.clear
32
+ rescue Exception => e
33
+ @log.error("Error when executing the scheduler! The error was <#{e.message}> and backtrace was <#{e.backtrace.join("\n")}>.")
34
+ end
35
+ end
36
+ end
37
+
38
+ # Adds a new IP to the list.
39
+ def new_connection(ip)
40
+ fail 'The IP address for a new connection cannot be empty!' if ip.nil? || ip.empty?
41
+ @join_list << ip
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,49 @@
1
+ module PxnxJruby
2
+ class EpsBroker
3
+ require 'java'
4
+ require 'jruby/core_ext'
5
+ java_import org.slf4j.Logger
6
+ java_import org.slf4j.LoggerFactory
7
+
8
+ attr_accessor :grid_connection, :eps, :ip
9
+
10
+ # Takes a GridConnection object.
11
+ def initialize(grid)
12
+ @grid = grid
13
+ # Doc says this is an instance method, but it isn't.
14
+ @eps_factory = Java::com.cisco.pxgrid.stub.eps.EPSClientStub.new
15
+ @eps = @eps_factory.createEPSQuery(@grid)
16
+ @log = LoggerFactory.getLogger(EpsBroker.become_java!)
17
+ end
18
+
19
+ # Sends an IP or an Array of IPs (Strings) to quarantine.
20
+ def quarantine_ip(ip)
21
+ @ip = ip.is_a?(Array) ? ip : Array.new(1, ip)
22
+ @ip.each do |single_ip|
23
+ @log.info("Quarantine on <#{single_ip}>")
24
+ begin
25
+ @eps.quarantineByIP(single_ip)
26
+ return true
27
+ rescue Java::com.cisco.pxgrid.TimeoutException => e
28
+ @log.error("Could not quarantine <#{single_ip}> the error was <#{e.message}>.")
29
+ return false
30
+ end
31
+ end
32
+ end
33
+
34
+ # Unquarantines an IP or an Array of IPs (Strings)
35
+ def unquarantine_ip(ip)
36
+ @ip = ip.is_a?(Array) ? ip : Array.new(1, ip)
37
+ @ip.each do |single_ip|
38
+ @log.info("Removing quarantine on <#{single_ip}>.")
39
+ begin
40
+ @eps.unquarantineByIP(single_ip)
41
+ true
42
+ rescue Java::com.cisco.pxgrid.TimeoutException => e
43
+ @log.error("Could not unquarantine <#{single_ip}> the error was <#{e.message}>.")
44
+ return false
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,62 @@
1
+ module PxnxJruby
2
+ class NexposeConnection
3
+ require_relative 'nx_logger'
4
+ require 'nexpose'
5
+ require 'waitutil'
6
+ require 'java'
7
+ require 'jruby/core_ext'
8
+ java_import org.slf4j.Logger
9
+ java_import org.slf4j.LoggerFactory
10
+ java_import 'java.util.concurrent.Callable'
11
+ include Callable
12
+ attr_accessor :ip, :nsc, :site, :scan_info, :devices_to_quarantine
13
+
14
+ def initialize(ips, options = {})
15
+ @log = LoggerFactory.getLogger(NexposeConnection.become_java!)
16
+ @nsc = Nexpose::Connection.new(options[:nexpose_data][:nxconsole],options[:nexpose_data][:nxuser], ENV['NEXPOSE_PASSWORD'])
17
+ @log.info("Connecting to nexpose console: #{options[:nexpose_data][:nxconsole]}.")
18
+ @nsc.login
19
+ PxnxJruby::NxLogger.instance.on_connect(options[:nexpose_data][:nxconsole], 3780, @nsc.session_id, "{}")
20
+ @ip_list = ips
21
+ @options = options
22
+ end
23
+
24
+ # TODO Allow scan options.
25
+ def call
26
+ begin
27
+ @ip = @ip_list.is_a?(Array) ? @ip_list : Array.new(1, @ip_list)
28
+ @site = Nexpose::Site.new("pxGrid-Nexpose-#{DateTime.now.strftime('%Y%jT%H%M%SZ')}", 'full-audit')
29
+ @site.description = "Rapid7 Nexpose - Cisco pxGrid Integration scan job saved at #{DateTime.now.strftime('%Y%jT%H%M%SZ')}"
30
+ @log.info("Scanning IPs <#{@ip.each{|ip| ip}}> on site <#{@site.name}>.")
31
+ @ip.each { |ip| @site.add_ip(ip) }
32
+ @site.engine = @options[:nexpose_data][:engine] unless @options.empty?
33
+ @site.save(@nsc)
34
+ @scan_info = @site.scan(@nsc)
35
+
36
+ # Hold this thread until the scan has finished
37
+ # Add ', :verbose => true' to get more info.
38
+ WaitUtil.wait_for_condition('waiting_for_scan_to_finish', :timeout_sec => @options[:nexpose_data][:scan_timeout], :delay_sec => 30) do
39
+ @completed = true
40
+ if %w(unknown dispatched running).include? (@nsc.scan_status(@scan_info.id))
41
+ @completed = false
42
+ @log.debug("Scan still running for site <#{@site.name}>")
43
+ end
44
+ @completed
45
+ end
46
+ @log.info("Scan completed for site #{@site.name}>")
47
+ devices = @nsc.devices(@site.id)
48
+ @devices_to_quarantine = devices.select { |d| d.risk_score >= @options[:nexpose_data][:riskscore]}
49
+ Thread.new do
50
+ eps_broker = PxnxJruby::EpsBroker.new(@options[:grid_connection].grid)
51
+ @log.info("Quarantining device <#{@devices_to_quarantine}> for site <#{@site.name}>.")
52
+ eps_broker.quarantine_ip(@devices_to_quarantine) unless @devices_to_quarantine.empty?
53
+ end unless @options[:debug] == true
54
+ @log.debug("Deleting temporary site <#{@site.name}>, logging out and exiting.")
55
+ @site.delete(@nsc) unless @options[:debug] == true
56
+ @nsc.logout unless @options[:debug] == true
57
+ rescue Exception => e
58
+ @log.error("Exception while running a Nexpose connection thread! Message is <#{e.message}> and stacktrace is <#{e.backtrace.join("\n")}>.")
59
+ end
60
+ end
61
+ end
62
+ end