PHP开发技巧:PHP设置端口扫描器代码
本文介绍PHP设置端口扫描器代码。
<?PHP
//Codz by angel
//Welcome to My Website
//http://www.4ngel.net
$youip=$HTTP_SERVER_VARS["REMOTE_ADDR"]; // 获取本机IP地址
$remoteip=$HTTP_POST_VARS['remoteip']; // 获取表单提交的IP地址
?>
<html>
<head>
<title>安全天使——端口在线检测</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style TYPE="text/css">
<!--
BODY { FONT-SIZE: 12px; FONT-FAMILY: Verdana;color:#000000;
}
TD {
FONT-SIZE: 12px;
FONT-FAMILY: Verdana;
color:#000000;
line-height: 14px;
}
.style1 {color: #FFFFFF}
-->
</style>
</head>
<body>
<center>
<?php
if (!empty($remoteip)){
// 如果表单不为空就进入IP地址格式的判断
function err() {
die("对不起,该IP地址不合法<p><a href=JavaScript:history.back(1)>点击这里返回</a>");
}
// 定义提交错误IP的提示信息
$ips=explode(".",$remoteip);
// 用.分割IP地址
if (intval($ips[0])<1 or intval($ips[0])>255 or intval($ips[3])<1 or intval($ips[3]>255)) err();
// 如果第一段和最后一段IP的数字小于1或者大于255,则提示出错
if (intval($ips[1])<0 or intval($ips[1])>255 or intval($ips[2])<0 or intval($ips[2]>255)) err();
// 如果第二段和第三段IP的数字小于0或者大于255,则提示出错
$closed='此端口目前处于关闭状态。';
$opened='<font color=red>此端口目前处于打开状态!</font>';
$close="关闭";
$open="<font color=red>打开</font>";
$port=array(21,23,25,79,80,110,135,137,138,139,143,443,445,1433,3306,3389);
$msg=array(
'Ftp',
'Telnet',
'Smtp',
'Finger',
'Http',
'Pop3',
'Location Service',
'Netbios-NS',
'Netbios-DGM',
'Netbios-SSN',
'IMAP',
'Https',
'Microsoft-DS',
'MSSQL',
'MySQL',
'Terminal Services'
);
// 通过IP格式的检查后用数组定义各端口对应的服务名称及状态
echo "<table border=0 cellpadding=15 cellspacing=0>n";
echo "<tr>n";
echo "<td align=center><strong>您扫描的IP:<font
color=red>".$remoteip."</font></strong></td>n";
echo "</tr>n";
echo "</table>n";
echo "<table cellpadding=5 cellspacing=1 bgcolor=#636194>n";
echo "<tr bgcolor=#7371A5 align=center>n";
echo "<td><span class=style1>端口</span></td>n";
echo "<td><span class=style1>服务</span></td>n";
echo "<td><span class=style1>检测结果</span></td>n";
echo "<td><span class=style1>描述</span></td>n";
echo "</tr>n";
// 输出显示的表格
for($i=0;$i<sizeof($port);$i++)
{
$fp = @fsockopen($remoteip, $port[$i], &$errno, &$errstr, 1);
if (!$fp) {
echo "<tr bgcolor=#FFFFFF><td align=center>".$port[$i]."</td><td>".$msg[$i]."</td><td
align=center>".$close."</td><td>".$closed."</td></tr>n";
} else {
echo "<tr bgcolor=#F4F7F9><td align=center>".$port[$i]."</td><td>".$msg[$i]."</td><td
align=center>".$open."</td><td>".$opened."</td></tr>";
}
}
// 用for语句,分别用fsockopen函数连接远程主机的相关端口,并输出结果
echo "<tr><td colspan=4 align=center>n";
echo "<a href=portscan.php><font color=#FFFFFF>继续扫描>>></font></a></td>n";
echo "</trn";
echo "</table>n";
echo "<TABLE cellSpacing=0 cellPadding=10 width=100% border=0>n";
echo "<TR>n";
echo "<TD align=center><b>Copyright © 2004 Security Angel Team[S4T] All Rights Reserved.</b></TD>n";
echo "</TR>n";
echo "</TABLE>n";
echo "</center>n";
echo "</body>n";
echo "</html>n";
exit;
}
// 探测结束
echo "<table border=0 cellpadding=15 cellspacing=0>n";
echo "<tr>n";
echo "<td align=center><strong>您的IP:<font color=red>".$youip."</font></strong></td>n";
echo "</tr>n";
echo "<form method=POST action=portscan.php>n";
echo "<tr><td>n";
echo "<input type=text name=remoteip size=12>n";
echo "<input type=submit value=扫描 name=scan>n";
echo "</td></tr>n";
echo "</form>";
echo "</table>n";
// 如果表单为空则显示提交IP地址的表单
?>
<TABLE cellSpacing=0 cellPadding=10 width="100%" border=0>
<TR>
<TD align=center><b>Copyright © 2004 Security Angel Team[S4T] All Rights Reserved.</b></TD>
</TR>
</TABLE>
</center>
</body>
</html>