From 6ed0c83dbf5bed4c2bb0dc34b9b07e0c0d82e776 Mon Sep 17 00:00:00 2001 From: David Goodwin Date: Thu, 21 Sep 2017 16:49:54 +0100 Subject: [PATCH 1/5] add mockery as I am too lazy to figure out phpunit mocking --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index fd969f1..ad000c8 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "symfony/process": "~2.0|~3.0" }, "require-dev": { - "phpunit/phpunit": "~3.7" + "phpunit/phpunit": "~3.7", + "mockery/mockery": "^1.0-dev" }, "autoload": { "psr-0": { "Nmap": "src/" } From a0ff96c1adf75aa7e2af1f7959352bc48160d794 Mon Sep 17 00:00:00 2001 From: David Goodwin Date: Thu, 21 Sep 2017 16:50:07 +0100 Subject: [PATCH 2/5] add test for addExtraOptions --- .../Tests/Fixtures/test_extra_options.xml | 26 +++++++++ tests/Nmap/Tests/NmapTest.php | 58 ++++++++++++++----- 2 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 tests/Nmap/Tests/Fixtures/test_extra_options.xml diff --git a/tests/Nmap/Tests/Fixtures/test_extra_options.xml b/tests/Nmap/Tests/Fixtures/test_extra_options.xml new file mode 100644 index 0000000..ce40171 --- /dev/null +++ b/tests/Nmap/Tests/Fixtures/test_extra_options.xml @@ -0,0 +1,26 @@ + + + + + + + + + +
+ + + + + + + + + + + + + + + + diff --git a/tests/Nmap/Tests/NmapTest.php b/tests/Nmap/Tests/NmapTest.php index 337bf43..833ff83 100644 --- a/tests/Nmap/Tests/NmapTest.php +++ b/tests/Nmap/Tests/NmapTest.php @@ -2,6 +2,7 @@ namespace Nmap\Tests; +use Mockery as m; use Nmap\Address; use Nmap\Host; use Nmap\Nmap; @@ -9,9 +10,15 @@ class NmapTest extends TestCase { + public function tearDown() + { + m::close(); + parent::tearDown(); + } + public function testScan() { - $outputFile = __DIR__ . '/Fixtures/test_scan.xml'; + $outputFile = __DIR__ . '/Fixtures/test_scan.xml'; $expectedCommand = sprintf("nmap -oX '%s' 'williamdurand.fr'", $outputFile); $executor = $this->getProcessExecutorMock(); @@ -21,7 +28,7 @@ public function testScan() ->with($this->equalTo($expectedCommand)) ->will($this->returnValue(0)); - $nmap = new Nmap($executor, $outputFile); + $nmap = new Nmap($executor, $outputFile); $hosts = $nmap->scan(array('williamdurand.fr')); $this->assertCount(1, $hosts); @@ -62,7 +69,7 @@ public function testScan() public function testScanSpecifyingPorts() { - $outputFile = __DIR__ . '/Fixtures/test_scan_specifying_ports.xml'; + $outputFile = __DIR__ . '/Fixtures/test_scan_specifying_ports.xml'; $expectedCommand = sprintf("nmap -p 21,22,80 -oX '%s' 'williamdurand.fr'", $outputFile); $executor = $this->getProcessExecutorMock(); @@ -72,8 +79,8 @@ public function testScanSpecifyingPorts() ->with($this->equalTo($expectedCommand)) ->will($this->returnValue(0)); - $nmap = new Nmap($executor, $outputFile); - $hosts = $nmap->scan(array('williamdurand.fr'), array(21,22,80)); + $nmap = new Nmap($executor, $outputFile); + $hosts = $nmap->scan(array('williamdurand.fr'), array(21, 22, 80)); $this->assertCount(1, $hosts); $host = current($hosts); @@ -106,7 +113,7 @@ public function testScanSpecifyingPorts() public function testScanWithOsDetection() { - $outputFile = __DIR__ . '/Fixtures/test_scan_with_os_detection.xml'; + $outputFile = __DIR__ . '/Fixtures/test_scan_with_os_detection.xml'; $expectedCommand = sprintf("nmap -O -oX '%s' 'williamdurand.fr'", $outputFile); $executor = $this->getProcessExecutorMock(); @@ -116,7 +123,7 @@ public function testScanWithOsDetection() ->with($this->equalTo($expectedCommand)) ->will($this->returnValue(0)); - $nmap = new Nmap($executor, $outputFile); + $nmap = new Nmap($executor, $outputFile); $hosts = $nmap ->enableOsDetection() ->scan(array('williamdurand.fr')); @@ -124,7 +131,7 @@ public function testScanWithOsDetection() public function testScanWithServiceInfo() { - $outputFile = __DIR__ . '/Fixtures/test_scan_with_service_info.xml'; + $outputFile = __DIR__ . '/Fixtures/test_scan_with_service_info.xml'; $expectedCommand = sprintf("nmap -sV -oX '%s' 'williamdurand.fr'", $outputFile); $executor = $this->getProcessExecutorMock(); @@ -134,7 +141,7 @@ public function testScanWithServiceInfo() ->with($this->equalTo($expectedCommand)) ->will($this->returnValue(0)); - $nmap = new Nmap($executor, $outputFile); + $nmap = new Nmap($executor, $outputFile); $hosts = $nmap ->enableServiceInfo() ->scan(array('williamdurand.fr')); @@ -154,7 +161,7 @@ public function testScanWithServiceInfo() public function testScanWithVerbose() { - $outputFile = __DIR__ . '/Fixtures/test_scan_with_verbose.xml'; + $outputFile = __DIR__ . '/Fixtures/test_scan_with_verbose.xml'; $expectedCommand = sprintf("nmap -v -oX '%s' 'williamdurand.fr'", $outputFile); $executor = $this->getProcessExecutorMock(); @@ -164,7 +171,7 @@ public function testScanWithVerbose() ->with($this->equalTo($expectedCommand)) ->will($this->returnValue(0)); - $nmap = new Nmap($executor, $outputFile); + $nmap = new Nmap($executor, $outputFile); $hosts = $nmap ->enableVerbose() ->scan(array('williamdurand.fr')); @@ -172,7 +179,7 @@ public function testScanWithVerbose() public function testPingScan() { - $outputFile = __DIR__ . '/Fixtures/test_ping_scan.xml'; + $outputFile = __DIR__ . '/Fixtures/test_ping_scan.xml'; $expectedCommand = sprintf("nmap -sn -oX '%s' 'williamdurand.fr'", $outputFile); $executor = $this->getProcessExecutorMock(); @@ -182,7 +189,7 @@ public function testPingScan() ->with($this->equalTo($expectedCommand)) ->will($this->returnValue(0)); - $nmap = new Nmap($executor, $outputFile); + $nmap = new Nmap($executor, $outputFile); $hosts = $nmap ->disablePortScan() ->scan(array('williamdurand.fr')); @@ -190,7 +197,7 @@ public function testPingScan() public function testScanWithoutReverseDNS() { - $outputFile = __DIR__ . '/Fixtures/test_ping_without_reverse_dns.xml'; + $outputFile = __DIR__ . '/Fixtures/test_ping_without_reverse_dns.xml'; $expectedCommand = sprintf("nmap -n -oX '%s' 'williamdurand.fr'", $outputFile); $executor = $this->getProcessExecutorMock(); @@ -200,7 +207,7 @@ public function testScanWithoutReverseDNS() ->with($this->equalTo($expectedCommand)) ->will($this->returnValue(0)); - $nmap = new Nmap($executor, $outputFile); + $nmap = new Nmap($executor, $outputFile); $hosts = $nmap ->disableReverseDNS() ->scan(array('williamdurand.fr')); @@ -267,6 +274,27 @@ public function testExecutableNotExecutable() new Nmap($executor); } + public function testRawOptions() + { + $mockExecutor = m::mock(\Nmap\Util\ProcessExecutor::class); + $mockExecutor->shouldReceive('execute')->once()->withArgs(function ($command) { + $this->assertEquals('nmap -h', $command); + return true; + })->andReturn(0); + + $mockExecutor->shouldReceive('execute')->once()->withArgs(function ($command, $timeout = 60) { + // Passing [ -e eth0, --min-hostgroup 3 ] ... should produce a command string a bit like ... + $this->assertRegExp("/^nmap -e eth0 --min-hostgroup 3.*127\.0\.0\.1'$/", $command); + return true; + })->andReturn(0); + + $nmap = new Nmap($mockExecutor, dirname(__FILE__) . '/Fixtures/test_extra_options.xml'); + $nmap->setExtraOptions(['-e eth0', '--min-hostgroup 3']); + $output = $nmap->scan(['127.0.0.1']); + + $this->assertNotEmpty($output); + } + /** * @return \PHPUnit_Framework_MockObject_MockObject | \Nmap\Util\ProcessExecutor */ From d6a4be706868c125a68fabb2f5d69625b3ce2ecc Mon Sep 17 00:00:00 2001 From: David Goodwin Date: Thu, 21 Sep 2017 16:52:03 +0100 Subject: [PATCH 3/5] add extra options for the scanner --- src/Nmap/Nmap.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Nmap/Nmap.php b/src/Nmap/Nmap.php index 7a84bc0..a1d74af 100644 --- a/src/Nmap/Nmap.php +++ b/src/Nmap/Nmap.php @@ -39,6 +39,8 @@ class Nmap private $timeout = 60; + private $extraOptions = []; + /** * @return Nmap */ @@ -79,7 +81,7 @@ public function scan(array $targets, array $ports = array()) return ProcessUtils::escapeArgument($target); }, $targets)); - $options = array(); + $options = $this->extraOptions; if (true === $this->enableOsDetection) { $options[] = '-O'; } @@ -147,6 +149,14 @@ public function enableServiceInfo($enable = true) return $this; } + /** + * Extra options to pass to NMAP (e.g. [ "-e eth0", "--min-hostgroup 4" ] ) + * @param array $options + */ + public function setExtraOptions(array $options) { + $this->extraOptions = $options; + } + /** * @param boolean $enable * From f226cb54ea2d19af95d28734a34ff69c14fd3aa2 Mon Sep 17 00:00:00 2001 From: David Goodwin Date: Fri, 22 Sep 2017 13:09:02 +0100 Subject: [PATCH 4/5] change travis php versions --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0f7023b..fc62c0d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,9 @@ language: php php: - - 5.3 - - 5.4 - 5.5 - 5.6 + - 7.0 before_script: - composer install From e767da539df62265ba02421139dd3a4dda720a99 Mon Sep 17 00:00:00 2001 From: David Goodwin Date: Fri, 22 Sep 2017 13:17:56 +0100 Subject: [PATCH 5/5] drop php 5.5 as it is not supported really (and this fixes mockery dependency) --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index fc62c0d..5b44548 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 5.5 - 5.6 - 7.0