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/base/sessions/sql.rb
Views: 1904
1
# -*- coding: binary -*-
2
3
class Msf::Sessions::Sql
4
5
# This interface supports basic interaction.
6
include Msf::Session::Basic
7
include Msf::Sessions::Scriptable
8
9
# @return console The interactive console
10
attr_accessor :console
11
# @return client The underlying client object used to make SQL queries
12
attr_accessor :client
13
attr_accessor :platform, :arch
14
15
def process_autoruns(datastore)
16
['InitialAutoRunScript', 'AutoRunScript'].each do |key|
17
next if datastore[key].nil? || datastore[key].empty?
18
19
args = ::Shellwords.shellwords(datastore[key])
20
print_status("Session ID #{sid} (#{tunnel_to_s}) processing #{key} '#{datastore[key]}'")
21
execute_script(args.shift, *args)
22
end
23
end
24
25
def execute_file(full_path, args)
26
if File.extname(full_path) == '.rb'
27
Rex::Script::Shell.new(self, full_path).run(args)
28
else
29
console.load_resource(full_path)
30
end
31
end
32
33
# @param [String] cmd The command to execute in the context of a session using the '-c' flag.
34
# @param [IO] output_object The IO where output should be written to
35
# For example, 'query select version()' for a PostgreSQL session.
36
def run_cmd(cmd, output_object=nil)
37
# This implementation is taken from Meterpreter.
38
stored_output_state = nil
39
# If the user supplied an Output IO object, then we tell
40
# the console to use that, while saving it's previous output/
41
if output_object
42
stored_output_state = console.output
43
console.send(:output=, output_object)
44
end
45
success = console.run_single(cmd)
46
# If we stored the previous output object of the channel
47
# we restore it here to put everything back the way we found it
48
# We re-use the conditional above, because we expect in many cases for
49
# the stored state to actually be nil here.
50
if output_object
51
console.send(:output=, stored_output_state)
52
end
53
success
54
end
55
56
# @return [String]
57
def type
58
self.class.type
59
end
60
61
# @return [String] The type of the session
62
def self.type
63
raise ::NotImplementedError
64
end
65
66
# @return [Boolean] Can the session clean up after itself
67
def self.can_cleanup_files
68
raise ::NotImplementedError
69
end
70
71
# @return [String] The session description
72
def desc
73
raise ::NotImplementedError
74
end
75
76
# @return [String] The peer address
77
def address
78
client.peerhost
79
end
80
81
# @return [Integer] The peer port
82
def port
83
client.peerport
84
end
85
86
# Initializes the console's I/O handles.
87
#
88
# @param [Object] input
89
# @param [Object] output
90
# @return [String]
91
def init_ui(input, output)
92
super(input, output)
93
94
console.init_ui(input, output)
95
console.set_log_source(log_source)
96
end
97
98
# Resets the console's I/O handles.
99
#
100
# @return [Object]
101
def reset_ui
102
console.unset_log_source
103
console.reset_ui
104
end
105
106
# Exit the console
107
#
108
# @return [TrueClass]
109
def exit
110
console.stop
111
end
112
113
protected
114
115
# Override the basic session interaction to use shell_read and
116
# shell_write instead of operating on rstream directly.
117
#
118
# @return [Object]
119
def _interact
120
framework.events.on_session_interact(self)
121
framework.history_manager.with_context(name: type.to_sym) { _interact_stream }
122
end
123
124
# @return [Object]
125
def _interact_stream
126
framework.events.on_session_interact(self)
127
128
console.framework = framework
129
# Call the console interaction of the mysql client and
130
# pass it a block that returns whether or not we should still be
131
# interacting. This will allow the shell to abort if interaction is
132
# canceled.
133
console.interact { interacting != true }
134
console.framework = nil
135
136
# If the stop flag has been set, then that means the user exited. Raise
137
# the EOFError so we can drop this handle like a bad habit.
138
raise ::EOFError if (console.stopped? == true)
139
end
140
end
141
142