Home > Back-end >  Port scanner in php
Port scanner in php

Time:10-22

I’m trying to get a php port scanner to work correctly. When I manually put in a range, say 10 to 20, it scans 10 to 20. But if I try and use a form, it starts at 0. I got the basic code of here I think, but can’t get it to work with ranges entered via text box and POST. Here is the code I have and yes I know it’s messy.



<form method="POST" >
    Domain/IP: 
    <input type="text" name="domain" /> 
    <br>Low:
    <input type="text" name="low" />
    <br>High:
    <input type="text" name="high" />
    <br>Step:
    <input type="text" name="step" />
    <br>
    <input type="submit" value="Scan" />
</form>
<br />

<?php
set_time_limit(300);
if(!empty($_POST['domain'])) {  

    $low=intval($_POST['low']);

//$low = $_POST['low'];
//$low = $_GET['low'];
//$low = $_POST['low'] ;
//$_SESSION['low'] = $low;
$high=$_POST['high'];
$step=$_POST['step'];
    //list of port numbers to scan
    //if(!empty($_POST['low'])){$low = $_POST['low'];}
    //if(!empty($_POST['high'])){$high = $_POST['high'];}
    //if(!empty($_POST['step'])){$step = $_POST['step'];}
    //echo 'low' , $low;

    
    $ports = range($low,$high,$step);
    
    $results = range($low,$high,$step);
    foreach($ports as $port) {
        
        if($pf = @fsockopen($_POST['domain'], $port, $err, $err_string, 1)) {
            $results[$port] = true;
            fclose($pf);
        } else {
            $results[$port] = false;
        }
    }

    foreach($results as $port=>$val) {
        $prot = getservbyport($port,"tcp");
                echo "Port $port ($prot): ";
        if($val) {
            echo "<span style=\"color:green\">OK</span><br/>";
        }
        else {
            echo "<span style=\"color:red\">Inaccessible</span><br/>";
        }
    }
}
echo $low;
?>

Any help would be greatly appreciated.

CodePudding user response:

Here is the code tidied up a a bit.

  • number type for the input element means you can't go above or below the port range
  • Inputs persist when the form is submitted

You didn't need to set $result to the same range, in fact doing that caused the script to output incorrectly if using a step other than 1.

<?php

/*

Author: username_313
Question: Port scanner in php
URL: https://stackoverflow.com/questions/74160800/port-scanner-in-php
Tags: php

*/

set_time_limit(300);
$domain = '';
if (!empty($_POST['domain'])) {
    $domain = $_POST['domain'];
}
$start_port = 1;
if (!empty($_POST['start_port'])) {
    $start_port = intval($_POST['start_port']);
}
$end_port = 10;
if (!empty($_POST['end_port'])) {
    $end_port = intval($_POST['end_port']);
}
$step = 1;
if (!empty($_POST['step'])) {
    $step = intval($_POST['step']);
}
?>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
    Domain/IP: 
    <input type="text" name="domain" value="<?=$domain?>" /> 
    <br>Start Port:
    <input type="number" name="start_port" min="1" max="65535" value="<?=$start_port?>" />
    <br>End Port:
    <input type="number" name="end_port"  min="1" max="65535" value="<?=$end_port?>" />
    <br>Step:
    <input type="number" name="step" value="<?=$step?>" />
    <br>
    <input type="submit" value="Scan" />
</form>
<br />

<?php

if(!empty($_POST['domain'])) {

    // A check for valid domain or IP should happen here, and the script should not conitnue unless it is valid

    $ports = range($start_port, $end_port, $step);
    $results = [];

    foreach($ports as $port) {
        $fp = @fsockopen($_POST['domain'], $port, $err, $err_string, 1);

        if (!$fp) {
            $results[$port] = false;
        } else {
            $results[$port] = true;
            fclose($fp);
        }
    }

    foreach($results as $port => $val) {
        $service = getservbyport($port, "tcp");
        echo "Port $port ($service): ";

        if($val) {
            echo "<span style=\"color:green\">OK</span><br/>";
        } else {
            echo "<span style=\"color:red\">Inaccessible</span><br/>";
        }
    }
}
?>
  •  Tags:  
  • php
  • Related