CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/post/meterpreter/ui/console/interactive_channel.rb
Views: 11791
1
# -*- coding: binary -*-
2
module Rex
3
module Post
4
module Meterpreter
5
module Ui
6
7
###
8
#
9
# Mixin that is meant to extend the base channel class from meterpreter in a
10
# manner that adds interactive capabilities.
11
#
12
###
13
module Console::InteractiveChannel
14
15
include Rex::Ui::Interactive
16
17
attr_accessor :raw
18
#
19
# Interacts with self.
20
#
21
def _interact
22
# If the channel has a left-side socket, then we can interact with it.
23
if (self.lsock)
24
self.interactive(true)
25
if raw
26
update_term_size
27
_local_fd.raw do
28
interact_stream(self)
29
end
30
else
31
interact_stream(self)
32
end
33
34
self.interactive(false)
35
else
36
print_error("Channel #{self.cid} does not support interaction.")
37
38
self.interacting = false
39
end
40
end
41
42
#
43
# Called when an interrupt is sent.
44
#
45
def _interrupt
46
prompt_yesno("Terminate channel #{self.cid}?")
47
end
48
49
#
50
# Suspends interaction with the channel.
51
#
52
def _suspend
53
# Ask the user if they would like to background the session
54
if (prompt_yesno("Background channel #{self.cid}?") == true)
55
self.interactive(false)
56
57
self.interacting = false
58
end
59
end
60
61
#
62
# Closes the channel like it aint no thang.
63
#
64
def _interact_complete
65
begin
66
self.interactive(false)
67
68
self.close
69
rescue IOError
70
end
71
end
72
73
#
74
# Reads data from local input and writes it remotely.
75
#
76
def _stream_read_local_write_remote(channel)
77
if raw
78
data = user_input.sysread(1024)
79
else
80
data = user_input.gets
81
end
82
return if not data
83
84
self.on_command_proc.call(data.strip) if self.on_command_proc
85
self.write(data)
86
end
87
88
#
89
# Reads from the channel and writes locally.
90
#
91
def _stream_read_remote_write_local(channel)
92
data = self.lsock.sysread(16384)
93
94
self.on_print_proc.call(data.strip) if self.on_print_proc
95
self.on_log_proc.call(data.strip) if self.on_log_proc
96
user_output.print(data)
97
end
98
99
#
100
# Returns the remote file descriptor to select on
101
#
102
def _remote_fd(stream)
103
self.lsock
104
end
105
106
attr_accessor :rows
107
attr_accessor :cols
108
109
def _winch
110
update_term_size
111
end
112
113
def update_term_size
114
return unless self.client.commands.include?(Extensions::Stdapi::COMMAND_ID_STDAPI_SYS_PROCESS_SET_TERM_SIZE)
115
rows, cols = ::IO.console.winsize
116
unless rows == self.rows && cols == self.cols
117
set_term_size(rows, cols)
118
self.rows = rows
119
self.cols = cols
120
end
121
end
122
123
def set_term_size(rows, columns)
124
if self.cid.nil?
125
raise IOError, 'Channel has been closed.', caller
126
end
127
128
request = Packet.create_request(Extensions::Stdapi::COMMAND_ID_STDAPI_SYS_PROCESS_SET_TERM_SIZE)
129
request.add_tlv(TLV_TYPE_CHANNEL_ID, self.cid)
130
request.add_tlv(Extensions::Stdapi::TLV_TYPE_TERMINAL_ROWS, rows)
131
request.add_tlv(Extensions::Stdapi::TLV_TYPE_TERMINAL_COLUMNS, columns)
132
self.client.send_packet(request)
133
end
134
135
attr_accessor :on_log_proc
136
137
end
138
139
end
140
end
141
end
142
end
143
144