|
1 | 1 | # libnmap |
2 | 2 |
|
3 | | -[](https://travis-ci.org/savon-noir/python-nmap-lib) |
4 | | - |
5 | 3 | ## Code status |
6 | | -A few basic stuff need to be added (check TODO, please forkme and issue pull |
7 | | -requests) |
8 | 4 |
|
9 | | -## What |
| 5 | +[](https://travis-ci.org/savon-noir/python-libnmap) |
| 6 | + |
| 7 | +## Use cases |
| 8 | + |
| 9 | +libnmap is a python library enabling python developpers to manipulate nmap process and data. |
| 10 | +libnmap is what you were looking for if you need to implement the following: |
| 11 | +- automate or schedule nmap scans on a regular basis |
| 12 | +- manipulate nmap scans results to do reporting for instance |
| 13 | +- compare and diff nmap scans to generate graphs for instance |
| 14 | +- batch process scan reports |
| 15 | +- ... |
| 16 | + |
| 17 | +The above uses cases will be easy to implement with the help of the libnmap modules |
| 18 | + |
| 19 | +## libnmap modules |
| 20 | + |
| 21 | +The lib currently offers the following modules: |
10 | 22 |
|
11 | | -libnmap is a python toolkit for manipulating nmap. It currently offers the following modules: |
12 | 23 | - process: enables you to launch nmap scans |
13 | 24 | - parse: enables you to parse nmap reports or scan results (only XML so far) from a file, a string,... |
14 | 25 | - report: enables you to manipulate a parsed scan result and de/serialize scan results in a json format |
15 | 26 | - diff: enables you to see what changed between two scans |
16 | 27 | - common: contains basic nmap objects like NmapHost and NmapService. It is to note that each object can be "diff()ed" with another similar object. |
17 | 28 | - plugins: enables you to support datastores for your scan results directly in the "NmapReport" object from report module |
18 | 29 | - mongodb: only plugin implemented so far, ultra basic, for POC purpose only |
19 | | - - couchdb: todo |
20 | | - - sqlalchemy: todo |
| 30 | + - sqlalchemy: on-going |
| 31 | + - csv: todo (easy to implement) |
21 | 32 | - elastic search: todo |
22 | | - - csv: todo |
23 | | - |
24 | | -## How |
25 | | - |
26 | | -### Launch a simple scan with event callback |
27 | | -Below a simple example on how to run a nmap scan using a callback function. |
28 | | -No advanced data manipulations with our parser. The callback will simply |
29 | | -printout the percentage done and the etc. The event callback is triggered |
30 | | -each time nmap outputs data. It is to note that a fixed options forces |
31 | | -nmap to send its progress to the lib every two seconds. Consequently, at least |
32 | | -every two seconds, the callback function is triggered even if nmap is not |
33 | | -printing out stuff. |
34 | | - |
35 | | -```python |
36 | | -#!/usr/bin/env python |
37 | | -import sys |
38 | | -from libnmap.process import NmapProcess |
39 | | - |
40 | | -def main(argv): |
41 | | - def mycallback(nmapscan=None, data=""): |
42 | | - if nmapscan.is_running(): |
43 | | - print "Progress: %s %% - ETC: %s" % (nmapscan.progress, |
44 | | - nmapscan.etc) |
45 | | - |
46 | | - nm = NmapProcess("scanme.nmap.org", options="-sV", event_callback=mycallback) |
47 | | - rc = nm.run() |
48 | | - |
49 | | - if rc == 0: |
50 | | - print "Scan started at {0} nmap version: {1}".format(nm.starttime, |
51 | | - nm.version) |
52 | | - print "state: {0} (rc: {1})".format(nm.state, nm.rc) |
53 | | - print "results size: {0}".format(len(nm.stdout)) |
54 | | - print "Scan ended {0}: {1}".format(nm.endtime, nm.summary) |
55 | | - else: |
56 | | - print "state: {0} (rc: {1})".format(nm.state, nm.rc) |
57 | | - print "Error: {stderr}".format(stderr=nm.stderr) |
58 | | - print "Result: {0}".format(nm.stdout) |
59 | | - |
60 | | - |
61 | | -if __name__ == '__main__': |
62 | | - main(sys.argv[1:]) |
63 | | -``` |
64 | | - |
65 | | -### Launch a nmap scan |
66 | | -Here a consequent example on how to use libnmap: |
67 | | -```python |
68 | | -#!/usr/bin/env python |
69 | | -import sys |
70 | | -from libnmap.process import NmapProcess |
71 | | -from libnmap.parser import NmapParser, NmapParserException |
72 | | - |
73 | | - |
74 | | -# start a new nmap scan on localhost with some specific options |
75 | | -def do_scan(targets, options): |
76 | | - nm = NmapProcess(targets, options) |
77 | | - rc = nm.run() |
78 | | - if rc != 0: |
79 | | - print "nmap scan failed: %s" % (nm.stderr) |
80 | | - |
81 | | - try: |
82 | | - parsed = NmapParser.parse(nm.stdout) |
83 | | - except NmapParserException as e: |
84 | | - print "Exception raised while parsing scan: %s" % (e.msg) |
85 | | - |
86 | | - return parsed |
87 | | - |
88 | | - |
89 | | -# print scan results from a nmap report |
90 | | -def print_scan(nmap_report): |
91 | | - print "Starting Nmap {0} ( http://nmap.org ) at {1}".format( |
92 | | - nmap_report._nmaprun['version'], |
93 | | - nmap_report._nmaprun['startstr']) |
94 | | - |
95 | | - for host in nmap_report.hosts: |
96 | | - if len(host.hostnames): |
97 | | - tmp_host = host.hostnames.pop() |
98 | | - else: |
99 | | - tmp_host = host.address |
100 | | - |
101 | | - print "Nmap scan report for {0} ({1})".format( |
102 | | - tmp_host, |
103 | | - host.address) |
104 | | - print "Host is {0}.".format(host.status) |
105 | | - print " PORT STATE SERVICE" |
106 | 33 |
|
107 | | - for serv in host.services: |
108 | | - pserv = "{0:>5s}/{1:3s} {2:12s} {3}".format( |
109 | | - str(serv.port), |
110 | | - serv.protocol, |
111 | | - serv.state, |
112 | | - serv.service) |
113 | | - if len(serv.banner): |
114 | | - pserv += " ({0})".format(serv.banner) |
115 | | - print pserv |
116 | | - print nmap_report.summary |
| 34 | +## Documentation |
117 | 35 |
|
| 36 | +All the documentation is available on [read the docs](<https://libnmap.readthedocs.org>). This documentation contains small code samples that you directly reuse. |
118 | 37 |
|
119 | | -if __name__ == "__main__": |
120 | | - report = do_scan("127.0.0.1", "-sV") |
121 | | - print_scan(report) |
122 | | -``` |
| 38 | +## Examples |
123 | 39 |
|
124 | | -### De/Serialize NmapReport |
125 | | -Easy: |
126 | | -```python |
127 | | -from libnmap.parser import NmapParser |
128 | | -from libnmap.reportjson import ReportDecoder, ReportEncoder |
129 | | -import json |
130 | | - |
131 | | -nmap_report_obj = NmapParser.parse_fromfile('/root/dev/python-nmap-lib/libnmap/test/files/1_hosts.xml') |
132 | | - |
133 | | -# create a json object from an NmapReport instance |
134 | | -nmap_report_json = json.dumps(nmap_report_obj, cls=ReportEncoder) |
135 | | - |
136 | | -# create a NmapReport instance from a json object |
137 | | -nmap_report_obj = json.loads(nmap_report_json, cls=ReportDecoder) |
138 | | -``` |
| 40 | +Some codes samples are available in the examples directory or in the [documentation](<https://libnmap.readthedocs.org>). |
0 commit comments