-
Notifications
You must be signed in to change notification settings - Fork 73
/
nmap-xml-to-table.php
executable file
·206 lines (179 loc) · 6.35 KB
/
nmap-xml-to-table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/php
<?php
/**
* Script to transform multiple nmap XML output files (presumably of the same host/port range with different scan options) into a HTML table
*
* The canonical source of the latest version of this script is:
* <https://github.com/jantman/misc-scripts/blob/master/nmap-xml-to-table.php>
*
* Copyright 2011 Jason Antman <[email protected]> <http://www.jasonantman.com> all rights reserved.
* Distribution and use of this script is unlimited, provided that you leave this notice intact and send any corrections/additions back to me.
*
* This is assuming a relatively simple scan - only one protocol, one or more hosts, one or more ports, and that different scans will be differentiated only by type
*
* USAGE: nmap-xml-to-table.php [xml file] [xml file] [...]
*
*
*/
array_shift($argv); // get rid of script name
$files = array();
foreach($argv as $file)
{
if(file_exists($file) && is_readable($file))
{
$files[] = $file;
}
else
{
fwrite(STDERR, "File $file does not exist or is not readable, skipping.\n");
}
}
$results = array();
$scanInfo = array();
$hostAndPort = array();
$count = 0;
foreach($files as $file)
{
$info = getScanResults($file);
$results[$count] = $info['hosts'];
$info['scan']['filename'] = $file;
$scanInfo[$count] = $info['scan'];
foreach($info['hosts'] as $host => $portArr)
{
if(! isset($hostAndPort[$host])){ $hostAndPort[$host] = array();}
foreach($portArr as $port => $foo)
{
$hostAndPort[$host][$port] = $foo['service'];
}
}
$count++;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Condensed NMAP scan results</title>
<style type="text/css">
table.result {
border-width: 1px 1px 1px 1px;
border-spacing: 0px;
border-style: solid solid solid solid;
border-color: black black black black;
border-collapse: separate;
background-color: white;
margin-left: auto;
margin-right: auto;
}
table.result th {
border-width: 1px 1px 1px 1px;
padding: 2px 2px 2px 2px;
border-style: solid solid solid solid;
border-color: black black black black;
background-color: white;
-moz-border-radius: 0px 0px 0px 0px;
}
table.result td {
border-width: 1px 1px 1px 1px;
padding: 2px 2px 2px 2px;
border-style: solid solid solid solid;
border-color: black black black black;
background-color: white;
-moz-border-radius: 0px 0px 0px 0px;
}
h2 { text-align: center;}
.lookhere { color: red;}
</style>
</head>
<body>
<h2>Nmap scan results (condensed)</h2>
<?php
// output scan information
echo '<table class="result">'."\n";
echo '<tr><th colspan="7">Scans</th></tr>'."\n";
echo '<tr><th>Num</th><th>proto</th><th>type</th><th>Services</th><th>num hosts</th><th>command</th><th>filename</th></tr>'."\n";
foreach($scanInfo as $num => $arr)
{
echo '<tr><td>'.$num.'</td><td>'.$arr['protocol'].'</td><td>'.$arr['type'].'</td><td>'.$arr['services'].'</td><td>'.$arr['numHosts'].'</td><td><pre>'.$arr['command'].'</pre></td><td>'.$arr['filename'].'</td></tr>'."\n";
}
echo '</table>'."\n";
echo '<br />'."\n";
echo '<table class="result">'."\n";
echo '<tr><th colspan="'.(count($scanInfo)+1).'">Results</th></tr>'."\n";
echo '<tr><th>Port</th>';
for($i = 0; $i < count($scanInfo); $i++){ echo '<th>'.$i.' ('.$scanInfo[$i]['type'].')</th>';}
echo '</tr>'."\n";
foreach($hostAndPort as $host => $portArr)
{
echo '<tr><th colspan="'.(count($scanInfo)+1).'">'.$host.'</th></tr>'."\n";
foreach($portArr as $portName => $service)
{
echo '<tr><td>'.$portName.($service != "" ? " (".$service.")" : "").'</td>';
foreach($scanInfo as $scanNum => $arr)
{
if(isset($results[$scanNum][$host][$portName]))
{
echo '<td>'.(($results[$scanNum][$host][$portName]['state'] == "open" || $results[$scanNum][$host][$portName]['state'] == "closed" || $results[$scanNum][$host][$portName]['state'] == "unfiltered") ? '<span class="lookhere">' : '').$results[$scanNum][$host][$portName]['state'].' ('.$results[$scanNum][$host][$portName]['state_reason'].(isset($results[$scanNum][$host][$portName]['state_reason_ip']) ? ' '.$results[$scanNum][$host][$portName]['state_reason_ip'] : '').')'.(($results[$scanNum][$host][$portName]['state'] == "open" || $results[$scanNum][$host][$portName]['state'] == "closed" || $results[$scanNum][$host][$portName]['state'] == "unfiltered") ? '</span>' : '').'</td>';
}
else
{
echo '<td><em>n/a</em></td>';
}
}
echo '</tr>'."\n";
}
}
echo '</table>'."\n";
/**
* Parse meaningful information from a nmap XML output file and return it as an array
*
* @return array
*/
function getScanResults($file)
{
$xml = simplexml_load_file($file);
$result = array("scan" => array(), "hosts" => array());
$command = $xml->attributes();
$command = (string)$command['args'];
$result['scan']['command'] = $command;
$foo = $xml->scaninfo->attributes();
$result['scan']['type'] = (string)$foo['type'];
$result['scan']['protocol'] = (string)$foo['protocol'];
$result['scan']['services'] = (string)$foo['services'];
$count = 0;
foreach($xml->host as $host)
{
$count++;
$foo = $host->address->attributes();
$addr = (string)$foo['addr'];
$arr = array(); // array to hold ports and results
// <port protocol="tcp" portid="445"><state state="filtered" reason="admin-prohibited" reason_ttl="241" reason_ip="67.83.255.41"/><service name="microsoft-ds" method="table" conf="3" /></port>
foreach($host->ports->port as $port)
{
$foo = $port->attributes();
$bar = array();
$name = (string)($foo['protocol']." ".$foo['portid']);
$bar['protocol'] = (string)$foo['protocol'];
$bar['portid'] = (string)$foo['portid'];
$bar['name'] = $name;
$foo = $port->state->attributes();
$bar['state'] = (string)$foo['state'];
$bar['state_reason'] = (string)$foo['reason'];
$bar['state_reason_ttl'] = (string)$foo['reason_ttl'];
if(isset($foo['reason_ip'])){ $bar['state_reason_ip'] = (string)$foo['reason_ip'];}
if($port->service)
{
$foo = $port->service->attributes();
$bar['service'] = (string)$foo['name'];
}
$arr[$name] = $bar;
}
$result['hosts'][$addr] = $arr;
}
$result['scan']['numHosts'] = $count;
return $result;
}
?>
</body>
</html>