From 0e4453c4c6df124fe299ffbd86cec7a8dcbeb64e Mon Sep 17 00:00:00 2001 From: Brendan Coles Date: Sun, 17 Feb 2019 12:22:23 +0000 Subject: [PATCH] Remove dm-serialize requirement --- Gemfile | 5 - extensions/network/extension.rb | 1 - extensions/network/rest/network.rb | 216 ++++++++++++++++------------- 3 files changed, 120 insertions(+), 102 deletions(-) diff --git a/Gemfile b/Gemfile index 127bb488a..4c3cc6528 100644 --- a/Gemfile +++ b/Gemfile @@ -70,11 +70,6 @@ group :ext_dns do gem 'rubydns', '~> 0.7.3' end -# network extension -group :ext_network do - gem 'dm-serializer' -end - # QRcode extension group :ext_qrcode do gem 'qr4r' diff --git a/extensions/network/extension.rb b/extensions/network/extension.rb index d3bafa611..82e80143d 100644 --- a/extensions/network/extension.rb +++ b/extensions/network/extension.rb @@ -21,5 +21,4 @@ require 'extensions/network/models/network_host' require 'extensions/network/models/network_service' require 'extensions/network/api' require 'extensions/network/rest/network' -require 'dm-serializer' diff --git a/extensions/network/rest/network.rb b/extensions/network/rest/network.rb index 9b57d36bf..443397d6d 100644 --- a/extensions/network/rest/network.rb +++ b/extensions/network/rest/network.rb @@ -28,151 +28,178 @@ module BeEF # Returns the entire list of network hosts for all zombies get '/hosts' do - begin - hosts = @nh.all(:unique => true, :order => [:id.asc]) - count = hosts.length + hosts = @nh.all(:unique => true, :order => [:id.asc]) + count = hosts.length - result = {} - result[:count] = count - result[:hosts] = hosts.to_json - result.to_json - rescue StandardError => e - print_error "Internal error while retrieving host list (#{e.message})" - halt 500 + result = {} + result[:count] = count + result[:hosts] = [] + hosts.each do |host| + result[:hosts] << host2hash(host) end + + result.to_json + rescue StandardError => e + print_error "Internal error while retrieving host list (#{e.message})" + halt 500 end # Returns the entire list of network services for all zombies get '/services' do - begin - services = @ns.all(:unique => true, :order => [:id.asc]) - count = services.length + services = @ns.all(:unique => true, :order => [:id.asc]) + count = services.length - result = {} - result[:count] = count - result[:services] = services.to_json - result.to_json - rescue StandardError => e - print_error "Internal error while retrieving service list (#{e.message})" - halt 500 + result = {} + result[:count] = count + result[:services] = [] + services.each do |service| + result[:services] << service2hash(service) end + + result.to_json + rescue StandardError => e + print_error "Internal error while retrieving service list (#{e.message})" + halt 500 end # Returns all hosts given a specific hooked browser id get '/hosts/:id' do - begin - id = params[:id] + id = params[:id] - hosts = @nh.all(:hooked_browser_id => id, :unique => true, :order => [:id.asc]) - count = hosts.length + hosts = @nh.all(:hooked_browser_id => id, :unique => true, :order => [:id.asc]) + count = hosts.length - result = {} - result[:count] = count - result[:hosts] = hosts - result.to_json - rescue InvalidParamError => e - print_error e.message - halt 400 - rescue StandardError => e - print_error "Internal error while retrieving hosts list for hooked browser with id #{id} (#{e.message})" - halt 500 + result = {} + result[:count] = count + result[:hosts] = [] + hosts.each do |host| + result[:hosts] << host2hash(host) end + + result.to_json + rescue InvalidParamError => e + print_error e.message + halt 400 + rescue StandardError => e + print_error "Internal error while retrieving hosts list for hooked browser with id #{id} (#{e.message})" + halt 500 end # Returns all services given a specific hooked browser id get '/services/:id' do - begin - id = params[:id] + id = params[:id] - services = @ns.all(:hooked_browser_id => id, :unique => true, :order => [:id.asc]) - count = services.length + services = @ns.all(:hooked_browser_id => id, :unique => true, :order => [:id.asc]) + count = services.length - result = {} - result[:count] = count - result[:services] = services - result.to_json - rescue InvalidParamError => e - print_error e.message - halt 400 - rescue StandardError => e - print_error "Internal error while retrieving service list for hooked browser with id #{id} (#{e.message})" - halt 500 + result = {} + result[:count] = count + result[:services] = [] + services.each do |service| + result[:services] << service2hash(service) end + + result.to_json + rescue InvalidParamError => e + print_error e.message + halt 400 + rescue StandardError => e + print_error "Internal error while retrieving service list for hooked browser with id #{id} (#{e.message})" + halt 500 end # Returns a specific host given its id get '/host/:id' do - begin - id = params[:id] + id = params[:id] - host = @nh.all(:id => id) - raise InvalidParamError, 'id' if host.nil? - halt 404 if host.empty? + host = @nh.all(:id => id) + raise InvalidParamError, 'id' if host.nil? + halt 404 if host.empty? - host.to_json - rescue InvalidParamError => e - print_error e.message - halt 400 - rescue StandardError => e - print_error "Internal error while retrieving host with id #{id} (#{e.message})" - halt 500 - end + host2hash(host).to_json + rescue InvalidParamError => e + print_error e.message + halt 400 + rescue StandardError => e + print_error "Internal error while retrieving host with id #{id} (#{e.message})" + halt 500 end - # Removes a specific host given its id + # Deletes a specific host given its id delete '/host/:id' do - begin - id = params[:id] - raise InvalidParamError, 'id' if id !~ /\A\d+\z/ + id = params[:id] + raise InvalidParamError, 'id' unless BeEF::Filters::nums_only?(id) - host = @nh.all(:id => id) - halt 404 if host.nil? + host = @nh.all(:id => id) + halt 404 if host.nil? - result = {} - result['success'] = @nh.delete(id) - result.to_json - rescue InvalidParamError => e - print_error e.message - halt 400 - rescue StandardError => e - print_error "Internal error while removing network host with id #{id} (#{e.message})" - halt 500 - end + result = {} + result['success'] = @nh.delete(id) + result.to_json + rescue InvalidParamError => e + print_error e.message + halt 400 + rescue StandardError => e + print_error "Internal error while removing network host with id #{id} (#{e.message})" + halt 500 end # Returns a specific service given its id get '/service/:id' do - begin - id = params[:id] + id = params[:id] - service = @ns.all(:id => id) - raise InvalidParamError, 'id' if service.nil? - halt 404 if service.empty? + service = @ns.all(:id => id) + raise InvalidParamError, 'id' if service.nil? + halt 404 if service.empty? - service.to_json - rescue InvalidParamError => e - print_error e.message - halt 400 - rescue StandardError => e - print_error "Internal error while retrieving service with id #{id} (#{e.message})" - halt 500 - end + service2hash(service).to_json + rescue InvalidParamError => e + print_error e.message + halt 400 + rescue StandardError => e + print_error "Internal error while retrieving service with id #{id} (#{e.message})" + halt 500 + end + + private + + # Convert a Network Host object to JSON + def host2hash(host) + { + :id => host.id, + :hooked_browser_id => host.hooked_browser_id, + :ip => host.ip, + :hostname => host.hostname, + :type => host.type, + :os => host.os, + :mac => host.mac, + :lastseen => host.lastseen + } + end + + # Convert a Network Service object to JSON + def service2hash(service) + { + :id => service.id, + :hooked_browser_id => service.hooked_browser_id, + :proto => service.proto, + :ip => service.ip, + :port => service.port, + :type => service.type, + } end # Raised when invalid JSON input is passed to an /api/network handler. class InvalidJsonError < StandardError - DEFAULT_MESSAGE = 'Invalid JSON input passed to /api/network handler' def initialize(message = nil) super(message || DEFAULT_MESSAGE) end - end # Raised when an invalid named parameter is passed to an /api/network handler. class InvalidParamError < StandardError - DEFAULT_MESSAGE = 'Invalid parameter passed to /api/network handler' def initialize(message = nil) @@ -180,11 +207,8 @@ module BeEF message = sprintf str, message unless message.nil? super(message) end - end - end - end end end