Class: Ronin::UI::Shell

Inherits:
Object show all
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

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :name (String) — default: ''

    The shell-name to use before the prompt.

  • :prompt (String) — default: DEFAULT_PROMPT

    The prompt to use for the shell.

Yields:

  • (shell, line)

    The block that will be passed every command entered.

Yield Parameters:

  • shell (Shell)

    The shell to use for output.

  • line (String)

    The command entered into the shell.

Since:

  • 0.3.0



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(options={},&block)
  @name     = options[:name]
  @prompt   = options.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

#commandsObject (readonly)

The commands available for the shell



107
108
109
# File 'lib/ronin/ui/shell.rb', line 107

def commands
  @commands
end

#nameObject

The shell name



101
102
103
# File 'lib/ronin/ui/shell.rb', line 101

def name
  @name
end

#promptObject

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.

Examples:

Shell.start(:prompt => '$') { |shell,line| system(line) }

Parameters:

Yields:

  • (shell, line)

    The block that will be passed every command entered.

Yield Parameters:

  • shell (Shell)

    The shell to use for output.

  • line (String)

    The command entered into the shell.

Returns:

  • (nil)


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.

Parameters:

  • line (String)

    A line of input received by the shell.

Since:

  • 0.3.0



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

#exitObject (protected)

Method which is called before exiting the shell.

Since:

  • 0.3.0



249
250
# File 'lib/ronin/ui/shell.rb', line 249

def exit
end

#helpObject (protected)

Prints the available commands and their arguments.

Since:

  • 0.3.0



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

#quitObject (protected)

See Also:

Since:

  • 0.3.0



257
258
259
# File 'lib/ronin/ui/shell.rb', line 257

def quit
  exit
end

#startObject

Starts the shell.

Since:

  • 0.3.0



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.message}"
      end
    end
  end

  history_rollback.times { Readline::HISTORY.pop }
  return nil
end