Skip to content

Commit 4af5c54

Browse files
authored
Land rapid7#18830, SQL sessions consolidation
2 parents c5eb4eb + 085071d commit 4af5c54

File tree

34 files changed

+735
-1402
lines changed

34 files changed

+735
-1402
lines changed

lib/msf/base/sessions/mssql.rb

Lines changed: 6 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
22

33
require 'rex/post/mssql'
44

5-
class Msf::Sessions::MSSQL
5+
class Msf::Sessions::MSSQL < Msf::Sessions::Sql
66

7-
include Msf::Session::Basic
8-
include Msf::Sessions::Scriptable
9-
10-
# @return [Rex::Post::MSSQL::Ui::Console] The interactive console
11-
attr_accessor :console
12-
# @return [MSSQL::Client] The MSSQL client
13-
attr_accessor :client
14-
attr_accessor :platform, :arch
7+
# @return [String] The address MSSQL is running on
8+
attr_accessor :address
9+
# @return [Integer] The port MSSQL is running on
10+
attr_accessor :port
1511
attr_reader :framework
1612

1713
def initialize(rstream, opts = {})
1814
@client = opts.fetch(:client)
19-
self.console = Rex::Post::MSSQL::Ui::Console.new(self, opts)
15+
self.console = ::Rex::Post::MSSQL::Ui::Console.new(self, opts)
2016

2117
super(rstream, opts)
2218
end
@@ -28,28 +24,6 @@ def bootstrap(datastore = {}, handler = nil)
2824
@info = "MSSQL #{datastore['USERNAME']} @ #{@peer_info}"
2925
end
3026

31-
def execute_file(full_path, args)
32-
if File.extname(full_path) == '.rb'
33-
Rex::Script::Shell.new(self, full_path).run(args)
34-
else
35-
console.load_resource(full_path)
36-
end
37-
end
38-
39-
def process_autoruns(datastore)
40-
['InitialAutoRunScript', 'AutoRunScript'].each do |key|
41-
next if datastore[key].nil? || datastore[key].empty?
42-
43-
args = Shellwords.shellwords(datastore[key])
44-
print_status("Session ID #{self.sid} (#{self.tunnel_to_s}) processing #{key} '#{datastore[key]}'")
45-
self.execute_script(args.shift, *args)
46-
end
47-
end
48-
49-
def type
50-
self.class.type
51-
end
52-
5327
# Returns the type of session.
5428
#
5529
def self.type
@@ -80,64 +54,4 @@ def port
8054
@address, @port = client.sock.peerinfo.split(':')
8155
@port
8256
end
83-
84-
##
85-
# :category: Msf::Session::Interactive implementors
86-
#
87-
# Initializes the console's I/O handles.
88-
#
89-
def init_ui(input, output)
90-
self.user_input = input
91-
self.user_output = output
92-
console.init_ui(input, output)
93-
console.set_log_source(log_source)
94-
95-
super
96-
end
97-
98-
##
99-
# :category: Msf::Session::Interactive implementors
100-
#
101-
# Resets the console's I/O handles.
102-
#
103-
def reset_ui
104-
console.unset_log_source
105-
console.reset_ui
106-
end
107-
108-
def exit
109-
console.stop
110-
end
111-
112-
##
113-
# :category: Msf::Session::Interactive implementors
114-
#
115-
# Override the basic session interaction to use shell_read and
116-
# shell_write instead of operating on rstream directly.
117-
def _interact
118-
framework.events.on_session_interact(self)
119-
framework.history_manager.with_context(name: type.to_sym) do
120-
_interact_stream
121-
end
122-
end
123-
124-
##
125-
# :category: Msf::Session::Interactive implementors
126-
#
127-
def _interact_stream
128-
framework.events.on_session_interact(self)
129-
130-
console.framework = framework
131-
# Call the console interaction of the MSSQL client and
132-
# pass it a block that returns whether or not we should still be
133-
# interacting. This will allow the shell to abort if interaction is
134-
# canceled.
135-
console.interact { interacting != true }
136-
console.framework = nil
137-
138-
# If the stop flag has been set, then that means the user exited. Raise
139-
# the EOFError so we can drop this handle like a bad habit.
140-
raise EOFError if (console.stopped? == true)
141-
end
142-
14357
end

lib/msf/base/sessions/mysql.rb

Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,7 @@
22

33
require 'rex/post/mysql'
44

5-
class Msf::Sessions::MySQL
6-
7-
# This interface supports basic interaction.
8-
include Msf::Session::Basic
9-
include Msf::Sessions::Scriptable
10-
11-
# @return [Rex::Post::MySQL::Ui::Console] The interactive console
12-
attr_accessor :console
13-
# @return [MySQL::Client]
14-
attr_accessor :client
15-
attr_accessor :platform, :arch
5+
class Msf::Sessions::MySQL < Msf::Sessions::Sql
166

177
# @param[Rex::IO::Stream] rstream
188
# @param [Hash] opts
@@ -32,21 +22,6 @@ def bootstrap(datastore = {}, handler = nil)
3222
@info = "MySQL #{datastore['USERNAME']} @ #{client.socket.peerinfo}"
3323
end
3424

