Class: Msf::DBManager::Export

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workspace) ⇒ Export


14
15
16
# File 'lib/msf/core/db_export.rb', line 14

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

#count_credentials(ptype, creds) ⇒ Object

Counts the total number of credentials for its type.


86
87
88
89
90
91
92
# File 'lib/msf/core/db_export.rb', line 86

def count_credentials(ptype,creds)
  sz = 0
  if creds[ptype]
    creds[ptype].each_pair { |svc, data| data.each { |c| sz +=1 } }
  end
  return sz
end

#create_xml_element(key, value) ⇒ Object


349
350
351
352
353
354
355
356
357
358
359
# File 'lib/msf/core/db_export.rb', line 349

def create_xml_element(key,value)
  tag = key.gsub("_","-")
  el = REXML::Element.new(tag)
  if value
    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)
  end
  return el
end

#extract_credential_entriesObject

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


308
309
310
311
# File 'lib/msf/core/db_export.rb', line 308

def extract_credential_entries
  @creds = []
  myworkspace.each_cred {|cred| @creds << cred}
end

#extract_credential_info(report_file) ⇒ Object

Extract credential data from @creds


625
626
627
628
629
630
631
632
633
634
635
636
# File 'lib/msf/core/db_export.rb', line 625

def extract_credential_info(report_file)
  @creds.each do |c|
    report_file.write("  <credential>\n")
    c.attributes.each_pair do |k,v|
      cr = create_xml_element(k,v)
      report_file.write("      #{cr}\n")
    end
    report_file.write("  </credential>\n")
    report_file.write("\n")
  end
  report_file.flush
end

#extract_credentialsObject

Extracts credentials and organizes by type, then by host, and finally by individual credential data. Will look something like:

{"smb_hash" => {"host1:445" => [user1,user2,user3], "host2:445" => [user4,user5]}},
{"ssh_key" => {"host3:22" => [user10,user20]}},
{"text" => {"host4:23" => [user100,user101]}}

This hash of hashes of arrays is, in turn, consumed by gen_export_pwdump.


175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/msf/core/db_export.rb', line 175

def extract_credentials
  creds = Hash.new
  creds["ssh_key"] = {}
  creds["smb_hash"] = {}
  creds["text"] = {}
  myworkspace.each_cred do |cred|
    next unless host_allowed?(cred.service.host.address)
    # Skip anything that's not associated with a specific host and port
    next unless (cred.service && cred.service.host && cred.service.host.address && cred.service.port)
    # TODO: Toggle active/all
    next unless cred.active
    svc = "%s:%d/%s (%s)" % [cred.service.host.address,cred.service.port,cred.service.proto,cred.service.name]
    case cred.ptype
    when /^password/
      ptype = "text"
    else
      ptype = cred.ptype
    end
    creds[ptype] ||= {}
    creds[ptype][svc] ||= []
    creds[ptype][svc] << cred
  end
  return creds
end

#extract_event_entriesObject

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


298
299
300
# File 'lib/msf/core/db_export.rb', line 298

def extract_event_entries
  @events = myworkspace.events.find :all, :order => 'created_at ASC'
end

#extract_event_info(report_file) ⇒ Object

Extract event data from @events


597
598
599
600
601
602
603
604
605
606
607
608
# File 'lib/msf/core/db_export.rb', line 597

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


287
288
289
290
291
292
293
294
295
# File 'lib/msf/core/db_export.rb', line 287

def extract_host_entries
  @owned_hosts = []
  @hosts = myworkspace.hosts
  @hosts.each do |host|
    if host.notes.find :first, :conditions => { :ntype => 'pro.system.compromise' }
      @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.


