Class: Ronin::UI::Shell
- Inherits:
-
Object
- Object
- Ronin::UI::Shell
- Includes:
- Output::Helpers
- Defined in:
- lib/ronin/ui/shell.rb
Overview
Spawns a ReadLine powered interactive Shell.
## Simple Shell
require 'ronin/ui/shell'
require 'ronin/network/tcp'
include Ronin::Network::TCP
tcp_session('victim.com',1337) do |socket|
UI::Shell.new(:name => 'bind_shell') do |shell,line|
socket.puts "#{line}; echo 'EOC'"
socket.each_line do |output|
puts output
break if output.chomp == 'EOC'
end
end
end
## Shell with Commands
require 'ronin/ui/shell'
require 'ronin/network/http'
class HTTPShell < Ronin::UI::Shell
include Ronin::Network::HTTP
def initialize(host)
super(:name => host)
@host = host
end
protected
def get(path)
print_response http_get(:host => @host, :path => path)
end
def post(path,*params)
print_response http_post(
:host => @host,
:path => path,
:post_data => Hash[params.map { |param| param.split('=') }]
)
end
private
def print_response(response)
response.canonical_each do |name,value|
puts "#{name}: #{value}"
end
puts
puts response.body
end
end
Constant Summary
- DEFAULT_PROMPT =
Default shell prompt
'>'
Instance Attribute Summary (collapse)
-
- (Object) commands
readonly
The commands available for the shell.
-
- (Object) name
The shell name.
-
- (Object) prompt
The shell prompt.
Class Method Summary (collapse)
-
+ (nil) start(*arguments) {|shell, line| ... }
Creates a new Shell object and starts it.
Instance Method Summary (collapse)
-
- (Object) call(line)
Handles input for the shell.
-
- (Shell) initialize(options = {}) {|shell, line| ... }
constructor
Creates a new shell.
-
- (Object) start
Starts the shell.
Methods included from Output::Helpers
#print_debug, #print_error, #print_exception, #print_info, #print_warning, #printf, #putc, #puts, #write
Constructor Details
- (Shell) initialize(options = {}) {|shell, line| ... }
Creates a new shell.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/ronin/ui/shell.rb', line 134 def initialize(={},&block) @name = [:name] @prompt = .fetch(:prompt,DEFAULT_PROMPT) @commands = Set['help', 'exit'] self.class.ancestors.each do |subclass| if subclass < Shell subclass.protected_instance_methods(false).each do |name| @commands << name.to_s end end end @input_handler = block end |
Instance Attribute Details
- (Object) commands (readonly)
The commands available for the shell
107 108 109 |
# File 'lib/ronin/ui/shell.rb', line 107 def commands @commands end |
- (Object) name
The shell name
101 102 103 |
# File 'lib/ronin/ui/shell.rb', line 101 def name @name end |
- (Object) prompt
The shell prompt
104 105 106 |
# File 'lib/ronin/ui/shell.rb', line 104 def prompt @prompt end |
Class Method Details
+ (nil) start(*arguments) {|shell, line| ... }
Creates a new Shell object and starts it.
172 173 174 |
# File 'lib/ronin/ui/shell.rb', line 172 def self.start(*arguments,&block) new(*arguments,&block).start end |
Instance Method Details
- (Object) call(line)
Handles input for the shell.
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/ronin/ui/shell.rb', line 218 def call(line) if @input_handler @input_handler.call(self,line) else arguments = line.split(/\s+/) command = arguments.shift # ignore empty lines return false unless command # no explicitly calling handler return false if command == 'handler' unless @commands.include?(command) print_error "Invalid command: #{command}" return false end return send(command,*arguments) end end |
- (Object) start
Starts the shell.
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/ronin/ui/shell.rb', line 181 def start history_rollback = 0 loop do unless (raw_line = Readline.readline("#{name}#{prompt} ")) break # user exited the shell end line = raw_line.strip if (line == 'exit' || line == 'quit') exit break elsif !(line.empty?) Readline::HISTORY << raw_line history_rollback += 1 begin call(line) rescue => e print_error "#{e.class.name}: #{e.}" end end end history_rollback.times { Readline::HISTORY.pop } return nil end |