Add logger - Fixes #1698

This commit is contained in:
Brendan Coles
2019-03-03 20:20:37 +00:00
parent 39aa3fdeea
commit 13c539effe
14 changed files with 169 additions and 86 deletions

View File

@@ -40,6 +40,10 @@ require 'optparse'
require 'resolv'
require 'digest'
require 'zip'
require 'logger'
# @note Logger
require 'core/logger'
# @note Include the filters
require 'core/filters'

21
core/logger.rb Normal file
View File

@@ -0,0 +1,21 @@
#
# Copyright (c) 2006-2019 Wade Alcorn - wade@bindshell.net
# Browser Exploitation Framework (BeEF) - http://beefproject.com
# See the file 'doc/COPYING' for copying permission
#
#
# @note log to file
#
module BeEF
class << self
attr_writer :logger
def logger
@logger ||= Logger.new("#{$home_dir}/beef.log").tap do |log|
log.progname = self.name
log.level = Logger::WARN
end
end
end
end

View File

@@ -30,8 +30,8 @@ module BeEF
@config.default = nil
@@config = config
rescue => e
print_error "Fatal Error: cannot load configuration file"
print_debug e
print_error "Fatal Error: cannot load configuration file '#{file}' : #{e.message}"
print_error e.backtrace
end
@@instance = self
@@ -45,8 +45,35 @@ module BeEF
raw = File.read file
YAML.safe_load raw
rescue => e
print_debug "Unable to load '#{file}' #{e}"
nil
print_debug "Unable to load configuration file '#{file}' : #{e.message}"
print_error e.backtrace
end
#
# @note balidate the configuration file
#
def validate
if @config.empty?
print_error 'Configuration file is empty'
return
end
if @config['beef'].nil?
print_error "Configuration file is malformed: 'beef' is nil"
return
end
if @config['beef']['credentials'].nil?
print_error "Configuration file is malformed: 'beef.credentials' is nil"
return
end
if @config['beef']['http'].nil?
print_error "Configuration file is malformed: 'beef.http' is nil"
return
end
true
end
#

View File

@@ -96,7 +96,7 @@ module BeEF
print_debug "[WebSocket] New message: #{msg_hash}" if @@debug
rescue => e
print_error "[WebSocket] Failed parsing WebSocket message: #{e.message}"
puts e.backtrace
print_error e.backtrace
next
end

View File

@@ -154,7 +154,7 @@ module BeEF
end
rescue => e
print_error "Failed to prepare HTTP server: #{e.message}"
puts e.backtrace
print_error e.backtrace
exit 1
end

View File

@@ -8,12 +8,14 @@
# @param [String] s String to be printed
def print_error(s)
puts Time.now.localtime.strftime("[%k:%M:%S]")+'[!]'.red+' '+s
BeEF.logger.error s.to_s
end
# Function used to print information to the console
# @param [String] s String to be printed
def print_info(s)
puts Time.now.localtime.strftime("[%k:%M:%S]")+'[*]'.blue+' '+s
BeEF.logger.info s.to_s
end
# Function used to print information to the console (wraps print_info)
@@ -26,6 +28,7 @@ end
# @param [String] s String to be printed
def print_warning(s)
puts Time.now.localtime.strftime("[%k:%M:%S]")+'[!]'.yellow+' '+s.to_s
BeEF.logger.warn s.to_s
end
# Function used to print debug information
@@ -35,6 +38,7 @@ def print_debug(s)
config = BeEF::Core::Configuration.instance
if config.get('beef.debug') || BeEF::Core::Console::CommandLine.parse[:verbose]
puts Time.now.localtime.strftime("[%k:%M:%S]")+'[>]'.yellow+' '+s.to_s
BeEF.logger.debug s.to_s
end
end
@@ -42,6 +46,7 @@ end
# @param [String] s String to be printed
def print_success(s)
puts Time.now.localtime.strftime("[%k:%M:%S]")+'[+]'.green+' '+s
BeEF.logger.info s.to_s
end
# Function used to print successes to the console (wraps print_success)
@@ -65,8 +70,10 @@ def print_more(s)
lines.each_with_index do |line, index|
if ((index+1) == lines.size)
puts "#{time} |_ #{line}"
BeEF.logger.info "#{time} |_ #{line}"
else
puts "#{time} | #{line}"
BeEF.logger.info "#{time} | #{line}"
end
end
end
@@ -77,4 +84,5 @@ end
def print_over(s)
time = Time.now.localtime.strftime("[%k:%M:%S]")
print "\r#{time}"+"[*]".blue+" #{s}"
BeEF.logger.info s.to_s
end