Skip to content

Commit

Permalink
Merge branch 'activerecord'
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Dec 17, 2019
2 parents bc6d7dc + 5a05bd9 commit 0b67f4c
Show file tree
Hide file tree
Showing 128 changed files with 814 additions and 817 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### BeEF ###
beef.db
beef.log
test/msf-test
extensions/admin_ui/media/javascript-min/
custom-config.yaml
Expand Down
28 changes: 7 additions & 21 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,23 @@

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'
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'

# 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
Expand All @@ -47,7 +33,6 @@ end

gem 'parseconfig'
gem 'erubis'
gem 'dm-migrations'

# Metasploit Integration extension
group :ext_msf do
Expand Down Expand Up @@ -94,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'
10 changes: 8 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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'


Expand Down Expand Up @@ -236,6 +238,10 @@ task :cde_beef_start => 'beef' do
puts '.'
end


################################

# ActiveRecord
namespace :db do
task :environment do
require_relative "beef"
end
end
58 changes: 16 additions & 42 deletions beef
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -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

#
Expand Down
23 changes: 1 addition & 22 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions core/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 3 additions & 0 deletions core/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ 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'
require 'core/main/models/command'
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'
Expand Down
4 changes: 2 additions & 2 deletions core/hbmanager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions core/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -41,7 +40,6 @@
require 'digest'
require 'zip'
require 'logger'

# @note Logger
require 'core/logger'

Expand Down
12 changes: 12 additions & 0 deletions core/main/ar-migrations/001_create_command_modules.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions core/main/ar-migrations/002_create_hooked_browsers.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions core/main/ar-migrations/003_create_logs.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions core/main/ar-migrations/004_create_commands.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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
t.boolean :instructions_sent, default: false
end

end

end
15 changes: 15 additions & 0 deletions core/main/ar-migrations/005_create_results.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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
end

end

end
12 changes: 12 additions & 0 deletions core/main/ar-migrations/006_create_option_caches.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions core/main/ar-migrations/007_create_browser_details.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions core/main/ar-migrations/008_create_executions.rb
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 0b67f4c

Please sign in to comment.