Class: Msf::DBExport

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

Overview

This class provides export capabilities

Constant Summary collapse

STATUS_START =
"start"
STATUS_COMPLETE =
"complete"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workspace) ⇒ DBExport

Returns a new instance of DBExport.



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

def initialize(workspace)
  self.workspace = workspace
end

Instance Attribute Details

#workspaceObject

Returns the value of attribute workspace.



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

def workspace
  @workspace
end

Instance Method Details

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



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

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



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

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



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

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

#extract_event_info(report_file) ⇒ Object

Extract event data from @events



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

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



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

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.



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

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.



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

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



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

def extract_note_entries
  @notes = myworkspace.notes
end

#extract_service_entriesObject

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



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

def extract_service_entries
  @services = myworkspace.services
end

#extract_service_info(report_file) ⇒ Object

Extract service data from @services



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

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.



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

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



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

def extract_vuln_entries
  @vulns = myworkspace.vulns
end

#extract_web_entriesObject

Extract all web entries, storing them in instance variables



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

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



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

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



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

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



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

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



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

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



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

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)


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

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?



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

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



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

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

#myworkspaceObject



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

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.



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

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.



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

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