A simple PHP online port scanner

Source: Internet
Author: User
Tags php online reserved

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 cc++.) ), 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 ']; Gets the IP address of the form submission


?>


<html>


<head>


<title> Safety Angel--Port online 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>


</head>


<body>


<center>


<?php


if (!empty ($REMOTEIP)) {


//If the form is not empty, enter the IP address format of the Judge





function Err () {


die ("Sorry, the IP address is not legal <p><a Href=javascript:history.back (1) > Click here to return </a>");


}


//define message for submitting 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 '


);


///IP format after the check with the array to define the corresponding service name and state





echo "<table border=0 cellpadding=15 cellspacing=0>n";


echo "<tr>n";


echo "<td align=center><strong> you scanned 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> 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 display table





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 statement, use the Fsockopen function to connect the remote host port 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 "</TRN";


echo "</table>n";


echo "<table cellspacing=0 cellpadding=10 width=100% border=0>n";


echo "<tr>n";


echo "<td align=center><b>copyright © 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;


}


//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";


//If the form is empty, displays the form that submitted the IP address





?>





<table cellspacing=0 cellpadding=10 width= "100%" border=0>


<TR>


<TD Align=center><b>copyright © Angel team[s4t] All Rights reserved.</b></td>


</TR>


</TABLE>


</center>


</body>


</html>

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.