rubocop extensions/network

This commit is contained in:
Brendan Coles
2019-02-25 10:29:46 +00:00
parent 205921b1a2
commit f707bf66f3
5 changed files with 115 additions and 84 deletions

View File

@@ -6,9 +6,7 @@
module BeEF
module Extension
module Network
module RegisterHttpHandler
BeEF::API::Registrar.instance.register(BeEF::Extension::Network::RegisterHttpHandler, BeEF::API::Server, 'mount_handler')
# Mounts the handler for processing network host info.
@@ -17,7 +15,6 @@ module BeEF
def self.mount_handler(beef_server)
beef_server.mount('/api/network', BeEF::Extension::Network::NetworkRest.new)
end
end
end
end

View File

@@ -4,21 +4,22 @@
# See the file 'doc/COPYING' for copying permission
#
module BeEF
module Extension
module Network
extend BeEF::API::Extension
@short_name = 'network'
@full_name = 'Network'
@description = "This extension provides a simple interface for interacting with hosts on a zombie browser's local area networks."
module Extension
#
# This extension provides a simple interface for interacting with hosts
# on a zombie browser's local area network(s).
#
module Network
extend BeEF::API::Extension
end
end
@short_name = 'network'
@full_name = 'Network'
@description = "This extension provides a simple interface for interacting with hosts on a zombie browser's local area networks."
end
end
end
require 'extensions/network/models/network_host'
require 'extensions/network/models/network_service'
require 'extensions/network/api'
require 'extensions/network/rest/network'

View File

@@ -10,32 +10,38 @@ module BeEF
# Table stores each host identified on the zombie browser's network(s)
#
class NetworkHost
include DataMapper::Resource
storage_names[:default] = 'network_host'
property :id, Serial
property :hooked_browser_id, Text, :lazy => false
property :ip, Text, :lazy => false
property :hostname, String, :lazy => false
property :type, String, :lazy => false # proxy, router, gateway, dns, etc
property :os, String, :lazy => false
property :mac, String, :lazy => false
property :lastseen, String, :length => 15
property :hooked_browser_id, Text, lazy: false
property :ip, Text, lazy: false
property :hostname, String, lazy: false
property :type, String, lazy: false # proxy, router, gateway, dns, etc
property :os, String, lazy: false
property :mac, String, lazy: false
property :lastseen, String, length: 15
#
# Stores a network host in the data store
#
def self.add(host={})
(print_error "Invalid hooked browser session"; return) unless BeEF::Filters.is_valid_hook_session_id?(host[:hooked_browser_id])
(print_error "Invalid IP address"; return) unless BeEF::Filters.is_valid_ip?(host[:ip])
def self.add(host = {})
unless BeEF::Filters.is_valid_hook_session_id?(host[:hooked_browser_id])
print_error 'Invalid hooked browser session'
return
end
unless BeEF::Filters.is_valid_ip?(host[:ip])
print_error 'Invalid IP address'
return
end
# save network hosts with private IP addresses only?
unless BeEF::Filters.is_valid_private_ip?(host[:ip])
configuration = BeEF::Core::Configuration.instance
if configuration.get("beef.extension.network.ignore_public_ips") == true
(print_debug "Ignoring network host with public IP address [ip: #{host[:ip]}]"; return)
if configuration.get('beef.extension.network.ignore_public_ips') == true
print_debug "Ignoring network host with public IP address [ip: #{host[:ip]}]"
return
end
end
@@ -51,13 +57,19 @@ module BeEF
# if host already exists in data store with the same details
# then update lastseen and return
existing_host = BeEF::Core::Models::NetworkHost.all(new_host)
(existing_host.update( :lastseen => Time.new.to_i ); return) unless existing_host.empty?
unless existing_host.empty?
existing_host.update(lastseen: Time.new.to_i)
return
end
# store the new network host details
new_host[:lastseen] = Time.new.to_i
network_host = BeEF::Core::Models::NetworkHost.new(new_host)
result = network_host.save
(print_error "Failed to save network host"; return) if result.nil?
if result.nil?
print_error 'Failed to save network host'
return
end
network_host
end
@@ -66,23 +78,32 @@ module BeEF
# Removes a network host from the data store
#
def self.delete(id)
(print_error "Failed to remove network host. Invalid host ID."; return) if id.to_s !~ /\A\d+\z/
unless BeEF::Filters.nums_only?(id.to_s)
print_error 'Failed to remove network host. Invalid host ID.'
return
end
host = BeEF::Core::Models::NetworkHost.get(id.to_i)
(print_error "Failed to remove network host [id: #{id}]. Host does not exist."; return) if host.nil?
if host.nil?
print_error "Failed to remove network host [id: #{id}]. Host does not exist."
return
end
host.destroy
end
#
# Convert a Network Host object to JSON
#
def to_h
{
:id => id,
:hooked_browser_id => hooked_browser_id,
:ip => ip,
:hostname => hostname,
:type => type,
:os => os,
:mac => mac,
:lastseen => lastseen
id: id,
hooked_browser_id: hooked_browser_id,
ip: ip,
hostname: hostname,
type: type,
os: os,
mac: mac,
lastseen: lastseen
}
end
end

