Method: Rex::Ui::Text::Shell#run

Defined in:
lib/rex/ui/text/shell.rb

#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