<?php
declare(strict_types=1);
ini_set('memory_limit', '350M');
if ($argc < 3) {
fwrite(STDERR, "Usage: php process_scans_cli.php <scanfile.txt> <output_dir>\n");
exit(1);
}
$inputFile = $argv[1];
$outputDir = rtrim($argv[2], '/');
if (!file_exists($inputFile)) {
fwrite(STDERR, "File not found: $inputFile\n");
exit(1);
}
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 = $existingUrls[0] ?? [];
preg_match_all('/[0-9A-Fa-f]{2}(:[0-9A-Fa-f]{2}){5}/', $fileContent, $existingMacs);
$existingMacs = $existingMacs[0] ?? [];
preg_match_all('/([a-zA-Z0-9._-]+)\/([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' => array_unique($existingUrls),
'macs' => array_unique($existingMacs),
'userpass' => array_unique($existingUserPass),
];
}
$lines = file($inputFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$currentPortal = null;
$portalBlocks = [];
foreach ($lines as $line) {
$line = trim($line);
if ($line === '') continue;
if (preg_match('/^http:\/\/[a-zA-Z0-9.-]+(:\d+)?\/?$/', $line)) {
$currentPortal = $line;
if (!isset($portalBlocks[$currentPortal])) {
$portalBlocks[$currentPortal] = ['macs' => [], 'userpass' => []];
}
} elseif ($currentPortal !== null) {
if (preg_match('/^[0-9A-Fa-f]{2}(:[0-9A-Fa-f]{2}){5}$/', $line)) {
$portalBlocks[$currentPortal]['macs'][] = strtoupper($line);
} elseif (preg_match('/get\.php\?username=([a-zA-Z0-9._-]+)&password=([a-zA-Z0-9._-]+)/', $line, $m)) {
$portalBlocks[$currentPortal]['userpass'][] = $m[1] . '/' . $m[2];
}
}
}
foreach ($portalBlocks as $portalUrl => $data) {
$host = parse_url($portalUrl, PHP_URL_HOST);
$port = parse_url($portalUrl, PHP_URL_PORT);
if (!$host) continue;
$baseName = preg_replace('/[^a-zA-Z0-9]+/', '_', $host);
if ($port !== null && $port != 80) {
$baseName .= '_' . $port;
}
$macFile = $outputDir . '/' . $baseName . '_mac.txt';
$upFile = $outputDir . '/' . $baseName . '_up.txt';
$existing = loadExistingFileData($macFile);
if (!in_array($portalUrl, $existing['urls'])) {
file_put_contents($macFile, $portalUrl . PHP_EOL, FILE_APPEND);
}
$newMacs = array_diff($data['macs'], $existing['macs']);
if (!empty($newMacs)) {
file_put_contents($macFile, implode(PHP_EOL, $newMacs) . PHP_EOL, FILE_APPEND);
chmod($macFile, 0755);
echo "MACs saved to $macFile\n";
}
$existing = loadExistingFileData($upFile);
if (!in_array($portalUrl, $existing['urls'])) {
file_put_contents($upFile, $portalUrl . PHP_EOL, FILE_APPEND);
}
$newUserPass = array_diff($data['userpass'], $existing['userpass']);
if (!empty($newUserPass)) {
file_put_contents($upFile, implode(PHP_EOL, $newUserPass) . PHP_EOL, FILE_APPEND);
chmod($upFile, 0755);
echo "User/Pass combos saved to $upFile\n";
}
}
if (empty($portalBlocks)) {
echo "No valid portal URLs found in $inputFile\n";
} else {
echo "Processing complete.\n";
}
?>