这可能是重复的帖子,但我还没有看到任何正确解决此问题的答案。
我正在尝试找到一个可以正确确定 TCP 或 UDP 端口状态的 php 脚本。
我已经尝试了几个我在网上找到的方法,但它们都返回了错误的结果。 EG:在我的本地网络上,我打开了端口 5000 UDP 和 5060 TCP/UDP,并且路由正确。
如果我通过 http://www.ipfingerprints.com/portscan.php 运行测试或 GRC Shields Up 然后返回正确的结果,但我尝试过的所有 PHP 脚本都失败并显示端口已关闭。
我正在通过我的托管帐户运行 php 脚本,并尝试扫描和测试我自己的网络。
这是我试过的脚本之一:
<html>
<head>
<?php echo "<title>Port Scanning " . $_GET['host'] . "</title>"; ?>
</head>
<body>
<?php
ini_set('display_errors', 0);
if(isset($_GET['host']) == true)
$host = $_GET['host'];
else
{
echo "You didn't set the host parameter";
die();
}
if(isset($_GET['sport']) == true)
$sport = $_GET['sport'];
else
{
echo "You didn't set the sport parameter";
die();
}
if(isset($_GET['eport']) == true)
$eport = $_GET['eport'];
else
{
echo "You didn't set the eport parameter";
die();
}
if($sport > $eport)
{
$sport = $eport;
}
$scanned = 0;
$openports = 0;
$maxports = 1;
$totalports = ($eport - $sport) + 1;
$mins = floor(($totalports / 10) / 60);
$secs = ($totalports / 10) - $mins * 60;
echo "<font color=\"blue\">Scanning " . $totalports . " ports on " . $host . ", this should take around " . $mins . " minute(s) and " . $secs . " second(s), probably more depending on local website load, hardware, and other factors.<br/><br/></font>";
flush();
do
{
if($scanned >= $maxports && $maxports != 0)
{
echo "<font color=\"darkgreen\" size=\"4\">Scan limit of " . $maxports . " ports reached, scanning stopped.</font><br/><br/><font color=\"darkred\" size=\"4\">Scanner written by Samo502</font>";
flush();
die();
}
if(fsockopen($host, $sport, $errorno, $errorstr, 0.1))
{
echo "<font color=\"darkgreen\">Port " . $sport . " is open on " . $host . "</font><br/>";
$openports++;
flush();
}
else
{
echo "<font color=\"red\">Port " . $sport . " is not open on " . $host . "</font><br/>";
flush();
}
$scanned++;
$sport++;
} while($sport <= $eport);
echo "<br/><font color=\"blue\">Done, " . $openports . " out of " . $totalports . " ports are open.</font><br/><br/><br/><font color=\"darkred\" size=\"4\">Scanner written by Samo502</font>";
die();
?>
</body>
</html>
有谁知道正确执行此操作的方法吗?
谢谢
**更新** 我发现这似乎工作得更好一些,但它仍然没有检测到端口 21 当前在我的网络上打开..
http://resources.infosecinstitute.com/php-build-your-own-mini-port-scanner-2/
感谢收到的任何帮助。谢谢
最佳答案
最好的解决方案是用 PHP 为 nmap
编写一个包装器 - 说真的,你无法重新编写比 nmap 更好的端口扫描程序。
如果你真的想这样做,你不应该简单地打开 TCP 端口并检查它是否连接,你应该从较低级别的套接字开始,原始套接字。
$mysocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
这使您可以调查 SYN
、ACK
握手标志并进行隐蔽或更快速的扫描。
您可以并行创建(非阻塞)套接字并使用 socket_select();
检查它们的缓冲区/连接,这是在 PHP 中处理多个套接字的最快和最可靠的方法。我可能会使用 fork 或线程或分派(dispatch)部分代码来在单个端口扫描的同时运行完整的端口扫描。
您想要使用的其他函数是 socket_set_timeout()
和 socket_set_blocking()
。
shell_exec not working with nmap command
https://unix.stackexchange.com/questions/28732/fastest-way-to-port-scan-nmap
http://phpnmap.sourceforge.net/
可能是最好的解决方案,一个 PEAR 包 nmap 包装器:http://pear.php.net/package/Net_Nmap/
关于PHP端口扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24279991/