469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# File 'lib/msf/core/db_export.rb', line 469

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|
      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.find(:all).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.find(:all).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.find_all_by_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.find_all_by_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.find_all_by_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

      # 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.find(:all).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.find(:all).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")

    # Credential sub-elements
    report_file.write("    <creds>\n")
    @creds.each do |cred|
      next unless cred.service.host.id == host_id
      report_file.write("      <cred>\n")
      report_file.write("      #{create_xml_element("port",cred.service.port)}\n")
      report_file.write("      #{create_xml_element("sname",cred.service.name)}\n")
      cred.attributes.each_pair do |k,v|
        next if k.strip =~ /id$/
        el = create_xml_element(k,v)
        report_file.write("      #{el}\n")
      end
      report_file.write("      </cred>\n")
    end
    report_file.write("    </creds>\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.


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
460
461
462
463
464
465
466
# File 'lib/msf/core/db_export.rb', line 372

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.find(:all).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.find(:all).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.find(:all).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.find(:all).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.find(:all).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.find(:all).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.find(:all).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


314
315
316
# File 'lib/msf/core/db_export.rb', line 314

def extract_note_entries
  @notes = myworkspace.notes
end

#extract_service_entriesObject

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


303
304
305
# File 'lib/msf/core/db_export.rb', line 303

def extract_service_entries
  @services = myworkspace.services
end

#extract_service_info(report_file) ⇒ Object

Extract service data from @services


611
612
613
614
615
616
617
618
619
620
621
622
# File 'lib/msf/core/db_export.rb', line 611

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.


276
277
278
279
280
281
282
283
284
# File 'lib/msf/core/db_export.rb', line 276

def extract_target_entries
  extract_host_entries
  extract_event_entries
  extract_service_entries
  extract_credential_entries
  extract_note_entries
  extract_vuln_entries
  extract_web_entries
end

#extract_vuln_entriesObject

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


319
320
321
# File 'lib/msf/core/db_export.rb', line 319

def extract_vuln_entries
  @vulns = myworkspace.vulns
end

#extract_web_entriesObject

Extract all web entries, storing them in instance variables


324
325
326
327
328
329
# File 'lib/msf/core/db_export.rb', line 324

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


709
710
711
# File 'lib/msf/core/db_export.rb', line 709

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


677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
# File 'lib/msf/core/db_export.rb', line 677

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


704
705
706
# File 'lib/msf/core/db_export.rb', line 704

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


653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
# File 'lib/msf/core/db_export.rb', line 653

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


714
715
716
# File 'lib/msf/core/db_export.rb', line 714

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.


27
28
29
# File 'lib/msf/core/db_export.rb', line 27

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?


334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/msf/core/db_export.rb', line 334

def marshalize(obj)
  case obj
  when String
    obj.strip
  when TrueClass, FalseClass, Float, Fixnum, Bignum, 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


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

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

#myworkspaceObject


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

def myworkspace
  self.workspace
end

#to_pwdump_file(path) {|:status, "start", "password dump"| ... } ⇒ Object

Creates the PWDUMP text file. smb_hash and ssh_key credentials are treated specially – all other ptypes are treated as plain text.

Some day in the very near future, this file format will be importable – the comment preceding the credential is always in the format of IPAddr:Port/Proto (name), so it should be a simple matter to read them back in and store credentials in the right place. Finally, this format is already parsable by John the Ripper, so hashes can be bruteforced offline.

Yields:

  • (:status, "start", "password dump")

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

def to_pwdump_file(path, &block)
  yield(:status, "start", "password dump") if block_given?
  creds = extract_credentials
  report_file = ::File.open(path, "wb")
  report_file.write "# Metasploit PWDump Export v1\n"
  report_file.write "# Generated: #{Time.now.utc}\n"
  report_file.write "# Project: #{myworkspace.name}\n"
  report_file.write "#\n"
  report_file.write "#" * 40; report_file.write "\n"

  count = count_credentials("smb_hash",creds)
  scount = creds.has_key?("smb_hash") ? creds["smb_hash"].size : 0
  yield(:status, "start", "LM/NTLM Hash dump") if block_given?
  report_file.write "# LM/NTLM Hashes (%d services, %d hashes)\n" % [scount, count]
  write_credentials("smb_hash",creds,report_file)

  count = count_credentials("smb_netv1_hash",creds)
  scount = creds.has_key?("smb_netv1_hash") ? creds["smb_netv1_hash"].size : 0
  yield(:status, "start", "NETLMv1/NETNTLMv1 Hash dump") if block_given?
  report_file.write "# NETLMv1/NETNTLMv1 Hashes (%d services, %d hashes)\n" % [scount, count]
  write_credentials("smb_netv1_hash",creds,report_file)

  count = count_credentials("smb_netv2_hash",creds)
  scount = creds.has_key?("smb_netv2_hash") ? creds["smb_netv2_hash"].size : 0
  yield(:status, "start", "NETLMv2/NETNTLMv2 Hash dump") if block_given?
  report_file.write "# NETLMv2/NETNTLMv2 Hashes (%d services, %d hashes)\n" % [scount, count]
  write_credentials("smb_netv2_hash",creds,report_file)

  count = count_credentials("ssh_key",creds)
  scount = creds.has_key?("ssh_key") ? creds["ssh_key"].size : 0
  yield(:status, "start", "SSH Key dump") if block_given?
  report_file.write "# SSH Private Keys (%d services, %d keys)\n" % [scount, count]
  write_credentials("ssh_key",creds,report_file)

  count = count_credentials("text",creds)
  scount = creds.has_key?("text") ? creds["text"].size : 0
  yield(:status, "start", "Plaintext Credential dump") if block_given?
  report_file.write "# Plaintext Credentials (%d services, %d credentials)\n" % [scount, count]
  write_credentials("text",creds,report_file)

  report_file.flush
  report_file.close
  yield(:status, "complete", "password dump") if block_given?
  true
end

#to_xml_file(path) {|:status, "start", "report"| ... } ⇒ Object

Yields:

  • (:status, "start", "report")

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

def to_xml_file(path, &block)

  yield(: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|<MetasploitV4>\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, "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, "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, "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, "start", "credentials") if block_given?
  report_file.write %Q|<credentials>\n|
  report_file.flush
  extract_credential_info(report_file)
  report_file.write %Q|</credentials>\n|

  yield(: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, "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, "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, "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, "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|</MetasploitV4>\n|
  report_file.flush
  report_file.close

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

  true
end

#write_credentials(ptype, creds, report_file) ⇒ Object

Formats credentials according to their type, and writes it out to the supplied report file. Note for reimporting: Blank values are <BLANK>


96
97
98
99
100
101
102
103
104
105
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/msf/core/db_export.rb', line 96

def write_credentials(ptype,creds,report_file)
  if creds[ptype]
    creds[ptype].each_pair do |svc, data|
      report_file.write "# #{svc}\n"
      case ptype
      when "smb_hash"
        data.each do |c|
          user = (c.user.nil? || c.user.empty?) ? "<BLANK>" : c.user
          pass = (c.pass.nil? || c.pass.empty?) ? "<BLANK>" : c.pass
          report_file.write "%s:%d:%s:::\n" % [user,c.id,pass]
        end
      when "smb_netv1_hash"
        data.each do |c|
          user = (c.user.nil? || c.user.empty?) ? "<BLANK>" : c.user
          pass = (c.pass.nil? || c.pass.empty?) ? "<BLANK>" : c.pass
          report_file.write "%s::%s\n" % [user,pass]
        end
      when "smb_netv2_hash"
        data.each do |c|
          user = (c.user.nil? || c.user.empty?) ? "<BLANK>" : c.user
          pass = (c.pass.nil? || c.pass.empty?) ? "<BLANK>" : c.pass
          if pass != "<BLANK>"
            pass = (c.pass.upcase =~ /^[\x20-\x7e]*:[A-F0-9]{48}:[A-F0-9]{50,}/nm) ? c.pass : "<BLANK>"
          end
          if pass == "<BLANK>"
            # Basically this is an error (maybe around [\x20-\x7e] in regex) above
            report_file.write(user + "::" + pass + ":")
            report_file.write(pass + ":" +  pass + ":" + pass + "\n")
          else
            datas = pass.split(":")
            if datas[1] != "00" * 24
              report_file.write "# netlmv2\n"
              report_file.write(user + "::" + datas[0] + ":")
              report_file.write(datas[3] + ":" +  datas[1][0,32] + ":" + datas[1][32,16] + "\n")
            end
            report_file.write "# netntlmv2\n"
            report_file.write(user + "::" + datas[0] + ":")
            report_file.write(datas[3] + ":" +  datas[2][0,32] + ":" + datas[2][32..-1] + "\n")
          end
        end
      when "ssh_key"
        data.each do |c|
          if ::File.exists?(c.pass) && ::File.readable?(c.pass)
            user = (c.user.nil? || c.user.empty?) ? "<BLANK>" : c.user
            key = ::File.open(c.pass) {|f| f.read f.stat.size}
            key_id = (c.proof && c.proof[/^KEY=/]) ? c.proof[4,47] : "<NO-ID>"
            report_file.write "#{user} '#{key_id}'\n"
            report_file.write key
            report_file.write "\n" unless key[-1,1] == "\n"
          # Report file missing / permissions issues in the report itself.
          elsif !::File.exists?(c.pass)
            report_file.puts "Warning: missing private key file '#{c.pass}'."
          else
            report_file.puts "Warning: could not read the private key '#{c.pass}'."
          end
        end
      when "text"
        data.each do |c|
          user = (c.user.nil? || c.user.empty?) ? "<BLANK>" : Rex::Text.ascii_safe_hex(c.user, true)
          pass = (c.pass.nil? || c.pass.empty?) ? "<BLANK>" : Rex::Text.ascii_safe_hex(c.pass, true)
          report_file.write "%s:%s:::\n" % [user,pass]
        end
      end
      report_file.flush
    end
  else
    report_file.write "# No credentials for this type were discovered.\n"
  end
  report_file.write "#" * 40; report_file.write "\n"
end