Skip to content

Instantly share code, notes, and snippets.

@ammarfaizi2
Last active September 16, 2018 07:44
Show Gist options
  • Save ammarfaizi2/3f565847adacd12e3eb959e40a81dbcc to your computer and use it in GitHub Desktop.
Save ammarfaizi2/3f565847adacd12e3eb959e40a81dbcc to your computer and use it in GitHub Desktop.
<?php
$pipes = null;
$descriptorspec = [
0 => ["pipe", "r"], // STDOUT
1 => ["pipe", "w"], // STDIN
2 => ["pipe", "w"] // STDERR
];
unset($_SERVER["argv"]);
$process = proc_open("/bin/nc 172.104.185.58 8080", $descriptorspec, $pipes, getcwd(), $_SERVER);
$pat = "/(?:Pesan:\s)(.*)\n/Us";
print fread($pipes[1], 1024).($aa = fread($pipes[1], 1024));
while ($n = preg_match($pat, $aa, $m)) {
print $aa = trdd($m[1]);
fwrite($pipes[0], $aa) xor print "\n".($aa = fread($pipes[1], 1024));
}
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
exit(0);
function trdd($str) {
$a = ["pertolongan","Suatu","hari","ada","seekor","Gajah","Tubuhnya","tinggi","besar","dan","gemuk","Belalainya","sangat","panjang","kuat","Sepasang","gading","yang","kokoh","itu","baik","hati","selalu","memberikan","makanan","kepada","kelaparan","Dan","ia","pun","mereka","kesusahan","Baik","maupun","binatang","kecil","seperti","tikus","semut","Pada","suatu","mengadakan","perjalanan","jauh"];
$sta = explode(" ", $str);
$st = new Decryptor();
$result = [];
for ($i=-20; $i <= 20; $i++) {
foreach (explode(" ", $st->decrypt($str, $i)) as $k => $v) {
in_array($v, $a) && !isset($result[$sta[$k]]) and $result[$sta[$k]] = $v;
}
}
$rr = "";
foreach ($sta as $v) {
$rr .= $result[$v]." ";
}
return trim($rr);
}
class Decryptor
{
public function decrypt($string, $key)
{
$thisPtr = &$this;
return implode("", array_map(function ($crd) use ($key, &$thisPtr) {
return $thisPtr->shift($crd, $key);
}, str_split($string)));
}
private function shift($crd, $shift)
{
// https://en.wikipedia.org/wiki/Caesar_cipher
$cce = ord($crd);
$shifted = $cce - $shift % 26;
if ($cce >= 65 && $cce <= 90) {
return chr($this->up($shifted));
}
if ($cce >= 97 && $cce <= 122) {
return chr($this->lower($shifted));
}
return chr($cce);
}
private function up($cce)
{
$cce < 65 and $cce = 91 - (65 - $cce);
$cce > 90 and $cce = ($cce - 90) + 64;
return $cce;
}
private function lower($cce)
{
$cce < 97 and $cce = 123 - (97 - $cce);
$cce > 122 and $cce = ($cce - 122) + 96;
return $cce;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment