Skip to content

Commit 0dbfc9d

Browse files
committed
WIP: Drop session objects before JSON conversion
The session object is not intended to be store in the DB. There are a ton of subobjects and unneeded data that causes the JSON conversion to hang or fail with 'stack level too deep' errors.
1 parent 08b62db commit 0dbfc9d

File tree

7 files changed

+33
-14
lines changed

7 files changed

+33
-14
lines changed

lib/metasploit/framework/data_service/proxy/session_data_proxy.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ def report_session(opts)
55
data_service.report_session(opts)
66
rescue Exception => e
77
puts"Call to #{data_service.class}#report_session threw exception: #{e.message}"
8+
puts e.backtrace.each { |line| puts "#{line}\n" }
89
end
910
end
10-
end
11+
end
12+
13+
14+
15+

lib/metasploit/framework/data_service/remote/http/core.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def post_data(path, data_hash)
4545
begin
4646
raise 'Data to post to remote service cannot be null or empty' if (data_hash.nil? || data_hash.empty?)
4747

48+
puts "#{Time.now} - Posting #{data_hash} to #{path}"
4849
client = @client_pool.pop()
4950
request_opts = build_request_opts(POST_REQUEST, data_hash, path)
5051
request = client.request_raw(request_opts)
@@ -77,6 +78,8 @@ def post_data(path, data_hash)
7778
#
7879
def get_data(path, data_hash = nil)
7980
begin
81+
82+
puts "#{Time.now} - Getting #{path} with #{data_hash ? data_hash : "nil"}"
8083
client = @client_pool.pop()
8184
request_opts = build_request_opts(GET_REQUEST, data_hash, path)
8285
request = client.request_raw(request_opts)
@@ -192,11 +195,11 @@ def append_workspace(data_hash)
192195
end
193196

194197
if (workspace && (workspace.is_a?(OpenStruct) || workspace.is_a?(::Mdm::Workspace)))
195-
data_hash['workspace'] = workspace.name
198+
data_hash[:workspace] = workspace.name
196199
end
197200

198201
if (workspace.nil?)
199-
data_hash['workspace'] = current_workspace_name
202+
data_hash[:workspace] = current_workspace_name
200203
end
201204

202205
data_hash
@@ -206,9 +209,19 @@ def build_request_opts(request_type, data_hash, path)
206209
request_opts = {
207210
'method' => request_type,
208211
'ctype' => 'application/json',
209-
'uri' => path}
212+
'uri' => path
213+
}
210214

211215
if (!data_hash.nil? && !data_hash.empty?)
216+
data_hash.each do |k,v|
217+
if v.is_a?(Msf::Session)
218+
puts "#{Time.now} - DEBUG: Dropping Msf::Session object before converting to JSON."
219+
puts "data_hash is #{data_hash}"
220+
puts "Callstack:"
221+
caller.each { |line| puts "#{line}\n"}
222+
data_hash.delete(k)
223+
end
224+
end
212225
json_body = append_workspace(data_hash).to_json
213226
request_opts['data'] = json_body
214227
end

lib/metasploit/framework/data_service/remote/http/remote_session_data_service.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def parse_host_opts(msf_session)
4343
def parse_session_data(msf_session)
4444
hash = Hash.new()
4545
# TODO: what to do with this shiz
46-
# hash[:datastore] = msf_session.exploit_datastore.to_h
46+
hash[:datastore] = msf_session.exploit_datastore.to_h
4747
hash[:desc] = msf_session.info
4848
hash[:local_id] = msf_session.sid
4949
hash[:platform] = msf_session.session_type
@@ -58,7 +58,7 @@ def parse_host_opts(msf_session)
5858
hash = Hash.new()
5959
hash[:host] = msf_session.session_host
6060
hash[:arch] = msf_session.arch if msf_session.respond_to?(:arch) and msf_session.arch
61-
hash[:workspace] = msf_session[:workspace] || msf_session.workspace
61+
hash[:workspace] = msf_session.workspace || msf_session[:workspace]
6262
return hash
6363
end
6464

lib/metasploit/framework/data_service/remote/http/remote_session_event_data_service.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ module RemoteSessionEventDataService
44
include ResponseDataHelper
55

66
SESSION_EVENT_PATH = '/api/1/msf/session_event'
7-
SESSION_EVENT_SEARCH_PATH = SESSION_EVENT_PATH + "/search"
87

98
def session_events(opts = {})
109
json_to_open_struct_object(self.get_data(SESSION_EVENT_PATH, opts), [])

lib/msf/base/sessions/meterpreter.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,14 +497,17 @@ def load_session_info
497497
end
498498
end
499499

500+
sysinfo = sys.config.sysinfo
501+
host = Msf::Util::Host.normalize_host(self)
502+
500503
framework.db.report_note({
501504
:type => "host.os.session_fingerprint",
502-
:host => self,
505+
:host => host,
503506
:workspace => wspace,
504507
:data => {
505-
:name => sys.config.sysinfo["Computer"],
506-
:os => sys.config.sysinfo["OS"],
507-
:arch => sys.config.sysinfo["Architecture"],
508+
:name => sysinfo["Computer"],
509+
:os => sysinfo["OS"],
510+
:arch => sysinfo["Architecture"],
508511
}
509512
})
510513

lib/msf/core/db_manager/http/servlet/loot_servlet.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def self.report_loot
5555
ext = "txt"
5656
end
5757
# This method is available even if there is no database, don't bother checking
58-
host = Msf::Util::Host.normalize_host(host)
58+
host = Msf::Util::Host.normalize_host(opts[:host])
5959

6060
ws = (opts[:workspace] ? opts[:workspace] : 'default')
6161
name =

lib/msf/core/db_manager/session.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ def report_session_dto(session_dto)
120120

121121
::ActiveRecord::Base.connection_pool.with_connection {
122122
workspace = find_workspace(session_dto[:workspace])
123-
124123
host_data = session_dto[:host_data]
125124
h_opts = {}
126125
h_opts[:host] = host_data[:host]
@@ -144,7 +143,7 @@ def report_session_dto(session_dto)
144143
via_payload: session_data[:via_payload],
145144
}
146145

147-
if sess_data[:via_exploit] == 'exploit/multi/handler' and sess_data[:datastore]['ParentModule']
146+
if sess_data[:via_exploit] == 'exploit/multi/handler' and sess_data[:datastore] and sess_data[:datastore]['ParentModule']
148147
sess_data[:via_exploit] = sess_data[:datastore]['ParentModule']
149148
end
150149

0 commit comments

Comments
 (0)