Skip to content

Commit 1fee4d4

Browse files
committed
Merge remote branch 'origin/refactparser' into pluginRefactory
2 parents feff0a2 + ed43454 commit 1fee4d4

15 files changed

Lines changed: 444 additions & 195 deletions

README.md

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,73 @@ libnmap is a python toolkit for manipulating nmap. It currently offers the follo
2121
- elastic search: todo
2222
- csv: todo
2323

24-
How
25-
===
26-
Will be added very soon but I have to go get some sushis first :)
24+
## How
25+
26+
### Launch a nmap scan
27+
Here a consequent example on how to use libnmap:
28+
```python
29+
#!/usr/bin/env python
30+
from libnmap import NmapProcess, NmapParser, NmapParserException
31+
32+
33+
# start a new nmap scan on localhost with some specific options
34+
def do_scan(targets, options):
35+
nm = NmapProcess(targets, options)
36+
rc = nm.run()
37+
if rc != 0:
38+
print "nmap scan failed: %s" % (nm.stderr)
39+
40+
try:
41+
parsed = NmapParser.parse(nm.stdout)
42+
except NmapParserException as e:
43+
print "Exception raised while parsing scan: %s" % (e.msg)
44+
45+
return parsed
46+
47+
48+
# print scan results from a nmap report
49+
def print_scan(nmap_report):
50+
print "Starting Nmap {0} ( http://nmap.org ) at {1}".format(
51+
nmap_report._nmaprun['version'],
52+
nmap_report._nmaprun['startstr'])
53+
54+
for host in nmap_report.scanned_hosts:
55+
print "Nmap scan report for {0} ({1})".format(
56+
host.hostname,
57+
host.address)
58+
print "Host is {0}.".format(host.status)
59+
print " PORT STATE SERVICE"
60+
61+
for serv in host.services:
62+
pserv = "{0:>5s}/{1:3s} {2:12s} {3}".format(
63+
str(serv.port),
64+
serv.protocol,
65+
serv.state,
66+
serv.service)
67+
if len(serv.banner):
68+
pserv += " ({0})".format(serv.banner)
69+
print pserv
70+
print nmap_report.summary
71+
72+
73+
if __name__ == "__main__":
74+
report = do_scan("127.0.0.1", "-sV")
75+
print_scan(report)
76+
```
77+
78+
### De/Serialize NmapReport
79+
Easy:
80+
```python
81+
from libnmap import NmapParser, NmapReport
82+
from libnmap import ReportDecoder, ReportEncoder
83+
import json
84+
85+
r = NmapParser.parse_fromfile('/root/dev/python-nmap-lib/libnmap/test/files/1_hosts.xml')
86+
87+
# create a json object from an NmapReport instance
88+
j = json.dumps(r, cls=ReportEncoder)
89+
90+
# create a NmapReport instance from a json object
91+
nmapreport = json.loads(j, cls=ReportDecoder)
92+
nmapreport.name
93+
```

TODO

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
- add unittest for udp scans, ping sweeping
88
- add support for 'resume' capability (see nmap --resume)
99
- review thread management for NmapProcess
10+
- add a safe_run toggle in constructor of NmapProcess to cloak unsecure options like -oN, -oG, -oX
11+
- add support for "not shown ports" (extra ports) in NmapHost (via NmapParser)
12+
- add API support in NmapReport for returning nmap_run data

libnmap/__init__.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,3 @@
77
__all__ = ["NmapHost", "NmapService", "NmapParser", "NmapParserException",
88
"NmapReport", "NmapProcess", "DictDiffer", "NmapDiff",
99
"NmapDiffException", "ReportDecoder", "ReportEncoder"]
10-
11-
from diff import NmapDiff, DictDiffer, NmapDiffException
12-
from common import NmapHost, NmapService
13-
from process import NmapProcess
14-
from parser import NmapParser, NmapParserException
15-
from report import NmapReport
16-
from reportjson import ReportDecoder, ReportEncoder

libnmap/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
from libnmap import NmapDiff
2+
from libnmap.diff import NmapDiff
33

44

55
class NmapHost(object):

0 commit comments

Comments
 (0)