CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/msf/ui/web/web_console.rb
Views: 1904
1
# -*- coding: binary -*-
2
module Msf
3
module Ui
4
module Web
5
6
###
7
#
8
# This class implements a console instance for use by the web interface
9
#
10
###
11
12
class WebConsole
13
attr_accessor :pipe
14
attr_accessor :console
15
attr_accessor :console_id
16
attr_accessor :last_access
17
attr_accessor :framework
18
attr_accessor :thread
19
20
# Wrapper class in case we need to extend the pipe
21
class WebConsolePipe < Rex::Ui::Text::BidirectionalPipe
22
def prompting?
23
false
24
end
25
end
26
27
#
28
# Provides some overrides for web-based consoles
29
#
30
module WebConsoleShell
31
32
def supports_color?
33
false
34
end
35
36
def run_unknown_command(command)
37
Open3.popen2e(command) {|stdin,output,thread|
38
output.each {|outline|
39
print_line(outline.chomp)
40
}
41
}
42
end
43
44
end
45
46
def initialize(framework, console_id, opts={})
47
# Configure the framework
48
self.framework = framework
49
50
# Configure the ID
51
self.console_id = console_id
52
53
# Create a new pipe
54
self.pipe = WebConsolePipe.new
55
56
# Create a read subscriber
57
self.pipe.create_subscriber('msfweb')
58
59
# Skip database initialization if it is already configured
60
if framework.db && framework.db.active
61
opts['SkipDatabaseInit'] = true
62
if opts['workspace']
63
wspace = framework.db.find_workspace(opts['workspace'])
64
framework.db.workspace = wspace
65
end
66
end
67
68
# Initialize the console with our pipe
69
self.console = Msf::Ui::Console::Driver.new(
70
'msf',
71
'>',
72
opts.merge({
73
'Framework' => self.framework,
74
'LocalInput' => self.pipe,
75
'LocalOutput' => self.pipe,
76
'AllowCommandPassthru' => true,
77
'Resource' => [],
78
})
79
)
80
81
self.console.extend(WebConsoleShell)
82
self.console.block_command('irb')
83
84
self.thread = framework.threads.spawn("WebConsoleShell", false) { self.console.run }
85
86
update_access()
87
end
88
89
def update_access
90
self.last_access = Time.now
91
end
92
93
def read
94
update_access
95
self.pipe.read_subscriber('msfweb')
96
end
97
98
def write(buf)
99
update_access
100
self.pipe.write_input(buf)
101
end
102
103
def execute(cmd)
104
self.console.run_single(cmd)
105
end
106
107
def prompt
108
self.pipe.prompt
109
end
110
111
def tab_complete(cmd)
112
if(self.console.active_session)
113
return self.console.active_session.console.tab_complete(cmd)
114
end
115
self.console.tab_complete(cmd)
116
end
117
118
def shutdown
119
self.pipe.close
120
self.thread.kill
121
end
122
123
def busy
124
self.console.busy
125
end
126
127
def session_detach
128
if(self.console.active_session)
129
#background interactive meterpreter channel
130
if(self.console.active_session.respond_to?('channels'))
131
self.console.active_session.channels.each_value do |ch|
132
if(ch.respond_to?('interacting') && ch.interacting)
133
ch.detach()
134
return
135
end
136
end
137
end
138
#background session
139
self.console.active_session.completed = true
140
self.console.active_session.detach()
141
end
142
end
143
144
def session_kill
145
self.thread.raise(Interrupt)
146
end
147
148
def active_module
149
self.console.active_module
150
end
151
152
def active_module=(val)
153
self.console.active_module = val
154
end
155
156
end
157
158
159
end
160
end
161
end
162
163
164