Skip to content
This repository has been archived by the owner on Oct 22, 2020. It is now read-only.

Commit

Permalink
Fix broken namespaces and migrate entry point into new bin
Browse files Browse the repository at this point in the history
  • Loading branch information
rastating committed Jul 14, 2018
1 parent c0f1f98 commit 3fe58dd
Show file tree
Hide file tree
Showing 42 changed files with 219 additions and 297 deletions.
51 changes: 51 additions & 0 deletions bin/wpxf
@@ -1 +1,52 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'wpxf'
require 'wpxf/cli/console'
require 'wpxf/cli/banner'

begin
Slop.parse do |o|
o.on '--version', 'print the version' do
puts Wpxf.version
exit
end
end
rescue Slop::UnknownOption => e
puts e.message
exit
end

console = Wpxf::Cli::Console.new
console.check_cache
console.clear

banner = Wpxf::Cli::Banner.new
banner.display

Dir.chdir(Dir.tmpdir) do
temp_directories = Dir.glob('wpxf_*')
unless temp_directories.empty?
print '[!] '.yellow
puts "#{temp_directories.length} temporary files were found that "\
'appear to no longer be needed.'
print ' Would you like to remove these files? [y/n]: '
temp_directories.each { |d| FileUtils.rm_r(d) } if gets.chomp =~ /^y$/i
puts
end
end

found_env_var = false
ENV.each do |name, value|
next if name.casecmp('wpxf_env').zero?
match = name.match(/^wpxf_(.+)/i)

if match
console.gset match.captures[0], value
found_env_var = true
end
end

puts if found_env_var
console.start
console.clear
61 changes: 0 additions & 61 deletions env.rb

This file was deleted.

1 change: 1 addition & 0 deletions lib/wpxf.rb
Expand Up @@ -49,3 +49,4 @@ def self.change_stdout_sync(enabled)
end

