module BeEF
module Extension
module Requester
class Handler
H = BeEF::Core::Models::Http
Z = BeEF::Core::Models::HookedBrowser
def initialize(data)
@data = data
setup
end
def setup
beef_hook = @data['beefhook'] || nil
if beef_hook.nil?
print_error 'beefhook is null'
return
end
request_id = @data['cid'].to_s
if request_id == ''
print_error 'Original request id (command id) is null'
return
end
unless BeEF::Filters.nums_only?(request_id)
print_error 'Original request id (command id) is invalid'
return
end
zombie_db = Z.where(session: beef_hook).first || nil
if zombie_db.nil?
(print_error 'Invalid beefhook id: the hooked browser cannot be found in the database'
return)
end
http_db = H.where(id: request_id.to_i, hooked_browser_id: zombie_db.session).first || nil
if http_db.nil?
print_error 'Invalid http_db: no such request found in the database'
return
end
if http_db.has_ran.eql? 'complete'
(print_error 'This http request has been saved before'
return)
end
response_code = @data['results']['response_status_code'] || nil
if response_code.nil?
(print_error 'Http response code is null'
return)
end
http_db.response_headers = @data['results']['response_headers']
http_db.response_status_code = @data['results']['response_status_code']
http_db.response_status_text = @data['results']['response_status_text']
http_db.response_port_status = @data['results']['response_port_status']
http_db.response_data = @data['results']['response_data']
http_db.response_date = Time.now
http_db.has_ran = 'complete'
http_db.response_data = http_db.response_data.unpack('a*') if http_db.response_headers =~ /Content-Type: image/
http_db.save
end
end
end
end
end