Module: Rex::Ui::Text::Shell

Included in:
DispatcherShell, PseudoShell
Defined in:
lib/rex/ui/text/shell.rb

Overview

The shell class provides a command-prompt style interface in a generic fashion.

Defined Under Namespace

Modules: InputShell

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#disable_outputObject

Whether or not output has been disabled.



328
329
330
# File 'lib/rex/ui/text/shell.rb', line 328

def disable_output
  @disable_output
end

#frameworkObject

Returns the value of attribute framework.



340
341
342
# File 'lib/rex/ui/text/shell.rb', line 340

def framework
  @framework
end

#inputObject

The input handle to read user input from.



332
333
334
# File 'lib/rex/ui/text/shell.rb', line 332

def input
  @input
end

#on_command_procObject

Returns the value of attribute on_command_proc.



338
339
340
# File 'lib/rex/ui/text/shell.rb', line 338

def on_command_proc
  @on_command_proc
end

#on_print_procObject

Returns the value of attribute on_print_proc.



339
340
341
# File 'lib/rex/ui/text/shell.rb', line 339

def on_print_proc
  @on_print_proc
end

#outputObject

The output handle to write output to.



336
337
338
# File 'lib/rex/ui/text/shell.rb', line 336

def output
  @output
end

Instance Method Details

#init_tab_completeObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rex/ui/text/shell.rb', line 57

def init_tab_complete
  if (self.input and self.input.supports_readline)
    self.input = Input::Readline.new(lambda { |str| tab_complete(str) })
    if Readline::HISTORY.length == 0 and histfile and File.exists?(histfile)
      File.readlines(histfile).each { |e|
        Readline::HISTORY << e.chomp
      }
      self.hist_last_saved = Readline::HISTORY.length
    end
    self.input.output = self.output
    update_prompt(input.prompt)
  end
end

#init_ui(in_input = nil, in_output = nil) ⇒ Object

Initializes the user interface input/output classes.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rex/ui/text/shell.rb', line 74

def init_ui(in_input = nil, in_output = nil)
  # Initialize the input and output methods
  self.input  = in_input
  self.output = in_output

  if (self.input)
    # Extend the input medium as an input shell if the input medium
    # isn't intrinsicly a shell.
    if (self.input.intrinsic_shell? == false)
      self.input.extend(InputShell)
    end

    self.input.output = self.output
  end
  update_prompt('')
end

#initialize(prompt, prompt_char = '>', histfile = nil, framework = nil) ⇒ Object

Initializes a shell that has a prompt and can be interacted with.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rex/ui/text/shell.rb', line 41

def initialize(prompt, prompt_char = '>', histfile = nil, framework = nil)
  # Set the stop flag to false
  self.stop_flag      = false
  self.disable_output = false
  self.stop_count	    = 0

  # Initialize the prompt
  self.init_prompt = prompt
  self.prompt_char = prompt_char

  self.histfile = histfile
  self.hist_last_saved = 0

  self.framework = framework
end

Prints a raw message to the output handle.



319
320
321
322
323
# File 'lib/rex/ui/text/shell.rb', line 319

def print(msg='')
  return if (disable_output == true)
  self.on_print_proc.call(msg) if self.on_print_proc
  log_output(output.print(msg))
end

Prints an error message to the output handle.



268
269
270
271
272
273
274
# File 'lib/rex/ui/text/shell.rb', line 268

def print_error(msg='')
  return if (output.nil?)

  self.on_print_proc.call(msg) if self.on_print_proc
  # Errors are not subject to disabled output
  log_output(output.print_error(msg))
end

Prints a good message to the output handle.



289
290
291
292
293
294
# File 'lib/rex/ui/text/shell.rb', line 289

def print_good(msg='')
  return if (disable_output == true)

  self.on_print_proc.call(msg) if self.on_print_proc
  log_output(output.print_good(msg))
end

Prints a line of text to the output handle.



299
300
301
302
303
304
# File 'lib/rex/ui/text/shell.rb', line 299

def print_line(msg='')
  return if (disable_output == true)

  self.on_print_proc.call(msg) if self.on_print_proc
  log_output(output.print_line(msg))
end

Prints a status message to the output handle.



279
280
281
282
283
284
# File 'lib/rex/ui/text/shell.rb', line 279

def print_status(msg='')
  return if (disable_output == true)

  self.on_print_proc.call(msg) if self.on_print_proc
  log_output(output.print_status(msg))
end

Prints a warning message to the output handle.



309
310
311
312
313
314
# File 'lib/rex/ui/text/shell.rb', line 309

def print_warning(msg='')
  return if (disable_output == true)

  self.on_print_proc.call(msg) if self.on_print_proc
  log_output(output.print_warning(msg))
end

#reset_uiObject

Resets the user interface handles.



