"❌ Cannot connect to $host:$port"]; } if (!ftp_login($conn, $username, $password)) { ftp_close($conn); return ["error" => "❌ Login failed with username: $username"]; } ftp_pasv($conn, true); $output = fopen($output_file, 'w'); if (!$output) { ftp_close($conn); return ["error" => "❌ Cannot create output file"]; } // Recursive FTP scanning function function scan_ftp_recursive($conn, $path, $output_handle, $current_path = '') { if (!@ftp_chdir($conn, $path)) { return; } $items = ftp_rawlist($conn, "."); if ($items === false) { return; } $dirs = []; $files = []; foreach ($items as $item) { $parts = preg_split('/\s+/', $item, 9); if (count($parts) < 9) continue; $permissions = $parts[0]; $name = $parts[8]; if ($name == '.' || $name == '..') continue; if ($permissions[0] === 'd') { $dirs[] = $name; } else { $files[] = $name; } } sort($dirs); sort($files); foreach ($dirs as $name) { $full_path = ($current_path == '' ? $name : "$current_path/$name"); fwrite($output_handle, $full_path . PHP_EOL); echo $full_path . PHP_EOL; $new_path = ($path == '/' ? "/$name" : "$path/$name"); scan_ftp_recursive($conn, $new_path, $output_handle, $full_path); ftp_cdup($conn); } foreach ($files as $name) { $full_path = ($current_path == '' ? $name : "$current_path/$name"); fwrite($output_handle, $full_path . PHP_EOL); echo $full_path . PHP_EOL; } } fwrite($output, "/" . PHP_EOL); echo "/" . PHP_EOL; scan_ftp_recursive($conn, '/', $output, ''); ftp_close($conn); fclose($output); return ["success" => true]; } function scan_sftp($host, $port, $username, $password, $output_file) { // Check if SSH2 extension is available if (!function_exists('ssh2_connect')) { return ["error" => "❌ SSH2 extension not available. Please install php-ssh2 extension."]; } $conn = ssh2_connect($host, $port); if (!$conn) { return ["error" => "❌ Cannot connect to $host:$port via SFTP"]; } if (!ssh2_auth_password($conn, $username, $password)) { return ["error" => "❌ SFTP login failed with username: $username"]; } $sftp = ssh2_sftp($conn); if (!$sftp) { return ["error" => "❌ Cannot initialize SFTP subsystem"]; } $output = fopen($output_file, 'w'); if (!$output) { return ["error" => "❌ Cannot create output file"]; } // Recursive SFTP scanning function function scan_sftp_recursive($sftp, $path, $output_handle, $current_path = '') { $dir_handle = opendir("ssh2.sftp://{$sftp}{$path}"); if (!$dir_handle) return; $items = []; while (false !== ($entry = readdir($dir_handle))) { if ($entry == '.' || $entry == '..') continue; $items[] = $entry; } closedir($dir_handle); // Separate directories and files $dirs = []; $files = []; foreach ($items as $item) { $full_path = $path . '/' . $item; $stat = ssh2_sftp_stat($sftp, $full_path); if ($stat && ($stat['mode'] & 040000)) { // Check if directory $dirs[] = $item; } else { $files[] = $item; } } sort($dirs); sort($files); foreach ($dirs as $name) { $full_path = ($current_path == '' ? $name : "$current_path/$name"); fwrite($output_handle, $full_path . PHP_EOL); echo $full_path . PHP_EOL; $new_path = ($path == '/' ? "/$name" : "$path/$name"); scan_sftp_recursive($sftp, $new_path, $output_handle, $full_path); } foreach ($files as $name) { $full_path = ($current_path == '' ? $name : "$current_path/$name"); fwrite($output_handle, $full_path . PHP_EOL); echo $full_path . PHP_EOL; } } fwrite($output, "/" . PHP_EOL); echo "/" . PHP_EOL; scan_sftp_recursive($sftp, '/', $output, ''); fclose($output); return ["success" => true]; } function scan_webdav($host, $port, $username, $password, $output_file) { // Simple WebDAV scanner using cURL $output = fopen($output_file, 'w'); if (!$output) { return ["error" => "❌ Cannot create output file"]; } // Basic WebDAV PROPFIND request $url = ($port == '443' ? 'https://' : 'http://') . $host . ':' . $port . '/'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PROPFIND'); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Depth: 1', 'Content-Type: text/xml' ]); $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_code != 207 && $http_code != 200) { curl_close($ch); fclose($output); return ["error" => "❌ WebDAV request failed with HTTP code: $http_code"]; } curl_close($ch); // Parse WebDAV response (simplified) fwrite($output, "/" . PHP_EOL); echo "/" . PHP_EOL; // For demonstration, we'll just list root // Note: Full WebDAV parsing would require XML parsing $lines = [ "WebDAV scanning requires XML parsing", "Root directory: /", "Note: Enable full WebDAV support with sabre/dav library" ]; foreach ($lines as $line) { fwrite($output, $line . PHP_EOL); echo $line . PHP_EOL; } fclose($output); return ["success" => true, "note" => "Basic WebDAV scan completed. Install sabre/dav for full support."]; } // Display HTML form echo '
Protocol: ' . strtoupper($protocol) . '
'; echo 'Host/IP: ' . htmlspecialchars($host) . '
'; echo 'Port: ' . htmlspecialchars($port) . '
'; echo 'Username: ' . htmlspecialchars($username) . '
'; echo 'Password: ' . str_repeat('*', strlen($password)) . '
'; echo 'Anonymous: ' . ($anonymous ? 'Yes' : 'No') . '
'; echo 'You can bookmark this URL with your preferred settings.
'; echo 'Protocol: ' . strtoupper($protocol) . '
'; echo 'Server: ' . htmlspecialchars($host) . ':' . htmlspecialchars($port) . '
'; echo str_repeat('-', 50) . PHP_EOL; // Clear previous output file_put_contents($output_file, ""); // Start scanning based on protocol $result = []; switch ($protocol) { case 'ftp': $result = scan_ftp($host, $port, $username, $password, $output_file); break; case 'sftp': $result = scan_sftp($host, $port, $username, $password, $output_file); break; case 'webdav': $result = scan_webdav($host, $port, $username, $password, $output_file); break; default: $result = ["error" => "❌ Unknown protocol: $protocol"]; } // Display result if (isset($result['error'])) { echo '' . $result['error'] . '
'; } else { echo '✅ Scan completed successfully!
'; if (isset($result['note'])) { echo '' . $result['note'] . '
'; } } echo 'Protocol: ' . strtoupper($protocol) . '
'; echo 'Server: ' . htmlspecialchars($host) . ':' . htmlspecialchars($port) . '
'; echo 'Output File: ' . htmlspecialchars(basename($output_file)) . '
'; echo 'Size: ' . filesize($output_file) . ' bytes
'; $lines = count(file($output_file)); echo 'Total Items: ' . $lines . '
'; // Count lines (simplified) $content = file_get_contents($output_file); $lines_array = explode(PHP_EOL, trim($content)); $valid_lines = array_filter($lines_array, function($line) { return !empty($line) && $line !== '/' && !str_contains($line, 'Note:') && !str_contains($line, 'WebDAV'); }); echo 'Valid Items: ' . count($valid_lines) . '
'; // Provide download link echo ''; echo '