NMAP result using PHP - how to show the NMAP result in a table in the browser?

Asked

Viewed 397 times

1

I am trying to make the output of nmap result appear in the browser in the form of a table. follow the code below:

<?php
$host = $_POST["host"];
$saida = shell_exec('nmap -P0 ' . $host);
$vetorLinhas = explode("\n", $saida);
echo "<pre>$alinhamento</pre>";
?>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>tecnologia</title>
    </head>
    <body>
        <table>
            <tr>
                <td colspan="5">Scanner de Porta TCP/UDP</td>
            </tr>
            <tr>
                <td height="60" colspan="5" align="center"> 
                    <form id="form1" name="form1" method="post" action="exemplo.php">
                        <br/>
                        Endereço de Host
                        <label for="textfield">
                            <input type="text" name="host" id="textfield" />
                            <input type="submit" name="button" id="button" value="Ir" />
                        </label>
                    </form>
                </td>
            </tr>
            <tr>
                <td height="60" colspan="5">
                    <pre>
                        <?php
                        echo "<b><center>$host</center></b><br>";
                        if (count($vetorLinhas)) {
                            foreach ($vetorLinhas as $indice => $linha) {
                                echo "<pre>$indice - $linha</pre>";
                            }
                        }
                        ?>
                    </pre>
                </td>
            </tr>
        </table>
    </body>
</html>

It’s coming out like this in the browser

0 - 
1 - Starting Nmap 7.01 ( https://nmap.org ) at 2016-11-20 19:36 BRT
2 - Nmap scan report for localhost (127.0.0.1)
3 - Host is up (0.000088s latency).
4 - Not shown: 994 closed ports
5 - PORT     STATE SERVICE
6 - 21/tcp   open  ftp
7 - 80/tcp   open  http
8 - 139/tcp  open  netbios-ssn
9 - 445/tcp  open  microsoft-ds
10 - 3306/tcp open  mysql
11 - 5432/tcp open  postgresql
12 - 
13 - Nmap done: 1 IP address (1 host up) scanned in 0.07 seconds
14 -

The question is what would this PHP algorithm look like to exit in table format in html from the line "PORT STATE SERVICE" and stop displaying on the line "Nmap done: 1 IP address (1 host up) Scanned in 0.07 Seconds" ??

  • 1

    If already answered, check. Will help anyone who has the same problem in the future ;)

1 answer

2

You can do it like this:

$saida = shell_exec('nmap -P0 localhost');
$vetorLinhas = explode("\n", $saida);

$start = array_keys(preg_grep('/^PORT/', $vetorLinhas))[0];

$data = array_slice($vetorLinhas, $start, -3)

echo "<pre>"; print_r($data); exit;

Exit:

Array
(
    [0] => PORT     STATE SERVICE
    [1] => 25/tcp   open  smtp
    [2] => 80/tcp   open  http
    [3] => 587/tcp  open  submission
    [4] => 902/tcp  open  iss-realsecure
    [5] => 3306/tcp open  mysql
)

Explanation:

The variable $start will receive the index of the line starting with PORT, the $data will receive the range of the array going from $start up to 3 lines before finishing (removing Nmap done...).

NOTE: I used this answer to conclude the reasoning: https://stackoverflow.com/a/5806057/6101515

  • Your tip helped a lot. Thanks!!

  • Hello @Andrei, if the answer helped you, mark it as a solution on the left side so that other people can be helped too! Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.