Skip to content
This repository has been archived by the owner on Jan 31, 2022. It is now read-only.

Commit

Permalink
Nmap executable can now be passed via constructor. Checks if executab…
Browse files Browse the repository at this point in the history
…le is really executable.
  • Loading branch information
matejvelikonja committed Aug 25, 2014
1 parent bfcdefa commit de6e39d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 18 deletions.
15 changes: 13 additions & 2 deletions src/Nmap/Nmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class Nmap

private $treatHostsAsOnline = false;

private $executable;

/**
* @return Nmap
*/
Expand All @@ -46,11 +48,19 @@ public static function create()
/**
* @param ProcessExecutor $executor
* @param string $outputFile
* @param string $executable
*
* @throws \InvalidArgumentException
*/
public function __construct(ProcessExecutor $executor = null, $outputFile = null)
public function __construct(ProcessExecutor $executor = null, $outputFile = null, $executable = 'nmap')
{
$this->executor = $executor ?: new ProcessExecutor();
$this->outputFile = $outputFile ?: sys_get_temp_dir() . '/output.xml';
$this->executable = $executable;

if ($executor->execute($this->executable)) {
throw new \InvalidArgumentException(sprintf('`%s` is not executable.', $this->executable));
}
}

/**
Expand Down Expand Up @@ -93,7 +103,8 @@ public function scan(array $targets, array $ports = array())
}

$options[] = '-oX';
$command = sprintf('nmap %s %s %s',
$command = sprintf('%s %s %s %s',
$this->executable,
implode(' ', $options),
ProcessUtils::escapeArgument($this->outputFile),
$targets
Expand Down
60 changes: 44 additions & 16 deletions tests/Nmap/Tests/NmapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public function testScan()
$outputFile = __DIR__ . '/Fixtures/test_scan.xml';
$expectedCommand = sprintf("nmap -oX '%s' 'williamdurand.fr'", $outputFile);

$executor = $this->getMock('Nmap\Util\ProcessExecutor');
$executor = $this->getProcessExecutorMock();
$executor
->expects($this->once())
->expects($this->at(1))
->method('execute')
->with($this->equalTo($expectedCommand))
->will($this->returnValue(0));
Expand Down Expand Up @@ -53,9 +53,9 @@ public function testScanSpecifyingPorts()
$outputFile = __DIR__ . '/Fixtures/test_scan_specifying_ports.xml';
$expectedCommand = sprintf("nmap -p 21,22,80 -oX '%s' 'williamdurand.fr'", $outputFile);

$executor = $this->getMock('Nmap\Util\ProcessExecutor');
$executor = $this->getProcessExecutorMock();
$executor
->expects($this->once())
->expects($this->at(1))
->method('execute')
->with($this->equalTo($expectedCommand))
->will($this->returnValue(0));
Expand Down Expand Up @@ -92,9 +92,9 @@ public function testScanWithOsDetection()
$outputFile = __DIR__ . '/Fixtures/test_scan_with_os_detection.xml';
$expectedCommand = sprintf("nmap -O -oX '%s' 'williamdurand.fr'", $outputFile);

$executor = $this->getMock('Nmap\Util\ProcessExecutor');
$executor = $this->getProcessExecutorMock();
$executor
->expects($this->once())
->expects($this->at(1))
->method('execute')
->with($this->equalTo($expectedCommand))
->will($this->returnValue(0));
Expand All @@ -110,9 +110,9 @@ public function testScanWithServiceInfo()
$outputFile = __DIR__ . '/Fixtures/test_scan_with_service_info.xml';
$expectedCommand = sprintf("nmap -sV -oX '%s' 'williamdurand.fr'", $outputFile);

$executor = $this->getMock('Nmap\Util\ProcessExecutor');
$executor = $this->getProcessExecutorMock();
$executor
->expects($this->once())
->expects($this->at(1))
->method('execute')
->with($this->equalTo($expectedCommand))
->will($this->returnValue(0));
Expand All @@ -128,9 +128,9 @@ public function testScanWithVerbose()
$outputFile = __DIR__ . '/Fixtures/test_scan_with_verbose.xml';
$expectedCommand = sprintf("nmap -v -oX '%s' 'williamdurand.fr'", $outputFile);

$executor = $this->getMock('Nmap\Util\ProcessExecutor');
$executor = $this->getProcessExecutorMock();
$executor
->expects($this->once())
->expects($this->at(1))
->method('execute')
->with($this->equalTo($expectedCommand))
->will($this->returnValue(0));
Expand All @@ -146,9 +146,9 @@ public function testPingScan()
$outputFile = __DIR__ . '/Fixtures/test_ping_scan.xml';
$expectedCommand = sprintf("nmap -sn -oX '%s' 'williamdurand.fr'", $outputFile);

$executor = $this->getMock('Nmap\Util\ProcessExecutor');
$executor = $this->getProcessExecutorMock();
$executor
->expects($this->once())
->expects($this->at(1))
->method('execute')
->with($this->equalTo($expectedCommand))
->will($this->returnValue(0));
Expand All @@ -164,9 +164,9 @@ public function testScanWithoutReverseDNS()
$outputFile = __DIR__ . '/Fixtures/test_ping_without_reverse_dns.xml';
$expectedCommand = sprintf("nmap -n -oX '%s' 'williamdurand.fr'", $outputFile);

$executor = $this->getMock('Nmap\Util\ProcessExecutor');
$executor = $this->getProcessExecutorMock();
$executor
->expects($this->once())
->expects($this->at(1))
->method('execute')
->with($this->equalTo($expectedCommand))
->will($this->returnValue(0));
Expand All @@ -182,14 +182,42 @@ public function testScanWithTreatHostsAsOnline()
$outputFile = __DIR__ . '/Fixtures/test_scan_with_verbose.xml';
$expectedCommand = sprintf("nmap -Pn -oX '%s' 'williamdurand.fr'", $outputFile);

$executor = $this->getMock('Nmap\Util\ProcessExecutor');
$executor = $this->getProcessExecutorMock();
$executor
->expects($this->once())
->expects($this->at(1))
->method('execute')
->with($this->equalTo($expectedCommand))
->will($this->returnValue(0));

$nmap = new Nmap($executor, $outputFile);
$hosts = $nmap->treatHostsAsOnline()->scan(array('williamdurand.fr'));
}

/**
* @expectedException \InvalidArgumentException
*/
public function testExecutableNotExecutable()
{
$executor = $this->getMock('Nmap\Util\ProcessExecutor');
$executor
->expects($this->once())
->method('execute')
->will($this->returnValue(1));

new Nmap($executor);
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject | \Nmap\Util\ProcessExecutor
*/
private function getProcessExecutorMock()
{
$executor = $this->getMock('Nmap\Util\ProcessExecutor');
$executor
->expects($this->at(0))
->method('execute')
->will($this->returnValue(0));

return $executor;
}
}

0 comments on commit de6e39d

Please sign in to comment.