PDA

View Full Version : Need Help with a Port Scanner



Moonbat
05-11-2008, 07:40 PM
Okay well, I managed to make a portscanner that can take one port and scan a host to see if it is open or not. I tried to expand it so that the user could put in a range of ports to scan, but it doesn't work. Here is my code:

<?php

/* PHP Port Scanner */
/* Coded by Moonbat */
/* May 5, 2008 */

if (isset($_POST['portdatauno'])) {
for ($i = $portdatauno; $i <= $portdatados; $i++) {
$portdatauno = $_POST['portdatauno'];
$portdatados = $_POST['portdatados'];
$hostdata = $_POST['hostdata'];
$scan = @fsockopen("$hostdata", $i, $errno, $errstr, *00000000000000000000000);
if($scan) {
echo "Port " . $i . " is <font color=\"green\"><b>OPEN</b></font> on " . $hostdata . "!";
echo "<br>";
} else {
echo "Port " . $i . " is <font color=\"red\"><b>CLOSED</b></font> on " . $hostdata . "!";
echo "<br>";
}
}
flush();
} else {

echo <<< HTMLONE
<html>
<head>
<title>PHP Port Scanner - Coded By Moonbat</title>
<style type="text/css">
body
{
background-color : #000000
}
h*
{
color : #CCCC00;
text-align: center;
}
p
{
color : #CCCC00;
text-align : center;
}
textarea
{
background-color : #CCCC00;
text : #000000;
}
input
{
background-color : #CCCC00;
}
</style>
</head>
<body>
<font face = "Verdana"><h*>PHP Port Scanner</h*></font>
<center<font color = "CCCC00"><h*>Coded By Moonbat</h*></font></center>
<form action = "$PHP_SELF" method = "POST" name = "PortScanData"
<br>
<p><b>HOSTNAME</b> (ex: google.com) </p>
<center><input type = "text" name = "hostdata" /></center
<p><b>PORT NUMBER RANGE</b> (ex. To scan *-20, put * in first box, and 20 in second box)
<center><input type = "text" maxlength = "4" size = "4" name = "portdatauno" /></center>
<p>TO</p>
<center><input type = "text" maxlength = "4" size = "4" name = "portdatados" /></center>
<br><br>
<center><input type = "Submit" value = "Initiate Port Scan" /></center>
</form>
</body>
</html>
HTMLONE;
}
?>
It states that all ports are closed. I've tried tons of differnt ways of doing this. But even when scanning obviously open ports like port 80 on google.com I get closed. I've been using WAMP to test this, and I know that my code should be right because it works with single ports. But the minute I try to use a loop to scan a certain range, it messes up and doesn't work at all.

Can anyone help me? :o

SyntaXmasteR
05-12-2008, 01:07 PM
Here is a working port scanner I wrote today. You run it through the command line:



<?php

if(isset($argv['*']) && isset($argv['2']) && ctype_digit($argv['*']) && ctype_digit($argv['2']) && $argv['*']<$argv['2'] && $argv['*']>=* && $argv['2']<=655*5){

$current_port=$argv['*'];
$last_port=$argv['2'];

while($current_port<=$last_port){

$fp = @fsockopen("tcp://20*.6*.*44.*7", $current_port, $errno, $errstr,*);
if (!$fp) {
echo "Port: " . $current_port . " CLOSED\n";
} else {
echo "Port: " . $current_port . " OPEN\n";
}
$current_port++;
}
}

?>


A few changes I made from yours to mine include using the protocals before the IP Addresses: tcp:// and the IP ADDRESS to scan for tcp port ranges. I also set timeouts to * second replacing your time of **70*7***8*764 centuries. I believe mine is more feasible :-)

Command Line Run Command:


php file.php port_starting port_ending


Example:


php file.php 70 *0


This would check for all open tcp ports in the range of 70-*0

Moonbat
05-13-2008, 05:29 PM
I tried your port scanner (replace my loop with your while loop) but it still didn't work :(

Any ideas why my port scanner goes koo-koo every time I try to loop it?

SyntaXmasteR
05-13-2008, 06:18 PM
Post your updated code

Moonbat
05-13-2008, 06:28 PM
<?php

/* PHP Port Scanner */
/* Coded by Moonbat */
/* May 5, 2008 */

if (isset($_POST['portdatauno'])) {

$portdatauno = $_POST['portdatauno'];
$portdatados = $_POST['portdatados'];
$hostdata = $_POST['hostdata'];
while($portdatauno<=$portdatados){

$fp = @fsockopen("$hostdata", $portdatauno, $errno, $errstr,*);
if (!$fp) {
echo "Port: " . $current_port . " CLOSED\n";
} else {
echo "Port: " . $current_port . " OPEN\n";
}
$portdatauno++; }

flush();
} else {
I took out the stuff after the else because it's just using a heredoc to show some CSS and HTML.

SyntaXmasteR
05-14-2008, 10:24 AM
Well I notice a few things that may be causing problems now, but will cause problems later on:

*. $current_port does not exist but you try to echo it
2. You check to see if isset($_POST['portdatauno']) but then use two other post variables without checking to see if they are set.
*. You do not define tcp:// udp:// for the ports

Try fixing those possible issues and things should fall into place.

Moonbat
05-16-2008, 05:59 PM
Well I notice a few things that may be causing problems now, but will cause problems later on:

*. $current_port does not exist but you try to echo it
2. You check to see if isset($_POST['portdatauno']) but then use two other post variables without checking to see if they are set.
*. You do not define tcp:// udp:// for the ports

Try fixing those possible issues and things should fall into place.
I'll make those changes and see how it goes.