94
95
96
# File 'lib/rex/ui/text/shell.rb', line 94

def reset_ui
  init_ui
end

#run(&block) ⇒ Object

Run the command processing loop.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
209
210
211
212
213
214
215
216
217
# File 'lib/rex/ui/text/shell.rb', line 122

def run(&block)

  begin

    while true
      # If the stop flag was set or we've hit EOF, break out
      break if (self.stop_flag or self.stop_count > 1)

      init_tab_complete

      if framework
        if input.prompt.include?("%T")
          t = Time.now
          if framework.datastore['PromptTimeFormat']
            t = t.strftime(framework.datastore['PromptTimeFormat'])
          end
          input.prompt.gsub!(/%T/, t.to_s)
        end

        if input.prompt.include?("%H")
          hostname = ENV['HOSTNAME']
          if hostname.nil?
            hostname = `hostname`.split('.')[0]
          end

          # check if hostname is still nil
          if hostname.nil?
            hostname = ENV['COMPUTERNAME']
          end

          if hostname.nil?
            hostname = 'unknown'
          end

          input.prompt.gsub!(/%H/, hostname.chomp)
        end

        if input.prompt.include?("%U")
          user = ENV['USER']
          if user.nil?
            user = `whoami`
          end

          # check if username is still nil
          if user.nil?
            user = ENV['USERNAME']
          end

          if user.nil?
            user = 'unknown'
          end

          input.prompt.gsub!(/%U/, user.chomp)
        end

        input.prompt.gsub!(/%S/, framework.sessions.length.to_s)
        input.prompt.gsub!(/%J/, framework.jobs.length.to_s)
        input.prompt.gsub!(/%L/, Rex::Socket.source_address("50.50.50.50"))
        input.prompt.gsub!(/%D/, ::Dir.getwd)
        self.init_prompt = input.prompt
      end

      line = input.pgets()
      log_output(input.prompt)

      # If a block was passed in, pass the line to it.  If it returns true,
      # break out of the shell loop.
      if (block)
        break if (line == nil or block.call(line))
      elsif(input.eof? or line == nil)
      # If you have sessions active, this will give you a shot to exit gravefully
      # If you really are ambitious, 2 eofs will kick this out
        self.stop_count += 1
        next if(self.stop_count > 1)
        run_single("quit")
      else
      # Otherwise, call what should be an overriden instance method to
      # process the line.
        ret = run_single(line)
        # don't bother saving lines that couldn't be found as a
        # command, create the file if it doesn't exist
        if ret and self.histfile
          File.open(self.histfile, "a+") { |f|
            f.puts(line)
          }
        end
        self.stop_count = 0
      end

    end
  # Prevent accidental console quits
  rescue ::Interrupt
    output.print("Interrupt: use the 'exit' command to quit\n")
    retry
  end
end

#set_log_source(log_source) ⇒ Object

Sets the log source that should be used for logging input and output.



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

def set_log_source(log_source)
  self.log_source = log_source
end

#stopObject

Stop processing user input.



222
223
224
# File 'lib/rex/ui/text/shell.rb', line 222

def stop
  self.stop_flag = true
end

#stopped?Boolean

Checks to see if the shell has stopped.

Returns:

  • (Boolean)


229
230
231
# File 'lib/rex/ui/text/shell.rb', line 229

def stopped?
  self.stop_flag
end

#tab_complete(str) ⇒ Object

Performs tab completion on the supplied string.



115
116
117
# File 'lib/rex/ui/text/shell.rb', line 115

def tab_complete(str)
  return tab_complete_proc(str) if (tab_complete_proc)
end

#unset_log_sourceObject

Unsets the log source so that logging becomes disabled.



108
109
110
# File 'lib/rex/ui/text/shell.rb', line 108

def unset_log_source
  set_log_source(nil)
end

#update_prompt(prompt = nil, new_prompt_char = nil, mode = false) ⇒ Object

Change the input prompt.

prompt - the actual prompt new_prompt_char the char to append to the prompt mode - append or not to append - false = append true = make a new prompt



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/rex/ui/text/shell.rb', line 239

def update_prompt(prompt = nil, new_prompt_char = nil, mode = false)
  if (self.input)
    if prompt
      new_prompt = self.init_prompt + ' ' + prompt + prompt_char + ' '
    else
      new_prompt = self.prompt || ''
    end

    if mode
      new_prompt = prompt + (new_prompt_char || prompt_char) + ' '
    end

    # Save the prompt before any substitutions
    self.prompt = new_prompt

    # Set the actual prompt to the saved prompt with any substitutions
    # or updates from our output driver, be they color or whatever
    self.input.prompt = self.output.update_prompt(new_prompt)
    self.prompt_char  = new_prompt_char if (new_prompt_char)
  end
end