2005-07-19 04:21:15 +00:00
|
|
|
module Rex
|
|
|
|
module Ui
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class implements the stubs that are needed to provide an interactive
|
|
|
|
# user interface that is backed against something arbitrary.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
module Interactive
|
|
|
|
|
|
|
|
#
|
|
|
|
# Interactive sessions by default may interact with the local user input
|
|
|
|
# and output.
|
|
|
|
#
|
|
|
|
include Rex::Ui::Subscriber
|
|
|
|
|
|
|
|
#
|
|
|
|
# Starts interacting with the session at the most raw level, simply
|
|
|
|
# forwarding input from user_input to rstream and forwarding input from
|
|
|
|
# rstream to user_output.
|
|
|
|
#
|
2007-02-18 05:07:56 +00:00
|
|
|
def interact(user_input, user_output)
|
2007-02-18 04:25:46 +00:00
|
|
|
|
2007-02-18 05:07:56 +00:00
|
|
|
# Detach from any existing console
|
2007-02-18 04:25:46 +00:00
|
|
|
if(self.interacting)
|
2007-02-18 05:07:56 +00:00
|
|
|
detach()
|
2007-02-18 04:25:46 +00:00
|
|
|
end
|
|
|
|
|
2007-02-18 05:07:56 +00:00
|
|
|
init_ui(user_input, user_output)
|
|
|
|
|
2005-07-19 04:21:15 +00:00
|
|
|
self.interacting = true
|
2007-02-18 04:25:46 +00:00
|
|
|
self.completed = false
|
2005-07-19 04:21:15 +00:00
|
|
|
|
|
|
|
eof = false
|
|
|
|
|
2006-08-01 01:50:26 +00:00
|
|
|
# Start the readline stdin monitor
|
|
|
|
# XXX disabled
|
|
|
|
# user_input.readline_start() if user_input.supports_readline
|
2006-07-29 20:03:05 +00:00
|
|
|
|
2005-07-19 04:21:15 +00:00
|
|
|
# Handle suspend notifications
|
|
|
|
handle_suspend
|
2006-07-29 20:03:05 +00:00
|
|
|
|
|
|
|
# As long as we're interacting...
|
|
|
|
while (self.interacting == true)
|
2005-07-19 04:21:15 +00:00
|
|
|
|
2006-07-29 20:03:05 +00:00
|
|
|
begin
|
|
|
|
_interact
|
|
|
|
|
|
|
|
rescue Interrupt
|
|
|
|
# If we get an interrupt exception, ask the user if they want to
|
|
|
|
# abort the interaction. If they do, then we return out of
|
|
|
|
# the interact function and call it a day.
|
|
|
|
eof = true if (_interrupt)
|
|
|
|
|
|
|
|
rescue EOFError, Errno::ECONNRESET, IOError
|
|
|
|
# If we reach EOF or the connection is reset...
|
|
|
|
eof = true
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
break if eof
|
2005-07-26 02:01:27 +00:00
|
|
|
end
|
2005-07-19 04:21:15 +00:00
|
|
|
|
2007-02-18 04:25:46 +00:00
|
|
|
begin
|
2006-07-29 20:03:05 +00:00
|
|
|
|
2007-02-18 04:25:46 +00:00
|
|
|
# Restore the suspend handler
|
|
|
|
restore_suspend
|
|
|
|
|
|
|
|
# If we've hit eof, call the interact complete handler
|
|
|
|
_interact_complete if (eof == true)
|
|
|
|
|
|
|
|
# Shutdown the readline thread
|
|
|
|
# XXX disabled
|
|
|
|
# user_input.readline_stop() if user_input.supports_readline
|
2007-02-18 05:07:56 +00:00
|
|
|
|
2007-02-18 04:25:46 +00:00
|
|
|
# Detach from the input/output handles
|
|
|
|
reset_ui()
|
2007-02-18 05:07:56 +00:00
|
|
|
|
2007-02-18 04:25:46 +00:00
|
|
|
ensure
|
|
|
|
# Mark this as completed
|
|
|
|
self.completed = true
|
|
|
|
end
|
2007-02-18 05:07:56 +00:00
|
|
|
|
2005-07-19 04:21:15 +00:00
|
|
|
# Return whether or not EOF was reached
|
|
|
|
return eof
|
|
|
|
end
|
|
|
|
|
2007-02-18 04:25:46 +00:00
|
|
|
#
|
|
|
|
# Stops the current interaction
|
|
|
|
#
|
|
|
|
def detach
|
|
|
|
if (self.interacting)
|
|
|
|
self.interacting = false
|
2007-02-18 05:07:56 +00:00
|
|
|
while(not self.completed)
|
|
|
|
select(nil, nil, nil, 0.25)
|
2007-02-18 04:25:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-07-19 04:21:15 +00:00
|
|
|
#
|
|
|
|
# Whether or not the session is currently being interacted with
|
|
|
|
#
|
2007-02-11 23:24:25 +00:00
|
|
|
attr_accessor :interacting
|
2007-02-18 04:25:46 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Whether or not the session has completed interaction
|
|
|
|
#
|
|
|
|
attr_accessor :completed
|
2005-07-19 04:21:15 +00:00
|
|
|
|
|
|
|
protected
|
2006-07-27 22:28:19 +00:00
|
|
|
|
2005-07-19 04:21:15 +00:00
|
|
|
#
|
|
|
|
# The original suspend proc.
|
|
|
|
#
|
|
|
|
attr_accessor :orig_suspend
|
|
|
|
|
|
|
|
#
|
|
|
|
# Stub method that is meant to handler interaction
|
|
|
|
#
|
|
|
|
def _interact
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Called when an interrupt is sent.
|
|
|
|
#
|
|
|
|
def _interrupt
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Called when a suspend is sent.
|
|
|
|
#
|
|
|
|
def _suspend
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Called when interaction has completed and one of the sides has closed.
|
|
|
|
#
|
|
|
|
def _interact_complete
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Read from remote and write to local.
|
|
|
|
#
|
|
|
|
def _stream_read_remote_write_local(stream)
|
|
|
|
data = stream.get
|
|
|
|
|
|
|
|
user_output.print(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Read from local and write to remote.
|
|
|
|
#
|
|
|
|
def _stream_read_local_write_remote(stream)
|
|
|
|
data = user_input.gets
|
2006-07-27 22:28:19 +00:00
|
|
|
|
2005-07-19 04:21:15 +00:00
|
|
|
stream.put(data)
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# The local file descriptor handle.
|
|
|
|
#
|
2005-07-19 04:21:15 +00:00
|
|
|
def _local_fd
|
|
|
|
user_input.fd
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# The remote file descriptor handle.
|
|
|
|
#
|
2005-07-19 04:21:15 +00:00
|
|
|
def _remote_fd(stream)
|
|
|
|
stream.fd
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Interacts with two streaming connections, reading data from one and
|
|
|
|
# writing it to the other. Both are expected to implement Rex::IO::Stream.
|
|
|
|
#
|
|
|
|
def interact_stream(stream)
|
2007-02-18 04:25:46 +00:00
|
|
|
while self.interacting
|
|
|
|
|
2005-07-19 04:21:15 +00:00
|
|
|
# Select input and rstream
|
2007-02-18 22:35:07 +00:00
|
|
|
sd = Rex::ThreadSafe.select([ _local_fd, _remote_fd(stream) ], nil, nil, 0.25)
|
|
|
|
|
2005-07-19 04:21:15 +00:00
|
|
|
# Cycle through the items that have data
|
|
|
|
# From the stream? Write to user_output.
|
|
|
|
sd[0].each { |s|
|
|
|
|
if (s == _remote_fd(stream))
|
|
|
|
_stream_read_remote_write_local(stream)
|
|
|
|
# From user_input? Write to stream.
|
|
|
|
elsif (s == _local_fd)
|
|
|
|
_stream_read_local_write_remote(stream)
|
|
|
|
end
|
|
|
|
} if (sd)
|
2007-02-18 22:35:07 +00:00
|
|
|
|
|
|
|
Thread.pass
|
2005-07-19 04:21:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Installs a signal handler to monitor suspend signal notifications.
|
|
|
|
#
|
|
|
|
def handle_suspend
|
|
|
|
if (orig_suspend == nil)
|
2005-11-03 05:11:01 +00:00
|
|
|
begin
|
|
|
|
self.orig_suspend = Signal.trap("TSTP") {
|
|
|
|
_suspend
|
|
|
|
}
|
|
|
|
rescue
|
|
|
|
end
|
2005-07-19 04:21:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Restores the previously installed signal handler for suspend
|
|
|
|
# notifications.
|
|
|
|
#
|
|
|
|
def restore_suspend
|
|
|
|
if (orig_suspend)
|
2005-11-03 05:11:01 +00:00
|
|
|
begin
|
|
|
|
Signal.trap("TSTP", orig_suspend)
|
|
|
|
rescue
|
|
|
|
end
|
2005-07-19 04:21:15 +00:00
|
|
|
|
|
|
|
self.orig_suspend = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Prompt the user for input if possible.
|
2006-07-27 22:28:19 +00:00
|
|
|
# XXX: This is not thread-safe on Windows
|
2005-07-19 04:21:15 +00:00
|
|
|
#
|
|
|
|
def prompt(query)
|
|
|
|
if (user_output and user_input)
|
|
|
|
user_output.print("\n" + query)
|
2005-12-07 03:40:09 +00:00
|
|
|
user_input.sysread(2)
|
2005-07-19 04:21:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Check the return value of a yes/no prompt
|
|
|
|
#
|
|
|
|
def prompt_yesno(query)
|
|
|
|
(prompt(query + " [y/N] ") =~ /^y/i) ? true : false
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|