Module: Msf::DBManager::ImportMsfXml

Included in:
Msf::DBManager
Defined in:
lib/msf/core/db_manager/import_msf_xml.rb

Overview

Handles importing of the xml format exported by Pro. The methods are in a module because (1) it's just good code layout and (2) it allows the methods to be overridden in Pro without using alias_method_chain as methods defined in a class cannot be overridden by including a module (unless you're running Ruby 2.0 and can use prepend)

Constant Summary collapse

MSF_WEB_PAGE_TEXT_ELEMENT_NAMES =

Elements that can be treated as text (i.e. do not need to be deserialized) in #import_msf_web_page_element

[
    'auth',
    'body',
    'code',
    'cookie',
    'ctype',
    'location',
    'mtime'
]
MSF_WEB_TEXT_ELEMENT_NAMES =

Elements that can be treated as text (i.e. do not need to be deserialized) in #import_msf_web_element.

[
    'created-at',
    'host',
    'path',
    'port',
    'query',
    'ssl',
    'updated-at',
    'vhost'
]
MSF_WEB_VULN_TEXT_ELEMENT_NAMES =

Elements that can be treated as text (i.e. do not need to be deserialized) in #import_msf_web_vuln_element.

[
    'blame',
    'category',
    'confidence',
    'description',
    'method',
    'name',
    'pname',
    'proof',
    'risk'
]

Instance Method Summary collapse

Instance Method Details

#import_msf_web_form_element(element, options = {}) {|event, data| ... } ⇒ void

This method returns an undefined value.

Imports web_form element using Msf::DBManager#report_web_form.

Parameters:

  • element (REXML::Element)

    web_form element.

  • options (Hash{Symbol => Object}) (defaults to: {})

    options

Options Hash (options):

  • :allow_yaml (Boolean) — default: false

    Whether to allow YAML when deserializing params.

  • :workspace (Mdm::Workspace, nil) — default: Msf::DBManager#workspace

    workspace under which to report the Mdm::WebForm

Yields:

  • (event, data)

Yield Parameters:

  • event (:web_page)

    The event name

  • data (String)

    path

Yield Returns:

  • (void)

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/msf/core/db_manager/import_msf_xml.rb', line 70

def import_msf_web_form_element(element, options={}, &notifier)
  options.assert_valid_keys(:allow_yaml, :workspace)

  import_msf_web_element(element,
                         :allow_yaml => options[:allow_yaml],
                         :notifier => notifier,
                         :type => :form,
                         :workspace => options[:workspace]) do |element, options|
    info = import_msf_text_element(element, 'method')

    # FIXME https://www.pivotaltracker.com/story/show/46578647
    # FIXME https://www.pivotaltracker.com/story/show/47128407
    unserialized_params = unserialize_object(
        element.elements['params'],
        options[:allow_yaml]
    )
    info[:params] = nils_for_nulls(unserialized_params)

    info
  end
end

#import_msf_web_page_element(element, options = {}) {|event, data| ... } ⇒ void

This method returns an undefined value.

Imports web_page element using Msf::DBManager#report_web_page.

Parameters:

  • element (REXML::Element)

    web_page element.

  • options (Hash{Symbol => Object}) (defaults to: {})

    options

Options Hash (options):

  • :allow_yaml (Boolean) — default: false

    Whether to allow YAML when deserializing headers.

  • :workspace (Mdm::Workspace, nil) — default: Msf::DBManager#workspace

    workspace under which to report the Mdm::WebPage.

Yields:

  • (event, data)

Yield Parameters:

  • event (:web_page)

    The event name

  • data (String)

    path

Yield Returns:

  • (void)

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/msf/core/db_manager/import_msf_xml.rb', line 106

def import_msf_web_page_element(element, options={}, &notifier)
  options.assert_valid_keys(:allow_yaml, :workspace)

  import_msf_web_element(element,
                         :allow_yaml => options[:allow_yaml],
                         :notifier => notifier,
                         :type => :page,
                         :workspace => options[:workspace]) do |element, options|
    info = {}

    MSF_WEB_PAGE_TEXT_ELEMENT_NAMES.each do |name|
      element_info = import_msf_text_element(element, name)
      info.merge!(element_info)
    end

    code = info[:code]

    if code
      info[:code] = code.to_i
    end

    # FIXME https://www.pivotaltracker.com/story/show/46578647
    # FIXME https://www.pivotaltracker.com/story/show/47128407
    unserialized_headers = unserialize_object(
        element.elements['headers'],
        options[:allow_yaml]
    )
    info[:headers] = nils_for_nulls(unserialized_headers)

    info
  end
