This repository has been archived by the owner on Oct 22, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
404 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
Sequel.migration do | ||
up do | ||
create_table :logs do | ||
primary_key :id | ||
|
||
column :key, :string, size: 50, unique: true, null: false | ||
column :value, :string, size: 100, null: false | ||
end | ||
end | ||
|
||
down do | ||
drop_table :logs | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
Sequel.migration do | ||
up do | ||
create_table :modules do | ||
primary_key :id | ||
|
||
column :path, :string, size: 255, null: false, unique: true | ||
column :name, :string, size: 255, null: false | ||
column :type, :string, size: 11, null: false | ||
column :class_name, :string, size: 255, null: false, unique: true | ||
end | ||
end | ||
|
||
down do | ||
drop_table :modules | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module Cli | ||
# A mixin to handle the database caching of module data. | ||
module ModuleCache | ||
def initialize | ||
super | ||
self.current_version_number = File.read(File.join(Wpxf.app_path, 'VERSION')) | ||
end | ||
|
||
def cache_valid? | ||
last_version_log = Models::Log.first(key: 'version') | ||
return false if last_version_log.nil? | ||
|
||
current_version = Gem::Version.new(current_version_number) | ||
last_version = Gem::Version.new(last_version_log.value) | ||
|
||
current_version == last_version | ||
end | ||
|
||
def create_module_models(type) | ||
namespace = type == 'exploit' ? Wpxf::Exploit : Wpxf::Auxiliary | ||
namespace.module_list.each do |mod| | ||
instance = mod[:class].new | ||
|
||
Models::Module.create( | ||
path: mod[:name], | ||
name: instance.module_name, | ||
type: type, | ||
class_name: mod[:class].to_s | ||
) | ||
end | ||
end | ||
|
||
def refresh_version_log | ||
log = Models::Log.first(key: 'version') | ||
log = Models::Log.new if log.nil? | ||
log.key = 'version' | ||
log.value = current_version_number | ||
log.save | ||
end | ||
|
||
def rebuild_cache | ||
Models::Module.truncate | ||
|
||
create_module_models 'exploit' | ||
create_module_models 'auxiliary' | ||
|
||
refresh_version_log | ||
end | ||
|
||
attr_accessor :current_version_number | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module Models | ||
# A miscellaneous log entry. | ||
class Log < Sequel::Model | ||
plugin :validation_helpers | ||
|
||
def validate | ||
super | ||
|
||
validates_presence :key | ||
validates_type String, :key | ||
validates_unique :key | ||
validates_presence :value | ||
|
||
validates_max_length 50, :key | ||
validates_max_length 100, :value | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
module Models | ||
# A cache of a {Wpxf::Module}'s metadata. | ||
class Module < Sequel::Model | ||
plugin :validation_helpers | ||
|
||
def validate | ||
super | ||
|
||
validates_presence :path | ||
validates_presence :name | ||
validates_presence :type | ||
validates_presence :class_name | ||
|
||
validates_type String, :path | ||
validates_type String, :name | ||
validates_type String, :class_name | ||
|
||
validates_unique :path | ||
validates_unique :class_name | ||
|
||
validates_max_length 255, :path | ||
validates_max_length 255, :name | ||
validates_max_length 255, :class_name | ||
|
||
validates_format /^auxiliary|exploit$/, :type | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../spec_helper' | ||
require 'cli/console' | ||
|
||
describe Cli::Console do | ||
let(:subject) { Cli::Console.new } | ||
|
||
before :each, 'setup spies' do | ||
allow(subject).to receive(:print_warning) | ||
allow(subject).to receive(:rebuild_cache) | ||
allow(subject).to receive(:puts) | ||
end | ||
|
||
describe '#start' do | ||
before :each, 'setup mocks' do | ||
allow(subject).to receive(:prompt_for_input).and_return('exit') | ||
allow(subject).to receive(:check_cache) | ||
end | ||
|
||
it 'should check the module cache' do | ||
subject.start | ||
expect(subject).to have_received(:check_cache).exactly(1).times | ||
end | ||
end | ||
|
||
describe '#check_cache' do | ||
context 'if the module cache is not valid' do | ||
before(:each) do | ||
allow(subject).to receive(:cache_valid?).and_return(false) | ||
end | ||
|
||
it 'should refresh the module cache' do | ||
subject.check_cache | ||
expect(subject).to have_received(:rebuild_cache).exactly(1).times | ||
end | ||
|
||
it 'should warn the user the cache is being refreshed' do | ||
subject.check_cache | ||
expect(subject).to have_received(:print_warning) | ||
.with('Refreshing the module cache...') | ||
end | ||
end | ||
|
||
context 'if the module cache is valid' do | ||
before(:each) do | ||
allow(subject).to receive(:cache_valid?).and_return(true) | ||
end | ||
|
||
it 'should not refresh the cache' do | ||
subject.check_cache | ||
expect(subject).to have_received(:rebuild_cache).exactly(0).times | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.