Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/Nmap/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class Host

private $ports;

private $scripts = [];

private $os;

private $os_accuracy;

public function __construct(array $addresses, string $state, array $hostnames = array(), array $ports = array())
{
$this->addresses = $addresses;
Expand All @@ -35,6 +41,21 @@ public function __construct(array $addresses, string $state, array $hostnames =
$this->ports = $ports;
}

public function setScripts(array $scripts)
{
$this->scripts = $scripts;
}

public function setOs(string $os)
{
$this->os = $os;
}

public function setOsAccuracy(string $accuracy)
{
$this->os_accuracy = $accuracy;
}

/**
* @return string
*
Expand Down Expand Up @@ -89,6 +110,22 @@ public function getState() : string
return $this->state;
}

/**
* @return string
*/
public function getOs() : ?string
{
return $this->os;
}

/**
* @return int
*/
public function getOsAccuracy() : ?int
{
return $this->os_accuracy;
}

/**
* @return Hostname[]
*/
Expand All @@ -97,6 +134,14 @@ public function getHostnames() : array
return $this->hostnames;
}

/**
* @return Script[]
*/
public function getScripts() : array
{
return $this->scripts;
}

/**
* @return Port[]
*/
Expand Down
15 changes: 15 additions & 0 deletions src/Nmap/Port.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Port

private $service;

private $scripts = [];

public function __construct(int $number, string $protocol, string $state, Service $service)
{
$this->number = (int) $number;
Expand All @@ -35,6 +37,11 @@ public function __construct(int $number, string $protocol, string $state, Servic
$this->service = $service;
}

public function setScripts(array $scripts)
{
$this->scripts = $scripts;
}

/**
* @return integer
*/
Expand Down Expand Up @@ -77,4 +84,12 @@ public function getService() : Service
{
return $this->service;
}

/**
* @return Script[]
*/
public function getScripts() : array
{
return $this->scripts;
}
}
34 changes: 34 additions & 0 deletions src/Nmap/Script.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Nmap;

class Script
{
private string $id;

private string $output;

private array $elems;

public function __construct(string $id, string $output, array $elems)
{
$this->id = $id;
$this->output = $output;
$this->elems = $elems;
}

public function getId()
{
return $this->id;
}

public function getOutput()
{
return $this->output;
}

public function getElems()
{
return $this->elems;
}
}
81 changes: 69 additions & 12 deletions src/Nmap/XmlOutputParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ public static function parseOutputFile($xmlFile)
$xml = simplexml_load_file($xmlFile);

$hosts = array();
foreach ($xml->host as $host) {
$hosts[] = new Host(
self::parseAddresses($host),
(string)$host->status->attributes()->state,
isset($host->hostnames) ? self::parseHostnames($host->hostnames->hostname) : array(),
isset($host->ports) ? self::parsePorts($host->ports->port) : array()
foreach ($xml->host as $xmlHost) {
$host = new Host(
self::parseAddresses($xmlHost),
(string)$xmlHost->status->attributes()->state,
isset($xmlHost->hostnames) ? self::parseHostnames($xmlHost->hostnames->hostname) : array(),
isset($xmlHost->ports) ? self::parsePorts($xmlHost->ports->port) : array(),
);
if (isset($xmlHost->hostscript)) {
$host->setScripts(self::parseScripts($xmlHost->hostscript->script));
}
if (isset($xmlHost->os->osmatch)) {
$host->setOs((string) $xmlHost->os->osmatch->attributes()->name);
$host->setOsAccuracy((int) $xmlHost->os->osmatch->attributes()->accuracy);
}
$hosts[] = $host;
}

return $hosts;
Expand Down Expand Up @@ -49,6 +57,51 @@ public static function parseHostnames(\SimpleXMLElement $xmlHostnames)
return $hostnames;
}

/**
* @param \SimpleXMLElement $xmlHostscript
* @return Script[]
*/
public static function parseScripts(\SimpleXMLElement $xmlScripts)
{
$scripts = array();
foreach ($xmlScripts as $xmlScript) {
$attrs = $xmlScript->attributes();
$scripts[] = new Script(
$attrs->id,
$attrs->output,
isset($xmlScript->elem) || isset($xmlScript->table) ? self::parseScriptElems($xmlScript) : array()
);
}

return $scripts;
}

public static function parseScriptElem(\SimpleXMLElement $xmlElems): array
{
$elems = array();
foreach ($xmlElems as $xmlElem) {
if (empty($xmlElem->attributes())) {
$elems[] = (string) $xmlElem[0];
} else {
$elems[(string) $xmlElem->attributes()->key] = (string) $xmlElem[0];
}
}
return $elems;
}

public static function parseScriptElems(\SimpleXMLElement $xmlScript): array
{
if (isset($xmlScript->table)) {
$elems = array();
foreach ($xmlScript->table as $xmlTable) {
$elems[(string) $xmlTable->attributes()->key] = self::parseScriptElem($xmlTable->elem);
}
return $elems;
}

return self::parseScriptElem($xmlScript->elem);
}

/**
* @param \SimpleXMLElement $xmlPorts
* @return Port[]
Expand All @@ -59,11 +112,11 @@ public static function parsePorts(\SimpleXMLElement $xmlPorts): array
*
*/
$ports = array();
foreach ($xmlPorts as $port) {
foreach ($xmlPorts as $xmlPort) {
$name = $product = $version = null;

if ($port->service) {
$attrs = $port->service->attributes();
if ($xmlPort->service) {
$attrs = $xmlPort->service->attributes();
if (!is_null($attrs)) {
$name = (string)$attrs->name;
$product = (string)$attrs->product;
Expand All @@ -77,14 +130,18 @@ public static function parsePorts(\SimpleXMLElement $xmlPorts): array
$version
);

$attrs = $port->attributes();
$attrs = $xmlPort->attributes();
if (!is_null($attrs)) {
$ports[] = new Port(
$port = new Port(
(int)$attrs->portid,
(string)$attrs->protocol,
(string)$port->state->attributes()->state,
(string)$xmlPort->state->attributes()->state,
$service
);
if (isset($xmlPort->script)) {
$port->setScripts(self::parseScripts($xmlPort->script));
}
$ports[] = $port;
}
}

Expand Down