end

#import_msf_web_vuln_element(element, options = {}) {|event, data| ... } ⇒ void

This method returns an undefined value.

Imports web_vuln element using Msf::DBManager#report_web_vuln.

Parameters:

  • element (REXML::Element)

    web_vuln element.

  • options (Hash{Symbol => Object}) (defaults to: {})

    options

Options Hash (options):

  • :allow_yaml (Boolean) — default: false

    Whether to allow YAML when deserializing headers.

  • :workspace (Mdm::Workspace, nil) — default: Msf::DBManager#workspace

    workspace under which to report the Mdm::WebPage.

Yields:

  • (event, data)

Yield Parameters:

  • event (:web_page)

    The event name

  • data (String)

    path

Yield Returns:

  • (void)

153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/msf/core/db_manager/import_msf_xml.rb', line 153

def import_msf_web_vuln_element(element, options={}, &notifier)
  options.assert_valid_keys(:allow_yaml, :workspace)

  import_msf_web_element(element,
                         :allow_yaml => options[:allow_yaml],
                         :notifier => notifier,
                         :workspace => options[:workspace],
                         :type => :vuln) do |element, options|
    info = {}

    MSF_WEB_VULN_TEXT_ELEMENT_NAMES.each do |name|
      element_info = import_msf_text_element(element, name)
      info.merge!(element_info)
    end

    confidence = info[:confidence]

    if confidence
      info[:confidence] = confidence.to_i
    end

    # FIXME https://www.pivotaltracker.com/story/show/46578647
    # FIXME https://www.pivotaltracker.com/story/show/47128407
    unserialized_params = unserialize_object(
        element.elements['params'],
        options[:allow_yaml]
    )
    info[:params] = nils_for_nulls(unserialized_params)

    risk = info[:risk]

    if risk
      info[:risk] = risk.to_i
    end

    info
  end
end

#import_msf_xml(args = {}, &block) ⇒ Object

For each host, step through services, notes, and vulns, and import them. TODO: loot, tasks, and reports


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
247
248
249
250
251
252
253
254
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
324
325
326
327
328
329
330
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/msf/core/db_manager/import_msf_xml.rb', line 195

