7

How to Nmap Scan with PHP

 3 years ago
source link: https://www.devdungeon.com/content/how-nmap-scan-php
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Nmap Scan with PHP

Submitted by NanoDano on Mon, 07/27/2015 - 17:54

Using the Net_Nmap library, we can perform nmap scans using PHP. Here is a demonstration of how to do a basic scan and handle the results.

<?php
// Scan network to retrieve hosts and services information.
require_once 'Net/Nmap.php';

//Define the target and options
$target = array('127.0.0.1','www.example.com');
$options = array('nmap_binary' => '/usr/local/bin/nmap');

try {
    $nmap = new Net_Nmap($options);
    $nmap_options = array(
        'os_detection' => true,
        'service_info' => true,
        'port_ranges' => 'U:53,111,137,T:21-25,80,139,8080', // Only specified ports
    );
    $nmap->enableOptions($nmap_options);

// Scan
    $res = $nmap->scan($target);

// Get failed hosts
    $failed_to_resolve = $nmap->getFailedToResolveHosts();
    if (count($failed_to_resolve) > 0) {
        echo 'Failed to resolve given hostname/IP: ' .
             implode (', ', $failed_to_resolve) .
             "\n";
    }

//Parse XML Output to retrieve Hosts Object
    $hosts = $nmap->parseXMLOutput();

//Print results
    foreach ($hosts as $key => $host) {
        echo 'Hostname: ' . $host->getHostname() . "\n";
        echo 'Address: ' . $host->getAddress() . "\n";
        echo 'OS: ' . $host->getOS() . "\n";
        echo 'Status: ' . $host->getStatus . "\n";
        $services = $host->getServices();
        echo 'Number of discovered services: ' . count($services) . "\n";
        foreach ($services as $key => $service) {
            echo "\n";
            echo 'Service Name: ' . $service->name . "\n";
            echo 'Port: ' . $service->port . "\n";
            echo 'Protocol: ' . $service->protocol . "\n";
            echo 'Product information: ' . $service->product . "\n";
            echo 'Product version: ' . $service->version . "\n";
            echo 'Product additional info: ' . $service->extrainfo . "\n";
        }
    }
} catch (Net_Nmap_Exception $ne) {
    echo $ne->getMessage();
}

Advertisement


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK