Skip to content

Commit

Permalink
text user interface crap
Browse files Browse the repository at this point in the history
git-svn-id: file:///home/svn/incoming/trunk@2508 4d416f70-5f16-0410-b530-b9f4589650da
  • Loading branch information
Matt Miller committed May 22, 2005
1 parent e69dece commit 9ba20c7
Show file tree
Hide file tree
Showing 15 changed files with 552 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/msf/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
###

# Unit testing
require 'test/unit'
#require 'test/unit'
require 'Msf/Core/UnitTestSuite'

# framework-core depends on framework-shared
Expand Down
3 changes: 3 additions & 0 deletions lib/msf/core/option_container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ def each_option(&block)

module Test

begin
###
#
# OptionContainerTestCase
Expand Down Expand Up @@ -334,6 +335,8 @@ def test_advanced
"advanced option failed")
end
end
rescue
end

end

Expand Down
2 changes: 2 additions & 0 deletions lib/msf/shared/Constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module Msf
#
# Log levels
#
# LEV_0 errors and warnings may be displayed to the user by default.
#
LEV_0 = 0
LEV_1 = 1
LEV_2 = 2
Expand Down
7 changes: 7 additions & 0 deletions lib/msf/ui.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Msf
module Ui
end
end

require 'Msf/Ui/Driver'
require 'Msf/Ui/Console'
10 changes: 10 additions & 0 deletions lib/msf/ui/console.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Msf
module Ui
module Console


end
end
end

require 'Msf/Ui/Console/Driver'
52 changes: 52 additions & 0 deletions lib/msf/ui/console/command_dispatcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module Msf
module Ui
module Console

module CommandDispatcher

def initialize(in_driver)
self.driver = in_driver
end

def print_error(msg)
driver.print_error(msg)
end

def print_status(msg)
driver.print_status(msg)
end

def print_line(msg)
driver.print_line(msg)
end

def print(msg)
driver.print(msg)
end

def update_prompt(prompt)
driver.update_prompt(prompt)
end

def framework
return driver.framework
end

def set_active_module(mod)
driver.datastore['_ActiveModule'] = mod
end

def get_active_module
return driver.datastore['_ActiveModule']
end

protected

attr_accessor :driver

end

end end end

require 'Msf/Ui/Console/ModuleCommandDispatcher'
require 'Msf/Ui/Console/CommandDispatcher/Core'
106 changes: 106 additions & 0 deletions lib/msf/ui/console/command_dispatcher/core.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
require 'Msf/Ui/Console/CommandDispatcher/Encoder'

module Msf
module Ui
module Console
module CommandDispatcher

class Core

include Msf::Ui::Console::CommandDispatcher

# Uses a module
def cmd_use(args)
if (args.length == 0)
print(
"Usage: use module_name\n\n" +
"The use command is used to interact with a module of a given name.\n")
return false
end

# Try to create an instance of the supplied module name
mod_name = args[0]

begin
if ((mod = framework.modules.create(mod_name)) == nil)
print_error("Failed to load module: #{mod_name}")
return false
end
rescue NameError => info
print_error("The supplied module name is ambiguous.")
return false
end

# Enstack the command dispatcher for this module type
dispatcher = nil

case mod.type
when MODULE_ENCODER
dispatcher = Encoder
end

if (dispatcher != nil)
driver.enstack_dispatcher(dispatcher)
end

# Update the active module
set_active_module(mod)

# Update the command prompt
driver.update_prompt("#{mod.type}(#{mod_name}) ")
end

# Adds one or more search paths
def cmd_search(args)
args.each { |path|
framework.modules.add_module_path(path)
}

print_line("Added #{args.length} search paths.")
end

# Sets a name to a value in a context aware environment
def cmd_set(args)

# Determine which data store we're operating on
if (mod = get_active_module())
datastore = mod.datastore
else
datastore = driver.datastore
end

# Dump the contents of the active datastore if no args were supplied
if (args.length == 0)
datastore.each_pair { |name, value|
print_line("#{name}: #{value}")
}

return true
elsif (args.length < 2)
print(
"Usage: set name value\n\n" +
"Sets an arbitrary name to an arbitrary value.\n")
return false
end

# Set the supplied name to the supplied value
name = args[0]
value = args[1]

datastore[name] = value

print_line("#{name} => #{value}")
end

# Instructs the driver to stop executing
def cmd_exit(args)
print_line("Exiting...")

driver.stop
end

alias cmd_quit cmd_exit

end

end end end end
15 changes: 15 additions & 0 deletions lib/msf/ui/console/command_dispatcher/encoder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Msf
module Ui
module Console
module CommandDispatcher

class Encoder

include Msf::Ui::Console::ModuleCommandDispatcher

def cmd_encode(args)
end

end

end end end end
89 changes: 89 additions & 0 deletions lib/msf/ui/console/driver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require 'Msf/Core'
require 'Msf/Ui'
require 'Msf/Ui/Console/Shell'
require 'Msf/Ui/Console/CommandDispatcher'

module Msf
module Ui
module Console


###
#
# Driver
# ------
#
# This class implements a user interface driver on a console interface.
#
###
class Driver < Msf::Ui::Driver

include Msf::Ui::Console::Shell

def initialize(prompt = "msf")
# Initialize attributes
self.framework = Msf::Framework.new
self.dispatcher_stack = []

# Add the core command dispatcher as the root of the dispatcher
# stack
enstack_dispatcher(CommandDispatcher::Core)

# Initialize the super
super(prompt)
end

# Run a single command line
def run_single(line)
arguments = parse_line(line)
method = arguments.shift
found = false

if (method)
entries = dispatcher_stack.length

dispatcher_stack.each { |dispatcher|
begin
eval("
if (dispatcher.respond_to?('cmd_' + method))
dispatcher.#{'cmd_' + method}(arguments)
found = true
end")
rescue
output.print_error("Error while running command #{method}: #{$!}.")
end

# If the dispatcher stack changed as a result of this command,
# break out
break if (dispatcher_stack.length != entries)
}

if (!found)
output.print_error("Unknown command: #{method}.")
end
end

return found
end

# Push a dispatcher to the front of the stack
def enstack_dispatcher(dispatcher)
self.dispatcher_stack.unshift(dispatcher.new(self))
end

# Pop a dispatcher from the front of the stacker
def destack_dispatcher
self.dispatcher_stack.shift
end

attr_reader :dispatcher_stack, :framework

protected

attr_writer :dispatcher_stack, :framework

end

end
end
end
68 changes: 68 additions & 0 deletions lib/msf/ui/console/input_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require 'Msf/Ui'

module Msf
module Ui
module Console

###
#
# InputMethod
# -----------
#
# This class is the base class for the three kinds of console input methods:
# Stdio, File, and Readline. These classes are nearly idential to the classes
# found in irb.
#
###
class InputMethod
def initialize
self.eof = false
end

def gets
raise NotImplementedError
end

def eof?
return eof
end

attr_accessor :prompt, :eof
end

class StdioInputMethod < InputMethod
def gets
print prompt
return $stdin.gets
end

def eof?
return $stdin.eof?
end
end

begin
require 'readline'

class ReadlineInputMethod < InputMethod
include Readline

def gets
if ((line = readline(prompt, true)))
HISTORY.pop if (line.empty?)
return line + "\n"
else
eof = true
return line
end
end
end
rescue LoadError
end

class FileInputMethod < InputMethod
end

end
end
end
Loading

0 comments on commit 9ba20c7

Please sign in to comment.