Class: 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 collapse
- DEFAULT_PROMPT =
Default shell prompt
'>'
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
The commands available for the shell.
-
#name ⇒ Object
The shell name.
-
#prompt ⇒ Object
The shell prompt.
Class Method Summary collapse
-
.start(*arguments) {|shell, line| ... } ⇒ nil
Creates a new Shell object and starts it.
Instance Method Summary collapse
-
#call(line) ⇒ Object
Handles input for the shell.
-
#exit ⇒ Object
protected
Method which is called before exiting the shell.
-
#help ⇒ Object
protected
Prints the available commands and their arguments.
-
#initialize(options = {}) {|shell, line| ... } ⇒ Shell
constructor
Creates a new shell.
-
#quit ⇒ Object
protected
-
#start ⇒ Object
Starts the shell.
Methods included from Output::Helpers
format, #print_debug, #print_error, #print_exception, #print_info, #print_warning, #printf, #putc, #puts, #write
Constructor Details
#initialize(options = {}) {|shell, line| ... } ⇒ Shell
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
#commands ⇒ Object (readonly)
The commands available for the shell
107 108 109 |
# File 'lib/ronin/ui/shell.rb', line 107 def commands @commands end |
#name ⇒ Object
The shell name
101 102 103 |
# File 'lib/ronin/ui/shell.rb', line 101 def name @name end |
#prompt ⇒ Object
The shell prompt
104 105 106 |
# File 'lib/ronin/ui/shell.rb', line 104 def prompt @prompt end |
Class Method Details
.start(*arguments) {|shell, line| ... } ⇒ nil
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
#call(line) ⇒ Object
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 |
#exit ⇒ Object (protected)
Method which is called before exiting the shell.
249 250 |
# File 'lib/ronin/ui/shell.rb', line 249 def exit end |
#help ⇒ Object (protected)
Prints the available commands and their arguments.
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/ronin/ui/shell.rb', line 266 def help puts "Available commands:" puts @commands.sort.each do |name| command_method = method(name) arguments = command_method.parameters.map do |param| case param[0] when :opt then "[#{param[1]}]" when :rest then "[#{param[1]} ...]" else param[1] end end puts " #{name} #{arguments.join(' ')}" end end |
#start ⇒ Object
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 |