Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGoodwin authored Dec 4, 2023
2 parents 998fc2e + 252d56c commit f5c67da
Show file tree
Hide file tree
Showing 19 changed files with 1,325 additions and 329 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2
10 changes: 8 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-18.04
strategy:
matrix:
php-versions: ['7.2', '7.3', '7.4', '8.0' ]
php-versions: ['8.0', '8.1']

steps:

Expand All @@ -19,6 +19,12 @@ jobs:
php-version: ${{ matrix.php-versions }}
tools: composer

- name: Install xmlstarlet
run: sudo apt-get install -y xmlstarlet

- name: Install nmap
run: sudo apt-get install -y nmap

- name: Validate composer.json and composer.lock
run: composer validate

Expand All @@ -32,7 +38,7 @@ jobs:
run: composer lint

- name: Run psalm
run: composer psalm
run: ./vendor/bin/psalm --php-version=${{ matrix.php-versions }} --output-format=github

- name: Run phpunit
run: composer phpunit
55 changes: 45 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
nmap
**This project is maintained fork of the original project: https://github.com/willdurand/nmap**

Nmap
====

**nmap** is a PHP wrapper for [Nmap](http://nmap.org/), a free security scanner
**Nmap** is a PHP wrapper for [Nmap](http://nmap.org/), a free security scanner
for network exploration.

![PHP Build](https://github.com/DavidGoodwin/nmap/workflows/PHP%20Build/badge.svg)

Usage
Starting a scan
-----

```php
Expand Down Expand Up @@ -75,25 +77,58 @@ $nmap
->scan([ 'example.com' ]);
```

You can run specific scripts with `setScripts()` and get the result with `getScripts()`:

``` php
$hosts = $nmap
->setTimeout(120)
->scan([ 'example.com' ], [ 443 ]);

$hosts[0]->setScripts(['ssl-heartbleed']);
$ports = $hosts[0]->getOpenPorts();

$ports[0]->getScripts();
```

Nmap XML output
-------------------------------

Parse existing output:

``` php
Nmap::parseOutput($xmlFile);
```

or

``` php
$parser = new XmlOutputParser($xmlFile);
$parser->parse();
```

Validation output file using the Nmap DTD. A custom DTD path can be passed to the validate function.

```php
$parser = new XmlOutputParser($xmlFile);
$parser->validate();
```

Installation
------------

The recommended way to install nmap is through
[Composer](http://getcomposer.org/):
The recommended way to install nmap is through [Composer](http://getcomposer.org/):

For PHP 7.2 and above, use version 2.x

For PHP 5.6 and above, use version 1.x
For PHP 8.0 and above -

``` json
{
"require": {
"palepurple/nmap": "^2.0"
"palepurple/nmap": "^3.0"
}
}
```


For older versions of PHP, try ^2.0; see also https://github.com/DavidGoodwin/nmap/releases/tag/2.0.1

License
-------
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
}
],
"require": {
"php": ">= 7.2",
"php": ">= 8.0",
"symfony/process": "~2.0|~3.0|~4.0|~5.0|~6.0|~7.0"
"ext-simplexml": "*",
"symfony/filesystem": "~6.0"
},
"require-dev": {
"phpunit/phpunit": "7.*|8.*",
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
xmlns:xi="http://www.w3.org/2001/XInclude"
>
<projectFiles>
<directory name="src" />
Expand Down
21 changes: 11 additions & 10 deletions src/Nmap/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,37 @@

namespace Nmap;

/**
* @author Dany Maillard <[email protected]>
*/
class Address
{

const TYPE_IPV4 = 'ipv4';

const TYPE_MAC = 'mac';

private $address;
private $type;
private $vendor;
private string $address;

private string $type;

private string $vendor;

public function __construct(string $address, string $type = self::TYPE_IPV4, $vendor = '')
public function __construct(string $address, string $type = self::TYPE_IPV4, string $vendor = '')
{
$this->address = $address;
$this->type = $type;
$this->vendor = $vendor;
}

public function getAddress() : string
public function getAddress(): string
{
return $this->address;
}

public function getType() : string
public function getType(): string
{
return $this->type;
}

public function getVendor() : string
public function getVendor(): string
{
return $this->vendor;
}
Expand Down
72 changes: 25 additions & 47 deletions src/Nmap/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,33 @@

namespace Nmap;

/**
* @author William Durand <[email protected]>
*/
class Host
{
const STATE_UP = 'up';

const STATE_UP = 'up';

const STATE_DOWN = 'down';

private $addresses;
private array $addresses;

private $state;
private string $state;

private $hostnames;
private array $hostnames;

private $ports;
private array $ports;

private $scripts = [];
private array $scripts = [];

private $os;
private ?string $os = null;

private $os_accuracy;
private ?int $os_accuracy = null;

public function __construct(array $addresses, string $state, array $hostnames = array(), array $ports = array())
public function __construct(array $addresses, string $state, array $hostnames = [], array $ports = [])
{
$this->addresses = $addresses;
$this->state = $state;
$this->state = $state;
$this->hostnames = $hostnames;
$this->ports = $ports;
$this->ports = $ports;
}

public function setScripts(array $scripts)
Expand All @@ -56,30 +54,18 @@ public function setOsAccuracy(int $accuracy)
$this->os_accuracy = $accuracy;
}

/**
* @return string
*
* @deprecated The Host::getAddress() method is deprecated since 0.4 version. Use Host::getIpv4Addresses() instead.
*/
public function getAddress() : string
{
return current($this->getIpv4Addresses())->getAddress();
}

/**
* @return Address[]
*/
public function getAddresses() : array
public function getAddresses(): array
{
return $this->addresses;
}

/**
* @param string $type
*
* @return Address[]
*/
private function getAddressesByType(string $type) : array
private function getAddressesByType(string $type): array
{
return array_filter($this->addresses, function (Address $address) use ($type) {
return $address->getType() === $type;
Expand All @@ -89,71 +75,65 @@ private function getAddressesByType(string $type) : array
/**
* @return Address[]
*/
public function getIpv4Addresses() : array
public function getIpv4Addresses(): array
{
return $this->getAddressesByType(Address::TYPE_IPV4);
}

/**
* @return Address[]
*/
public function getMacAddresses() : array
public function getMacAddresses(): array
{
return $this->getAddressesByType(Address::TYPE_MAC);
}

/**
* @return string
*/
public function getState() : string
public function getState(): string
{
return $this->state;
}

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

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

/**
* @return Hostname[]
*/
public function getHostnames() : array
public function getHostnames(): array
{
return $this->hostnames;
}

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

/**
* @return Port[]
*/
public function getPorts() : array
public function getPorts(): array
{
return $this->ports;
}

/**
* @return Port[]
*/
public function getOpenPorts() : array
public function getOpenPorts(): array
{
return array_filter($this->ports, function ($port) {
return $port->isOpen();
Expand All @@ -163,10 +143,8 @@ public function getOpenPorts() : array
/**
* @return Port[]
*/
public function getClosedPorts() : array
public function getClosedPorts(): array
{
return array_filter($this->ports, function ($port) {
return $port->isClosed();
});
return array_filter($this->ports, fn ($port) => $port->isClosed());
}
}
Loading

0 comments on commit f5c67da

Please sign in to comment.