Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/plugins/besecure.rb
Views: 11705
#1# This plugin provides integration with beSECURE. Written by Noam Rathaus.2#3# Distributed under MIT license:4# http://www.opensource.org/licenses/mit-license.php5#6# Version 10.5.1778require 'base64'9require 'zlib'10require 'tempfile'11require 'pathname'1213module Msf14class Plugin::BeSECURE < Msf::Plugin15class BeSECURECommandDispatcher16include Msf::Ui::Console::CommandDispatcher1718def name19'beSECURE'20end2122def commands23{24'besecure_help' => 'Displays help',25'besecure_version' => 'Display the version of the beSECURE server',26'besecure_apikey' => 'Set the beSECURE API Key',27'besecure_hostname' => 'Set the beSECURE Hostname',28'besecure_debug' => 'Enable/Disable debugging',29'besecure_ssl_verify' => 'Enable/Disable SSL verification',3031'besecure_report_list' => 'Display list of reports',3233'besecure_report_download' => 'Save a report to disk',34'besecure_report_import' => 'Import report specified by ID into framework'35}36end3738def cmd_besecure_help39print_status('besecure_help Display this help')40print_status('besecure_debug Enable/Disable debugging')41print_status('besecure_version Display the version of the beSECURE server')42print_status('besecure_apikey Set the beSECURE API Key')43print_status('besecure_ssl_verify Set whether to verify or not SSL')44print_status('besecure_hostname Set the beSECURE Hostname')4546print_status47print_status('REPORTS')48print_status('=======')49print_status('besecure_report_list Lists reports')50print_status('besecure_report_download Downloads an beSECURE report specified by ID')51print_status('besecure_report_import Import report specified by ID into framework')52end5354# Verify the database is connected and usable55def database?56if !(framework.db && framework.db.usable)57return false58else59return true60end61end6263# Verify correct number of arguments and verify -h was not given. Return64# true if correct number of arguments and help was not requested.65def args?(args, min = 1, max = nil)66if !max then max = min end67if ((args.length < min) || (args.length > max) || (args[0] == '-h'))68return false69end7071return true72end7374#--------------------------75# Basic Functions76#--------------------------77def cmd_besecure_hostname(*args)78if args?(args)79@hostname = args[0]80print_good(@hostname)81else82print_status('Usage:')83print_status('besecure_hostname string')84end85end8687def cmd_besecure_apikey(*args)88if args?(args)89@apikey = args[0]90print_good(@apikey)91else92print_status('Usage:')93print_status('besecure_apikey string')94end95end9697def cmd_besecure_ssl_verify(*args)98if args?(args)99@ssl_verify = args[0]100if (@ssl_verify != 'yes') && (@ssl_verify != 'no')101@ssl_verify = 'yes'102end103print_good(@ssl_verify)104else105print_status('Usage:')106print_status("besecure_ssl_verify 'yes'/'no' (default is yes)")107end108end109110def cmd_besecure_debug(*args)111if args?(args)112@debug = args[0].to_i113print_good(@debug)114else115print_status('Usage:')116print_status('besecure_debug integer')117end118end119120def cmd_besecure_version121req = Net::HTTP::Post.new('/json.cgi', { 'Host' => @hostname })122req.set_form_data({ 'apikey' => @apikey, 'primary' => 'interface' })123124if @debug125print_status(req.body)126end127128http = Net::HTTP.new(@hostname, 443)129if @debug130http.set_debug_output($stdout) # Logger.new("foo.log") works too131end132133http.use_ssl = true134if @ssl_verify == 'no'135http.verify_mode = OpenSSL::SSL::VERIFY_NONE136end137138res = http.start { |h| h.request(req) }139140unless res141print_error("#{@hostname} - Connection timed out")142return ''143end144145body = ''146begin147body = JSON.parse(res.body)148rescue JSON::ParserError149print_error("#{@hostname} - Unable to parse the response")150return ''151end152153if body['error']154print_error("#{@hostname} - An error occurred:")155print_error(body)156return ''157end158159print_good(body['version'])160end161162#--------------------------163# Report Functions164#--------------------------165166def cmd_besecure_report_list(*_args)167tbl = Rex::Text::Table.new(168'Columns' => ['ID', 'Name', 'Hosts']169)170171if @hostname.empty?172print_error('Missing host value')173return ''174end175176req = Net::HTTP::Post.new('/json.cgi', { 'Host' => @hostname })177req.set_form_data({ 'apikey' => @apikey, 'primary' => 'admin', 'secondary' => 'networks', 'action' => 'returnnetworks', 'search_limit' => 10000 })178179if @debug180print_status(req.body)181end182183http = Net::HTTP.new(@hostname, 443)184if @debug185http.set_debug_output($stdout) # Logger.new("foo.log") works too186end187188http.use_ssl = true189if @ssl_verify == 'no'190http.verify_mode = OpenSSL::SSL::VERIFY_NONE191end192193res = http.start { |h| h.request(req) }194195unless res196print_error("#{@hostname} - Connection timed out")197return ''198end199200body = ''201begin202body = JSON.parse(res.body)203rescue JSON::ParserError204print_error("#{@hostname} - Unable to parse the response")205return ''206end207208if body['error']209print_error("#{@hostname} - An error occurred:")210print_error(body)211return ''212end213214data = body['data']215data.each do |item|216tbl << [ item['ID'], item['Name'], item['PrettyRange']]217end218219# print_good(body)220221print_good('beSECURE list of reports')222print_line223print_line tbl.to_s224print_line225end226227def cmd_besecure_report_download(*args)228if args?(args, 4)229req = Net::HTTP::Post.new('/json.cgi', { 'Host' => @hostname })230format_file = args[1]231req.set_form_data({ 'apikey' => @apikey, 'primary' => 'vulnerabilities', 'secondary' => 'report', 'action' => 'getreport', 'network' => args[0], 'format' => format_file })232233http = Net::HTTP.new(@hostname, 443)234if @debug235http.set_debug_output($stdout) # Logger.new("foo.log") works too236end237238http.use_ssl = true239if @ssl_verify == 'no'240http.verify_mode = OpenSSL::SSL::VERIFY_NONE241end242243res = http.start { |h| h.request(req) }244245unless res246print_error("#{@hostname} - Connection timed out")247return ''248end249250body = ''251begin252body = JSON.parse(res.body)253rescue JSON::ParserError254print_error("#{@hostname} - Unable to parse the response")255return ''256end257258if body['error']259print_error("#{@hostname} - An error occurred:")260print_error(body)261return ''262end263264decompressed = ''265if format_file != 'json'266compressed_base64 = body['compresseddata']267compressed = Base64.decode64(compressed_base64)268decompressed = Zlib::Inflate.inflate(compressed)269else270decompressed = body271end272273if @debug274print_status(decompressed)275end276277::FileUtils.mkdir_p(args[2])278name = ::File.join(args[2], args[3])279print_status("Saving report to #{name}")280output = ::File.new(name, 'w')281output.puts(decompressed)282output.close283284###285# Return the report286return decompressed287else288print_status('Usage: besecure_report_download <network_id> <format_name> <path> <report_name>')289end290291return ''292end293294def cmd_besecure_report_import(*args)295if args?(args, 2)296if !database?297print_error('Database not ready')298return ''299end300301tempfile = Tempfile.new('results')302303res = cmd_besecure_report_download(args[0], 'nbe', File.dirname(tempfile) + '/', File.basename(tempfile))304if res.empty?305print_error('An empty report has been received')306return ''307end308309print_status('Importing report to database.')310framework.db.import_file({ filename: tempfile })311312tempfile.unlink313else314print_status('Usage: besecure_report_import <network_id> <format_name>')315print_status('Only the NBE and XML formats are supported for importing.')316end317end318end319320#------------------------------321# Plugin initialization322#------------------------------323324def initialize(framework, opts)325super326add_console_dispatcher(BeSECURECommandDispatcher)327print_status('Welcome to beSECURE integration by Noam Rathaus.')328print_status329print_status('beSECURE integration requires a database connection. Once the ')330print_status('database is ready, connect to the beSECURE server using besecure_connect.')331print_status('For additional commands use besecure_help.')332print_status333334@debug = nil335end336337def cleanup338remove_console_dispatcher('beSECURE')339end340341def name342'beSECURE'343end344345def desc346'Integrates with the beSECURE - open source vulnerability management'347end348end349end350351352