前言
php是一種功能強(qiáng)大的語(yǔ)言。開(kāi)發(fā)效率高,語(yǔ)法簡(jiǎn)單,為動(dòng)態(tài)網(wǎng)站量身定做,加強(qiáng)面向?qū)ο螅ㄏ騝++靠攏,與java搭了點(diǎn)邊),可惜單線程(這是至命弱點(diǎn),據(jù)說(shuō)php是用c/c++編寫(xiě)的。),也能用c、c++、java開(kāi)發(fā)中間層,調(diào)用com,服務(wù)器維護(hù)難度小,故障少。
既然是為動(dòng)態(tài)網(wǎng)站量身定做,所以注定不能開(kāi)發(fā)出x-scan這樣的超強(qiáng)掃描器,不過(guò)如果要實(shí)現(xiàn)一些簡(jiǎn)單的功能,還是綽綽有余的。
端口掃描是我們最常用的踩點(diǎn)手段。如果在網(wǎng)吧這種地方,下載一個(gè)專(zhuān)門(mén)的掃描器又比較麻煩,如果利用現(xiàn)有的web服務(wù)提供的端口掃描。那可真的是省了不少事。
下面我們就看看我寫(xiě)的這個(gè)php端口掃描器的源代碼:
<?php
//codz by angel
//welcome to my website
//http://www.4ngel.net
$youip=$http_server_vars["remote_addr"]; // 獲取本機(jī)ip地址
$remoteip=$http_post_vars['remoteip']; // 獲取表單提交的ip地址
?>
<html>
<head>
<title>安全天使——端口在線檢測(cè)</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)){
// 如果表單不為空就進(jìn)入ip地址格式的判斷
function err() {
die("對(duì)不起,該ip地址不合法<p><a href=javascript:history.back(1)>點(diǎn)擊這里返回</a>");
}
// 定義提交錯(cuò)誤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的數(shù)字小于1或者大于255,則提示出錯(cuò)
if (intval($ips[1])<0 or intval($ips[1])>255 or intval($ips[2])<0 or intval($ips[2]>255)) err();
// 如果第二段和第三段ip的數(shù)字小于0或者大于255,則提示出錯(cuò)
$closed='此端口目前處于關(guān)閉狀態(tài)。';
$opened='<font color=red>此端口目前處于打開(kāi)狀態(tài)!</font>';
$close="關(guān)閉";
$open="<font color=red>打開(kāi)</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'
);
// 通過(guò)ip格式的檢查后用數(shù)組定義各端口對(duì)應(yīng)的服務(wù)名稱(chēng)及狀態(tài)
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>服務(wù)</span></td>/n";
echo "<td><span class=style1>檢測(cè)結(jié)果</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語(yǔ)句,分別用fsockopen函數(shù)連接遠(yuǎn)程主機(jī)的相關(guān)端口,并輸出結(jié)果
echo "<tr><td colspan=4 align=center>/n";
echo "<a href=portscan.php><font color=#ffffff>繼續(xù)掃描>>></font></a></td>/n";
echo "</tr/n";
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;
}
// 探測(cè)結(jié)束
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>
后記
這個(gè)掃描器很簡(jiǎn)單。就是用了一個(gè)數(shù)組來(lái)定義端口的相關(guān)信息,原理就是用fsockopen函數(shù)連接,如果可以連接,就表示端口打開(kāi),否則就是關(guān)閉。
最大的缺點(diǎn)就是php是單線程的,所以速度會(huì)很慢,這個(gè)是用方便、簡(jiǎn)單作為代價(jià)的,其實(shí)寫(xiě)這個(gè)代碼的就是想告訴大家,php并不僅僅用于動(dòng)態(tài)網(wǎng)站的開(kāi)發(fā),也可以用于網(wǎng)絡(luò)安全領(lǐng)域,往往太注意事物的本職工作,就會(huì)忽略其他方面的特點(diǎn)。注冊(cè)會(huì)員,創(chuàng)建你的web開(kāi)發(fā)資料庫(kù),