Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/post/meterpreter/ui/console/interactive_channel.rb
Views: 11791
# -*- coding: binary -*-1module Rex2module Post3module Meterpreter4module Ui56###7#8# Mixin that is meant to extend the base channel class from meterpreter in a9# manner that adds interactive capabilities.10#11###12module Console::InteractiveChannel1314include Rex::Ui::Interactive1516attr_accessor :raw17#18# Interacts with self.19#20def _interact21# If the channel has a left-side socket, then we can interact with it.22if (self.lsock)23self.interactive(true)24if raw25update_term_size26_local_fd.raw do27interact_stream(self)28end29else30interact_stream(self)31end3233self.interactive(false)34else35print_error("Channel #{self.cid} does not support interaction.")3637self.interacting = false38end39end4041#42# Called when an interrupt is sent.43#44def _interrupt45prompt_yesno("Terminate channel #{self.cid}?")46end4748#49# Suspends interaction with the channel.50#51def _suspend52# Ask the user if they would like to background the session53if (prompt_yesno("Background channel #{self.cid}?") == true)54self.interactive(false)5556self.interacting = false57end58end5960#61# Closes the channel like it aint no thang.62#63def _interact_complete64begin65self.interactive(false)6667self.close68rescue IOError69end70end7172#73# Reads data from local input and writes it remotely.74#75def _stream_read_local_write_remote(channel)76if raw77data = user_input.sysread(1024)78else79data = user_input.gets80end81return if not data8283self.on_command_proc.call(data.strip) if self.on_command_proc84self.write(data)85end8687#88# Reads from the channel and writes locally.89#90def _stream_read_remote_write_local(channel)91data = self.lsock.sysread(16384)9293self.on_print_proc.call(data.strip) if self.on_print_proc94self.on_log_proc.call(data.strip) if self.on_log_proc95user_output.print(data)96end9798#99# Returns the remote file descriptor to select on100#101def _remote_fd(stream)102self.lsock103end104105attr_accessor :rows106attr_accessor :cols107108def _winch109update_term_size110end111112def update_term_size113return unless self.client.commands.include?(Extensions::Stdapi::COMMAND_ID_STDAPI_SYS_PROCESS_SET_TERM_SIZE)114rows, cols = ::IO.console.winsize115unless rows == self.rows && cols == self.cols116set_term_size(rows, cols)117self.rows = rows118self.cols = cols119end120end121122def set_term_size(rows, columns)123if self.cid.nil?124raise IOError, 'Channel has been closed.', caller125end126127request = Packet.create_request(Extensions::Stdapi::COMMAND_ID_STDAPI_SYS_PROCESS_SET_TERM_SIZE)128request.add_tlv(TLV_TYPE_CHANNEL_ID, self.cid)129request.add_tlv(Extensions::Stdapi::TLV_TYPE_TERMINAL_ROWS, rows)130request.add_tlv(Extensions::Stdapi::TLV_TYPE_TERMINAL_COLUMNS, columns)131self.client.send_packet(request)132end133134attr_accessor :on_log_proc135136end137138end139end140end141end142143144