Skip to content

Commit aaddc9d

Browse files
committed
don't rely on side-effects in db util function
1 parent 7386f66 commit aaddc9d

File tree

17 files changed

+76
-25
lines changed

17 files changed

+76
-25
lines changed

lib/msf/core/db_manager/cred.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def creds(opts)
99
end
1010

1111
wspace = Msf::Util::DBManager.process_opts_workspace(opts, framework)
12-
search_term = opts.delete(:search_term)
12+
search_term = opts[:search_term]
1313

1414
query = Metasploit::Credential::Core.where( workspace_id: wspace.id )
1515
query = query.includes(:private, :public, :logins, :realm).references(:private, :public, :logins, :realm)
@@ -108,24 +108,24 @@ def report_auth_info(opts={})
108108
end
109109

110110
::ActiveRecord::Base.connection_pool.with_connection {
111-
host = opts.delete(:host)
112-
ptype = opts.delete(:type) || "password"
113-
token = [opts.delete(:user), opts.delete(:pass)]
114-
sname = opts.delete(:sname)
115-
port = opts.delete(:port)
116-
proto = opts.delete(:proto) || "tcp"
117-
proof = opts.delete(:proof)
118-
source_id = opts.delete(:source_id)
119-
source_type = opts.delete(:source_type)
120-
duplicate_ok = opts.delete(:duplicate_ok)
111+
host = opts[:host]
112+
ptype = opts[:type] || "password"
113+
token = [opts[:user], opts[:pass]]
114+
sname = opts[:sname]
115+
port = opts[:port]
116+
proto = opts[:proto] || "tcp"
117+
proof = opts[:proof]
118+
source_id = opts[:source_id]
119+
source_type = opts[:source_type]
120+
duplicate_ok = opts[:duplicate_ok]
121121
# Nil is true for active.
122122
active = (opts[:active] || opts[:active].nil?) ? true : false
123123

124124
wspace = Msf::Util::DBManager.process_opts_workspace(opts, framework)
125125

126126
# Service management; assume the user knows what
127127
# he's talking about.
128-
service = opts.delete(:service) || report_service(:host => host, :port => port, :proto => proto, :name => sname, :workspace => wspace)
128+
service = opts[:service] || report_service(:host => host, :port => port, :proto => proto, :name => sname, :workspace => wspace)
129129

130130
# Non-US-ASCII usernames are tripping up the database at the moment, this is a temporary fix until we update the tables
131131
if (token[0])
@@ -226,6 +226,8 @@ def update_credential(opts)
226226
::ActiveRecord::Base.connection_pool.with_connection {
227227
# process workspace string for update if included in opts
228228
wspace = Msf::Util::DBManager.process_opts_workspace(opts, framework, false)
229+
opts = opts.clone()
230+
opts.delete(:workspace)
229231
opts[:workspace] = wspace if wspace
230232

231233
if opts[:public]

lib/msf/core/db_manager/event.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def events(opts)
2727
end
2828

2929
wspace = Msf::Util::DBManager.process_opts_workspace(opts, framework)
30+
opts = opts.clone()
31+
opts.delete(:workspace)
3032

3133
order = opts.delete(:order)
3234
order = order.nil? ? DEFAULT_ORDER : order.to_sym
@@ -52,6 +54,9 @@ def report_event(opts)
5254
::ActiveRecord::Base.connection_pool.with_connection {
5355
wspace = Msf::Util::DBManager.process_opts_workspace(opts, framework)
5456
return if not wspace # Temp fix?
57+
58+
opts = opts.clone()
59+
opts.delete(:workspace)
5560
uname = opts.delete(:username)
5661

5762
if !opts[:host].nil? && !opts[:host].kind_of?(::Mdm::Host)
@@ -61,4 +66,4 @@ def report_event(opts)
6166
::Mdm::Event.create(opts.merge(:workspace_id => wspace[:id], :username => uname))
6267
}
6368
end
64-
end
69+
end

lib/msf/core/db_manager/exploit_attempt.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ def report_exploit_failure(opts)
5858
# Bail if we dont have a host object
5959
return if not host
6060

61-
opts = opts.dup
61+
opts = opts.clone()
62+
opts.delete(:workspace)
6263
opts[:service] = svc
6364
opts[:host] = host
6465

@@ -74,14 +75,15 @@ def report_exploit_success(opts)
7475
host = opts[:host] || return
7576

7677
wspace = Msf::Util::DBManager.process_opts_workspace(opts, framework)
78+
# it is rude to modify arguments in place
79+
opts = opts.clone()
80+
opts.delete(:workspace) # does this really need to be removed?
7781
port = opts[:port]
7882
prot = opts[:proto] || Msf::DBManager::DEFAULT_SERVICE_PROTO
7983
svc = opts[:service]
8084

8185
# Look up or generate the service as appropriate
8286
if port and svc.nil?
83-
# it is rude to modify arguments in place
84-
opts = opts.dup
8587
opts[:proto] ||= Msf::DBManager::DEFAULT_SERVICE_PROTO
8688
opts[:service] = report_service(
8789
workspace: wspace, host: host, port: port, proto: prot

lib/msf/core/db_manager/host.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ def report_host(opts)
188188

189189
::ActiveRecord::Base.connection_pool.with_connection {
190190
wspace = Msf::Util::DBManager.process_opts_workspace(opts, framework)
191+
opts = opts.clone
192+
opts.delete(:workspace)
191193

192194
begin
193195
retry_attempts ||= 0
@@ -280,6 +282,7 @@ def update_host(opts)
280282
::ActiveRecord::Base.connection_pool.with_connection {
281283
# process workspace string for update if included in opts
282284
wspace = Msf::Util::DBManager.process_opts_workspace(opts, framework, false)
285+
opts = opts.clone()
283286
opts[:workspace] = wspace if wspace
284287

285288
id = opts.delete(:id)

lib/msf/core/db_manager/host_tag.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ def report_host_tag(opts)
1414

1515

1616
host = get_host(:workspace => wspace, :address => addr)
17-
desc = opts.delete(:desc)
18-
summary = opts.delete(:summary)
19-
detail = opts.delete(:detail)
20-
crit = opts.delete(:crit)
17+
desc = opts[:desc]
18+
summary = opts[:summary]
19+
detail = opts[:detail]
20+
crit = opts[:crit]
2121
possible_tags = Mdm::Tag.includes(:hosts).where("hosts.workspace_id = ? and tags.name = ?", wspace.id, name).order("tags.id DESC").limit(1)
2222
tag = (possible_tags.blank? ? Mdm::Tag.new : possible_tags.first)
2323
tag.name = name

lib/msf/core/db_manager/import.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ def import(args={}, &block)
9494
data = args[:data] || args['data']
9595
ftype = import_filetype_detect(data)
9696
yield(:filetype, @import_filedata[:type]) if block
97-
self.send "import_#{ftype}".to_sym, args.merge(workspace: wspace.name), &block
97+
# this code looks to intentionally convert workspace to a string, why?
98+
opts = args.clone()
99+
opts.delete(:workspace)
100+
self.send "import_#{ftype}".to_sym, opts.merge(workspace: wspace.name), &block
98101
# post process the import here for missing default port maps
99102
mrefs, mports, _mservs = Msf::Modules::Metadata::Cache.instance.all_remote_exploit_maps
100103
# the map build above is a little expensive, another option is to do
@@ -209,10 +212,13 @@ def import_file(args={}, &block)
209212
# Override REXML's expansion text limit to 50k (default: 10240 bytes)
210213
REXML::Security.entity_expansion_text_limit = 51200
211214

215+
# this code looks to intentionally convert workspace to a string, why?
216+
opts = args.clone()
217+
opts.delete(:workspace)
212218
if block
213-
import(args.merge(data: data, workspace: wspace.name)) { |type,data| yield type,data }
219+
import(opts.merge(data: data, workspace: wspace.name)) { |type,data| yield type,data }
214220
else
215-
import(args.merge(data: data, workspace: wspace.name))
221+
import(opts.merge(data: data, workspace: wspace.name))
216222
end
217223
end
218224

lib/msf/core/db_manager/import/metasploit_framework/xml.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ def import_msf_web_vuln_element(element, options={}, &notifier)
229229
def import_msf_xml(args={}, &block)
230230
data = args[:data]
231231
wspace = Msf::Util::DBManager.process_opts_workspace(args, framework).name
232+
args = args.clone()
233+
args.delete(:workspace)
232234
bl = validate_ips(args[:blacklist]) ? args[:blacklist].split : []
233235

234236
doc = Nokogiri::XML::Reader.from_memory(data)

lib/msf/core/db_manager/import/metasploit_framework/zip.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module Msf::DBManager::Import::MetasploitFramework::Zip
44
def import_msf_collateral(args={}, &block)
55
data = ::File.open(args[:filename], "rb") {|f| f.read(f.stat.size)}
66
wspace = Msf::Util::DBManager.process_opts_workspace(args, framework).name
7+
args = args.clone()
8+
args.detele(:workspace)
79
bl = validate_ips(args[:blacklist]) ? args[:blacklist].split : []
810
basedir = args[:basedir] || args['basedir'] || ::File.join(Msf::Config.data_directory, "msf")
911

lib/msf/core/db_manager/login.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def logins(opts)
88
def update_login(opts)
99
::ActiveRecord::Base.connection_pool.with_connection {
1010
wspace = Msf::Util::DBManager.process_opts_workspace(opts, framework, false)
11+
opts = opts.clone()
1112
opts[:workspace] = wspace if wspace
1213
id = opts.delete(:id)
1314
login = Metasploit::Credential::Login.find(id)

lib/msf/core/db_manager/loot.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def loots(opts)
2323
data = opts.delete(:data)
2424

2525
wspace = Msf::Util::DBManager.process_opts_workspace(opts, framework)
26+
opts = opts.clone()
27+
opts.delete(:workspace)
2628
opts[:workspace_id] = wspace.id
2729

2830
if search_term && !search_term.empty?
@@ -46,6 +48,8 @@ def report_loot(opts)
4648
return if not active
4749
::ActiveRecord::Base.connection_pool.with_connection {
4850
wspace = Msf::Util::DBManager.process_opts_workspace(opts, framework)
51+
opts = opts.clone()
52+
opts.delete(:workspace)
4953
path = opts.delete(:path) || (raise RuntimeError, "A loot :path is required")
5054

5155
host = nil
@@ -101,6 +105,8 @@ def update_loot(opts)
101105
wspace = Msf::Util::DBManager.process_opts_workspace(opts, framework, false)
102106
# Prevent changing the data field to ensure the file contents remain the same as what was originally looted.
103107
raise ArgumentError, "Updating the data attribute is not permitted." if opts[:data]
108+
opts = opts.clone()
109+
opts.delete(:workspace)
104110
opts[:workspace] = wspace if wspace
105111

106112
id = opts.delete(:id)

0 commit comments

Comments
 (0)