View File

@@ -10,56 +10,71 @@ module BeEF
# Table stores each open port identified on the zombie browser's network(s)
#
class NetworkService
include DataMapper::Resource
storage_names[:default] = 'network_service'
property :id, Serial
property :hooked_browser_id, Text, :lazy => false
property :proto, String, :lazy => false
property :ip, Text, :lazy => false
property :port, String, :lazy => false
property :type, String, :lazy => false
property :hooked_browser_id, Text, lazy: false
property :proto, String, lazy: false
property :ip, Text, lazy: false
property :port, String, lazy: false
property :type, String, lazy: false
#
# Stores a network service in the data store
#
def self.add(service={})
(print_error "Invalid hooked browser session"; return) if not BeEF::Filters.is_valid_hook_session_id?(service[:hooked_browser_id])
(print_error "Invalid IP address"; return) if not BeEF::Filters.is_valid_ip?(service[:ip])
(print_error "Invalid port"; return) if not BeEF::Filters.is_valid_port?(service[:port])
def self.add(service = {})
unless BeEF::Filters.is_valid_hook_session_id?(service[:hooked_browser_id])
print_error 'Invalid hooked browser session'
return
end
unless BeEF::Filters.is_valid_ip?(service[:ip])
print_error 'Invalid IP address'
return
end
unless BeEF::Filters.is_valid_port?(service[:port])
print_error 'Invalid port'
return
end
# save network services with private IP addresses only?
unless BeEF::Filters.is_valid_private_ip?(service[:ip])
configuration = BeEF::Core::Configuration.instance
if configuration.get("beef.extension.network.ignore_public_ips") == true
(print_debug "Ignoring network service with public IP address [ip: #{service[:ip]}]"; return)
if configuration.get('beef.extension.network.ignore_public_ips') == true
print_debug "Ignoring network service with public IP address [ip: #{service[:ip]}]"
return
end
end
# store the returned network host details
BeEF::Core::Models::NetworkHost.add(
:hooked_browser_id => service[:hooked_browser_id],
:ip => service[:ip])
hooked_browser_id: service[:hooked_browser_id],
ip: service[:ip]
)
# prevent duplicates
return unless BeEF::Core::Models::NetworkService.all(
:hooked_browser_id => service[:hooked_browser_id],
:proto => service[:proto],
:ip => service[:ip],
:port => service[:port],
:type => service[:type]).empty?
hooked_browser_id: service[:hooked_browser_id],
proto: service[:proto],
ip: service[:ip],
port: service[:port],
type: service[:type]
).empty?
# store the returned network service details
network_service = BeEF::Core::Models::NetworkService.new(
:hooked_browser_id => service[:hooked_browser_id],
:proto => service[:proto],
:ip => service[:ip],
:port => service[:port],
:type => service[:type])
hooked_browser_id: service[:hooked_browser_id],
proto: service[:proto],
ip: service[:ip],
port: service[:port],
type: service[:type]
)
result = network_service.save
(print_error "Failed to save network service"; return) if result.nil?
if result.nil?
print_error 'Failed to save network service'
return
end
network_service
end
@@ -67,12 +82,12 @@ module BeEF
# Convert a Network Service object to JSON
def to_h
{
:id => id,
:hooked_browser_id => hooked_browser_id,
:proto => proto,
:ip => ip,
:port => port,
:type => type,
id: id,
hooked_browser_id: hooked_browser_id,
proto: proto,
ip: ip,
port: port,
type: type
}
end
end

