Class: Msf::DBManager::Export

Inherits:
Object
  • Object
show all
Defined in:
lib/msf/core/db_export.rb

Constant Summary collapse

STATUS_START =
"start"
STATUS_COMPLETE =
"complete"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workspace) ⇒ Export

Returns a new instance of Export.


17
18
19
# File 'lib/msf/core/db_export.rb', line 17

def initialize(workspace)
  self.workspace = workspace
end

Instance Attribute Details

#workspaceObject

Returns the value of attribute workspace


12
13
14
# File 'lib/msf/core/db_export.rb', line 12

def workspace
  @workspace
end

Instance Method Details

#create_xml_element(key, value, skip_encoding = false) ⇒ Object


190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/msf/core/db_export.rb', line 190

def create_xml_element(key,value,skip_encoding=false)
  tag = key.tr("_","-")
  el = REXML::Element.new(tag)
  if value
    unless skip_encoding
      data = marshalize(value)
      data.force_encoding(Encoding::BINARY) if data.respond_to?('force_encoding')
      data.gsub!(/([\x00-\x08\x0b\x0c\x0e-\x1f\x80-\xFF])/n){ |x| "\\x%.2x" % x.unpack("C*")[0] }
      el << REXML::Text.new(data)
    else
      el << value
    end
  end
  return el
end

#extract_credential_entriesObject

Extracts all credentials from a project, storing them in @creds


150
151
152
# File 'lib/msf/core/db_export.rb', line 150

def extract_credential_entries
  @creds = Metasploit::Credential::Core.with_logins.with_public.with_private.workspace_id(myworkspace.id)
end

#extract_event_entriesObject

Extracts all events from a project, storing them in @events


140
141
142
# File 'lib/msf/core/db_export.rb', line 140

def extract_event_entries
  @events = myworkspace.events.order('created_at ASC')
end

#extract_event_info(report_file) ⇒ Object

Extract event data from @events


440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/msf/core/db_export.rb', line 440

def extract_event_info(report_file)
  @events.each do |e|
    report_file.write("  <event>\n")
    e.attributes.each_pair do |k,v|
      el = create_xml_element(k,v)
      report_file.write("      #{el}\n")
    end
    report_file.write("  </event>\n")
    report_file.write("\n")
  end
  report_file.flush
end

#extract_host_entriesObject

Extracts all the hosts from a project, storing them in @hosts and @owned_hosts


129
130
131
132
133
134
135
136
137
# File 'lib/msf/core/db_export.rb', line 129

def extract_host_entries
  @owned_hosts = []
  @hosts = myworkspace.hosts
  @hosts.each do |host|
    if host.notes.where(ntype: 'pro.system.compromise').first
      @owned_hosts << host
    end
  end
end

#extract_host_info(report_file) ⇒ Object

ActiveRecord's to_xml is easy and wrong. This isn't, on both counts.


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
# File 'lib/msf/core/db_export.rb', line 314