require_relative '../db/env'
require 'wpxf/core'
2 changes: 1 addition & 1 deletion lib/wpxf/cli/auto_complete.rb
Expand Up @@ -51,7 +51,7 @@ def refresh_autocomplete_options
def build_cmd_list
cmds = {}
permitted_commands.each { |c| cmds[c] = {} }
Models::Module.each { |m| cmds['use'][m.path] = {} }
Wpxf::Models::Module.each { |m| cmds['use'][m.path] = {} }
cmds['show'] = {
'options' => {},
'advanced' => {},
Expand Down
4 changes: 2 additions & 2 deletions lib/wpxf/cli/banner.rb
Expand Up @@ -20,11 +20,11 @@ def format_colour(value)
end

def auxiliary_count
Models::Module.where(type: 'auxiliary').count
Wpxf::Models::Module.where(type: 'auxiliary').count
end

def exploit_count
Models::Module.where(type: 'exploit').count
Wpxf::Models::Module.where(type: 'exploit').count
end

def format_data(value)
Expand Down
2 changes: 1 addition & 1 deletion lib/wpxf/cli/context.rb
Expand Up @@ -18,7 +18,7 @@ def load_module(path)
end

def reload
load("#{@module_path}.rb")
load("wpxf/modules/#{@module_path}.rb")
load_module(@module_path)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/wpxf/cli/creds.rb
Expand Up @@ -5,7 +5,7 @@ module Cli
# A mixin to provide functions to interact with credentials stored in the database.
module Creds
def delete_credential(id)
item = Models::Credential.first(
item = Wpxf::Models::Credential.first(
id: id.to_i,
workspace: active_workspace
)
Expand All @@ -28,7 +28,7 @@ def list_credentials
type: 'Type'
)

Models::Credential.where(workspace: active_workspace).each do |cred|
Wpxf::Models::Credential.where(workspace: active_workspace).each do |cred|
rows.push(
id: cred.id,
host: "#{cred.host}:#{cred.port}",
Expand Down
4 changes: 2 additions & 2 deletions lib/wpxf/cli/help.rb
Expand Up @@ -65,7 +65,7 @@ def help
end

def show_exploits
modules = Models::Module.where(type: 'exploit')
modules = Wpxf::Models::Module.where(type: 'exploit')
.order(:path)
.map { |m| { path: m.path, title: m.name } }

Expand All @@ -74,7 +74,7 @@ def show_exploits
end

def show_auxiliary
modules = Models::Module.where(type: 'auxiliary')
modules = Wpxf::Models::Module.where(type: 'auxiliary')
.order(:path)
.map { |m| { path: m.path, title: m.name } }

Expand Down
4 changes: 2 additions & 2 deletions lib/wpxf/cli/loot.rb
Expand Up @@ -18,7 +18,7 @@ def build_loot_table
type: 'Type'
)

Models::LootItem.where(workspace: active_workspace).each do |item|
Wpxf::Models::LootItem.where(workspace: active_workspace).each do |item|
rows.push(
id: item.id,
host: "#{item.host}:#{item.port}",
Expand All @@ -39,7 +39,7 @@ def list_loot
end

def find_loot_item(id)
item = Models::LootItem.first(
item = Wpxf::Models::LootItem.first(
id: id.to_i,
workspace: active_workspace
)
Expand Down
12 changes: 6 additions & 6 deletions lib/wpxf/cli/module_cache.rb
Expand Up @@ -6,11 +6,11 @@ module Cli
module ModuleCache
def initialize
super
self.current_version_number = File.read(File.join(Wpxf.app_path, 'VERSION'))
self.current_version_number = Wpxf.version
end

def cache_valid?
last_version_log = Models::Log.first(key: 'version')
last_version_log = Wpxf::Models::Log.first(key: 'version')
return false if last_version_log.nil?

current_version = Gem::Version.new(current_version_number)
Expand All @@ -24,7 +24,7 @@ def create_module_models(type)
namespace.module_list.each do |mod|
instance = mod[:class].new

Models::Module.create(
Wpxf::Models::Module.create(
path: mod[:name],
name: instance.module_name,
type: type,
Expand All @@ -34,8 +34,8 @@ def create_module_models(type)
end

def refresh_version_log
log = Models::Log.first(key: 'version')
log = Models::Log.new if log.nil?
log = Wpxf::Models::Log.first(key: 'version')
log = Wpxf::Models::Log.new if log.nil?
log.key = 'version'
log.value = current_version_number
log.save
Expand All @@ -44,7 +44,7 @@ def refresh_version_log
def rebuild_cache
print_warning 'Refreshing the module cache...'

Models::Module.truncate
Wpxf::Models::Module.truncate

create_module_models 'exploit'
create_module_models 'auxiliary'
Expand Down
2 changes: 1 addition & 1 deletion lib/wpxf/cli/modules.rb
Expand Up @@ -59,7 +59,7 @@ def module_name_from_class(klass)
end

def search_modules(args)
modules = Models::Module.where(Sequel.ilike(:name, "%#{args.join(' ')}%"))
modules = Wpxf::Models::Module.where(Sequel.ilike(:name, "%#{args.join(' ')}%"))
modules.map { |m| { path: m.path, title: m.name } }
end

Expand Down
12 changes: 6 additions & 6 deletions lib/wpxf/cli/workspace.rb
Expand Up @@ -7,7 +7,7 @@ module Workspace
def initialize
super

self.active_workspace = Models::Workspace.first(name: 'default')
self.active_workspace = Wpxf::Models::Workspace.first(name: 'default')
end

def workspace(*args)
Expand All @@ -24,16 +24,16 @@ def workspace(*args)
end

def workspaces
Models::Workspace.all
Wpxf::Models::Workspace.all
end

def add_workspace(name)
unless Models::Workspace.where(name: name).count.zero?
unless Wpxf::Models::Workspace.where(name: name).count.zero?
return print_warning "#{name} already exists"
end

begin
Models::Workspace.create(name: name)
Wpxf::Models::Workspace.create(name: name)
return print_good "Added workspace: #{name}"
rescue Sequel::ValidationFailed
print_warning 'Workspace names may only contain 1-50 alphanumeric characters and underscores'
Expand All @@ -51,7 +51,7 @@ def list_workspaces
end

def switch_workspace(name)
next_workspace = Models::Workspace.first(name: name)
next_workspace = Wpxf::Models::Workspace.first(name: name)

if next_workspace
self.active_workspace = next_workspace
Expand All @@ -69,7 +69,7 @@ def delete_workspace(name)
end

current_name = active_workspace.name
Models::Workspace.where(name: name).destroy
Wpxf::Models::Workspace.where(name: name).destroy
print_good "Deleted workspace: #{name}"
switch_workspace 'default' if name == current_name
end
Expand Down
4 changes: 2 additions & 2 deletions lib/wpxf/db/credentials.rb
Expand Up @@ -8,15 +8,15 @@ module Wpxf::Db::Credentials
# @param type [String] the type of string stored in the password field.
# @return [Models::Credential] the newly created {Models::Credential}.
def store_credentials(username, password = '', type = 'plain')
credential = Models::Credential.first(
credential = Wpxf::Models::Credential.first(
host: target_host,
port: target_port,
username: username,
type: type,
workspace: active_workspace
)

credential = Models::Credential.new if credential.nil?
credential = Wpxf::Models::Credential.new if credential.nil?
credential.host = target_host
credential.port = target_port
credential.username = username
Expand Down
2 changes: 1 addition & 1 deletion lib/wpxf/db/loot.rb
Expand Up @@ -7,7 +7,7 @@ module Wpxf::Db::Loot
# @param type [String] the type of loot acquired.
# @return [Models::LootItem] the newly created {Models::LootItem}.
def store_loot(path, notes = '', type = 'unknown')
Models::LootItem.create(
Wpxf::Models::LootItem.create(
host: target_host,
port: target_port,
path: path,
Expand Down
4 changes: 2 additions & 2 deletions lib/wpxf/modules.rb
Expand Up @@ -6,7 +6,7 @@ def self.build_module_list(namespace, folder_name = '')
namespace.const_get(c).is_a? Class
end

modules_directory = File.join(Wpxf.app_path, folder_name)
modules_directory = File.join(Wpxf.app_path, 'lib', 'wpxf', folder_name)

modules.map do |m|
klass = namespace.const_get(m)
Expand All @@ -19,7 +19,7 @@ def self.build_module_list(namespace, folder_name = '')
end

def self.load_module(path)
mod = Models::Module.first(path: path)
mod = Wpxf::Models::Module.first(path: path)
raise "\"#{path}\" is not a valid module" if mod.nil?
Object.const_get(mod.class_name).new
end
Expand Down
11 changes: 4 additions & 7 deletions spec/lib/wpxf/cli/banner_spec.rb
Expand Up @@ -10,7 +10,7 @@
allow(subject).to receive(:puts)

(1..5).each do |i|
Models::Module.create(
Wpxf::Models::Module.create(
path: "test#{i}/",
name: "Test #{i}",
class_name: "Test#{i}",
Expand All @@ -19,7 +19,7 @@
end

(6..8).each do |i|
Models::Module.create(
Wpxf::Models::Module.create(
path: "test#{i}/",
name: "Test #{i}",
class_name: "Test#{i}",
Expand All @@ -30,7 +30,7 @@

describe '#new' do
it 'should initialise #raw_content with the content of `data/banners/default.txt`' do
base_path = File.expand_path(File.join(__dir__, '..', '..', '..'))
base_path = File.expand_path(File.join(__dir__, '..', '..', '..', '..'))
banner_path = File.join(base_path, 'data', 'banners', 'default.txt')
expected = File.read(banner_path)

Expand Down Expand Up @@ -79,10 +79,7 @@

describe '#format_data' do
it 'should replace {VERSION} with the current version of WPXF' do
base_path = File.expand_path(File.join(__dir__, '..', '..', '..'))
version_path = File.join(base_path, 'VERSION')
expected = File.read(version_path).strip

expected = Wpxf.version
expect(subject.format_data('V:{VERSION}')).to eql "V:#{expected}"
end

Expand Down

0 comments on commit 3fe58dd

Please sign in to comment.