Module: Msf::DBManager::ModuleCache
- Included in:
- Msf::DBManager
- Defined in:
- lib/msf/core/db_manager/module_cache.rb
Instance Attribute Summary collapse
-
#modules_cached ⇒ Object
Flag to indicate that modules are cached.
-
#modules_caching ⇒ Object
Flag to indicate that the module cacher is running.
Instance Method Summary collapse
-
#match_values(values) ⇒ Array<String>
Wraps values in ‘%’ for Arel::Prediciation#matches_any and other match* methods that map to SQL ‘LIKE’ or ‘ILIKE’.
- #module_to_details_hash(m) ⇒ Object
-
#purge_all_module_details ⇒ void
Destroys all Mdm::Module::Details in the database.
-
#remove_module_details(mtype, refname) ⇒ void
Destroys Mdm::Module::Detail if one exists for the given Mdm::Module::Detail#mtype and Mdm::Module::Detail#refname.
-
#search_modules(search_string) ⇒ [], ActiveRecord::Relation
This provides a standard set of search filters for every module.
-
#update_all_module_details ⇒ void
Destroys the old Mdm::Module::Detail and creates a new Mdm::Module::Detail for any module with an Mdm::Module::Detail where the modification time of the Mdm::Module::Detail#file differs from the Mdm::Module::Detail#mtime.
-
#update_module_details(module_instance) ⇒ void
Creates an Mdm::Module::Detail from a module instance.
Instance Attribute Details
#modules_cached ⇒ Object
Flag to indicate that modules are cached
13 14 15 |
# File 'lib/msf/core/db_manager/module_cache.rb', line 13 def modules_cached @modules_cached end |
#modules_caching ⇒ Object
Flag to indicate that the module cacher is running
16 17 18 |
# File 'lib/msf/core/db_manager/module_cache.rb', line 16 def modules_caching @modules_caching end |
Instance Method Details
#match_values(values) ⇒ Array<String>
Wraps values in ‘%’ for Arel::Prediciation#matches_any and other match* methods that map to SQL ‘LIKE’ or ‘ILIKE’
27 28 29 |
# File 'lib/msf/core/db_manager/module_cache.rb', line 27 def match_values(values) values.collect { |value| "%#{value}%" } end |
#module_to_details_hash(m) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/msf/core/db_manager/module_cache.rb', line 31 def module_to_details_hash(m) res = {} bits = [] res[:mtime] = ::File.mtime(m.file_path) rescue Time.now res[:file] = m.file_path res[:mtype] = m.type res[:name] = m.name.to_s res[:refname] = m.refname res[:fullname] = m.fullname res[:rank] = m.rank.to_i res[:license] = m.license.to_s res[:description] = m.description.to_s.strip m.arch.map{ |x| bits << [ :arch, { :name => x.to_s } ] } m.platform.platforms.map{ |x| bits << [ :platform, { :name => x.to_s.split('::').last.downcase } ] } m..map{|x| bits << [ :author, { :name => x.to_s } ] } m.references.map do |r| bits << [ :ref, { :name => [r.ctx_id.to_s, r.ctx_val.to_s].join("-") } ] end res[:privileged] = m.privileged? if m.disclosure_date begin res[:disclosure_date] = m.disclosure_date.to_datetime.to_time rescue ::Exception res.delete(:disclosure_date) end end if(m.type == "exploit") m.targets.each_index do |i| bits << [ :target, { :index => i, :name => m.targets[i].name.to_s } ] if m.targets[i].platform m.targets[i].platform.platforms.each do |name| bits << [ :platform, { :name => name.to_s.split('::').last.downcase } ] end end if m.targets[i].arch bits << [ :arch, { :name => m.targets[i].arch.to_s } ] end end if (m.default_target) res[:default_target] = m.default_target end # Some modules are a combination, which means they are actually aggressive res[:stance] = m.stance.to_s.index("aggressive") ? "aggressive" : "passive" m.class.mixins.each do |x| bits << [ :mixin, { :name => x.to_s } ] end end if(m.type == "auxiliary") m.actions.each_index do |i| bits << [ :action, { :name => m.actions[i].name.to_s } ] end if (m.default_action) res[:default_action] = m.default_action.to_s end res[:stance] = m.passive? ? "passive" : "aggressive" end res[:bits] = bits.uniq res end |
#purge_all_module_details ⇒ void
Does nothing unless Msf::DBManager::Migration#migrated is true
and #modules_caching is false
.
This method returns an undefined value.
Destroys all Mdm::Module::Details in the database.
124 125 126 127 128 129 130 131 |
# File 'lib/msf/core/db_manager/module_cache.rb', line 124 def purge_all_module_details return unless self.migrated return if self.modules_caching ::ApplicationRecord.connection_pool.with_connection do Mdm::Module::Detail.destroy_all end end |
#remove_module_details(mtype, refname) ⇒ void
This method returns an undefined value.
Destroys Mdm::Module::Detail if one exists for the given Mdm::Module::Detail#mtype and Mdm::Module::Detail#refname.
139 140 141 142 143 144 145 |
# File 'lib/msf/core/db_manager/module_cache.rb', line 139 def remove_module_details(mtype, refname) return if not self.migrated ApplicationRecord.connection_pool.with_connection do Mdm::Module::Detail.where(:mtype => mtype, :refname => refname).destroy_all end end |
#search_modules(search_string) ⇒ [], ActiveRecord::Relation
This provides a standard set of search filters for every module.
Supported keywords with the format <keyword>:<search_value>:
author
-
Matches modules with the given author email or name.
bid
-
Matches modules with the given Bugtraq ID.
cve
-
Matches modules with the given CVE ID.
edb
-
Matches modules with the given Exploit-DB ID.
name
-
Matches modules with the given full name or name.
os
,platform
-
Matches modules with the given platform or target name.
ref
-
Matches modules with the given reference ID.
type
-
Matches modules with the given type.
Any text not associated with a keyword is matched against the description, the full name, and the name of the module; the name of the module actions; the name of the module archs; the name of the module authors; the name of module platform; the module refs; or the module target.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/msf/core/db_manager/module_cache.rb', line 169 def search_modules(search_string) search_string ||= '' search_string += " " # Split search terms by space, but allow quoted strings terms = Shellwords.shellwords(search_string) terms.delete('') # All terms are either included or excluded value_set_by_keyword = Hash.new { |hash, keyword| hash[keyword] = Set.new } terms.each do |term| keyword, value = term.split(':', 2) unless value value = keyword keyword = 'text' end unless value.empty? keyword.downcase! value_set = value_set_by_keyword[keyword] value_set.add value end end ApplicationRecord.connection_pool.with_connection do @query = Mdm::Module::Detail.all @archs = Set.new @authors = Set.new @names = Set.new @os = Set.new @refs = Set.new @text = Set.new @types = Set.new value_set_by_keyword.each do |keyword, value_set| formatted_values = match_values(value_set) case keyword when 'arch' @archs << formatted_values when 'author' @authors << formatted_values when 'name' @names << formatted_values when 'os', 'platform' @os << formatted_values when 'ref' @refs << formatted_values when 'cve', 'bid', 'edb' formatted_values = value_set.collect { |value| prefix = keyword.upcase "#{prefix}-%#{value}%" } @refs << formatted_values when 'text' @text << formatted_values when 'type' @types << formatted_values end end end @query = @query.module_arch( @archs.to_a.flatten ) if @archs.any? @query = @query.( @authors.to_a.flatten ) if @authors.any? @query = @query.module_name( @names.to_a.flatten ) if @names.any? @query = @query.module_os_or_platform( @os.to_a.flatten ) if @os.any? @query = @query.module_text( @text.to_a.flatten ) if @text.any? @query = @query.module_type( @types.to_a.flatten ) if @types.any? @query = @query.module_ref( @refs.to_a.flatten ) if @refs.any? @query.uniq end |
#update_all_module_details ⇒ void
This method returns an undefined value.
Destroys the old Mdm::Module::Detail and creates a new Mdm::Module::Detail for any module with an Mdm::Module::Detail where the modification time of the Mdm::Module::Detail#file differs from the Mdm::Module::Detail#mtime. If the Mdm::Module::Detail#file no only exists on disk, then the Mdm::Module::Detail is just destroyed without a new one being created.
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/msf/core/db_manager/module_cache.rb', line 255 def update_all_module_details return if not self.migrated return if self.modules_caching self.framework.cache_thread = Thread.current self.modules_cached = false self.modules_caching = true ApplicationRecord.connection_pool.with_connection do refresh = [] skip_reference_name_set_by_module_type = Hash.new { |hash, module_type| hash[module_type] = Set.new } Mdm::Module::Detail.find_each do |md| unless md.ready refresh << md next end unless md.file and ::File.exist?(md.file) refresh << md next end if ::File.mtime(md.file).to_i != md.mtime.to_i refresh << md next end skip_reference_name_set = skip_reference_name_set_by_module_type[md.mtype] skip_reference_name_set.add(md.refname) end refresh.each { |md| md.destroy } [ ['exploit', framework.exploits], ['auxiliary', framework.auxiliary], ['post', framework.post], ['payload', framework.payloads], ['encoder', framework.encoders], ['nop', framework.nops] ].each do |mt| skip_reference_name_set = skip_reference_name_set_by_module_type[mt[0]] mt[1].keys.sort.each do |mn| next if skip_reference_name_set.include? mn obj = mt[1].create(mn) next if not obj begin update_module_details(obj) rescue ::Exception => e elog("Error updating module details for #{obj.fullname}", error: e) end end end self.framework.cache_initialized = true end # in reverse order of section before with_connection block self.modules_caching = false self.modules_cached = true self.framework.cache_thread = nil end |
#update_module_details(module_instance) ⇒ void
This method returns an undefined value.
Creates an Mdm::Module::Detail from a module instance.
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/msf/core/db_manager/module_cache.rb', line 331 def update_module_details(module_instance) return if not self.migrated ApplicationRecord.connection_pool.with_connection do info = module_to_details_hash(module_instance) bits = info.delete(:bits) || [] module_detail = Mdm::Module::Detail.create!(info) bits.each do |args| otype, vals = args case otype when :action module_detail.add_action(vals[:name]) when :arch module_detail.add_arch(vals[:name]) when :author module_detail.(vals[:name], vals[:email]) when :platform module_detail.add_platform(vals[:name]) when :ref module_detail.add_ref(vals[:name]) when :target module_detail.add_target(vals[:index], vals[:name]) end end module_detail.ready = true module_detail.save! end end |