def extract_host_info(report_file)
  @hosts.each do |h|
    report_file.write("  <host>\n")
    host_id = h.attributes["id"]

    # Host attributes
    h.attributes.each_pair do |k,v|
      # Convert IPAddr -> String
      v = v.to_s if k == 'address'
      el = create_xml_element(k,v)
      report_file.write("    #{el}\n") # Not checking types
    end

    # Host details sub-elements
    report_file.write("    <host_details>\n")
    h.host_details.each do |d|
      report_file.write("        <host_detail>\n")
      d.attributes.each_pair do |k,v|
        el = create_xml_element(k,v)
        report_file.write("            #{el}\n")
      end
      report_file.write("        </host_detail>\n")
    end
    report_file.write("    </host_details>\n")

    # Host exploit attempts sub-elements
    report_file.write("    <exploit_attempts>\n")
    h.exploit_attempts.each do |d|
      report_file.write("        <exploit_attempt>\n")
      d.attributes.each_pair do |k,v|
        el = create_xml_element(k,v)
        report_file.write("            #{el}\n")
      end
      report_file.write("        </exploit_attempt>\n")
    end
    report_file.write("    </exploit_attempts>\n")

    # Service sub-elements
    report_file.write("    <services>\n")
    @services.where(host_id: host_id).each do |e|
      report_file.write("      <service>\n")
      e.attributes.each_pair do |k,v|
        el = create_xml_element(k,v)
        report_file.write("      #{el}\n")
      end
      report_file.write("      </service>\n")
    end
    report_file.write("    </services>\n")

    # Notes sub-elements
    report_file.write("    <notes>\n")
    @notes.where(host_id: host_id).each do |e|
      report_file.write("      <note>\n")
      e.attributes.each_pair do |k,v|
        el = create_xml_element(k,v)
        report_file.write("      #{el}\n")
      end
      report_file.write("      </note>\n")
    end
    report_file.write("    </notes>\n")

    # Vulns sub-elements
    report_file.write("    <vulns>\n")
    @vulns.where(host_id: host_id).each do |e|
      report_file.write("      <vuln>\n")
      e.attributes.each_pair do |k,v|
        el = create_xml_element(k,v)
        report_file.write("      #{el}\n")
      end

      # Notes attached to vulns instead of the host
      report_file.write("        <notes>\n")
      @notes.where(vuln_id: e.id).each do |note|
        report_file.write("      <note>\n")
        note.attributes.each_pair do |k,v|
          el = create_xml_element(k,v)
          report_file.write("      #{el}\n")
        end
        report_file.write("      </note>\n")
      end
      report_file.write("        </notes>\n")

      # References
      report_file.write("        <refs>\n")
      e.refs.each do |ref|
        el = create_xml_element("ref",ref.name)
        report_file.write("          #{el}\n")
      end
      report_file.write("        </refs>\n")


      # Vuln details sub-elements
      report_file.write("            <vuln_details>\n")
      e.vuln_details.each do |d|
        report_file.write("                <vuln_detail>\n")
        d.attributes.each_pair do |k,v|
          el = create_xml_element(k,v)
          report_file.write("                    #{el}\n")
        end
        report_file.write("                </vuln_detail>\n")
      end
      report_file.write("            </vuln_details>\n")


      # Vuln attempts sub-elements
      report_file.write("            <vuln_attempts>\n")
      e.vuln_attempts.each do |d|
        report_file.write("                <vuln_attempt>\n")
        d.attributes.each_pair do |k,v|
          el = create_xml_element(k,v)
          report_file.write("                    #{el}\n")
        end
        report_file.write("                </vuln_attempt>\n")
      end
      report_file.write("            </vuln_attempts>\n")

      report_file.write("      </vuln>\n")
    end
    report_file.write("    </vulns>\n")

    report_file.write("  </host>\n")
  end
  report_file.flush
end

#extract_module_detail_info(report_file) ⇒ void

Note:

there is no single root element output by #extract_module_detail_info, so if calling #extract_module_detail_info directly, it is the caller's responsibility to add an opening and closing tag to report_file around the call to #extract_module_detail_info.

This method returns an undefined value.

Writes a module_detail element to the report_file for each Mdm::Module::Detail.

