PhieuLang

c.php

Mar 20th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.78 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.  
  5. function caesar($str, $n) {
  6.  
  7.     $ret = "";
  8.     for($i = 0, $l = strlen($str); $i < $l; ++$i) {
  9.         $c = ord($str[$i]);
  10.         if (97 <= $c && $c < 123) {
  11.             $ret.= chr(($c + $n + 7) % 26 + 97);
  12.         } else if(65 <= $c && $c < 91) {
  13.             $ret.= chr(($c + $n + 13) % 26 + 65);
  14.         } else {
  15.             $ret.= $str[$i];
  16.         }
  17.     }
  18.     return $ret;
  19. }
  20.  
  21. function crack_caesar($str) {
  22.  
  23.     $max = 0;
  24.  
  25.     $weight = array(
  26.         6.51, 1.89, 3.06, 5.08, 17.4,
  27.         1.66, 3.01, 4.76, 7.55, 0.27,
  28.         1.21, 3.44, 2.53, 9.78, 2.51,
  29.         0.29, 0.02, 7.00, 7.27, 6.15,
  30.         4.35, 0.67, 1.89, 0.03, 0.04, 1.13);
  31.  
  32.     $c = $s = array(
  33.         0, 0, 0, 0, 0,
  34.         0, 0, 0, 0, 0,
  35.         0, 0, 0, 0, 0,
  36.         0, 0, 0, 0, 0,
  37.         0, 0, 0, 0, 0, 0);
  38.  
  39.     for($i = 0, $l = strlen($str); $i < $l; ++$i) {
  40.         $x = (ord($str[$i]) | 32) - 97;
  41.         if (0 <= $x && $x < 26) {
  42.             ++$c[$x];
  43.         }
  44.     }
  45.  
  46.     for ($off = 0; $off < 26; ++$off) {
  47.  
  48.         for ($i = 0; $i < 26; ++$i) {
  49.             if ($max < ($s[$off]+= 0.01 * $c[$i] * $weight[($i + $off) % 26])) {
  50.                 $max = $s[$off];
  51.             }
  52.         }
  53.     }
  54.     return (26 - array_search($max, $s)) % 26;
  55. }
  56.  
  57. if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
  58. {
  59.     $errorcode = socket_last_error();
  60.     $errormsg = socket_strerror($errorcode);
  61.      
  62.     die("Couldn't create socket: [$errorcode] $errormsg \n");
  63. }
  64. if(!socket_connect($sock , 'web.lasactf.com' , 4056))
  65. {
  66.     $errorcode = socket_last_error();
  67.     $errormsg = socket_strerror($errorcode);
  68.      
  69.     die("Could not connect: [$errorcode] $errormsg \n");
  70. }
  71.  
  72. while(True){
  73.     $text = socket_read ($sock, 1024) or die("Could not read server response\n");
  74.     echo $text;
  75.     if (strpos($text, 'lasactf{') !== false) {
  76.         break;
  77.     }
  78.     $message = caesar($text,26-crack_caesar($text));
  79.     socket_write($sock, $message, strlen($message)) or die("Could not send data to server\n");
  80. }
  81.  
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment