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/msf/ui/web/driver.rb
Views: 11783
1
# -*- coding: binary -*-
2
3
4
module Msf
5
module Ui
6
module Web
7
8
9
10
###
11
#
12
# This class implements a user interface driver on a web interface.
13
#
14
###
15
class Driver < Msf::Ui::Driver
16
17
18
attr_accessor :framework # :nodoc:
19
attr_accessor :consoles # :nodoc:
20
attr_accessor :sessions # :nodoc:
21
attr_accessor :last_console # :nodoc:
22
23
ConfigCore = "framework/core"
24
ConfigGroup = "framework/ui/web"
25
26
#
27
# Initializes a web driver instance and prepares it for listening to HTTP
28
# requests. The constructor takes a hash of options that can control how
29
# the web server will operate.
30
#
31
def initialize(opts = {})
32
# Call the parent
33
super()
34
35
# Set the passed options hash for referencing later on.
36
self.opts = opts
37
38
self.consoles = {}
39
self.sessions = {}
40
41
if(opts[:framework])
42
self.framework = opts[:framework]
43
else
44
# Initialize configuration
45
Msf::Config.init
46
47
# Initialize logging
48
initialize_logging
49
50
# Initialize attributes
51
self.framework = Msf::Simple::Framework.create
52
end
53
54
# Initialize the console count
55
self.last_console = 0
56
end
57
58
def create_console(opts={})
59
# Destroy any unused consoles
60
clean_consoles
61
62
console = WebConsole.new(self.framework, self.last_console, opts)
63
self.last_console += 1
64
self.consoles[console.console_id.to_s] = console
65
console.console_id.to_s
66
end
67
68
def destroy_console(cid)
69
con = self.consoles[cid]
70
if(con)
71
con.shutdown
72
self.consoles.delete(cid)
73
end
74
end
75
76
77
def write_console(id, buf)
78
self.consoles[id] ? self.consoles[id].write(buf) : nil
79
end
80
81
def read_console(id)
82
self.consoles[id] ? self.consoles[id].read() : nil
83
end
84
85
def clean_consoles(timeout=300)
86
self.consoles.each_pair do |id, con|
87
if (con.last_access + timeout < Time.now)
88
con.shutdown
89
self.consoles.delete(id)
90
end
91
end
92
end
93
94
def write_session(id, buf)
95
ses = self.framework.sessions[id]
96
return if not ses
97
return if not ses.user_input
98
ses.user_input.put(buf)
99
end
100
101
def read_session(id)
102
ses = self.framework.sessions[id]
103
return if not ses
104
return if not ses.user_output
105
ses.user_output.read_subscriber('session_reader')
106
end
107
108
# Detach the session from an existing input/output pair
109
def connect_session(id)
110
111
# Ignore invalid sessions
112
ses = self.framework.sessions[id]
113
return if not ses
114
115
# Has this session already been detached?
116
if (ses.user_output)
117
return if ses.user_output.has_subscriber?('session_reader')
118
end
119
120
# Create a new pipe
121
spipe = WebConsole::WebConsolePipe.new
122
spipe.input = spipe.pipe_input
123
124
# Create a read subscriber
125
spipe.create_subscriber('session_reader')
126
127
framework.threads.spawn("ConnectSessionInteraction", false) do
128
ses.interact(spipe.input, spipe)
129
end
130
end
131
132
def sessions
133
self.framework.sessions
134
end
135
136
#
137
# Stub
138
#
139
def run
140
true
141
end
142
143
protected
144
145
attr_accessor :opts # :nodoc:
146
147
#
148
# Initializes logging for the web interface
149
#
150
def initialize_logging
151
level = (opts['LogLevel'] || 0).to_i
152
153
Msf::Logging.enable_log_source(LogSource, level)
154
end
155
156
end
157
158
end
159
end
160
end
161
162
163