From 704e675edf0adfba37a4aeb82ae5e7842da76589 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Tue, 8 Oct 2019 16:10:00 +1000 Subject: [PATCH 01/46] Removed datamapper for otr-activerecord --- Gemfile | 20 ++------------------ Rakefile | 10 ++++++++-- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/Gemfile b/Gemfile index a521b01669..3f931914e6 100644 --- a/Gemfile +++ b/Gemfile @@ -17,29 +17,14 @@ gem 'mime-types' gem 'execjs' gem 'ansi' gem 'term-ansicolor', :require => 'term/ansicolor' -gem 'dm-core' gem 'json' -gem 'data_objects' gem 'rubyzip', '>= 1.2.2' gem 'espeak-ruby', '>= 1.0.4' # Text-to-Voice gem 'nokogiri', '>= 1.10.4' gem 'rake' gem 'therubyracer' - -# SQLite support -group :sqlite do - gem 'dm-sqlite-adapter' -end - -# PostgreSQL support -group :postgres do - #gem dm-postgres-adapter -end - -# MySQL support -group :mysql do - #gem dm-mysql-adapter -end +gem 'otr-activerecord' +gem 'sqlite3' # Geolocation support group :geoip do @@ -48,7 +33,6 @@ end gem 'parseconfig' gem 'erubis' -gem 'dm-migrations' # Metasploit Integration extension group :ext_msf do diff --git a/Rakefile b/Rakefile index 1ed8fcc451..7a07c5b1ae 100644 --- a/Rakefile +++ b/Rakefile @@ -4,6 +4,8 @@ # See the file 'doc/COPYING' for copying permission # require 'yaml' +require 'bundler/setup' +load 'tasks/otr-activerecord.rake' #require 'pry-byebug' @@ -236,6 +238,10 @@ task :cde_beef_start => 'beef' do puts '.' end - ################################ - +# ActiveRecord +namespace :db do + task :environment do + require_relative "beef" + end +end From 06d1ba7754e9f1d525d7de34cfb5c8e4fee5f2d9 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Tue, 8 Oct 2019 16:12:53 +1000 Subject: [PATCH 02/46] Reverted config to sqlite only --- config.yaml | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/config.yaml b/config.yaml index d79087c6a9..c896e16c7b 100644 --- a/config.yaml +++ b/config.yaml @@ -90,28 +90,7 @@ beef: cert: "beef_cert.pem" database: - # For information on using other databases please read the - # README.databases file - - # supported DBs: sqlite, mysql, postgres - # NOTE: you must change the Gemfile adding a gem require line like: - # gem "dm-postgres-adapter" - # or - # gem "dm-mysql-adapter" - # if you want to switch drivers from sqlite to postgres (or mysql). - # Finally, run a 'bundle install' command and start BeEF. - driver: "sqlite" - - # db_file is only used for sqlite - db_file: "beef.db" - - # db connection information is only used for mysql/postgres - db_host: "localhost" - db_port: 3306 - db_name: "beef" - db_user: "beef" - db_passwd: "beef" - db_encoding: "UTF-8" + file: "beef.db" # Autorun Rule Engine autorun: From 0cd8878a3f3dd330afb5ec375eb2e4fcb343b1d5 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Tue, 8 Oct 2019 16:13:12 +1000 Subject: [PATCH 03/46] Added migrations --- .../001_create_command_modules.rb | 12 ++++++++++++ .../002_create_hooked_browsers.rb | 19 +++++++++++++++++++ core/main/ar-migrations/003_create_logs.rb | 14 ++++++++++++++ .../main/ar-migrations/004_create_commands.rb | 14 ++++++++++++++ core/main/ar-migrations/005_create_results.rb | 13 +++++++++++++ .../ar-migrations/006_create_option_caches.rb | 12 ++++++++++++ .../007_create_browser_details.rb | 13 +++++++++++++ 7 files changed, 97 insertions(+) create mode 100644 core/main/ar-migrations/001_create_command_modules.rb create mode 100644 core/main/ar-migrations/002_create_hooked_browsers.rb create mode 100644 core/main/ar-migrations/003_create_logs.rb create mode 100644 core/main/ar-migrations/004_create_commands.rb create mode 100644 core/main/ar-migrations/005_create_results.rb create mode 100644 core/main/ar-migrations/006_create_option_caches.rb create mode 100644 core/main/ar-migrations/007_create_browser_details.rb diff --git a/core/main/ar-migrations/001_create_command_modules.rb b/core/main/ar-migrations/001_create_command_modules.rb new file mode 100644 index 0000000000..d43259b894 --- /dev/null +++ b/core/main/ar-migrations/001_create_command_modules.rb @@ -0,0 +1,12 @@ +class CreateCommandModules < ActiveRecord::Migration[6.0] + + def change + + create_table :command_modules do |t| + t.text :name + t.text :path + end + + end + +end diff --git a/core/main/ar-migrations/002_create_hooked_browsers.rb b/core/main/ar-migrations/002_create_hooked_browsers.rb new file mode 100644 index 0000000000..c1e932888c --- /dev/null +++ b/core/main/ar-migrations/002_create_hooked_browsers.rb @@ -0,0 +1,19 @@ +class CreateHookedBrowsers < ActiveRecord::Migration[6.0] + + def change + + create_table :hooked_browsers do |t| + t.text :session + t.text :ip + t.text :firstseen + t.text :lastseen + t.text :httpheaders + t.text :domain + t.integer :port + t.integer :count + t.boolean :is_proxy + end + + end + +end diff --git a/core/main/ar-migrations/003_create_logs.rb b/core/main/ar-migrations/003_create_logs.rb new file mode 100644 index 0000000000..e3614718ae --- /dev/null +++ b/core/main/ar-migrations/003_create_logs.rb @@ -0,0 +1,14 @@ +class CreateLogs < ActiveRecord::Migration[6.0] + + def change + + create_table :logs do |t| + t.text :logtype + t.text :event + t.datetime :date + t.references :hooked_browser + end + + end + +end diff --git a/core/main/ar-migrations/004_create_commands.rb b/core/main/ar-migrations/004_create_commands.rb new file mode 100644 index 0000000000..c56d72be03 --- /dev/null +++ b/core/main/ar-migrations/004_create_commands.rb @@ -0,0 +1,14 @@ +class CreateCommands < ActiveRecord::Migration[6.0] + + def change + + create_table :commands do |t| + t.text :data + t.datetime :creationdate + t.text :label + t.boolean :instructions_sent + end + + end + +end diff --git a/core/main/ar-migrations/005_create_results.rb b/core/main/ar-migrations/005_create_results.rb new file mode 100644 index 0000000000..9a1265dceb --- /dev/null +++ b/core/main/ar-migrations/005_create_results.rb @@ -0,0 +1,13 @@ +class CreateResults < ActiveRecord::Migration[6.0] + + def change + + create_table :results do |t| + t.datetime :date + t.integer :status + t.text :data + end + + end + +end diff --git a/core/main/ar-migrations/006_create_option_caches.rb b/core/main/ar-migrations/006_create_option_caches.rb new file mode 100644 index 0000000000..6f605663ad --- /dev/null +++ b/core/main/ar-migrations/006_create_option_caches.rb @@ -0,0 +1,12 @@ +class CreateOptionCaches < ActiveRecord::Migration[6.0] + + def change + + create_table :option_caches do |t| + t.text :name + t.text :value + end + + end + +end diff --git a/core/main/ar-migrations/007_create_browser_details.rb b/core/main/ar-migrations/007_create_browser_details.rb new file mode 100644 index 0000000000..5404453d26 --- /dev/null +++ b/core/main/ar-migrations/007_create_browser_details.rb @@ -0,0 +1,13 @@ +class CreateBrowserDetails < ActiveRecord::Migration[6.0] + + def change + + create_table :browser_details do |t| + t.text :session_id + t.text :detail_key + t.text :detail_value + end + + end + +end From 405241c5d0e1479349db2e9caa2b655056877fef Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Tue, 8 Oct 2019 16:13:36 +1000 Subject: [PATCH 04/46] Added base model --- core/main/model.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 core/main/model.rb diff --git a/core/main/model.rb b/core/main/model.rb new file mode 100644 index 0000000000..0401a78fee --- /dev/null +++ b/core/main/model.rb @@ -0,0 +1,14 @@ +# +# Copyright (c) 2006-2019 Wade Alcorn - wade@bindshell.net +# Browser Exploitation Framework (BeEF) - http://beefproject.com +# See the file 'doc/COPYING' for copying permission +# + +module BeEF +module Core + class Model < ActiveRecord::Base + # Tell ActiveRecord that this is not a model + self.abstract_class = true + end +end +end From 9babcba7c3c95adb7cc98c580a10de50439bbd58 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Tue, 8 Oct 2019 16:14:09 +1000 Subject: [PATCH 05/46] Fixed loaders --- core/core.rb | 1 + core/loader.rb | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/core.rb b/core/core.rb index ed84f1aa94..2f6914ae02 100644 --- a/core/core.rb +++ b/core/core.rb @@ -10,6 +10,7 @@ module Core end # @note Includes database models - the order must be consistent otherwise DataMapper goes crazy +require 'core/main/model' require 'core/main/models/commandmodule' require 'core/main/models/hookedbrowser' require 'core/main/models/log' diff --git a/core/loader.rb b/core/loader.rb index 10c57333b2..827a2bfce6 100644 --- a/core/loader.rb +++ b/core/loader.rb @@ -31,8 +31,7 @@ require 'ansi' require 'term/ansicolor' require 'json' -require 'data_objects' -require 'dm-do-adapter' +require 'otr-activerecord' require 'parseconfig' require 'erubis' require 'mime/types' From 4cecca4075b1e5e593e661a4711d8b5c5a0c2940 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Tue, 8 Oct 2019 16:14:46 +1000 Subject: [PATCH 06/46] Allow usage of connection management --- core/main/server.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/main/server.rb b/core/main/server.rb index 4536b1ed54..495f7c2026 100644 --- a/core/main/server.rb +++ b/core/main/server.rb @@ -162,7 +162,9 @@ def prepare # Starts the BeEF http server # def start - @http_server.start + @http_server.start do + use OTR::ActiveRecord::ConnectionManagement + end rescue RuntimeError => e # port is in use raise unless e.message.include? 'no acceptor' From d588c56391700219cd23c80852f7c2b8003d2258 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Tue, 8 Oct 2019 16:15:55 +1000 Subject: [PATCH 07/46] Refactored models to use ActiveRecord --- core/main/models/browserdetails.rb | 9 +------ core/main/models/command.rb | 14 ++-------- core/main/models/commandmodule.rb | 16 ++---------- core/main/models/hookedbrowser.rb | 42 +++--------------------------- core/main/models/log.rb | 13 +++------ core/main/models/optioncache.rb | 10 +------ core/main/models/result.rb | 11 +------- 7 files changed, 14 insertions(+), 101 deletions(-) diff --git a/core/main/models/browserdetails.rb b/core/main/models/browserdetails.rb index cb4f1c4f99..907e90031e 100644 --- a/core/main/models/browserdetails.rb +++ b/core/main/models/browserdetails.rb @@ -11,15 +11,8 @@ module Models # # For example, the type and version of browser the hooked browsers are using. # - class BrowserDetails + class BrowserDetails < BeEF::Core::Model - include DataMapper::Resource - - storage_names[:default] = 'core_browserdetails' - property :session_id, String, :length => 255, :key => true - property :detail_key, String, :length => 255, :lazy => false, :key => true - property :detail_value, Text, :lazy => false - # # Returns the requested value from the data store # diff --git a/core/main/models/command.rb b/core/main/models/command.rb index 04e4a83ca1..995e4ecc43 100644 --- a/core/main/models/command.rb +++ b/core/main/models/command.rb @@ -9,19 +9,9 @@ module Core module Models # @note Table stores the commands that have been sent to the Hooked Browsers. - class Command + class Command < BeEF::Core::Model - include DataMapper::Resource - - storage_names[:default] = 'commands' - - property :id, Serial - property :data, Text - property :creationdate, String, :length => 15, :lazy => false - property :label, Text, :lazy => false - property :instructions_sent, Boolean, :default => false - - has n, :results + has_many :results # # Save results and flag that the command has been run on the hooked browser diff --git a/core/main/models/commandmodule.rb b/core/main/models/commandmodule.rb index 60b0b336d0..b35275af87 100644 --- a/core/main/models/commandmodule.rb +++ b/core/main/models/commandmodule.rb @@ -7,22 +7,10 @@ module BeEF module Core module Models - class CommandModule + class CommandModule < BeEF::Core::Model - include DataMapper::Resource + has_many :commands - storage_names[:default] = 'core_commandmodules' - - # @note command module ID - property :id, Serial - - # @note command module name - property :name, Text, :lazy => false - - # @note command module path - property :path, Text, :lazy => false - - has n, :commands end end diff --git a/core/main/models/hookedbrowser.rb b/core/main/models/hookedbrowser.rb index f5cc3065ee..c11d0db41a 100644 --- a/core/main/models/hookedbrowser.rb +++ b/core/main/models/hookedbrowser.rb @@ -9,45 +9,11 @@ module Models # # # - class HookedBrowser + class HookedBrowser < BeEF::Core::Model - include DataMapper::Resource - - storage_names[:default] = 'core_hookedbrowsers' - - # @note zombie ID - property :id, Serial - - # @note hooked browser session ID - property :session, Text, :lazy => false - - # @note IP address of the hooked browser - property :ip, Text, :lazy => false - - # @note timestamp first time the browser communicated with BeEF - property :firstseen, String, :length => 15 - - # @note timestamp last time the browser communicated with BeEF - property :lastseen, String, :length => 15 - - # @note HTTP headers sent be the browser to the BeEF server upon first hook - property :httpheaders, Text, :lazy => false - - # @note the domain originating the hook request - property :domain, Text, :lazy => false - - # @note the port on the domain originating the hook request - property :port, Integer, :default => 80 - - # @note number of times the zombie has polled - property :count, Integer, :lazy => false - - # @note if true the HB is used as a tunneling proxy - property :is_proxy, Boolean, :default => false - - has n, :commands - has n, :results - has n, :logs + has_many :commands + has_many :results + has_many :logs # @note Increases the count of a zombie def count! diff --git a/core/main/models/log.rb b/core/main/models/log.rb index 0dd9efbaee..2c789ccaef 100644 --- a/core/main/models/log.rb +++ b/core/main/models/log.rb @@ -7,17 +7,10 @@ module BeEF module Core module Models - class Log - - include DataMapper::Resource - - storage_names[:default] = 'core_logs' + class Log < BeEF::Core::Model + + has_one :hooked_browser - property :id, Serial - property :type, Text, :lazy => false - property :event, Text, :lazy => false - property :date, DateTime, :lazy => false - property :hooked_browser_id, Text, :lazy => false end end diff --git a/core/main/models/optioncache.rb b/core/main/models/optioncache.rb index 08ed3676be..9289e04adb 100644 --- a/core/main/models/optioncache.rb +++ b/core/main/models/optioncache.rb @@ -7,15 +7,7 @@ module BeEF module Core module Models -class OptionCache - - include DataMapper::Resource - - storage_names[:default] = 'core_optioncache' - - property :id, Serial - property :name, Text - property :value, Text +class OptionCache < BeEF::Core::Model end diff --git a/core/main/models/result.rb b/core/main/models/result.rb index f4f29486ff..2128450fd3 100644 --- a/core/main/models/result.rb +++ b/core/main/models/result.rb @@ -7,16 +7,7 @@ module BeEF module Core module Models - class Result - - include DataMapper::Resource - - storage_names[:default] = 'core_results' - - property :id, Serial - property :date, String, :length => 15, :lazy => false - property :status, Integer - property :data, Text + class Result < BeEF::Core::Model end From 21d0906c12d718620cbb8e284ec72f561ff1d697 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Tue, 8 Oct 2019 16:17:48 +1000 Subject: [PATCH 08/46] Changed ./beef to use ActiveRecord --- beef | 58 ++++++++++++++++------------------------------------------ 1 file changed, 16 insertions(+), 42 deletions(-) diff --git a/beef b/beef index 2e2d83ba27..15e62b4227 100755 --- a/beef +++ b/beef @@ -119,14 +119,6 @@ unless config.get('beef.http.public_port').to_s.eql?('') || BeEF::Filters.is_val exit 1 end -# -# @note Validate database driver -# -unless ['sqlite', 'postgres', 'mysql'].include? config.get('beef.database.driver') - print_error 'No default database selected. Please add one in config.yaml' - exit 1 -end - # # @note After the BeEF core is loaded, bootstrap the rest of the framework internals # @@ -160,43 +152,25 @@ BeEF::Modules.load Socket.do_not_reverse_lookup = true # -# @note Database setup - use DataMapper::Logger.new($stdout, :debug) for development debugging -# -case config.get("beef.database.driver") - when "sqlite" - DataMapper.setup(:default, "sqlite3://#{$root_dir}/#{config.get("beef.database.db_file")}") - when "mysql", "postgres" - DataMapper.setup(:default, - :adapter => config.get("beef.database.driver"), - :host => config.get("beef.database.db_host"), - :port => config.get("beef.database.db_port"), - :username => config.get("beef.database.db_user"), - :password => config.get("beef.database.db_passwd"), - :database => config.get("beef.database.db_name"), - :encoding => config.get("beef.database.db_encoding") - ) - else - print_error 'No default database selected. Please add one in config.yaml' - exit 1 -end - +# @note Database setup +# # # @note Load the database # -begin - # @note Resets the database if the -x flag was passed - if BeEF::Core::Console::CommandLine.parse[:resetdb] - print_info 'Resetting the database for BeEF.' - DataMapper.auto_migrate! - else - DataMapper.auto_upgrade! - end -rescue => e - print_error "Could not connect to database: #{e.message}" - if config.get("beef.database.driver") == 'sqlite' - print_error "Ensure the #{$root_dir}/#{config.get("beef.database.db_file")} database file is writable" - end - exit 1 +db_file = config.get('beef.database.file') +# @note Resets the database if the -x flag was passed +if BeEF::Core::Console::CommandLine.parse[:resetdb] + print_info 'Resetting the database for BeEF.' + File.delete(db_file) if File.exists?(db_file) +end +# Connect to DB +ActiveRecord::Base.logger = nil +OTR::ActiveRecord.migrations_paths = [File.join('core', 'main', 'ar-migrations')] +OTR::ActiveRecord.configure_from_hash!(adapter:'sqlite3', database:db_file) +# Migrate (if required) +context = ActiveRecord::Migration.new.migration_context +if context.needs_migration? + ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration).migrate end # From 91265cad77c6f341f08cc9da0c35b852f1ff3b34 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Tue, 8 Oct 2019 16:18:21 +1000 Subject: [PATCH 09/46] Updated migrations to use ActiveRecord --- core/main/migration.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/core/main/migration.rb b/core/main/migration.rb index 589f527438..ca2a6d9683 100644 --- a/core/main/migration.rb +++ b/core/main/migration.rb @@ -27,14 +27,10 @@ def update_db! def update_commands! config = BeEF::Core::Configuration.instance - db_modules = [] - BeEF::Core::Models::CommandModule.all.each do |mod| - db_modules << mod.name - end + db_modules = BeEF::Core::Models::CommandModule.all.pluck(:name) config.get('beef.module').each do |k, v| - h = { :name => k, :path => "#{v['path']}module.rb" } - BeEF::Core::Models::CommandModule.new(h).save unless db_modules.include? k + BeEF::Core::Models::CommandModule.new(name: k, path: "#{v['path']}module.rb").save! unless db_modules.include? k end BeEF::Core::Models::CommandModule.all.each do |mod| From 0574bdf00297c7d5acd8e56a347186243591c024 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Wed, 9 Oct 2019 10:03:27 +1000 Subject: [PATCH 10/46] Moved autorun models to active record core models --- core/bootstrap.rb | 2 -- core/core.rb | 2 ++ core/main/autorun_engine/models/execution.rb | 31 ------------------ core/main/autorun_engine/models/rule.rb | 34 -------------------- core/main/models/execution.rb | 14 ++++++++ core/main/models/rule.rb | 16 +++++++++ 6 files changed, 32 insertions(+), 67 deletions(-) delete mode 100644 core/main/autorun_engine/models/execution.rb delete mode 100644 core/main/autorun_engine/models/rule.rb create mode 100644 core/main/models/execution.rb create mode 100644 core/main/models/rule.rb diff --git a/core/bootstrap.rb b/core/bootstrap.rb index 9be4b48b65..2eb259cb7b 100644 --- a/core/bootstrap.rb +++ b/core/bootstrap.rb @@ -30,8 +30,6 @@ module Core require 'core/main/network_stack/api' # @note Include the autorun engine -require 'core/main/autorun_engine/models/rule' -require 'core/main/autorun_engine/models/execution' require 'core/main/autorun_engine/parser' require 'core/main/autorun_engine/engine' require 'core/main/autorun_engine/rule_loader' diff --git a/core/core.rb b/core/core.rb index 2f6914ae02..ca989944a2 100644 --- a/core/core.rb +++ b/core/core.rb @@ -18,6 +18,8 @@ module Core require 'core/main/models/result' require 'core/main/models/optioncache' require 'core/main/models/browserdetails' +require 'core/main/models/rule' +require 'core/main/models/execution' # @note Include the constants require 'core/main/constants/browsers' diff --git a/core/main/autorun_engine/models/execution.rb b/core/main/autorun_engine/models/execution.rb deleted file mode 100644 index cc22c709fd..0000000000 --- a/core/main/autorun_engine/models/execution.rb +++ /dev/null @@ -1,31 +0,0 @@ -# -# Copyright (c) 2006-2019 Wade Alcorn - wade@bindshell.net -# Browser Exploitation Framework (BeEF) - http://beefproject.com -# See the file 'doc/COPYING' for copying permission -# - -module BeEF - module Core - module AutorunEngine - module Models - # @note Stored info about the execution of the ARE on hooked browsers. - class Execution - - include DataMapper::Resource - - storage_names[:default] = 'core_areexecution' - - property :id, Serial - property :session, Text # hooked browser session where a ruleset triggered - property :mod_count, Integer # number of command modules of the ruleset - property :mod_successful, Integer # number of command modules that returned with success - # By default Text is only 65K, so field length increased to 1 MB - property :mod_body, Text, :length => 1024000 # entire command module(s) body to be sent - property :exec_time, String, :length => 15 # timestamp of ruleset triggering - property :rule_token, String, :length => 10 # unique token to be appended to wrapper function names - property :is_sent, Boolean - end - end - end - end -end diff --git a/core/main/autorun_engine/models/rule.rb b/core/main/autorun_engine/models/rule.rb deleted file mode 100644 index 56a5df9253..0000000000 --- a/core/main/autorun_engine/models/rule.rb +++ /dev/null @@ -1,34 +0,0 @@ -# -# Copyright (c) 2006-2019 Wade Alcorn - wade@bindshell.net -# Browser Exploitation Framework (BeEF) - http://beefproject.com -# See the file 'doc/COPYING' for copying permission -# - -module BeEF - module Core - module AutorunEngine - module Models - # @note Table stores the rules for the Distributed Engine. - class Rule - include DataMapper::Resource - - storage_names[:default] = 'core_arerules' - - property :id, Serial - property :name, Text # rule name - property :author, String # rule author - property :browser, String, :length => 10 # browser name - property :browser_version, String, :length => 15 # browser version - property :os, String, :length => 10 # OS name - property :os_version, String, :length => 15 # OS version - property :modules, Text # JSON stringyfied representation of the JSON rule for further parsing - property :execution_order, Text # command module execution order - property :execution_delay, Text # command module time delays - property :chain_mode, String, :length => 40 # rule chaining mode - - has n, :executions - end - end - end - end -end diff --git a/core/main/models/execution.rb b/core/main/models/execution.rb new file mode 100644 index 0000000000..38f361b3d8 --- /dev/null +++ b/core/main/models/execution.rb @@ -0,0 +1,14 @@ +# +# Copyright (c) 2006-2019 Wade Alcorn - wade@bindshell.net +# Browser Exploitation Framework (BeEF) - http://beefproject.com +# See the file 'doc/COPYING' for copying permission +# + +module BeEF + module Core + module Models # @note Stored info about the execution of the ARE on hooked browsers. + class Execution < BeEF::Core::Model + end + end + end +end diff --git a/core/main/models/rule.rb b/core/main/models/rule.rb new file mode 100644 index 0000000000..b6f7030faf --- /dev/null +++ b/core/main/models/rule.rb @@ -0,0 +1,16 @@ +# +# Copyright (c) 2006-2019 Wade Alcorn - wade@bindshell.net +# Browser Exploitation Framework (BeEF) - http://beefproject.com +# See the file 'doc/COPYING' for copying permission +# + +module BeEF + module Core + module Models + # @note Table stores the rules for the Distributed Engine. + class Rule < BeEF::Core::Model + has_many :executions + end + end + end +end From 5bfd1e54df0c3f7b458d9cbb7f649027dd27a518 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Wed, 9 Oct 2019 10:04:17 +1000 Subject: [PATCH 11/46] Added autorun model migrations --- .../ar-migrations/008_create_executions.rb | 17 ++++++++++++++++ core/main/ar-migrations/009_create_rules.rb | 20 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 core/main/ar-migrations/008_create_executions.rb create mode 100644 core/main/ar-migrations/009_create_rules.rb diff --git a/core/main/ar-migrations/008_create_executions.rb b/core/main/ar-migrations/008_create_executions.rb new file mode 100644 index 0000000000..172d1cc01f --- /dev/null +++ b/core/main/ar-migrations/008_create_executions.rb @@ -0,0 +1,17 @@ +class CreateExecutions < ActiveRecord::Migration[6.0] + + def change + + create_table :executions do |t| + t.text :session_id + t.integer :mod_count + t.integer :mod_successful + t.text :mod_body + t.text :exec_time + t.text :rule_token + t.boolean :is_sent + end + + end + +end diff --git a/core/main/ar-migrations/009_create_rules.rb b/core/main/ar-migrations/009_create_rules.rb new file mode 100644 index 0000000000..de53677912 --- /dev/null +++ b/core/main/ar-migrations/009_create_rules.rb @@ -0,0 +1,20 @@ +class CreateRules < ActiveRecord::Migration[6.0] + + def change + + create_table :rules do |t| + t.text :name + t.text :author + t.text :browser + t.text :browser_version + t.text :os + t.text :os_version + t.text :modules + t.text :execution_order + t.text :execution_delay + t.text :chain_mode + end + + end + +end From c85e3c01b5fba5db48a8250dd3c6d04fc802d98d Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Sat, 2 Nov 2019 08:21:49 +1000 Subject: [PATCH 12/46] Upgraded to latest version of gems --- Gemfile | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index 3f931914e6..cee0fa64f3 100644 --- a/Gemfile +++ b/Gemfile @@ -8,9 +8,9 @@ gem 'eventmachine' gem 'thin' -gem 'sinatra', '~> 2.0' -gem 'rack', '~> 2.0' -gem 'rack-protection', '~> 2.0' +gem 'sinatra' +gem 'rack' +gem 'rack-protection' gem 'em-websocket' # WebSocket support gem 'uglifier' gem 'mime-types' @@ -79,7 +79,8 @@ group :test do gem 'capybara' # RESTful API tests/generic command module tests gem 'rest-client', '>= 2.0.1' - gem 'byebug' + gem 'irb' + gem 'pry-byebug' end source 'https://rubygems.org' From d1d5d1d648ac82ee05d1cf3eb5199a7808ffed8a Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Sat, 2 Nov 2019 08:24:39 +1000 Subject: [PATCH 13/46] Fixes #1731, Fixes #1629. Corrected params overloading causing parsing errors. --- core/main/handlers/hookedbrowsers.rb | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/core/main/handlers/hookedbrowsers.rb b/core/main/handlers/hookedbrowsers.rb index 1e6f70691c..502f02325f 100644 --- a/core/main/handlers/hookedbrowsers.rb +++ b/core/main/handlers/hookedbrowsers.rb @@ -24,7 +24,7 @@ class HookedBrowsers < BeEF::Core::Router::Router # and deploy some command modules or extensions to the hooked browser. get '/' do @body = '' - @params = request.query_string + params = request.query_string #@response = Rack::Response.new(body=[], 200, header={}) config = BeEF::Core::Configuration.instance @@ -48,7 +48,12 @@ class HookedBrowsers < BeEF::Core::Router::Router # @note get zombie if already hooked the framework hook_session_name = config.get('beef.http.hook_session_name') hook_session_id = request[hook_session_name] - hooked_browser = BeEF::Core::Models::HookedBrowser.first(:session => hook_session_id) if not hook_session_id.nil? + begin + raise ActiveRecord::RecordNotFound if hook_session_id.nil? + hooked_browser = BeEF::Core::Models::HookedBrowser.where(:session => hook_session_id).first + rescue ActiveRecord::RecordNotFound + hooked_browser = false + end # @note is a new browser so return instructions to set up the hook if not hooked_browser @@ -82,21 +87,21 @@ class HookedBrowsers < BeEF::Core::Router::Router end hooked_browser.count! - hooked_browser.save + hooked_browser.save! # @note add all available command module instructions to the response - zombie_commands = BeEF::Core::Models::Command.all(:hooked_browser_id => hooked_browser.id, :instructions_sent => false) + zombie_commands = BeEF::Core::Models::Command.where(:hooked_browser_id => hooked_browser.id, :instructions_sent => false) zombie_commands.each{|command| add_command_instructions(command, hooked_browser)} # @note Check if there are any ARE rules to be triggered. If is_sent=false rules are triggered - are_executions = BeEF::Core::AutorunEngine::Models::Execution.all(:is_sent => false, :session => hook_session_id) + are_executions = BeEF::Core::Models::Execution.where(:is_sent => false, :session_id => hook_session_id) are_executions.each do |are_exec| @body += are_exec.mod_body are_exec.update(:is_sent => true, :exec_time => Time.new.to_i) end # @note We dynamically get the list of all browser hook handler using the API and register them - BeEF::API::Registrar.instance.fire(BeEF::API::Server::Hook, 'pre_hook_send', hooked_browser, @body, @params, request, response) + BeEF::API::Registrar.instance.fire(BeEF::API::Server::Hook, 'pre_hook_send', hooked_browser, @body, params, request, response) end # @note set response headers and body From aac1b0bc107c145e49f7baf42eb8a80e101badbb Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Sat, 2 Nov 2019 08:26:41 +1000 Subject: [PATCH 14/46] Added command references --- core/main/ar-migrations/004_create_commands.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/main/ar-migrations/004_create_commands.rb b/core/main/ar-migrations/004_create_commands.rb index c56d72be03..9bdeba3382 100644 --- a/core/main/ar-migrations/004_create_commands.rb +++ b/core/main/ar-migrations/004_create_commands.rb @@ -3,6 +3,8 @@ class CreateCommands < ActiveRecord::Migration[6.0] def change create_table :commands do |t| + t.references :command_module + t.references :hooked_browser t.text :data t.datetime :creationdate t.text :label From 80bfc215162a733d9f37c41423c17ce2f18c8b9c Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Sun, 3 Nov 2019 07:59:34 +1000 Subject: [PATCH 15/46] AR convert hbmanager --- core/hbmanager.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/hbmanager.rb b/core/hbmanager.rb index 21f45c3adf..047eb40b56 100644 --- a/core/hbmanager.rb +++ b/core/hbmanager.rb @@ -10,14 +10,14 @@ module HBManager # @param [String] sid hooked browser session id string # @return [BeEF::Core::Models::HookedBrowser] returns the associated Hooked Browser def self.get_by_session(sid) - BeEF::Core::Models::HookedBrowser.first(:session => sid) + BeEF::Core::Models::HookedBrowser.where(:session => sid).first end # Get hooked browser by id # @param [Integer] id hooked browser database id # @return [BeEF::Core::Models::HookedBrowser] returns the associated Hooked Browser def self.get_by_id(id) - BeEF::Core::Models::HookedBrowser.first(:id => id) + BeEF::Core::Models::HookedBrowser.find(id) end end From 88c488969e24da044fb6cd9c3027f85a838082d1 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Sun, 3 Nov 2019 07:59:59 +1000 Subject: [PATCH 16/46] AR convert command --- core/main/command.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/main/command.rb b/core/main/command.rb index 116da56f13..e4e0778c7b 100644 --- a/core/main/command.rb +++ b/core/main/command.rb @@ -179,7 +179,7 @@ def output return end - command = BeEF::Core::Models::Command.first(:id => @command_id) + command = BeEF::Core::Models::Command.find(@command_id) @eruby = Erubis::FastEruby.new(File.read(f)) @@ -237,7 +237,7 @@ def use(component) # @todo TODO Document def oc_value(name) - option = BeEF::Core::Models::OptionCache.first(:name => name) + option = BeEF::Core::Models::OptionCache.where(:name => name).first return nil unless option option.value end From 91332844c8b860ec25dd2450a3f396480817e081 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Sun, 3 Nov 2019 08:00:29 +1000 Subject: [PATCH 17/46] AR convert crypto --- core/main/crypto.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/main/crypto.rb b/core/main/crypto.rb index 6da601f9c8..1258cb38c2 100644 --- a/core/main/crypto.rb +++ b/core/main/crypto.rb @@ -82,7 +82,7 @@ def self.dns_rule_id begin id = random_hex_string(8) - BeEF::Core::Models::Dns::Rule.each { |rule| throw StandardError if id == rule.id } + BeEF::Core::Models::Dns::Rule.all.each { |rule| throw StandardError if id == rule.id } rescue StandardError retry end From 557452b95d257faf76f1cfd876589c088a787074 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Sun, 3 Nov 2019 08:00:51 +1000 Subject: [PATCH 18/46] AR convert module loader --- core/modules.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/modules.rb b/core/modules.rb index 4994a121f8..b94715ae32 100644 --- a/core/modules.rb +++ b/core/modules.rb @@ -48,7 +48,7 @@ def self.get_categories # Get all modules currently stored in the database # @return [Array] DataMapper array of all BeEF::Core::Models::CommandModule's in the database def self.get_stored_in_db - BeEF::Core::Models::CommandModule.all(:order => [:id.asc]) + BeEF::Core::Models::CommandModule.all.order(:id) end # Loads all enabled modules From 66bec03158b6d7ca6cf9504a6f788d5071cd99e3 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Sun, 3 Nov 2019 08:01:15 +1000 Subject: [PATCH 19/46] AR convert logger --- core/main/logger.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/main/logger.rb b/core/main/logger.rb index 34c6d4ea54..0cb4c02b46 100644 --- a/core/main/logger.rb +++ b/core/main/logger.rb @@ -40,7 +40,7 @@ def register(from, event, hb = 0) raise TypeError, '"Hooked Browser ID" needs to be an integer' unless hb.integer? # logging the new event into the database - @logs.new(:type => from.to_s, :event => event.to_s, :date => time_now, :hooked_browser_id => hb).save + @logs.create(:logtype => from.to_s, :event => event.to_s, :date => time_now, :hooked_browser_id => hb).save! print_debug "Event: #{event}" # if notifications are enabled send the info there too if @notifications From 8cbae6a83054e2c0753e2674fc7d166780aeb0e0 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Sun, 3 Nov 2019 08:01:54 +1000 Subject: [PATCH 20/46] Cast print error to string when there are cases it recieves stack traces --- core/ruby/print.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/ruby/print.rb b/core/ruby/print.rb index 4b650e453d..4281717685 100644 --- a/core/ruby/print.rb +++ b/core/ruby/print.rb @@ -7,14 +7,14 @@ # Function used to print errors to the console # @param [String] s String to be printed def print_error(s) - puts Time.now.localtime.strftime("[%k:%M:%S]")+'[!]'+' '+s + puts Time.now.localtime.strftime("[%k:%M:%S]")+'[!]'+' '+s.to_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]")+'[*]'+' '+s + puts Time.now.localtime.strftime("[%k:%M:%S]")+'[*]'+' '+s.to_s BeEF.logger.info s.to_s end @@ -45,7 +45,7 @@ def print_debug(s) # Function used to print successes to the console # @param [String] s String to be printed def print_success(s) - puts Time.now.localtime.strftime("[%k:%M:%S]")+'[+]'+' '+s + puts Time.now.localtime.strftime("[%k:%M:%S]")+'[+]'+' '+s.to_s BeEF.logger.info s.to_s end From 96e4063e2ad1a28009167a447e0f28abc39255a1 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Sun, 3 Nov 2019 08:02:52 +1000 Subject: [PATCH 21/46] AR corrected models --- core/main/models/browserdetails.rb | 13 +++++++------ core/main/models/command.rb | 8 +++++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/core/main/models/browserdetails.rb b/core/main/models/browserdetails.rb index 907e90031e..dc35444c8e 100644 --- a/core/main/models/browserdetails.rb +++ b/core/main/models/browserdetails.rb @@ -17,7 +17,7 @@ class BrowserDetails < BeEF::Core::Model # Returns the requested value from the data store # def self.get(session_id, key) - browserdetail = first(:session_id => session_id, :detail_key => key) + browserdetail = self.where(:session_id => session_id, :detail_key => key).first return nil if browserdetail.nil? return nil if browserdetail.detail_value.nil? @@ -28,19 +28,20 @@ def self.get(session_id, key) # Stores or updates an existing key->value pair in the data store # def self.set(session_id, detail_key, detail_value) - browserdetails = BeEF::Core::Models::BrowserDetails.all( + browserdetails = BeEF::Core::Models::BrowserDetails.where( :session_id => session_id, - :detail_key => detail_key ) - if browserdetails.nil? || browserdetails.empty? + :detail_key => detail_key ).first + if browserdetails.nil? # store the new browser details key/value browserdetails = BeEF::Core::Models::BrowserDetails.new( :session_id => session_id, :detail_key => detail_key, :detail_value => detail_value || '') - result = browserdetails.save + result = browserdetails.save! else # update the browser details key/value - result = browserdetails.update(:detail_value => detail_value || '') + browserdetails.detail_value = detail_value || '' + result = browserdetails.save! print_debug "Browser has updated '#{detail_key}' to '#{detail_value}'" end diff --git a/core/main/models/command.rb b/core/main/models/command.rb index 995e4ecc43..3a621483a9 100644 --- a/core/main/models/command.rb +++ b/core/main/models/command.rb @@ -12,6 +12,8 @@ module Models class Command < BeEF::Core::Model has_many :results + has_one :command_module + has_one :hooked_browser # # Save results and flag that the command has been run on the hooked browser @@ -30,12 +32,12 @@ def self.save_result(hook_session_id, command_id, command_friendly_name, result, raise TypeError, '"status" needs to be an integer' unless status.integer? # @note get the hooked browser structure and id from the database - hooked_browser = BeEF::Core::Models::HookedBrowser.first(:session => hook_session_id) || nil + hooked_browser = BeEF::Core::Models::HookedBrowser.where(:session => hook_session_id).first || nil raise TypeError, "hooked_browser is nil" if hooked_browser.nil? raise TypeError, "hooked_browser.id is nil" if hooked_browser.id.nil? # @note get the command module data structure from the database - command = first(:id => command_id, :hooked_browser_id => hooked_browser.id) || nil + command = self.where(:id => command_id, :hooked_browser_id => hooked_browser.id).first || nil raise TypeError, "command is nil" if command.nil? # @note create the entry for the results @@ -45,7 +47,7 @@ def self.save_result(hook_session_id, command_id, command_friendly_name, result, :status => status, :date => Time.now.to_i ) - command.save + command.save! s = show_status(status) log = "Hooked browser [id:#{hooked_browser.id}, ip:#{hooked_browser.ip}]" From 3068fbead5bdaba457700180fd173663caa59625 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Sun, 3 Nov 2019 08:03:56 +1000 Subject: [PATCH 22/46] AR convert handlers --- core/main/handlers/browserdetails.rb | 8 ++++---- core/main/handlers/modules/command.rb | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/main/handlers/browserdetails.rb b/core/main/handlers/browserdetails.rb index 17f5d5a5b2..d9592ccb2c 100644 --- a/core/main/handlers/browserdetails.rb +++ b/core/main/handlers/browserdetails.rb @@ -30,7 +30,7 @@ def setup() # validate hook session value session_id = get_param(@data, 'beefhook') (self.err_msg "session id is invalid"; return) if not BeEF::Filters.is_valid_hook_session_id?(session_id) - hooked_browser = HB.first(:session => session_id) + hooked_browser = HB.where(:session => session_id).first return if not hooked_browser.nil? # browser is already registered with framework # create the structure representing the hooked browser @@ -73,7 +73,7 @@ def setup() @http_headers[key.sub(/^HTTP_/, '')] = value.force_encoding('UTF-8') } zombie.httpheaders = @http_headers.to_json - zombie.save + zombie.save! #print_debug "[INIT] HTTP Headers: #{zombie.httpheaders}" # add a log entry for the newly hooked browser @@ -211,7 +211,7 @@ def setup() if config.get("beef.extension.network.enable") == true if proxy_server =~ /^([\d\.]+):([\d]+)$/ print_debug("Hooked browser [id:#{zombie.id}] is using a proxy [ip: #{$1}]") - BeEF::Core::Models::NetworkHost.add(:hooked_browser_id => session_id, :ip => $1, :type => 'Proxy') + BeEF::Core::Models::NetworkHost.create(:hooked_browser_id => session_id, :ip => $1, :type => 'Proxy') end end end @@ -504,7 +504,7 @@ def setup() # add localhost as network host if config.get('beef.extension.network.enable') print_debug("Hooked browser has network interface 127.0.0.1") - BeEF::Core::Models::NetworkHost.add(:hooked_browser_id => session_id, :ip => '127.0.0.1', :hostname => 'localhost', :os => BeEF::Core::Models::BrowserDetails.get(session_id, 'host.os.name')) + BeEF::Core::Models::NetworkHost.create(:hooked_browser_id => session_id, :ip => '127.0.0.1', :hostname => 'localhost', :os => BeEF::Core::Models::BrowserDetails.get(session_id, 'host.os.name')) end # check if any ARE rules shall be triggered only if the channel is != WebSockets (XHR). If the channel diff --git a/core/main/handlers/modules/command.rb b/core/main/handlers/modules/command.rb index 48e873887d..9409de26e9 100644 --- a/core/main/handlers/modules/command.rb +++ b/core/main/handlers/modules/command.rb @@ -21,7 +21,7 @@ def add_command_instructions(command, hooked_browser) config = BeEF::Core::Configuration.instance # @note get the command module - command_module = BeEF::Core::Models::CommandModule.first(:id => command.command_module_id) + command_module = BeEF::Core::Models::CommandModule.where(:id => command.command_module_id).first (print_error "command_module is nil"; return) if command_module.nil? (print_error "command_module.path is nil"; return) if command_module.path.nil? @@ -70,7 +70,7 @@ def add_command_instructions(command, hooked_browser) # @note flag that the command has been sent to the hooked browser command.instructions_sent = true - command.save + command.save! end end From fce763e9f32b2e1ad51f5a8769acda2ea0313aa8 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Sun, 3 Nov 2019 08:04:38 +1000 Subject: [PATCH 23/46] AR covert websocket --- core/main/network_stack/websocket/websocket.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core/main/network_stack/websocket/websocket.rb b/core/main/network_stack/websocket/websocket.rb index 69b521f7b3..5c9c1d2295 100644 --- a/core/main/network_stack/websocket/websocket.rb +++ b/core/main/network_stack/websocket/websocket.rb @@ -108,7 +108,7 @@ def start_websocket_server(ws_options) print_debug("[WebSocket] activeSocket content [#{@@activeSocket}]") hb_session = msg_hash["cookie"] - hooked_browser = BeEF::Core::Models::HookedBrowser.first(:session => hb_session) + hooked_browser = BeEF::Core::Models::HookedBrowser.where(:session => hb_session).first if hooked_browser.nil? print_error '[WebSocket] Fingerprinting not finished yet.' print_more 'ARE rules were not triggered. You may want to trigger them manually via REST API.' @@ -126,7 +126,7 @@ def start_websocket_server(ws_options) # polling zombie unless msg_hash['alive'].nil? - hooked_browser = BeEF::Core::Models::HookedBrowser.first(:session => msg_hash["alive"]) + hooked_browser = BeEF::Core::Models::HookedBrowser.where(:session => msg_hash["alive"]).first # This will happen if you reset BeEF database (./beef -x), # and existing zombies try to connect. These zombies will be ignored, @@ -141,15 +141,15 @@ def start_websocket_server(ws_options) hooked_browser.lastseen = Time.new.to_i hooked_browser.count! - hooked_browser.save + hooked_browser.save! # Check if new modules need to be sent - zombie_commands = BeEF::Core::Models::Command.all(:hooked_browser_id => hooked_browser.id, :instructions_sent => false) + zombie_commands = BeEF::Core::Models::Command.where(:hooked_browser_id => hooked_browser.id, :instructions_sent => false) zombie_commands.each { |command| add_command_instructions(command, hooked_browser) } # Check if there are any ARE rules to be triggered. If is_sent=false rules are triggered are_body = '' - are_executions = BeEF::Core::AutorunEngine::Models::Execution.all(:is_sent => false, :session => hooked_browser.session) + are_executions = BeEF::Core::Models::Execution.where(:is_sent => false, :session => hooked_browser.session) are_executions.each do |are_exec| are_body += are_exec.mod_body are_exec.update(:is_sent => true, :exec_time => Time.new.to_i) From 58447e40076e525d0e998eccc6c102135ef86585 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Sun, 3 Nov 2019 08:06:43 +1000 Subject: [PATCH 24/46] AR convert rest components --- core/main/rest/handlers/autorun_engine.rb | 6 ++-- core/main/rest/handlers/browserdetails.rb | 4 +-- core/main/rest/handlers/hookedbrowsers.rb | 40 +++++++++++------------ core/main/rest/handlers/logs.rb | 10 +++--- core/main/rest/handlers/modules.rb | 20 ++++++------ 5 files changed, 40 insertions(+), 40 deletions(-) diff --git a/core/main/rest/handlers/autorun_engine.rb b/core/main/rest/handlers/autorun_engine.rb index 0e8a008add..ae2bcaa0d5 100644 --- a/core/main/rest/handlers/autorun_engine.rb +++ b/core/main/rest/handlers/autorun_engine.rb @@ -38,7 +38,7 @@ class AutorunEngine < BeEF::Core::Router::Router get '/rule/delete/:rule_id' do begin rule_id = params[:rule_id] - rule = BeEF::Core::AutorunEngine::Models::Rule.get(rule_id) + rule = BeEF::Core::AutorunEngine::Models::Rule.find(rule_id) rule.destroy { 'success' => true}.to_json rescue => e @@ -53,13 +53,13 @@ class AutorunEngine < BeEF::Core::Router::Router begin rule_id = params[:rule_id] - online_hooks = BeEF::Core::Models::HookedBrowser.all(:lastseen.gte => (Time.new.to_i - 15)) + online_hooks = BeEF::Core::Models::HookedBrowser.where('lastseen >= ?', (Time.new.to_i - 15)) are = BeEF::Core::AutorunEngine::Engine.instance if online_hooks != nil online_hooks.each do |hb| hb_details = BeEF::Core::Models::BrowserDetails - browser_name = hb_details.get(hb.session, 'browser.name') + browser_name = hb_details.get(hb.session, 'browser.name') browser_version = hb_details.get(hb.session, 'browser.version') os_name = hb_details.get(hb.session, 'host.os.name') os_version = hb_details.get(hb.session, 'host.os.version') diff --git a/core/main/rest/handlers/browserdetails.rb b/core/main/rest/handlers/browserdetails.rb index 3854a9588d..626415a731 100644 --- a/core/main/rest/handlers/browserdetails.rb +++ b/core/main/rest/handlers/browserdetails.rb @@ -24,10 +24,10 @@ class BrowserDetails < BeEF::Core::Router::Router # @note Get all browser details for the specified session # get '/:session' do - hb = BeEF::Core::Models::HookedBrowser.first(:session => params[:session]) + hb = BeEF::Core::Models::HookedBrowser.where(:session => params[:session]).first error 404 if hb.nil? - details = BeEF::Core::Models::BrowserDetails.all(:session_id => hb.session) + details = BeEF::Core::Models::BrowserDetails.where(:session_id => hb.session) error 404 if details.nil? result = [] diff --git a/core/main/rest/handlers/hookedbrowsers.rb b/core/main/rest/handlers/hookedbrowsers.rb index 2acb551cea..edbd3c9dbc 100644 --- a/core/main/rest/handlers/hookedbrowsers.rb +++ b/core/main/rest/handlers/hookedbrowsers.rb @@ -24,8 +24,8 @@ class HookedBrowsers < BeEF::Core::Router::Router # @note Get online and offline hooked browsers details (like name, version, os, ip, port, ...) # get '/' do - online_hooks = hb_to_json(BeEF::Core::Models::HookedBrowser.all(:lastseen.gte => (Time.new.to_i - 15))) - offline_hooks = hb_to_json(BeEF::Core::Models::HookedBrowser.all(:lastseen.lt => (Time.new.to_i - 15))) + online_hooks = hb_to_json(BeEF::Core::Models::HookedBrowser.where('lastseen >= ?', (Time.new.to_i - 15))) + offline_hooks = hb_to_json(BeEF::Core::Models::HookedBrowser.where('lastseen <= ?', (Time.new.to_i - 15))) output = { 'hooked-browsers' => { @@ -37,33 +37,33 @@ class HookedBrowsers < BeEF::Core::Router::Router end get '/:session/delete' do - hb = BeEF::Core::Models::HookedBrowser.first(:session => params[:session]) + hb = BeEF::Core::Models::HookedBrowser.where(:session => params[:session]).first error 401 unless hb != nil - details = BeEF::Core::Models::BrowserDetails.all(:session_id => hb.session) + details = BeEF::Core::Models::BrowserDetails.where(:session_id => hb.session) details.destroy - logs = BeEF::Core::Models::Log.all(:hooked_browser_id => hb.id) + logs = BeEF::Core::Models::Log.where(:hooked_browser_id => hb.id) logs.destroy - commands = BeEF::Core::Models::Command.all(:hooked_browser_id => hb.id) + commands = BeEF::Core::Models::Command.where(:hooked_browser_id => hb.id) commands.destroy - results = BeEF::Core::Models::Result.all(:hooked_browser_id => hb.id) + results = BeEF::Core::Models::Result.where(:hooked_browser_id => hb.id) results.destroy begin - requester = BeEF::Core::Models::Http.all(:hooked_browser_id => hb.id) + requester = BeEF::Core::Models::Http.where(:hooked_browser_id => hb.id) requester.destroy rescue => e #the requester module may not be enabled end begin - xssraysscans = BeEF::Core::Models::Xssraysscan.all(:hooked_browser_id => hb.id) + xssraysscans = BeEF::Core::Models::Xssraysscan.where(:hooked_browser_id => hb.id) xssraysscans.destroy - xssraysdetails = BeEF::Core::Models::Xssraysdetail.all(:hooked_browser_id => hb.id) + xssraysdetails = BeEF::Core::Models::Xssraysdetail.where(:hooked_browser_id => hb.id) xssraysdetails.destroy rescue => e #the xssraysscan module may not be enabled @@ -96,7 +96,7 @@ class HookedBrowsers < BeEF::Core::Router::Router # Useful if you need to query the API via jQuery.dataTable < 1.10 which is currently used in PhishingFrenzy # get '/pf/online' do - online_hooks = hbs_to_array(BeEF::Core::Models::HookedBrowser.all(:lastseen.gte => (Time.new.to_i - 15))) + online_hooks = hbs_to_array(BeEF::Core::Models::HookedBrowser.where('lastseen >= ?', (Time.new.to_i - 15))) output = { 'aaData' => online_hooks @@ -109,7 +109,7 @@ class HookedBrowsers < BeEF::Core::Router::Router # Useful if you need to query the API via jQuery.dataTable < 1.10 which is currently used in PhishingFrenzy # get '/pf/offline' do - offline_hooks = hbs_to_array(BeEF::Core::Models::HookedBrowser.all(:lastseen.lt => (Time.new.to_i - 15))) + offline_hooks = hbs_to_array(BeEF::Core::Models::HookedBrowser.where('lastseen <= ?', (Time.new.to_i - 15))) output = { 'aaData' => offline_hooks @@ -121,10 +121,10 @@ class HookedBrowsers < BeEF::Core::Router::Router # @note Get all the hooked browser details (plugins enabled, technologies enabled, cookies) # get '/:session' do - hb = BeEF::Core::Models::HookedBrowser.first(:session => params[:session]) + hb = BeEF::Core::Models::HookedBrowser.where(:session => params[:session]).first error 401 unless hb != nil - details = BeEF::Core::Models::BrowserDetails.all(:session_id => hb.session) + details = BeEF::Core::Models::BrowserDetails.where(:session_id => hb.session) result = {} details.each do |property| result[property.detail_key] = property.detail_value @@ -140,16 +140,16 @@ class HookedBrowsers < BeEF::Core::Router::Router os_version = body['os_version'] arch = body['arch'] - hb = BeEF::Core::Models::HookedBrowser.first(:session => params[:session]) + hb = BeEF::Core::Models::HookedBrowser.where(:session => params[:session]).first error 401 unless hb != nil - BeEF::Core::Models::BrowserDetails.first(:session_id => hb.session, :detail_key => 'host.os.name').destroy - BeEF::Core::Models::BrowserDetails.first(:session_id => hb.session, :detail_key => 'host.os.version').destroy + BeEF::Core::Models::BrowserDetails.where(:session_id => hb.session, :detail_key => 'host.os.name').destroy + BeEF::Core::Models::BrowserDetails.where(:session_id => hb.session, :detail_key => 'host.os.version').destroy #BeEF::Core::Models::BrowserDetails.first(:session_id => hb.session, :detail_key => 'Arch').destroy - BeEF::Core::Models::BrowserDetails.new(:session_id => hb.session, :detail_key => 'host.os.name', :detail_value => os).save - BeEF::Core::Models::BrowserDetails.new(:session_id => hb.session, :detail_key => 'host.os.version', :detail_value => os_version).save - BeEF::Core::Models::BrowserDetails.new(:session_id => hb.session, :detail_key => 'Arch', :detail_value => arch).save + BeEF::Core::Models::BrowserDetails.create(:session_id => hb.session, :detail_key => 'host.os.name', :detail_value => os) + BeEF::Core::Models::BrowserDetails.create(:session_id => hb.session, :detail_key => 'host.os.version', :detail_value => os_version) + BeEF::Core::Models::BrowserDetails.create(:session_id => hb.session, :detail_key => 'Arch', :detail_value => arch) # TODO if there where any ARE rules defined for this hooked browser, # after updating OS/arch, force a retrigger of the rule. diff --git a/core/main/rest/handlers/logs.rb b/core/main/rest/handlers/logs.rb index 80c3ee736d..9f4bc08c39 100644 --- a/core/main/rest/handlers/logs.rb +++ b/core/main/rest/handlers/logs.rb @@ -24,7 +24,7 @@ class Logs < BeEF::Core::Router::Router # @note Get all global logs # get '/' do - logs = BeEF::Core::Models::Log.all() + logs = BeEF::Core::Models::Log.all logs_to_json(logs) end @@ -44,10 +44,10 @@ class Logs < BeEF::Core::Router::Router # @note Get hooked browser logs # get '/:session' do - hb = BeEF::Core::Models::HookedBrowser.first(:session => params[:session]) + hb = BeEF::Core::Models::HookedBrowser.where(:session => params[:session]).first error 401 unless hb != nil - logs = BeEF::Core::Models::Log.all(:hooked_browser_id => hb.id) + logs = BeEF::Core::Models::Log.where(:hooked_browser_id => hb.id) logs_to_json(logs) end @@ -62,7 +62,7 @@ def logs_to_json(logs) 'id' => log.id.to_i, 'date' => log.date.to_s, 'event' => log.event.to_s, - 'type' => log.type.to_s, + 'logtype' => log.logtype.to_s, 'hooked_browser_id' => log.hooked_browser_id.to_s } end @@ -84,7 +84,7 @@ def logs_to_rss(logs) logs.reverse.each do |log| maker.items.new_item do |item| item.id = log.id.to_s - item.title = "[#{log.type}] #{log.event}" + item.title = "[#{log.logtype}] #{log.event}" item.updated = log.date.to_s end end diff --git a/core/main/rest/handlers/modules.rb b/core/main/rest/handlers/modules.rb index d5d2f8a939..5070afafa9 100644 --- a/core/main/rest/handlers/modules.rb +++ b/core/main/rest/handlers/modules.rb @@ -43,7 +43,7 @@ class Modules < BeEF::Core::Router::Router end get '/search/:mod_name' do - mod = BeEF::Core::Models::CommandModule.first(:name => params[:mod_name]) + mod = BeEF::Core::Models::CommandModule.where(:name => params[:mod_name]).first result = {} if mod != nil result = {'id' => mod.id} @@ -55,7 +55,7 @@ class Modules < BeEF::Core::Router::Router # @note Get the module definition (info, options) # get '/:mod_id' do - cmd = BeEF::Core::Models::CommandModule.get(params[:mod_id]) + cmd = BeEF::Core::Models::CommandModule.find(params[:mod_id]) error 404 unless cmd != nil modk = BeEF::Module.get_key_by_database_id(params[:mod_id]) error 404 unless modk != nil @@ -81,12 +81,12 @@ class Modules < BeEF::Core::Router::Router #{"date":"1331637093","data":"{\"data\":\"text=michele\"}"} # get '/:session/:mod_id/:cmd_id' do - hb = BeEF::Core::Models::HookedBrowser.first(:session => params[:session]) + hb = BeEF::Core::Models::HookedBrowser.where(:session => params[:session]).first error 401 unless hb != nil - cmd = BeEF::Core::Models::Command.first(:hooked_browser_id => hb.id, - :command_module_id => params[:mod_id], :id => params[:cmd_id]) + cmd = BeEF::Core::Models::Command.where(:hooked_browser_id => hb.id, + :command_module_id => params[:mod_id], :id => params[:cmd_id]).first error 404 unless cmd != nil - results = BeEF::Core::Models::Result.all(:hooked_browser_id => hb.id, :command_id => cmd.id) + results = BeEF::Core::Models::Result.where(:hooked_browser_id => hb.id, :command_id => cmd.id) error 404 unless results != nil results_hash = {} @@ -137,7 +137,7 @@ class Modules < BeEF::Core::Router::Router #{"success":"true","command_id":"not_available"} # post '/:session/:mod_id' do - hb = BeEF::Core::Models::HookedBrowser.first(:session => params[:session]) + hb = BeEF::Core::Models::HookedBrowser.where(:session => params[:session]).first error 401 unless hb != nil modk = BeEF::Module.get_key_by_database_id(params[:mod_id]) error 404 unless modk != nil @@ -198,7 +198,7 @@ class Modules < BeEF::Core::Router::Router # run on all hooked browsers currently online? if hb_ids.first =~ /\Aall_online\z/i hb_ids = [] - BeEF::Core::Models::HookedBrowser.all( + BeEF::Core::Models::HookedBrowser.where( :lastseen.gte => (Time.new.to_i - 15)).each {|hb| hb_ids << hb.id } # run on all hooked browsers? elsif hb_ids.first =~ /\Aall\z/i @@ -208,7 +208,7 @@ class Modules < BeEF::Core::Router::Router # run modules hb_ids.each do |hb_id| - hb = BeEF::Core::Models::HookedBrowser.first(:id => hb_id) + hb = BeEF::Core::Models::HookedBrowser.find(hb_id) if hb == nil results[hb_id] = 0 next @@ -256,7 +256,7 @@ class Modules < BeEF::Core::Router::Router request.body.rewind begin body = JSON.parse request.body.read - hb = BeEF::Core::Models::HookedBrowser.first(:session => body["hb"]) + hb = BeEF::Core::Models::HookedBrowser.where(:session => body["hb"]).first error 401 unless hb != nil results = Hash.new From b88774cdbfdbf4631dbb779aa9bb4b0a68284bac Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Sun, 3 Nov 2019 08:08:22 +1000 Subject: [PATCH 25/46] AR convert autorun --- core/main/autorun_engine/engine.rb | 16 ++++++++-------- core/main/autorun_engine/parser.rb | 2 +- core/main/autorun_engine/rule_loader.rb | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/core/main/autorun_engine/engine.rb b/core/main/autorun_engine/engine.rb index 5ae992afc3..679e8f01d7 100644 --- a/core/main/autorun_engine/engine.rb +++ b/core/main/autorun_engine/engine.rb @@ -41,7 +41,7 @@ def trigger(rule_ids, hb_id) hb_session = hb.session rule_ids.each do |rule_id| - rule = BeEF::Core::AutorunEngine::Models::Rule.get(rule_id) + rule = BeEF::Core::Models::Rule.find(rule_id) modules = JSON.parse(rule.modules) execution_order = JSON.parse(rule.execution_order) @@ -57,7 +57,7 @@ def trigger(rule_ids, hb_id) rule_token = SecureRandom.hex(5) modules.each do |cmd_mod| - mod = BeEF::Core::Models::CommandModule.first(:name => cmd_mod['name']) + mod = BeEF::Core::Models::CommandModule.where(:name => cmd_mod['name']).first options = [] replace_input = false cmd_mod['options'].each do|k,v| @@ -85,7 +85,7 @@ def trigger(rule_ids, hb_id) # TODO catch error, which should never happen as values are checked way before ;-) end - are_exec = BeEF::Core::AutorunEngine::Models::Execution.new( + are_exec = BeEF::Core::Models::Execution.new( :session => hb_session, :mod_count => modules.length, :mod_successful => 0, @@ -94,7 +94,7 @@ def trigger(rule_ids, hb_id) :is_sent => false, :rule_id => rule_id ) - are_exec.save + are_exec.save! # Once Engine.check() verified that the hooked browser match a Rule, trigger the Rule ;-) print_more "Triggering ruleset #{rule_ids.to_s} on HB #{hb_id}" end @@ -272,9 +272,9 @@ def prepare_command(mod, options, hb_id, replace_input, rule_token) :creationdate => Time.new.to_i, :instructions_sent => true ) - command.save + command.save! - command_module = BeEF::Core::Models::CommandModule.first(:id => mod.id) + command_module = BeEF::Core::Models::CommandModule.find(mod.id) if (command_module.path.match(/^Dynamic/)) # metasploit and similar integrations command_module = BeEF::Modules::Commands.const_get(command_module.path.split('/').last.capitalize).new @@ -385,9 +385,9 @@ def clean_command_body(command_body, replace_input) def match(browser, browser_version, os, os_version, rule_id=nil) match_rules = [] if rule_id != nil - rules = [BeEF::Core::AutorunEngine::Models::Rule.get(rule_id)] + rules = [BeEF::Core::Models::Rule.find(rule_id)] else - rules = BeEF::Core::AutorunEngine::Models::Rule.all() + rules = BeEF::Core::Models::Rule.all end return nil if rules == nil return nil unless rules.length > 0 diff --git a/core/main/autorun_engine/parser.rb b/core/main/autorun_engine/parser.rb index 27faa444b4..43c05f24ea 100644 --- a/core/main/autorun_engine/parser.rb +++ b/core/main/autorun_engine/parser.rb @@ -53,7 +53,7 @@ def parse(name,author,browser, browser_version, os, os_version, modules, exec_or # check if module names, conditions and options are ok modules.each do |cmd_mod| - mod = BeEF::Core::Models::CommandModule.first(:name => cmd_mod['name']) + mod = BeEF::Core::Models::CommandModule.where(:name => cmd_mod['name']).first if mod != nil modk = BeEF::Module.get_key_by_database_id(mod.id) mod_options = BeEF::Module.get_options(modk) diff --git a/core/main/autorun_engine/rule_loader.rb b/core/main/autorun_engine/rule_loader.rb index 8d9904ce21..faed997c36 100644 --- a/core/main/autorun_engine/rule_loader.rb +++ b/core/main/autorun_engine/rule_loader.rb @@ -52,7 +52,7 @@ def load(data) print_more "Exec order: #{exec_order}" print_more "Exec delay: #{exec_delay}" end - are_rule = BeEF::Core::AutorunEngine::Models::Rule.new( + are_rule = BeEF::Core::Models::Rule.new( :name => name, :author => author, :browser => browser, From eff7b993934670cdf0a81d044e8da4bd343bbd3c Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Sun, 3 Nov 2019 08:09:18 +1000 Subject: [PATCH 26/46] AR convert admin_ui --- .../admin_ui/controllers/modules/modules.rb | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/extensions/admin_ui/controllers/modules/modules.rb b/extensions/admin_ui/controllers/modules/modules.rb index 24bda85050..01d875bd55 100644 --- a/extensions/admin_ui/controllers/modules/modules.rb +++ b/extensions/admin_ui/controllers/modules/modules.rb @@ -225,10 +225,10 @@ def select_command_modules_tree # if dynamic modules are found in the DB, then we don't have yaml config for them # and loading must proceed in a different way. - dynamic_modules = BeEF::Core::Models::CommandModule.all(:path.like => "Dynamic/") + dynamic_modules = BeEF::Core::Models::CommandModule.where('path LIKE ?', 'Dynamic/') if(dynamic_modules != nil) - all_modules = BeEF::Core::Models::CommandModule.all(:order => [:id.asc]) + all_modules = BeEF::Core::Models::CommandModule.all.order(:id) all_modules.each{|dyn_mod| next if !dyn_mod.path.split('/')[1].match(/^metasploit/) command_mod_name = dyn_mod["name"] @@ -257,7 +257,7 @@ def select_command_modules_tree def select_command_module command_module_id = @params['command_module_id'] || nil (print_error "command_module_id is nil";return) if command_module_id.nil? - command_module = BeEF::Core::Models::CommandModule.get(command_module_id) + command_module = BeEF::Core::Models::CommandModule.find(command_module_id) key = BeEF::Module.get_key_by_database_id(command_module_id) payload_name = @params['payload_name'] || nil @@ -284,12 +284,12 @@ def select_command_module_commands (print_error "nonce incorrect";return) if @session.get_nonce != nonce # get the browser id - zombie = Z.first(:session => zombie_session) + zombie = Z.where(:session => zombie_session).first (print_error "Zombie is nil";return) if zombie.nil? zombie_id = zombie.id (print_error "Zombie id is nil";return) if zombie_id.nil? - C.all(:command_module_id => command_module_id, :hooked_browser_id => zombie_id).each do |command| + C.where(:command_module => command_module_id, :hooked_browser => zombie_id).each do |command| commands.push({ 'id' => i, 'object_id' => command.id, @@ -346,7 +346,7 @@ def reexecute_command_module # get params command_id = @params['command_id'] || nil (print_error "Command id is nil";return) if command_id.nil? - command = BeEF::Core::Models::Command.first(:id => command_id.to_i) || nil + command = BeEF::Core::Models::Command.find(command_id.to_i) || nil (print_error "Command is nil";return) if command.nil? # validate nonce nonce = @params['nonce'] || nil @@ -382,11 +382,11 @@ def attach_dynamic_command_module oc.save } - zombie = Z.first(:session => zombie_session) + zombie = Z.where(:session => zombie_session).first (print_error "Zombie is nil";return) if zombie.nil? zombie_id = zombie.id (print_error "Zombie id is nil";return) if zombie_id.nil? - command_module = BeEF::Core::Models::CommandModule.get(command_module_id) + command_module = BeEF::Core::Models::CommandModule.find(command_module_id) if(command_module != nil && command_module.path.match(/^Dynamic/)) dyn_mod_name = command_module.path.split('/').last @@ -423,14 +423,14 @@ def select_command_results # get params command_id = @params['command_id']|| nil (print_error "Command id is nil";return) if command_id.nil? - command = BeEF::Core::Models::Command.first(:id => command_id.to_i) || nil + command = BeEF::Core::Models::Command.find(command_id.to_i) || nil (print_error "Command is nil";return) if command.nil? # get command_module - command_module = BeEF::Core::Models::CommandModule.first(:id => command.command_module_id) + command_module = BeEF::Core::Models::CommandModule.find(command.command_module_id) (print_error "command_module is nil";return) if command_module.nil? - resultsdb = BeEF::Core::Models::Result.all(:command_id => command_id) + resultsdb = BeEF::Core::Models::Result.where(:command_id => command_id) (print_error "Command id result is nil";return) if resultsdb.nil? resultsdb.each{ |result| results.push({'date' => result.date, 'data' => JSON.parse(result.data)}) } @@ -450,10 +450,10 @@ def select_command # get params command_id = @params['command_id'] || nil (print_error "Command id is nil";return) if command_id.nil? - command = BeEF::Core::Models::Command.first(:id => command_id.to_i) || nil + command = BeEF::Core::Models::Command.find(command_id.to_i) || nil (print_error "Command is nil";return) if command.nil? - command_module = BeEF::Core::Models::CommandModule.get(command.command_module_id) + command_module = BeEF::Core::Models::CommandModule.find(command.command_module_id) (print_error "command_module is nil";return) if command_module.nil? if(command_module.path.split('/').first.match(/^Dynamic/)) @@ -503,7 +503,7 @@ def command_modules2json(command_modules) def dynamic_modules2json(id) command_modules_json = {} - mod = BeEF::Core::Models::CommandModule.first(:id => id) + mod = BeEF::Core::Models::CommandModule.find(id) # if the module id is not in the database return false return {'success' => 'false'}.to_json if(not mod) @@ -525,7 +525,7 @@ def dynamic_modules2json(id) def dynamic_payload2json(id, payload_name) command_modules_json = {} - command_module = BeEF::Core::Models::CommandModule.get(id) + command_module = BeEF::Core::Models::CommandModule.find(id) (print_error "Module does not exists";return 'success' => 'false') if command_module.nil? payload_options = BeEF::Module.get_payload_options(command_module.name,payload_name) From 87b80935049b2eb70edf479bd83c668be3457a08 Mon Sep 17 00:00:00 2001 From: Ben Passmore Date: Fri, 8 Nov 2019 08:03:35 +1000 Subject: [PATCH 27/46] Corrected command execution. --- core/main/ar-migrations/004_create_commands.rb | 2 +- core/main/ar-migrations/005_create_results.rb | 2 ++ core/main/models/command.rb | 4 ++-- core/main/models/result.rb | 3 +++ extensions/admin_ui/controllers/modules/modules.rb | 2 +- extensions/demos/html/basic.html | 1 + 6 files changed, 10 insertions(+), 4 deletions(-) diff --git a/core/main/ar-migrations/004_create_commands.rb b/core/main/ar-migrations/004_create_commands.rb index 9bdeba3382..20c9d632ac 100644 --- a/core/main/ar-migrations/004_create_commands.rb +++ b/core/main/ar-migrations/004_create_commands.rb @@ -8,7 +8,7 @@ def change t.text :data t.datetime :creationdate t.text :label - t.boolean :instructions_sent + t.boolean :instructions_sent, default: false end end diff --git a/core/main/ar-migrations/005_create_results.rb b/core/main/ar-migrations/005_create_results.rb index 9a1265dceb..97d1c6fad7 100644 --- a/core/main/ar-migrations/005_create_results.rb +++ b/core/main/ar-migrations/005_create_results.rb @@ -3,6 +3,8 @@ class CreateResults < ActiveRecord::Migration[6.0] def change create_table :results do |t| + t.references :command + t.references :hooked_browser t.datetime :date t.integer :status t.text :data diff --git a/core/main/models/command.rb b/core/main/models/command.rb index 3a621483a9..d43cb59175 100644 --- a/core/main/models/command.rb +++ b/core/main/models/command.rb @@ -41,13 +41,13 @@ def self.save_result(hook_session_id, command_id, command_friendly_name, result, raise TypeError, "command is nil" if command.nil? # @note create the entry for the results - command.results.new( + BeEF::Core::Models::Result.create( :hooked_browser_id => hooked_browser.id, + :command_id => command.id, :data => result.to_json, :status => status, :date => Time.now.to_i ) - command.save! s = show_status(status) log = "Hooked browser [id:#{hooked_browser.id}, ip:#{hooked_browser.ip}]" diff --git a/core/main/models/result.rb b/core/main/models/result.rb index 2128450fd3..a78c5ddd79 100644 --- a/core/main/models/result.rb +++ b/core/main/models/result.rb @@ -8,6 +8,9 @@ module Core module Models class Result < BeEF::Core::Model + + has_one :command + has_one :hooked_browser end diff --git a/extensions/admin_ui/controllers/modules/modules.rb b/extensions/admin_ui/controllers/modules/modules.rb index 01d875bd55..e1c8c964d2 100644 --- a/extensions/admin_ui/controllers/modules/modules.rb +++ b/extensions/admin_ui/controllers/modules/modules.rb @@ -289,7 +289,7 @@ def select_command_module_commands zombie_id = zombie.id (print_error "Zombie id is nil";return) if zombie_id.nil? - C.where(:command_module => command_module_id, :hooked_browser => zombie_id).each do |command| + C.where(:command_module_id => command_module_id, :hooked_browser_id => zombie_id).each do |command| commands.push({ 'id' => i, 'object_id' => command.id, diff --git a/extensions/demos/html/basic.html b/extensions/demos/html/basic.html index 0b4eaddf27..f2dd3cfcc4 100644 --- a/extensions/demos/html/basic.html +++ b/extensions/demos/html/basic.html @@ -7,6 +7,7 @@ --> BeEF Basic Demo +