diff --git a/.travis.yml b/.travis.yml index 0f7023b..5b44548 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,8 @@ language: php php: - - 5.3 - - 5.4 - - 5.5 - 5.6 + - 7.0 before_script: - composer install 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/" } 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 * 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 */