Skip to content

Commit

Permalink
Merge pull request #32 from bmx0r/master
Browse files Browse the repository at this point in the history
Add the ability to give the full qualified path
  • Loading branch information
savon-noir committed May 15, 2014
2 parents f19a40c + 277ffb9 commit 1c14ca1
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 7 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ python:
# - "3.2" Not ready yet
# command to install dependencies
env:
- MONGO_VERSION=1.2.12
- MONGO_VERSION=1.3.2
- MONGO_VERSION=1.3.7
# - MONGO_VERSION=1.2.12
# - MONGO_VERSION=1.3.2
# - MONGO_VERSION=1.3.7
- MONGO_VERSION=2.4.3

services: mongodb
Expand Down
17 changes: 13 additions & 4 deletions libnmap/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class NmapProcess(Thread):
parsed out via the NmapParser class from libnmap.parser module.
"""
def __init__(self, targets="127.0.0.1",
options="-sT", event_callback=None, safe_mode=True):
options="-sT", event_callback=None, safe_mode=True, fqp=None):
"""
Constructor of NmapProcess class.
Expand All @@ -47,6 +47,9 @@ def __init__(self, targets="127.0.0.1",
:param safe_mode: parameter to protect unsafe options like -oN, -oG,
-iL, -oA,...
:param fqp: full qualified path, if None, nmap will be searched
in the PATH
:return: NmapProcess object
"""
Expand All @@ -57,10 +60,16 @@ def __init__(self, targets="127.0.0.1",
unsafe_opts = set(['-oG', '-oN', '-iL', '-oA', '-oS', '-oX',
'--iflist', '--resume', '--stylesheet',
'--datadir'])

self.__nmap_binary_name = "nmap"
if fqp:
if os.path.isfile(fqp) and os.access(fqp, os.X_OK):
self.__nmap_binary = fqp
else:
raise EnvironmentError(1, "wrong path or not executable", fqp)
else:
self.__nmap_binary_name = "nmap"
self.__nmap_binary = self._whereis(self.__nmap_binary_name)
self.__nmap_fixed_options = "-oX - -vvv --stats-every 2s"
self.__nmap_binary = self._whereis(self.__nmap_binary_name)

if self.__nmap_binary is None:
raise EnvironmentError(1, "nmap is not installed or could "
"not be found in system path")
Expand Down
63 changes: 63 additions & 0 deletions libnmap/test/process-stressbox/check_fqp_nmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python
from libnmap.process import NmapProcess
from libnmap.parser import NmapParser, NmapParserException


# start a new nmap scan on localhost with some specific options
def do_scan(targets, options, fqp=None):
nm = NmapProcess(targets, options, fqp=fqp)
rc = nm.run()
if rc != 0:
print "nmap scan failed: %s" % (nm.stderr)

try:
parsed = NmapParser.parse(nm.stdout)
except NmapParserException as e:
print "Exception raised while parsing scan: %s" % (e.msg)

return parsed


# print scan results from a nmap report
def print_scan(nmap_report):
print "Starting Nmap {0} ( http://nmap.org ) at {1}".format(
nmap_report._nmaprun['version'],
nmap_report._nmaprun['startstr'])

for host in nmap_report.hosts:
if len(host.hostnames):
tmp_host = host.hostnames.pop()
else:
tmp_host = host.address

print "Nmap scan report for {0} ({1})".format(
tmp_host,
host.address)
print "Host is {0}.".format(host.status)
print " PORT STATE SERVICE"

for serv in host.services:
pserv = "{0:>5s}/{1:3s} {2:12s} {3}".format(
str(serv.port),
serv.protocol,
serv.state,
serv.service)
if len(serv.banner):
pserv += " ({0})".format(serv.banner)
print pserv
print nmap_report.summary


if __name__ == "__main__":
report = do_scan("127.0.0.1", "-sV")
print_scan(report)
# test with full path to bin
# /usr/bin/nmap
report = do_scan("127.0.0.1", "-sV", fqp="/usr/bin/nmap")
print_scan(report)
# /usr/bin/lol --> will throw exception
try:
report = do_scan("127.0.0.1", "-sV", fqp="/usr/bin/lol")
print_scan(report)
except Exception as exc:
print exc

0 comments on commit 1c14ca1

Please sign in to comment.