Tuesday, November 5, 2024

Tutorial: PHP Port Scanner that Checks If Port 80 is Open

Here’s an example of a PHP page that allows a user to input a starting and ending IP address range and scan for open port 80. It will display a list of links for the webpage(s) hosted on port 80 for IP addresses that have the port open. This implementation uses PHP, AJAX, and Bootstrap for the user interface.

Create the Webpage Interface for the Scanner

index.php

<!DOCTYPE html>
<html>
<head>
    <title>IP Range Scanner</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
    <div class="container">
        <h1>IP Range Scanner</h1>
        <form id="ipRangeForm">
            <div class="form-group">
                <label for="startIP">Starting IP:</label>
                <input type="text" class="form-control" id="startIP" name="startIP" placeholder="Enter starting IP address">
            </div>
            <div class="form-group">
                <label for="endIP">Ending IP:</label>
                <input type="text" class="form-control" id="endIP" name="endIP" placeholder="Enter ending IP address">
            </div>
            <button type="submit" class="btn btn-primary">Scan</button>
        </form>
        <div id="resultContainer"></div>
    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script>
        $(document).ready(function() {
            $('#ipRangeForm').submit(function(e) {
                e.preventDefault();

                var startIP = $('#startIP').val();
                var endIP = $('#endIP').val();

                $.ajax({
                    url: 'scan_ips2.php',
                    type: 'POST',
                    data: { startIP: startIP, endIP: endIP },
                    beforeSend: function() {
                        $('#resultContainer').html('<p>Scanning IP range...</p>');
                    },
                    success: function(response) {
                        $('#resultContainer').html(response);
                    }
                });
            });
        });
    </script>
</body>
</html>

In the code above, we have a simple HTML form where users can input the starting and ending IP addresses. When the form is submitted, an AJAX request is sent to scan_ips.php with the entered IP range. The scan_ips.php file will handle the IP scanning logic and display the results.

Create the Code to Scan the IP Range

In the scan_ips.php file, you can use the following PHP code to scan the IP range and check if port 80 is open:

scan_ips.php

<?php
$startIP = $_POST['startIP'];
$endIP = $_POST['endIP'];

// Function to check if port 80 is open
function isPortOpen($ip, $port)
{
    $connection = @fsockopen($ip, $port, $errno, $errstr, 2);
    if (is_resource($connection)) {
        fclose($connection);
        return true;
    } else {
        return false;
    }
}

// Loop through the IP range and check if port 80 is open
$ipRange = range(ip2long($startIP), ip2long($endIP));
foreach ($ipRange as $ip) {
    $ipAddress = long2ip($ip);
    if (isPortOpen($ipAddress, 80)) {
        $url = "http://{$ipAddress}/";
        echo "<a href='{$url}' target='_blank'>{$url}</a><br>";
    }
}
?>

In the scan_ips.php file, we iterate through each IP address in the range, convert it to long format using ip2long(), and check if port 80 is open using the isPortOpen() function. If the port is open, we display a list of links of the webpage(s) hosted on port 80.

Note that you may need to adjust the code based on your specific requirements and server configuration. Additionally, make sure to handle user input validation and sanitize the input to prevent security vulnerabilities.

Related Articles

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

How to fixed google ads suspended account.