2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2006-09-10 02:21:23 +00:00
|
|
|
module Rex
|
|
|
|
module IO
|
|
|
|
|
|
|
|
require 'rex/ui/text/output'
|
|
|
|
require 'rex/ui/text/output/buffer'
|
|
|
|
require 'rex/ui/text/input/buffer'
|
|
|
|
|
2007-01-19 08:46:06 +00:00
|
|
|
class BidirectionalPipe < Rex::Ui::Text::Input
|
2006-09-10 02:21:23 +00:00
|
|
|
|
|
|
|
def initialize
|
|
|
|
@subscribers_out = {}
|
2007-02-11 09:22:06 +00:00
|
|
|
@subscribers_ref = {}
|
|
|
|
@subscribers_idx = 0
|
2006-09-10 02:21:23 +00:00
|
|
|
@pipe_input = Rex::Ui::Text::Input::Buffer.new
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2007-02-11 19:33:01 +00:00
|
|
|
# We are the shell, the input, and the output
|
|
|
|
self.output = self
|
|
|
|
self.input = self
|
2006-09-10 02:21:23 +00:00
|
|
|
end
|
|
|
|
|
2007-01-19 08:46:06 +00:00
|
|
|
def pipe_input
|
|
|
|
@pipe_input
|
|
|
|
end
|
|
|
|
|
2006-09-10 02:21:23 +00:00
|
|
|
def close
|
|
|
|
@pipe_input.close
|
|
|
|
end
|
|
|
|
|
2007-02-11 09:22:06 +00:00
|
|
|
def has_subscriber?(id)
|
|
|
|
@subscribers_out.has_key?(id)
|
|
|
|
end
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2007-02-11 09:22:06 +00:00
|
|
|
def create_subscriber(id=nil)
|
|
|
|
id ||= (@subscribers_idx += 1).to_s
|
2006-09-10 02:21:23 +00:00
|
|
|
@subscribers_out[id] = Rex::Ui::Text::Output::Buffer.new
|
2007-02-11 09:22:06 +00:00
|
|
|
return id
|
2006-09-10 02:21:23 +00:00
|
|
|
end
|
|
|
|
|
2007-02-11 09:22:06 +00:00
|
|
|
def create_subscriber_proc(id=nil, &block)
|
|
|
|
id = create_subscriber(id)
|
|
|
|
@subscribers_ref[id] = block
|
|
|
|
end
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2006-09-10 02:21:23 +00:00
|
|
|
def remove_subscriber(id)
|
|
|
|
@subscribers_out.delete(id)
|
2007-02-11 09:22:06 +00:00
|
|
|
@subscribers_ref.delete(id)
|
2006-09-10 02:21:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def write_input(buf)
|
|
|
|
@pipe_input.put(buf)
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_subscriber(id)
|
|
|
|
output = @subscribers_out[id]
|
|
|
|
|
|
|
|
return '' if output.nil?
|
|
|
|
|
|
|
|
buf = output.buf
|
|
|
|
|
|
|
|
output.reset
|
|
|
|
|
|
|
|
buf
|
|
|
|
end
|
|
|
|
|
2007-02-11 19:40:33 +00:00
|
|
|
def print(msg='')
|
2006-09-10 02:21:23 +00:00
|
|
|
@subscribers_out.each_pair { |id, buf|
|
2007-02-11 09:22:06 +00:00
|
|
|
begin
|
|
|
|
@subscribers_ref[id] ? @subscribers_ref[id].call(msg) : buf.print(msg)
|
|
|
|
rescue ::Exception => e
|
2008-12-19 07:11:08 +00:00
|
|
|
# $stderr.puts "Error handling subscriber #{id}: #{e} #{e.backtrace.inspect}"
|
2007-02-11 19:33:01 +00:00
|
|
|
raise e
|
2007-02-11 09:22:06 +00:00
|
|
|
end
|
2006-09-10 02:21:23 +00:00
|
|
|
}
|
2007-02-11 19:33:01 +00:00
|
|
|
msg
|
2006-09-10 02:21:23 +00:00
|
|
|
end
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2007-02-11 19:40:33 +00:00
|
|
|
def print_error(msg='')
|
2007-01-20 22:19:32 +00:00
|
|
|
print_line('[-] ' + msg)
|
|
|
|
end
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2007-02-11 19:40:33 +00:00
|
|
|
def print_line(msg='')
|
2007-01-20 22:19:32 +00:00
|
|
|
print(msg + "\n")
|
|
|
|
end
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2007-02-11 19:40:33 +00:00
|
|
|
def print_good(msg='')
|
2007-01-20 22:19:32 +00:00
|
|
|
print_line('[+] ' + msg)
|
|
|
|
end
|
|
|
|
|
2010-02-02 21:03:07 +00:00
|
|
|
def print_debug(msg='')
|
|
|
|
print_line('[!] ' + msg)
|
|
|
|
end
|
|
|
|
|
2007-01-20 22:19:32 +00:00
|
|
|
def flush
|
|
|
|
end
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2007-02-11 19:40:33 +00:00
|
|
|
def print_status(msg='')
|
2007-01-20 22:19:32 +00:00
|
|
|
print_line('[*] ' + msg)
|
|
|
|
end
|
2006-09-10 02:21:23 +00:00
|
|
|
|
2012-10-13 02:48:15 +00:00
|
|
|
def print_warning(msg='')
|
|
|
|
print_warning('[!] ' + msg)
|
|
|
|
end
|
|
|
|
|
2007-02-11 09:22:06 +00:00
|
|
|
#
|
|
|
|
# Wrappers for the pipe_input methods
|
|
|
|
#
|
|
|
|
|
|
|
|
def close
|
|
|
|
@pipe_input.close
|
|
|
|
end
|
|
|
|
|
|
|
|
def sysread(len = 1)
|
|
|
|
@pipe_input.sysread(len)
|
|
|
|
end
|
|
|
|
|
|
|
|
def put(msg)
|
|
|
|
@pipe_input.put(msg)
|
|
|
|
end
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2007-02-11 09:22:06 +00:00
|
|
|
def gets
|
|
|
|
@pipe_input.gets
|
|
|
|
end
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2007-02-11 09:22:06 +00:00
|
|
|
def eof?
|
|
|
|
@pipe_input.eof?
|
|
|
|
end
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2007-02-11 09:22:06 +00:00
|
|
|
def fd
|
|
|
|
@pipe_input.fd
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Wrappers for shell methods
|
|
|
|
#
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2007-02-11 19:33:01 +00:00
|
|
|
attr_accessor :output, :prompt, :input
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2007-02-11 09:22:06 +00:00
|
|
|
def intrinsic_shell?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def supports_readline
|
|
|
|
false
|
|
|
|
end
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2007-02-11 19:33:01 +00:00
|
|
|
def supports_color?
|
|
|
|
false
|
|
|
|
end
|
2009-11-10 15:59:49 +00:00
|
|
|
|
2007-02-11 09:22:06 +00:00
|
|
|
def pgets
|
|
|
|
gets
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2006-09-10 02:21:23 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-11-10 15:59:49 +00:00
|
|
|
end
|
|
|
|
|