View File

@@ -6,10 +6,8 @@
module BeEF
module Extension
module Network
# This class handles the routing of RESTful API requests that interact with network services on the zombie's LAN
class NetworkRest < BeEF::Core::Router::Router
# Filters out bad requests before performing any routing
before do
config = BeEF::Core::Configuration.instance
@@ -29,7 +27,7 @@ module BeEF
# Returns the entire list of network hosts for all zombies
get '/hosts' do
begin
hosts = @nh.all(:unique => true, :order => [:id.asc])
hosts = @nh.all(unique: true, order: [:id.asc])
count = hosts.length
result = {}
@@ -49,7 +47,7 @@ module BeEF
# Returns the entire list of network services for all zombies
get '/services' do
begin
services = @ns.all(:unique => true, :order => [:id.asc])
services = @ns.all(unique: true, order: [:id.asc])
count = services.length
result = {}
@@ -71,7 +69,7 @@ module BeEF
begin
id = params[:id]
hosts = @nh.all(:hooked_browser_id => id, :unique => true, :order => [:id.asc])
hosts = @nh.all(hooked_browser_id: id, unique: true, order: [:id.asc])
count = hosts.length
result = {}
@@ -96,7 +94,7 @@ module BeEF
begin
id = params[:id]
services = @ns.all(:hooked_browser_id => id, :unique => true, :order => [:id.asc])
services = @ns.all(hooked_browser_id: id, unique: true, order: [:id.asc])
count = services.length
result = {}
@@ -121,7 +119,7 @@ module BeEF
begin
id = params[:id]
host = @nh.all(:id => id)
host = @nh.all(id: id)
raise InvalidParamError, 'id' if host.nil?
halt 404 if host.empty?
@@ -139,9 +137,9 @@ module BeEF
delete '/host/:id' do
begin
id = params[:id]
raise InvalidParamError, 'id' unless BeEF::Filters::nums_only?(id)
raise InvalidParamError, 'id' unless BeEF::Filters.nums_only?(id)
host = @nh.all(:id => id)
host = @nh.all(id: id)
halt 404 if host.nil?
result = {}
@@ -161,7 +159,7 @@ module BeEF
begin
id = params[:id]
service = @ns.all(:id => id)
service = @ns.all(id: id)
raise InvalidParamError, 'id' if service.nil?
halt 404 if service.empty?
@@ -177,7 +175,7 @@ module BeEF
# 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'
DEFAULT_MESSAGE = 'Invalid JSON input passed to /api/network handler'.freeze
def initialize(message = nil)
super(message || DEFAULT_MESSAGE)
@@ -186,11 +184,10 @@ module BeEF
# 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'
DEFAULT_MESSAGE = 'Invalid parameter passed to /api/network handler'.freeze
def initialize(message = nil)
str = "Invalid \"%s\" parameter passed to /api/network handler"
message = sprintf str, message unless message.nil?
message = "Invalid \"#{message}\" parameter passed to /api/network handler" unless message.nil?
super(message)
end
end