Created
March 11, 2019 21:47
-
-
Save flashwave/a1a5186d4574a8eb72f2994518a05099 to your computer and use it in GitHub Desktop.
SockTP reconstructor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$lines = explode("\n", file_get_contents('out.txt')); | |
$user = '1'; | |
$contexts = []; | |
class STPCTX { | |
public $blocks = []; | |
public $fileName = ''; | |
public $streamHash = ''; | |
public $sender = 0; | |
public function __construct(string $hash, string $fileName, int $blocks, int $sender = 0) { | |
$this->fileName = base64_decode($fileName); | |
$this->streamHash = $hash; | |
$this->blocks = array_fill(0, $blocks, 0); | |
$this->sender = 0; | |
} | |
public function verifySender(int $sender): bool { | |
return $sender < 1 || $this->sender === $sender; | |
} | |
public function appendBlock(int $num, string $data): void { | |
$this->blocks[$num] = base64_decode($data); | |
} | |
public function save(): bool { | |
$data = ''; | |
for ($i = 0; $i < count($this->blocks); $i++) | |
$data .= $this->blocks[$i]; | |
if (hash('sha256', $data) !== $this->streamHash) | |
return false; | |
file_put_contents($this->fileName, gzdecode($data)); | |
return true; | |
} | |
} | |
foreach ($lines as $line) { | |
$sc = explode("\t", $line); | |
if (count($sc) < 3) | |
continue; | |
$csv = explode(',', $sc[2]); | |
switch ($csv[0]) { | |
case 'STPv1OPN': | |
if (array_key_exists($csv[1], $contexts)) { | |
echo 'Context is already active?', PHP_EOL; | |
break; | |
} | |
echo "N[{$csv[1]}] H[{$csv[2]}] F[{$csv[3]}] B[{$csv[4]}] Opened new SockTP context", PHP_EOL; | |
$contexts[$csv[1]] = new STPCTX($csv[2], $csv[3], $csv[4], (int)$sc[1]); | |
break; | |
case 'STPv1BLK': | |
if (!array_key_exists($csv[1], $contexts)) { | |
echo 'Invalid context.', PHP_EOL; | |
break; | |
} | |
if ($contexts[$csv[1]]->verifySender((int)$sc[1])) { | |
echo 'Someone pretended to be the sender, packet ignored.', PHP_EOL; | |
break; | |
} | |
echo "N[{$csv[1]}] I[{$csv[2]}] Received block", PHP_EOL; | |
$contexts[$csv[1]]->appendBlock($csv[2], $csv[3]); | |
break; | |
case 'STPv1FIN': | |
if (!array_key_exists($csv[1], $contexts)) { | |
echo 'Invalid context.', PHP_EOL; | |
break; | |
} | |
if ($contexts[$csv[1]]->verifySender((int)$sc[1])) { | |
echo 'Someone pretended to be the sender, packet ignored.', PHP_EOL; | |
break; | |
} | |
echo "N[{$csv[1]}] Transfer finished!", PHP_EOL; | |
if (!$contexts[$csv[1]]->save()) | |
echo 'Stream hash mismatch, transfer failed.', PHP_EOL; | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment