Skip to content

Commit f2504b8

Browse files
author
Brent Cook
committed
use the same logic with 'get_note' and 'del_note' for selecting notes
factor out the selector from 'get_note' and use it in both places
1 parent 29b97f4 commit f2504b8

File tree

1 file changed

+41
-84
lines changed

1 file changed

+41
-84
lines changed

lib/msf/core/rpc/v10/rpc_db.rb

Lines changed: 41 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,42 @@ def init_db_opts_workspace(xopts)
104104
return opts, opts[:workspace]
105105
end
106106

107+
def get_notes(xopts)
108+
::ActiveRecord::Base.connection_pool.with_connection {
109+
opts, wspace = init_db_opts_workspace(xopts)
110+
111+
ret = {}
112+
ret[:note] = []
113+
114+
host = self.framework.db.get_host(opts)
115+
116+
return ret if not host
117+
notes = []
118+
if opts[:proto] && opts[:port]
119+
services = []
120+
nret = host.services.find_by_proto_and_port(opts[:proto], opts[:port])
121+
return ret if nret == nil
122+
services << nret if nret.class == ::Mdm::Service
123+
services |= nret if nret.class == Array
124+
125+
services.each do |s|
126+
nret = nil
127+
if opts[:ntype]
128+
nret = s.notes.find_by_ntype(opts[:ntype])
129+
else
130+
nret = s.notes
131+
end
132+
next if nret == nil
133+
notes << nret if nret.class == ::Mdm::Note
134+
notes |= nret if nret.class == Array
135+
end
136+
else
137+
notes = host.notes
138+
end
139+
notes
140+
}
141+
end
142+
107143
public
108144

109145

@@ -769,10 +805,12 @@ def rpc_get_service(xopts)
769805
}
770806
end
771807

772-
773808
# Returns a note.
774809
#
775810
# @param [Hash] xopts Options.
811+
# @option xopts [String] :addr Host address.
812+
# @option xopts [String] :address Same as :addr.
813+
# @option xopts [String] :host Same as :address.
776814
# @option xopts [String] :proto Protocol.
777815
# @option xopts [Fixnum] :port Port.
778816
# @option xopts [String] :ntype Note type.
@@ -794,37 +832,8 @@ def rpc_get_service(xopts)
794832
# @example Here's how you would use this from the client:
795833
# rpc.call('db.get_note', {:proto => 'tcp', :port => 80})
796834
def rpc_get_note(xopts)
797-
::ActiveRecord::Base.connection_pool.with_connection {
798-
opts, wspace = init_db_opts_workspace(xopts)
835+
notes = get_notes(xopts)
799836

800-
ret = {}
801-
ret[:note] = []
802-
803-
host = self.framework.db.get_host(opts)
804-
805-
return ret if( not host)
806-
notes = []
807-
if(opts[:proto] && opts[:port])
808-
services = []
809-
nret = host.services.find_by_proto_and_port(opts[:proto], opts[:port])
810-
return ret if nret == nil
811-
services << nret if nret.class == ::Mdm::Service
812-
services |= nret if nret.class == Array
813-
814-
services.each do |s|
815-
nret = nil
816-
if opts[:ntype]
817-
nret = s.notes.find_by_ntype(opts[:ntype])
818-
else
819-
nret = s.notes
820-
end
821-
next if nret == nil
822-
notes << nret if nret.class == ::Mdm::Note
823-
notes |= nret if nret.class == Array
824-
end
825-
else
826-
notes = host.notes
827-
end
828837
notes.each do |n|
829838
note = {}
830839
host = n.host
@@ -842,7 +851,6 @@ def rpc_get_note(xopts)
842851
ret[:note] << note
843852
end
844853
ret
845-
}
846854
end
847855

848856

@@ -1144,58 +1152,8 @@ def rpc_del_vuln(xopts)
11441152
# @example Here's how you would use this from the client:
11451153
# rpc.call('db.del_note', {:workspace=>'default', :host=>ip, :port=>443, :proto=>'tcp'})
11461154
def rpc_del_note(xopts)
1147-
::ActiveRecord::Base.connection_pool.with_connection {
1148-
opts, wspace = init_db_opts_workspace(xopts)
1149-
hosts = []
1150-
services = []
1151-
notes = []
1152-
1153-
if opts[:host] or opts[:address] or opts[:addresses]
1154-
hosts = opts_to_hosts(xopts)
1155-
end
1156-
1157-
if opts[:port] or opts[:proto]
1158-
if opts[:host] or opts[:address] or opts[:addresses]
1159-
services = opts_to_services(hosts,opts)
1160-
else
1161-
services = opts_to_services([],opts)
1162-
end
1163-
end
1155+
notes = get_notes(xopts)
11641156

1165-
if opts[:port] or opts[:proto]
1166-
services.each do |s|
1167-
nret = nil
1168-
if opts[:ntype]
1169-
nret = s.notes.find_by_ntype(opts[:ntype])
1170-
else
1171-
nret = s.notes
1172-
end
1173-
next if nret == nil
1174-
notes << nret if nret.class == ::Mdm::Note
1175-
notes |= nret if nret.class == Array
1176-
end
1177-
elsif opts[:address] or opts[:host] or opts[:addresses]
1178-
hosts.each do |h|
1179-
nret = nil
1180-
if opts[:ntype]
1181-
nret = h.notes.find_by_ntype(opts[:ntype])
1182-
else
1183-
nret = h.notes
1184-
end
1185-
next if nret == nil
1186-
notes << nret if nret.class == ::Mdm::Note
1187-
notes |= nret if nret.class == Array
1188-
end
1189-
else
1190-
nret = nil
1191-
if opts[:ntype]
1192-
nret = wspace.notes.find_by_ntype(opts[:ntype])
1193-
else
1194-
nret = wspace.notes
1195-
end
1196-
notes << nret if nret.class == ::Mdm::Note
1197-
notes |= nret if nret.class == Array
1198-
end
11991157
deleted = []
12001158
notes.each do |n|
12011159
dent = {}
@@ -1208,7 +1166,6 @@ def rpc_del_note(xopts)
12081166
end
12091167

12101168
return { :result => 'success', :deleted => deleted }
1211-
}
12121169
end
12131170

12141171

0 commit comments

Comments
 (0)