<?php
declare(strict_types=1);
ini_set('memory_limit', '350M');
if ($argc < 3) {
fwrite(STDERR, "Usage: php conv.php <scanfile.txt> <output_base>\n");
fwrite(STDERR, "Example: php conv.php ./scan/scan_123.txt ./scan/done/example\n");
exit(1);
}
$inputFile = $argv[1];
$outputBase = $argv[2];
if (!file_exists($inputFile)) {
fwrite(STDERR, "File not found: $inputFile\n");
exit(1);
}
$outputDir = dirname($outputBase);
if (!is_dir($outputDir)) {
mkdir($outputDir, 0755, true);
}
function loadExistingFileData(string $filePath): array {
$existingUrls = [];
$existingMacs = [];
$existingUserPass = [];
if (file_exists($filePath)) {
$fileContent = file_get_contents($filePath);
preg_match_all('/http:\/\/([a-zA-Z0-9.-]+)(:\d+)?/', $fileContent, $existingUrls);
$existingUrls = isset($existingUrls[0]) ? array_unique($existingUrls[0]) : [];
preg_match_all('/[0-9A-Fa-f]{2}(:[0-9A-Fa-f]{2}){5}/', $fileContent, $existingMacs);
$existingMacs = isset($existingMacs[0]) ? array_unique($existingMacs[0]) : [];
preg_match_all('/get\.php\?username=([a-zA-Z0-9._]+)&password=([a-zA-Z0-9._]+)/', $fileContent, $existingUserPass);
$existingUserPass = (isset($existingUserPass[1], $existingUserPass[2]))
? array_map(fn($u, $p) => "$u/$p", $existingUserPass[1], $existingUserPass[2])
: [];
}
return [
'urls' => $existingUrls,
'macs' => $existingMacs,
'userpass' => $existingUserPass,
];
}
$inputData = file_get_contents($inputFile);
preg_match_all('/http:\/\/([a-zA-Z0-9.-]+)(:\d+)?/', $inputData, $urls);
preg_match_all('/[0-9A-Fa-f]{2}(:[0-9A-Fa-f]{2}){5}/', $inputData, $macs);
preg_match_all('/get\.php\?username=([a-zA-Z0-9._]+)&password=([a-zA-Z0-9._]+)/', $inputData, $userPass);
$outputMacFile = $outputBase . '_mac.txt';
$outputUpFile = $outputBase . '_up.txt';
if (!empty($urls[0])) {
$portalUrl = $urls[0][0];
$existingData = loadExistingFileData($outputMacFile);
$existingUrls = $existingData['urls'];
$existingMacs = $existingData['macs'];
$existingUserPass = loadExistingFileData($outputUpFile)['userpass'];
$newMacs = array_unique(array_diff($macs[0], $existingMacs));
$newUserPassCombos = array_map(fn($u, $p) => "$u/$p", $userPass[1], $userPass[2]);
$newUserPassCombos = array_unique(array_diff($newUserPassCombos, $existingUserPass));
if (!file_exists($outputMacFile) || strpos(file_get_contents($outputMacFile), $portalUrl) === false) {
file_put_contents($outputMacFile, $portalUrl . PHP_EOL, FILE_APPEND);
}
if (!empty($newMacs)) {
$macFileHandle = fopen($outputMacFile, 'a');
foreach ($newMacs as $mac) {
fwrite($macFileHandle, "$mac\n");
}
fclose($macFileHandle);
chmod($outputMacFile, 0755);
echo "MACs saved to $outputMacFile\n";
}
if (!file_exists($outputUpFile) || strpos(file_get_contents($outputUpFile), $portalUrl) === false) {
file_put_contents($outputUpFile, $portalUrl . PHP_EOL, FILE_APPEND);
}
if (!empty($newUserPassCombos)) {
$upFileHandle = fopen($outputUpFile, 'a');
foreach ($newUserPassCombos as $combo) {
fwrite($upFileHandle, "$combo\n");
}
fclose($upFileHandle);
chmod($outputUpFile, 0755);
echo "User/Pass combos saved to $outputUpFile\n";
}
if (empty($newMacs) && empty($newUserPassCombos)) {
echo "Nothing new to add. Files are already up-to-date.\n";
}
} else {
echo "No valid portal URLs found in scan file.\n";
}