Move to_h methods to models

This commit is contained in:
Brendan Coles
2019-02-25 09:38:16 +00:00
parent 449f36cd70
commit c689884a24
3 changed files with 30 additions and 36 deletions

View File

@@ -72,8 +72,20 @@ module BeEF
host.destroy host.destroy
end 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
}
end
end end
end end
end end
end end

View File

@@ -64,8 +64,18 @@ module BeEF
network_service network_service
end end
# 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,
}
end
end end
end end
end end
end end

View File

@@ -36,7 +36,7 @@ module BeEF
result[:count] = count result[:count] = count
result[:hosts] = [] result[:hosts] = []
hosts.each do |host| hosts.each do |host|
result[:hosts] << host2hash(host) result[:hosts] << host.to_h
end end
result.to_json result.to_json
@@ -56,7 +56,7 @@ module BeEF
result[:count] = count result[:count] = count
result[:services] = [] result[:services] = []
services.each do |service| services.each do |service|
result[:services] << service2hash(service) result[:services] << service.to_h
end end
result.to_json result.to_json
@@ -78,7 +78,7 @@ module BeEF
result[:count] = count result[:count] = count
result[:hosts] = [] result[:hosts] = []
hosts.each do |host| hosts.each do |host|
result[:hosts] << host2hash(host) result[:hosts] << host.to_h
end end
result.to_json result.to_json
@@ -103,7 +103,7 @@ module BeEF
result[:count] = count result[:count] = count
result[:services] = [] result[:services] = []
services.each do |service| services.each do |service|
result[:services] << service2hash(service) result[:services] << service.to_h
end end
result.to_json result.to_json
@@ -125,7 +125,7 @@ module BeEF
raise InvalidParamError, 'id' if host.nil? raise InvalidParamError, 'id' if host.nil?
halt 404 if host.empty? halt 404 if host.empty?
host2hash(host).to_json host.to_h.to_json
rescue InvalidParamError => e rescue InvalidParamError => e
print_error e.message print_error e.message
halt 400 halt 400
@@ -165,7 +165,7 @@ module BeEF
raise InvalidParamError, 'id' if service.nil? raise InvalidParamError, 'id' if service.nil?
halt 404 if service.empty? halt 404 if service.empty?
service2hash(service).to_json service.to_h.to_json
rescue InvalidParamError => e rescue InvalidParamError => e
print_error e.message print_error e.message
halt 400 halt 400
@@ -175,34 +175,6 @@ module BeEF
end end
end 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. # Raised when invalid JSON input is passed to an /api/network handler.
class InvalidJsonError < StandardError class InvalidJsonError < StandardError
DEFAULT_MESSAGE = 'Invalid JSON input passed to /api/network handler' DEFAULT_MESSAGE = 'Invalid JSON input passed to /api/network handler'