PHP Port Scan

This may be a duplicate of the message, BUT I did not see an answer that correctly resolves this.

I am trying to find a php script that can correctly determine the status of a TCP or UDP port.

I tried several of the ones that I found on the Internet, and they all return the wrong results. EG: On my local network, I have 5000 UDP and 5060 TCP / UDP ports open and routed correctly.

If I run the test through http://www.ipfingerprints.com/portscan.php or GRC Shields Up, then the correct results will return, but all the PHP scripts I've tried to crash and show the ports as closed.

I am running a php script through my hosted account and trying to test and test my own network.

This is one of the scripts I tried:

    <html>
<head>
<?php echo "<title>Port Scanning " . $_GET['host'] . "</title>"; ?>
</head>
<body>
<?php
ini_set('display_errors', 0);

if(isset($_GET['host']) == true) 
    $host = $_GET['host'];
else
{
    echo "You didn't set the host parameter";
    die();
}

if(isset($_GET['sport']) == true) 
    $sport = $_GET['sport'];
else
{
    echo "You didn't set the sport parameter";
    die();
}

if(isset($_GET['eport']) == true) 
    $eport = $_GET['eport'];
else
{
    echo "You didn't set the eport parameter";
    die();
}

if($sport > $eport)
{
    $sport = $eport;
}

$scanned = 0;
$openports = 0;
$maxports = 1;
$totalports = ($eport - $sport) + 1;
$mins = floor(($totalports / 10) / 60);
$secs = ($totalports / 10) - $mins * 60;

echo "<font color=\"blue\">Scanning " . $totalports . " ports on " . $host . ", this should take around " . $mins . " minute(s) and " . $secs . " second(s), probably more depending on local website load, hardware, and other factors.<br/><br/></font>";
flush();

do
{
    if($scanned >= $maxports && $maxports != 0)
    {
        echo "<font color=\"darkgreen\" size=\"4\">Scan limit of " . $maxports . " ports reached, scanning stopped.</font><br/><br/><font color=\"darkred\" size=\"4\">Scanner written by Samo502</font>";
        flush();
        die();
    }
    if(fsockopen($host, $sport, $errorno, $errorstr, 0.1))
    {
        echo "<font color=\"darkgreen\">Port " . $sport . " is open on " . $host . "</font><br/>";
        $openports++;
        flush();
    }
    else
    {
        echo "<font color=\"red\">Port " . $sport . " is not open on " . $host . "</font><br/>";
        flush();
    }
    $scanned++;
    $sport++;
} while($sport <= $eport);

echo "<br/><font color=\"blue\">Done, " . $openports . " out of " . $totalports . " ports are open.</font><br/><br/><br/><font color=\"darkred\" size=\"4\">Scanner written by Samo502</font>";
die();
?>
</body>
</html>

- , ?

** UPDATE ** , , , , 21 .

http://resources.infosecinstitute.com/php-build-your-own-mini-port-scanner-2/

.

+4
3

- PHP nmap - , , nmap.

, TCP- , , , .

$mysocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

SYN, ACK .

() / socket_select();, PHP. , , , , .

, , socket_set_timeout() socket_set_blocking().

shell_exec nmap

https://unix.stackexchange.com/questions/28732/fastest-way-to-port-scan-nmap

http://phpnmap.sourceforge.net/

, , nmap PEAR: http://pear.php.net/package/Net_Nmap/

0

if(fsockopen($host, $sport, $errorno, $errorstr, 0.1))

Try

$connection = @fsockopen($host, $sport);
if (is_resource($connection)){
  // OK
}
0

, . PEAR, , , , -, "yahoo.com", , . APACHE PEAR, . :

www.admin-toolkit.com/port_scan.html

. $scanhost, $port $timeout ( ), :

$status = Net_Portscan::checkPort($scanhost, $port, $timeout);

$servicename = Net_Portscan::getService($port);

if ($status == NET_PORTSCAN_SERVICE_FOUND) {
    if($servicename == null)
        echo $port.": OPEN ".PHP_EOL;
   else
        echo $port." (".$servicename .") : OPEN ".PHP_EOL;
   }
else{
   if($servicename == null)
      echo $port.": CLOSED".PHP_EOL;
   else
      echo $port." (".$servicename ."): CLOSED".PHP_EOL;
}
0

Source: https://habr.com/ru/post/1545029/


All Articles