35-
def process_autoruns(datastore)
36-
['InitialAutoRunScript', 'AutoRunScript'].each do |key|
37-
next if datastore[key].nil? || datastore[key].empty?
38-
39-
args = Shellwords.shellwords(datastore[key])
40-
print_status("Session ID #{session.sid} (#{session.tunnel_to_s}) processing #{key} '#{datastore[key]}'")
41-
execute_script(args.shift, *args)
42-
end
43-
end
44-
45-
# @return [String]
46-
def type
47-
self.class.type
48-
end
49-
5025
# @return [String] The type of the session
5126
def self.type
5227
'mysql'
@@ -77,60 +52,4 @@ def port
7752
@address, @port = @client.socket.peerinfo.split(':')
7853
@port
7954
end
80-
81-
# Initializes the console's I/O handles.
82-
#
83-
# @param [Object] input
84-
# @param [Object] output
85-
# @return [String]
86-
def init_ui(input, output)
87-
super(input, output)
88-
89-
console.init_ui(input, output)
90-
console.set_log_source(log_source)
91-
end
92-
93-
# Resets the console's I/O handles.
94-
#
95-
# @return [Object]
96-
def reset_ui
97-
console.unset_log_source
98-
console.reset_ui
99-
end
100-
101-
102-
# Exit the console
103-
#
104-
# @return [TrueClass]
105-
def exit
106-
console.stop
107-
end
108-
109-
protected
110-
111-
# Override the basic session interaction to use shell_read and
112-
# shell_write instead of operating on rstream directly.
113-
#
114-
# @return [Object]
115-
def _interact
116-
framework.events.on_session_interact(self)
117-
framework.history_manager.with_context(name: type.to_sym) { _interact_stream }
118-
end
119-
120-
# @return [Object]
121-
def _interact_stream
122-
framework.events.on_session_interact(self)
123-
124-
console.framework = framework
125-
# Call the console interaction of the mysql client and
126-
# pass it a block that returns whether or not we should still be
127-
# interacting. This will allow the shell to abort if interaction is
128-
# canceled.
129-
console.interact { interacting != true }
130-
console.framework = nil
131-
132-
# If the stop flag has been set, then that means the user exited. Raise
133-
# the EOFError so we can drop this handle like a bad habit.
134-
raise ::EOFError if (console.stopped? == true)
135-
end
13655
end

lib/msf/base/sessions/postgresql.rb

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,7 @@
22

33
require 'rex/post/postgresql'
44

5-
class Msf::Sessions::PostgreSQL
6-
#
7-
# This interface supports basic interaction.
8-
#
9-
include Msf::Session::Basic
10-
include Msf::Sessions::Scriptable
11-
12-
# @return [Rex::Post::PostgreSQL::Ui::Console] The interactive console
13-
attr_accessor :console
14-
# @return [PostgreSQL::Client]
15-
attr_accessor :client
16-
attr_accessor :platform, :arch
5+
class Msf::Sessions::PostgreSQL < Msf::Sessions::Sql
176

187
# @param[Rex::IO::Stream] rstream
198
# @param [Hash] opts
@@ -31,28 +20,6 @@ def bootstrap(datastore = {}, handler = nil)
3120
@info = "PostgreSQL #{datastore['USERNAME']} @ #{@peer_info}"
3221
end
3322

34-
def execute_file(full_path, args)
35-
if File.extname(full_path) == '.rb'
36-
Rex::Script::Shell.new(self, full_path).run(args)
37-
else
38-
console.load_resource(full_path)
39-
end
40-
end
41-
42-
def process_autoruns(datastore)
43-
['InitialAutoRunScript', 'AutoRunScript'].each do |key|
44-
next if datastore[key].nil? || datastore[key].empty?
45-
46-
args = Shellwords.shellwords(datastore[key])
47-
print_status("Session ID #{self.sid} (#{self.tunnel_to_s}) processing #{key} '#{datastore[key]}'")
48-
self.execute_script(args.shift, *args)
49-
end
50-
end
51-
52-
def type
53-
self.class.type
54-
end
55-
5623
#
5724
# @return [String] The type of the session
5825
#
@@ -86,62 +53,4 @@ def port
8653
@address, @port = @client.conn.peerinfo.split(':')
8754
@port
8855
end
89-
90-
##
91-
# :category: Msf::Session::Interactive implementors
92-
#
93-
# Initializes the console's I/O handles.
94-
#
95-
def init_ui(input, output)
96-
super(input, output)
97-
98-
console.init_ui(input, output)
99-
console.set_log_source(self.log_source)
100-
end
101-
102-
##
103-
# :category: Msf::Session::Interactive implementors
104-
#
105-
# Resets the console's I/O handles.
106-
#
107-
def reset_ui
108-
console.unset_log_source
109-
console.reset_ui
110-
end
111-
112-
def exit
113-
console.stop
114-
end
115-
116-
protected
117-
118-
##
119-
# :category: Msf::Session::Interactive implementors
120-
#
121-
# Override the basic session interaction to use shell_read and
122-
# shell_write instead of operating on rstream directly.
123-
def _interact
124-
framework.events.on_session_interact(self)
125-
framework.history_manager.with_context(name: type.to_sym) { _interact_stream }
126-
end
127-
128-
##
129-
# :category: Msf::Session::Interactive implementors
130-
#
131-
def _interact_stream
132-
framework.events.on_session_interact(self)
133-
134-
console.framework = framework
135-
136-
# Call the console interaction of the PostgreSQL client and
137-
# pass it a block that returns whether or not we should still be
138-
# interacting. This will allow the shell to abort if interaction is
139-
# canceled.
140-
console.interact { interacting != true }
141-
console.framework = nil
142-
143-
# If the stop flag has been set, then that means the user exited. Raise
144-
# the EOFError so we can drop this handle like a bad habit.
145-
raise ::EOFError if (console.stopped? == true)
146-
end
14756
end

0 commit comments

Comments
 (0)