A simple PHP online port scanner

Source: Internet
Author: User
Tags format define array empty connect php online reserved
Port Scan | Online Preface

PHP is a powerful web development language. The development of high efficiency, simple syntax, for dynamic Web sites tailored to strengthen the object-oriented (to C + +, and Java to build a point), but a single thread (this is the Achilles ' heel, it is said that PHP is written with c\c++.) ), can also use C, C + +, Java development of the middle tier, call COM, server maintenance difficult, less trouble.

Since it is tailor-made for dynamic Web sites, it is doomed not to develop such a x-scan scanner, but if you want to achieve a number of simple functions, or more than sufficient.

Port scanning is our most common method of casing. If you're in a place like this, downloading a specialized scanner is a hassle if you use an existing Web service-provided port scan. That's really a lot to save.

Let's take a look at the source code of this PHP port scanner I wrote:

Code:

--------------------------------------------------------------------------------

<?php

Codz by Angel

$youip = $HTTP _server_vars["REMOTE_ADDR"]; Get native IP address
$remoteip = $HTTP _post_vars[' Remoteip ']; Get the IP address of the form submission
?>
<title> Security Angel--port on-line detection </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>
<body>
<center>
<?php
if (!empty ($REMOTEIP)) {
The decision to enter the IP address format if the form is not empty

function err () {
Die ("Sorry, the IP address is not legal <p><a Href=javascript:history.back (1) > Click here to return </a>");
}
To define a message that submits error IP

$ips =explode (".", $remoteip);
Use. Split IP Address

if (intval ($ips [0]) <1 or intval ($ips [0]) >255 or Intval ($ips [3]) <1 or intval ($ips [3]>255)) Err ();
If the number of the first and last IP is less than 1 or greater than 255, an error is prompted

if (Intval ($ips [1]) <0 or Intval ($ips [1]) >255 or Intval ($ips [2]) <0 or intval ($ips [2]>255)) Err ();
If the number of IP in the second and third segments is less than 0 or greater than 255, an error is prompted

$closed = ' This port is currently off. ';
$opened = ' <font color=red> This port is currently open! </font> ';
$close = "Close";
$open = "<font color=red> open </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 '
);
Define the service name and status of each port by using an array after checking IP format

echo "<table border=0 cellpadding=15 cellspacing=0>\n";
echo "<tr>\n";
echo "<td align=center><strong> the Ip:<font you scanned
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> port </span></td>\n";
echo "<td><span class=style1> service </span></td>\n";
echo "<td><span class=style1> test results </span></td>\n";
echo "<td><span class=style1> description </span></td>\n";
echo "</tr>\n";
Output the table displayed

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> ";
}
}
Using the For statement, connect the remote host's associated port with the Fsockopen function, and output the result

echo "<tr><td colspan=4 align=center>\n";
echo "<a Href=portscan.php><font color= #FFFFFF > Continue scanning >>></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 "&LT;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 "Exit
}
Probe end

echo "<table border=0 cellpadding=15 cellspacing=0>\n";
echo "<tr>\n";
echo "<td align=center><strong> your 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= scan name=scan>\n";
echo "</td></tr>\n";
echo "</form>";
echo "</table>\n";
Displays a form that submits an IP address if the form is empty

?>

<table cellspacing=0 cellpadding=10 width= "100%" border=0>
<TR>
&LT;TD align=center><b>copyright©2004 Security Angel team[s4t] all Rights reserved.</b></td>
</TR>
</TABLE>
</center>
</body>

--------------------------------------------------------------------------------



Postscript

This scanner is very simple. is to use an array to define the relevant information of the port, the principle is to use the Fsockopen function connection, if you can connect, it means that the port is open, otherwise it is closed.

The biggest drawback is that PHP is single-threaded, so the speed will be very slow, this is convenient, simple as the price, in fact, write this code is to tell you, PHP is not only for dynamic Web site development, can also be used in the field of network security, often too much attention to the work of things, will ignore other aspects of the characteristics.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.