Parameters:

  • report_file (#write, #flush)

    IO stream to which to write the module_detail elements.


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
# File 'lib/msf/core/db_export.rb', line 217

def extract_module_detail_info(report_file)
    Mdm::Module::Detail.all.each do |m|
    report_file.write("<module_detail>\n")
    #m_id = m.attributes["id"]

    # Module attributes
    m.attributes.each_pair do |k,v|
      el = create_xml_element(k,v)
      report_file.write("    #{el}\n") # Not checking types
    end

    # Authors sub-elements
    # @todo https://www.pivotaltracker.com/story/show/48451001
    report_file.write("    <module_authors>\n")
    m.authors.each do |d|
      d.attributes.each_pair do |k,v|
        el = create_xml_element(k,v)
        report_file.write("        #{el}\n")
      end
    end
    report_file.write("    </module_authors>\n")

    # Refs sub-elements
    # @todo https://www.pivotaltracker.com/story/show/48451001
    report_file.write("    <module_refs>\n")
    m.refs.each do |d|
      d.attributes.each_pair do |k,v|
        el = create_xml_element(k,v)
        report_file.write("        #{el}\n")
      end
    end
    report_file.write("    </module_refs>\n")


    # Archs sub-elements
    # @todo https://www.pivotaltracker.com/story/show/48451001
    report_file.write("    <module_archs>\n")
    m.archs.each do |d|
      d.attributes.each_pair do |k,v|
        el = create_xml_element(k,v)
        report_file.write("        #{el}\n")
      end
    end
    report_file.write("    </module_archs>\n")


    # Platforms sub-elements
    # @todo https://www.pivotaltracker.com/story/show/48451001
    report_file.write("    <module_platforms>\n")
    m.platforms.each do |d|
      d.attributes.each_pair do |k,v|
        el = create_xml_element(k,v)
        report_file.write("        #{el}\n")
      end
    end
    report_file.write("    </module_platforms>\n")


    # Targets sub-elements
    # @todo https://www.pivotaltracker.com/story/show/48451001
    report_file.write("    <module_targets>\n")
    m.targets.each do |d|
      d.attributes.each_pair do |k,v|
        el = create_xml_element(k,v)
        report_file.write("        #{el}\n")
      end
    end
    report_file.write("    </module_targets>\n")

    # Actions sub-elements
    # @todo https://www.pivotaltracker.com/story/show/48451001
    report_file.write("    <module_actions>\n")
    m.actions.each do |d|
      d.attributes.each_pair do |k,v|
        el = create_xml_element(k,v)
        report_file.write("        #{el}\n")
      end
    end
    report_file.write("    </module_actions>\n")

    # Mixins sub-elements
    # @todo https://www.pivotaltracker.com/story/show/48451001
    report_file.write("    <module_mixins>\n")
    m.mixins.each do |d|
      d.attributes.each_pair do |k,v|
        el = create_xml_element(k,v)
        report_file.write("        #{el}\n")
      end
    end
    report_file.write("    </module_mixins>\n")

    report_file.write("</module_detail>\n")
  end
  report_file.flush
end

#extract_note_entriesObject

Extracts all notes from a project, storing them in @notes


155
156
157
# File 'lib/msf/core/db_export.rb', line 155

def extract_note_entries
  @notes = myworkspace.notes
end

#extract_service_entriesObject

Extracts all services from a project, storing them in @services


145
146
147
# File 'lib/msf/core/db_export.rb', line 145

def extract_service_entries
  @services = myworkspace.services
end

#extract_service_info(report_file) ⇒ Object

Extract service data from @services


454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/msf/core/db_export.rb', line 454

def extract_service_info(report_file)
  @services.each do |e|
    report_file.write("  <service>\n")
    e.attributes.each_pair do |k,v|
      el = create_xml_element(k,v)
      report_file.write("      #{el}\n")
    end
    report_file.write("  </service>\n")
    report_file.write("\n")
  end
  report_file.flush
end

#extract_target_entriesObject

A convenience function that bundles together host, event, and service extraction.


119
120
121
122
123
124
125
126
# File 'lib/msf/core/db_export.rb', line 119

def extract_target_entries
  extract_host_entries
  extract_event_entries
  extract_service_entries
  extract_note_entries
  extract_vuln_entries
  extract_web_entries
end

#extract_vuln_entriesObject

Extracts all vulns from a project, storing them in @vulns


160
161
162
# File 'lib/msf/core/db_export.rb', line 160

def extract_vuln_entries
  @vulns = myworkspace.vulns
end

#extract_web_entriesObject

Extract all web entries, storing them in instance variables


165
166
167
168
169
170
# File 'lib/msf/core/db_export.rb', line 165

def extract_web_entries
  @web_sites = myworkspace.web_sites
  @web_pages = myworkspace.web_pages
  @web_forms = myworkspace.web_forms
  @web_vulns = myworkspace.web_vulns
end

#extract_web_form_info(report_file) ⇒ Object

Extract web forms


539
540
541
# File 'lib/msf/core/db_export.rb', line 539

def extract_web_form_info(report_file)
  extract_web_info(report_file, "web_form", @web_forms)
end

#extract_web_info(report_file, tag, entries) ⇒ Object

Extract web pages, forms, and vulns


507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# File 'lib/msf/core/db_export.rb', line 507

def extract_web_info(report_file, tag, entries)
  entries.each do |e|
    report_file.write("  <#{tag}>\n")
    e.attributes.each_pair do |k,v|
      el = create_xml_element(k,v)
      report_file.write("      #{el}\n")
    end

    site = e.web_site
    el = create_xml_element("vhost", site.vhost)
    report_file.write("      #{el}\n")

    el = create_xml_element("host", site.service.host.address)
    report_file.write("      #{el}\n")

    el = create_xml_element("port", site.service.port)
    report_file.write("      #{el}\n")

    el = create_xml_element("ssl", site.service.name == "https")
    report_file.write("      #{el}\n")

    report_file.write("  </#{tag}>\n")
  end
  report_file.flush
end

#extract_web_page_info(report_file) ⇒ Object

Extract web pages


534
535
536
# File 'lib/msf/core/db_export.rb', line 534

def extract_web_page_info(report_file)
  extract_web_info(report_file, "web_page", @web_pages)
end

#extract_web_site_info(report_file) ⇒ Object

Extract web site data from @web_sites


483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/msf/core/db_export.rb', line 483

def extract_web_site_info(report_file)
  @web_sites.each do |e|
    report_file.write("  <web_site>\n")
    e.attributes.each_pair do |k,v|
      el = create_xml_element(k,v)
      report_file.write("      #{el}\n")
    end

    site = e
    el = create_xml_element("host", site.service.host.address)
    report_file.write("      #{el}\n")

    el = create_xml_element("port", site.service.port)
    report_file.write("      #{el}\n")

    el = create_xml_element("ssl", site.service.name == "https")
    report_file.write("      #{el}\n")

    report_file.write("  </web_site>\n")
  end
  report_file.flush
end

#extract_web_vuln_info(report_file) ⇒ Object

Extract web vulns


544
545
546
# File 'lib/msf/core/db_export.rb', line 544

def extract_web_vuln_info(report_file)
  extract_web_info(report_file, "web_vuln", @web_vulns)
end

#host_allowed?(arg) ⇒ Boolean

Hosts are always allowed. This is really just a stub.

Returns:

  • (Boolean)

30
31
32
# File 'lib/msf/core/db_export.rb', line 30

def host_allowed?(arg)
  true
end

#marshalize(obj) ⇒ Object

Simple marshalling, for now. Can I use ActiveRecord::ConnectionAdapters::Quoting#quote directly? Is it better to just marshal everything and destroy readability? Howabout XML safety?


175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/msf/core/db_export.rb', line 175

def marshalize(obj)
  case obj
  when String
    obj.strip
  when TrueClass, FalseClass, Float, Integer, Time
    obj.to_s.strip
  when BigDecimal
    obj.to_s("F")
  when NilClass
    "NULL"
  else
    [Marshal.dump(obj)].pack("m").gsub(/\s+/,"")
  end
end

#myusernameObject


25
26
27
# File 'lib/msf/core/db_export.rb', line 25

def myusername
  @username ||= (ENV['LOGNAME'] || ENV['USERNAME'] || ENV['USER'] || "unknown").to_s.strip.gsub(/[^A-Za-z0-9\x20]/n,"_")
end

#myworkspaceObject


21
22
23
# File 'lib/msf/core/db_export.rb', line 21

def myworkspace
  self.workspace
end

#to_pwdump_file(path, &block) ⇒ String

Performs an export of the workspace's `Metasploit::Credential::Login` objects in pwdump format

Parameters:

  • path (String)

    the path on the local filesystem where the exported data will be written

Returns:

  • (String)

    The path to the location of the written file.


38
39
40
41
42
43
44
45
# File 'lib/msf/core/db_export.rb', line 38

def to_pwdump_file(path, &block)
  exporter = Metasploit::Credential::Exporter::Pwdump.new(workspace: workspace)

  output_file = File.open(path, 'w') do |file|
    file << exporter.rendered_output
  end
  output_file.path
end

#to_xml_file(path) {|:status, STATUS_START, "report"| ... } ⇒ String

Performs an export of the workspace's `Metasploit::Credential::Login` objects in XML format

Parameters:

  • path (String)

    the path on the local filesystem where the exported data will be written

Yields:

Returns:

  • (String)

    The path to the location of the written file.


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_export.rb', line 50

def to_xml_file(path, &block)

  yield(:status, STATUS_START, "report") if block_given?
  extract_target_entries
  report_file = ::File.open(path, "wb")

  report_file.write %Q|<?xml version="1.0" encoding="UTF-8"?>\n|
  report_file.write %Q|<MetasploitV5>\n|
  report_file.write %Q|<generated time="#{Time.now.utc}" user="#{myusername}" project="#{myworkspace.name.gsub(/[^A-Za-z0-9\x20]/n,"_")}" product="framework"/>\n|

  yield(:status, STATUS_START, "hosts") if block_given?
  report_file.write %Q|<hosts>\n|
  report_file.flush
  extract_host_info(report_file)
  report_file.write %Q|</hosts>\n|

  yield(:status, STATUS_START, "events") if block_given?
  report_file.write %Q|<events>\n|
  report_file.flush
  extract_event_info(report_file)
  report_file.write %Q|</events>\n|

  yield(:status, STATUS_START, "services") if block_given?
  report_file.write %Q|<services>\n|
  report_file.flush
  extract_service_info(report_file)
  report_file.write %Q|</services>\n|

  yield(:status, STATUS_START, "web sites") if block_given?
  report_file.write %Q|<web_sites>\n|
  report_file.flush
  extract_web_site_info(report_file)
  report_file.write %Q|</web_sites>\n|

  yield(:status, STATUS_START, "web pages") if block_given?
  report_file.write %Q|<web_pages>\n|
  report_file.flush
  extract_web_page_info(report_file)
  report_file.write %Q|</web_pages>\n|

  yield(:status, STATUS_START, "web forms") if block_given?
  report_file.write %Q|<web_forms>\n|
  report_file.flush
  extract_web_form_info(report_file)
  report_file.write %Q|</web_forms>\n|

  yield(:status, STATUS_START, "web vulns") if block_given?
  report_file.write %Q|<web_vulns>\n|
  report_file.flush
  extract_web_vuln_info(report_file)
  report_file.write %Q|</web_vulns>\n|

  yield(:status, STATUS_START, "module details") if block_given?
  report_file.write %Q|<module_details>\n|
  report_file.flush
  extract_module_detail_info(report_file)
  report_file.write %Q|</module_details>\n|


  report_file.write %Q|</MetasploitV5>\n|
  report_file.flush
  report_file.close

  yield(:status, STATUS_COMPLETE, "report") if block_given?

  report_file.path
end