Skip to content

Commit

Permalink
Moved parser to run before third-party Sinatra one
Browse files Browse the repository at this point in the history
Fixing CLI option naming
  • Loading branch information
nzqo committed Mar 8, 2023
1 parent 073c7f7 commit 4a7dad0
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 36 deletions.
30 changes: 20 additions & 10 deletions beef
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ $root_dir = File.join(File.expand_path(File.dirname(File.realpath(__FILE__))), '
$:.unshift($root_dir)
$home_dir = File.expand_path("#{Dir.home}/.beef/", __FILE__).freeze


#
# @note Parse beef command line arguments before requiring loader.
# The loader will require bundler default arguments, bringing in Sinatra which will
# then hog the Option Parsing. Thus, we need to parse before that.
#
require 'core/main/console/commandline'
BeEF::Core::Console::CommandLine.parse


#
# @note Require core loader
#
Expand All @@ -48,8 +58,8 @@ require 'timeout'
#
# @note Ask user if they would like to update beef
#
if File.exist?("#{$root_dir}git") && BeEF::Core::Console::CommandLine.parse[:update_disabled] == false
if BeEF::Core::Console::CommandLine.parse[:update_auto] == true
if File.exist?("#{$root_dir}git") && BeEF::Core::Console::CommandLine.get_options[:update_disabled] == false
if BeEF::Core::Console::CommandLine.get_options[:update_auto] == true
print 'Checking latest BeEF repository and updating'
`git pull && bundle`
elsif `git rev-parse master` != `git rev-parse origin/master`
Expand Down Expand Up @@ -79,10 +89,10 @@ end
#
# @note Initialize the Configuration object. Loads a different config.yaml if -c flag was passed.
#
if BeEF::Core::Console::CommandLine.parse[:ext_config].empty?
if BeEF::Core::Console::CommandLine.get_options[:ext_config].empty?
config = BeEF::Core::Configuration.new("#{$root_dir}/config.yaml")
else
config = BeEF::Core::Configuration.new("#{BeEF::Core::Console::CommandLine.parse[:ext_config]}")
config = BeEF::Core::Configuration.new("#{BeEF::Core::Console::CommandLine.get_options[:ext_config]}")
end

#
Expand All @@ -105,12 +115,12 @@ end
#
# @note Check if port and WebSocket port need to be updated from command line parameters
#
unless BeEF::Core::Console::CommandLine.parse[:port].empty?
config.set('beef.http.port', BeEF::Core::Console::CommandLine.parse[:port])
unless BeEF::Core::Console::CommandLine.get_options[:port].empty?
config.set('beef.http.port', BeEF::Core::Console::CommandLine.get_options[:port])
end

unless BeEF::Core::Console::CommandLine.parse[:ws_port].empty?
config.set('beef.http.websocket.port', BeEF::Core::Console::CommandLine.parse[:ws_port])
unless BeEF::Core::Console::CommandLine.get_options[:ws_port].empty?
config.set('beef.http.websocket.port', BeEF::Core::Console::CommandLine.get_options[:ws_port])
end

#
Expand Down Expand Up @@ -150,7 +160,7 @@ require 'core/bootstrap'
#
# @note Prints the BeEF ascii art if the -a flag was passed
#
if BeEF::Core::Console::CommandLine.parse[:ascii_art] == true
if BeEF::Core::Console::CommandLine.get_options[:ascii_art] == true
BeEF::Core::Console::Banners.print_ascii_art
end

Expand Down Expand Up @@ -182,7 +192,7 @@ Socket.do_not_reverse_lookup = true
#
db_file = config.get('beef.database.file')
# @note Resets the database if the -x flag was passed
if BeEF::Core::Console::CommandLine.parse[:resetdb]
if BeEF::Core::Console::CommandLine.get_options[:resetdb]
print_info 'Resetting the database for BeEF.'
begin
File.delete(db_file) if File.exist?(db_file)
Expand Down
63 changes: 40 additions & 23 deletions core/main/console/commandline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@
# Browser Exploitation Framework (BeEF) - http://beefproject.com
# See the file 'doc/COPYING' for copying permission
#
require 'optparse'

module BeEF
module Core
module Console
#
# This module parses the command line argument when running beef.
#
module CommandLine
@options = {}
@options[:verbose] = false
@options[:resetdb] = false
@options[:ascii_art] = false
@options[:ext_config] = ''
@options[:port] = ''
@options[:ws_port] = ''
@options[:interactive] = false
@options[:update_disabled] = false
@options[:update_auto] = false
@options = {
verbose: false,
resetdb: false,
ascii_art: false,
ext_config: '',
port: '',
ws_port: '',
interactive: false,
update_disabled: false,
update_auto: false,
}

@already_parsed = false

Expand All @@ -28,8 +31,6 @@ module CommandLine
# It also populates the 'options' hash.
#
def self.parse
return @options if @already_parsed

optparse = OptionParser.new do |opts|
opts.on('-x', '--reset', 'Reset the database') do
@options[:resetdb] = true
Expand All @@ -39,11 +40,11 @@ def self.parse
@options[:verbose] = true
end

opts.on('-a', '--ascii_art', 'Prints BeEF ascii art') do
opts.on('-a', '--ascii-art', 'Prints BeEF ascii art') do
@options[:ascii_art] = true
end

opts.on('-c', '--config FILE', "Load a different configuration file: if it's called custom-config.yaml, git automatically ignores it.") do |f|
opts.on('-c', '--config FILE', "Specify configuration file to load (instead of ./config.yaml)") do |f|
@options[:ext_config] = f
end

Expand All @@ -55,26 +56,42 @@ def self.parse
@options[:ws_port] = ws_port
end

opts.on('-ud', '--update_disabled', 'Skips update') do
opts.on('--update-disable', 'Skips update') do
@options[:update_disabled] = true
end

opts.on('-ua', '--update_auto', 'Automatic update with no prompt') do
opts.on('--update-auto', 'Automatic update with no prompt') do
@options[:update_auto] = true
end

# opts.on('-i', '--interactive', 'Starts with the Console Shell activated') do
# @options[:interactive] = true
# end
opts.on("-h", "--help", "Prints this help") do
puts opts
# NOTE:
# Dont exit here. Beef is also a Sinatra app and that comes with its own options.
# Therefore, we just fall through and hand over parsing to Sinatra afterwards.
puts("\nSinatra webapp options:")
end
end

optparse.parse!
# NOTE:
# Since OptionParser consumes ARGV, all options would be removed from it after parsing
# Sinatra would not receive anything anymore. To avoid that, we parse on a copy.
args_copy = ARGV.dup
optparse.parse!(args_copy)
@already_parsed = true
@options
rescue OptionParser::InvalidOption
puts 'Invalid command line option provided. Please run beef --help'
exit 1
puts 'Provided option not recognized by beef. If you provided a Sinatra option, you may ignore this warning. Run beef --help for more information.'
@already_parsed = true
end

#
# Return the parsed options
#
def self.get_options
raise 'Must parse options before retrieving them. Call CommandLine.parse' unless @already_parsed
return @options
end

end
end
end
Expand Down
2 changes: 1 addition & 1 deletion core/ruby/print.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def print_warning(s)
# @note This function will only print messages if the debug flag is set to true
def print_debug(s)
config = BeEF::Core::Configuration.instance
return unless config.get('beef.debug') || BeEF::Core::Console::CommandLine.parse[:verbose]
return unless config.get('beef.debug') || BeEF::Core::Console::CommandLine.get_options[:verbose]

puts Time.now.localtime.strftime('[%k:%M:%S]') + '[>]' + ' ' + s.to_s
BeEF.logger.debug s.to_s
Expand Down
2 changes: 1 addition & 1 deletion extensions/metasploit/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def self.post_soft_load

msf_module_config = {}
path = "#{$root_dir}/#{BeEF::Core::Configuration.instance.get('beef.extension.metasploit.path')}/msf-exploits.cache"
if !BeEF::Core::Console::CommandLine.parse[:resetdb] && File.exist?(path)
if !BeEF::Core::Console::CommandLine.get_options[:resetdb] && File.exist?(path)
print_debug 'Attempting to use Metasploit exploits cache file'
raw = File.read(path)
begin
Expand Down
2 changes: 1 addition & 1 deletion spec/beef/api/auth_rate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
print_info "Loading database"
db_file = @config.get('beef.database.file')

if BeEF::Core::Console::CommandLine.parse[:resetdb]
if BeEF::Core::Console::CommandLine.get_options[:resetdb]
print_info 'Resetting the database for BeEF.'
File.delete(db_file) if File.exist?(db_file)
end
Expand Down

0 comments on commit 4a7dad0

Please sign in to comment.