def import_msf_xml(args={}, &block)
  data = args[:data]
  wspace = args[:wspace] || workspace
  bl = validate_ips(args[:blacklist]) ? args[:blacklist].split : []

  doc = rexmlify(data)
   = check_msf_xml_version!(doc)
  allow_yaml = [:allow_yaml]
  btag = [:root_tag]

  doc.elements.each("/#{btag}/hosts/host") do |host|
    host_data = {}
    host_data[:workspace] = wspace
    host_data[:host] = nils_for_nulls(host.elements["address"].text.to_s.strip)
    if bl.include? host_data[:host]
      next
    else
      yield(:address,host_data[:host]) if block
    end
    host_data[:mac] = nils_for_nulls(host.elements["mac"].text.to_s.strip)
    if host.elements["comm"].text
      host_data[:comm] = nils_for_nulls(host.elements["comm"].text.to_s.strip)
    end
    %W{created-at updated-at name state os-flavor os-lang os-name os-sp purpose}.each { |datum|
      if host.elements[datum].text
        host_data[datum.gsub('-','_')] = nils_for_nulls(host.elements[datum].text.to_s.strip)
      end
    }
    host_address = host_data[:host].dup # Preserve after report_host() deletes
    hobj = report_host(host_data)

    host.elements.each("host_details/host_detail") do |hdet|
      hdet_data = {}
      hdet.elements.each do |det|
        next if ["id", "host-id"].include?(det.name)
        if det.text
          hdet_data[det.name.gsub('-','_')] = nils_for_nulls(det.text.to_s.strip)
        end
      end
      report_host_details(hobj, hdet_data)
    end

    host.elements.each("exploit_attempts/exploit_attempt") do |hdet|
      hdet_data = {}
      hdet.elements.each do |det|
        next if ["id", "host-id", "session-id", "vuln-id", "service-id", "loot-id"].include?(det.name)
        if det.text
          hdet_data[det.name.gsub('-','_')] = nils_for_nulls(det.text.to_s.strip)
        end
      end
      report_exploit_attempt(hobj, hdet_data)
    end

    host.elements.each('services/service') do |service|
      service_data = {}
      service_data[:workspace] = wspace
      service_data[:host] = hobj
      service_data[:port] = nils_for_nulls(service.elements["port"].text.to_s.strip).to_i
      service_data[:proto] = nils_for_nulls(service.elements["proto"].text.to_s.strip)
      %W{created-at updated-at name state info}.each { |datum|
        if service.elements[datum].text
          if datum == "info"
            service_data["info"] = nils_for_nulls(unserialize_object(service.elements[datum], false))
          else
            service_data[datum.gsub("-","_")] = nils_for_nulls(service.elements[datum].text.to_s.strip)
          end
        end
      }
      report_service(service_data)
    end

    host.elements.each('notes/note') do |note|
      note_data = {}
      note_data[:workspace] = wspace
      note_data[:host] = hobj
      note_data[:type] = nils_for_nulls(note.elements["ntype"].text.to_s.strip)
      note_data[:data] = nils_for_nulls(unserialize_object(note.elements["data"], allow_yaml))

      if note.elements["critical"].text
        note_data[:critical] = true unless note.elements["critical"].text.to_s.strip == "NULL"
      end
      if note.elements["seen"].text
        note_data[:seen] = true unless note.elements["critical"].text.to_s.strip == "NULL"
      end
      %W{created-at updated-at}.each { |datum|
        if note.elements[datum].text
          note_data[datum.gsub("-","_")] = nils_for_nulls(note.elements[datum].text.to_s.strip)
        end
      }
      report_note(note_data)
    end

    host.elements.each('tags/tag') do |tag|
      tag_data = {}
      tag_data[:addr] = host_address
      tag_data[:wspace] = wspace
      tag_data[:name] = tag.elements["name"].text.to_s.strip
      tag_data[:desc] = tag.elements["desc"].text.to_s.strip
      if tag.elements["report-summary"].text
        tag_data[:summary] = tag.elements["report-summary"].text.to_s.strip
      end
      if tag.elements["report-detail"].text
        tag_data[:detail] = tag.elements["report-detail"].text.to_s.strip
      end
      if tag.elements["critical"].text
        tag_data[:crit] = true unless tag.elements["critical"].text.to_s.strip == "NULL"
      end
      report_host_tag(tag_data)
    end

    host.elements.each('vulns/vuln') do |vuln|
      vuln_data = {}
      vuln_data[:workspace] = wspace
      vuln_data[:host] = hobj
      vuln_data[:info] = nils_for_nulls(unserialize_object(vuln.elements["info"], allow_yaml))
      vuln_data[:name] = nils_for_nulls(vuln.elements["name"].text.to_s.strip)
      %W{created-at updated-at exploited-at}.each { |datum|
        if vuln.elements[datum] and vuln.elements[datum].text
          vuln_data[datum.gsub("-","_")] = nils_for_nulls(vuln.elements[datum].text.to_s.strip)
        end
      }
      if vuln.elements["refs"]
        vuln_data[:refs] = []
        vuln.elements.each("refs/ref") do |ref|
          vuln_data[:refs] << nils_for_nulls(ref.text.to_s.strip)
        end
      end

      vobj = report_vuln(vuln_data)

      vuln.elements.each("vuln_details/vuln_detail") do |vdet|
        vdet_data = {}
        vdet.elements.each do |det|
          next if ["id", "vuln-id"].include?(det.name)
          if det.text
            vdet_data[det.name.gsub('-','_')] = nils_for_nulls(det.text.to_s.strip)
          end
        end
        report_vuln_details(vobj, vdet_data)
      end

      vuln.elements.each("vuln_attempts/vuln_attempt") do |vdet|
        vdet_data = {}
        vdet.elements.each do |det|
          next if ["id", "vuln-id", "loot-id", "session-id"].include?(det.name)
          if det.text
            vdet_data[det.name.gsub('-','_')] = nils_for_nulls(det.text.to_s.strip)
          end
        end
        report_vuln_attempt(vobj, vdet_data)
      end
    end

    host.elements.each('creds/cred') do |cred|
      cred_data = {}
      cred_data[:workspace] = wspace
      cred_data[:host] = hobj
      %W{port ptype sname proto proof active user pass}.each {|datum|
        if cred.elements[datum].respond_to? :text
          cred_data[datum.intern] = nils_for_nulls(cred.elements[datum].text.to_s.strip)
        end
      }
      %W{created-at updated-at}.each { |datum|
        if cred.elements[datum].respond_to? :text
          cred_data[datum.gsub("-","_")] = nils_for_nulls(cred.elements[datum].text.to_s.strip)
        end
      }
      %W{source-type source-id}.each { |datum|
        if cred.elements[datum].respond_to? :text
          cred_data[datum.gsub("-","_").intern] = nils_for_nulls(cred.elements[datum].text.to_s.strip)
        end
      }
      if cred_data[:pass] == "*MASKED*"
        cred_data[:pass] = ""
        cred_data[:active] = false
      elsif cred_data[:pass] == "*BLANK PASSWORD*"
        cred_data[:pass] = ""
      end
      report_cred(cred_data)
    end

    host.elements.each('sessions/session') do |sess|
      sess_id = nils_for_nulls(sess.elements["id"].text.to_s.strip.to_i)
      sess_data = {}
      sess_data[:host] = hobj
      %W{desc platform port stype}.each {|datum|
        if sess.elements[datum].respond_to? :text
          sess_data[datum.intern] = nils_for_nulls(sess.elements[datum].text.to_s.strip)
        end
      }
      %W{opened-at close-reason closed-at via-exploit via-payload}.each {|datum|
        if sess.elements[datum].respond_to? :text
          sess_data[datum.gsub("-","_").intern] = nils_for_nulls(sess.elements[datum].text.to_s.strip)
        end
      }
      sess_data[:datastore] = nils_for_nulls(unserialize_object(sess.elements["datastore"], allow_yaml))
      if sess.elements["routes"]
        sess_data[:routes] = nils_for_nulls(unserialize_object(sess.elements["routes"], allow_yaml)) || []
      end
      if not sess_data[:closed_at] # Fake a close if we don't already have one
        sess_data[:closed_at] = Time.now.utc
        sess_data[:close_reason] = "Imported at #{Time.now.utc}"
      end

      existing_session = get_session(
          :workspace => sess_data[:host].workspace,
          :addr => sess_data[:host].address,
          :time => sess_data[:opened_at]
      )
      this_session = existing_session || report_session(sess_data)
      next if existing_session
      sess.elements.each('events/event') do |sess_event|
        sess_event_data = {}
        sess_event_data[:session] = this_session
        %W{created-at etype local-path remote-path}.each {|datum|
          if sess_event.elements[datum].respond_to? :text
            sess_event_data[datum.gsub("-","_").intern] = nils_for_nulls(sess_event.elements[datum].text.to_s.strip)
          end
        }
        %W{command output}.each {|datum|
          if sess_event.elements[datum].respond_to? :text
            sess_event_data[datum.gsub("-","_").intern] = nils_for_nulls(unserialize_object(sess_event.elements[datum], allow_yaml))
          end
        }
        report_session_event(sess_event_data)
      end
    end
  end

  # Import web sites
  doc.elements.each("/#{btag}/web_sites/web_site") do |web|
    info = {}
    info[:workspace] = wspace

    %W{host port vhost ssl comments}.each do |datum|
      if web.elements[datum].respond_to? :text
        info[datum.intern] = nils_for_nulls(web.elements[datum].text.to_s.strip)
      end
    end

    info[:options]   = nils_for_nulls(unserialize_object(web.elements["options"], allow_yaml)) if web.elements["options"].respond_to?(:text)
    info[:ssl]       = (info[:ssl] and info[:ssl].to_s.strip.downcase == "true") ? true : false

    %W{created-at updated-at}.each { |datum|
      if web.elements[datum].text
        info[datum.gsub("-","_")] = nils_for_nulls(web.elements[datum].text.to_s.strip)
      end
    }

    report_web_site(info)
    yield(:web_site, "#{info[:host]}:#{info[:port]} (#{info[:vhost]})") if block
  end

  %W{page form vuln}.each do |wtype|
    doc.elements.each("/#{btag}/web_#{wtype}s/web_#{wtype}") do |element|
      send(
          "import_msf_web_#{wtype}_element",
          element,
          :allow_yaml => allow_yaml,
          :workspace => wspace,
          &block
      )
    end
  end
end