File: report.py

package info (click to toggle)
python-libnmap 0.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,416 kB
  • sloc: xml: 5,572; python: 4,299; makefile: 149
file content (424 lines) | stat: -rw-r--r-- 11,064 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# -*- coding: utf-8 -*-
from libnmap.diff import NmapDiff


class NmapReport(object):
    """
    NmapReport is the usual interface for the end user to
    read scans output.

    A NmapReport as the following structure:

    - Scan headers data
    - A list of scanned hosts (NmapReport.hosts)
    - Scan footer data

    It is to note that each NmapHost comprised in NmapReport.hosts array
    contains also a list of scanned services (NmapService object).

    This means that if NmapParser.parse*() is the input interface for the
    end user of the lib. NmapReport is certainly the output interface for
    the end user of the lib.
    """

    def __init__(self, raw_data=None):
        """
        Constructor for NmapReport object.

        This is usually called by the NmapParser module.
        """
        self._nmaprun = {}
        self._scaninfo = {}
        self._hosts = []
        self._runstats = {}
        if raw_data is not None:
            self.__set_raw_data(raw_data)

    def save(self, backend):
        """
        This method gets a NmapBackendPlugin representing the backend.

        :param backend: libnmap.plugins.PluginBackend object.

        Object created by BackendPluginFactory and enabling nmap reports
        to be saved/stored in any type of backend implemented in plugins.

        The primary key of the stored object is returned.

        :return: str
        """
        if backend is not None:
            _id = backend.insert(self)
        else:
            raise RuntimeError
        return _id

    def diff(self, other):
        """
        Calls NmapDiff to check the difference between self and
        another NmapReport object.

        Will return a NmapDiff object.

        :return: NmapDiff object
        :todo: remove is_consistent approach, diff() should be generic.
        """
        if self.is_consistent() and other.is_consistent():
            _rdiff = NmapDiff(self, other)
        else:
            _rdiff = set()
        return _rdiff

    @property
    def started(self):
        """
        Accessor returning a unix timestamp of when the scan was started.

        :return: integer
        """
        rval = -1
        try:
            s_start = self._nmaprun["start"]
            rval = int(s_start)
        except (KeyError, TypeError, ValueError):
            pass
        return rval

    @property
    def startedstr(self):
        """
        Accessor returning a human readable string of when the
        scan was started

        :return: string
        """
        rval = ""
        try:
            rval = self._nmaprun["startstr"]
        except (KeyError, TypeError, ValueError):
            pass
        return rval

    @property
    def commandline(self):
        """
        Accessor returning the full nmap command line fired.

        :return: string
        """
        return self._nmaprun["args"]

    @property
    def version(self):
        """
        Accessor returning the version of the
        nmap binary used to perform the scan.

        :return: string
        """
        return self._nmaprun["version"]

    @property
    def xmlversion(self):
        """
        Accessor returning the XML output
        version of the nmap report.

        :return: string
        """
        return self._nmaprun["xmloutputversion"]

    @property
    def scan_type(self):
        """
        Accessor returning a string which identifies what type of scan
        was launched (syn, ack, tcp,...).

        :return: string
        """
        return self._scaninfo.get("type")

    @property
    def numservices(self):
        """
        Accessor returning the number of services the
        scan attempted to enumerate.

        :return: integer
        """
        rval = -1
        try:
            s_numsvcs = self._scaninfo.get("numservices")
            rval = int(s_numsvcs)
        except (KeyError, TypeError, ValueError):
            pass
        return rval

    @property
    def hosts(self):
        """
        Accessor returning an array of scanned hosts.

        Scanned hosts are NmapHost objects.

        :return: array of NmapHost
        """
        return self._hosts

    def get_host_byid(self, host_id):
        """
        Gets a NmapHost object directly from the host array
        by looking it up by id.

        :param ip_addr: ip address of the host to lookup
        :type ip_addr: string

        :return: NmapHost
        """
        rval = None
        for _rhost in self._hosts:
            if _rhost.address == host_id:
                rval = _rhost
        return rval

    @property
    def endtime(self):
        """
        Accessor returning a unix timestamp of when the scan ended.

        :return: integer
        """
        rval = -1
        try:
            rval = int(self._runstats["finished"]["time"])
        except (KeyError, TypeError, ValueError):
            pass
        return rval

    @property
    def endtimestr(self):
        """
        Accessor returning a human readable time string
        of when the scan ended.

        :return: string
        """
        rval = ""
        try:
            rval = self._runstats["finished"]["timestr"]
        except (KeyError, TypeError, ValueError):
            pass
        return rval

    @property
    def summary(self):
        """
        Accessor returning a string describing and
        summarizing the scan.

        :return: string
        """
        rval = ""
        try:
            rval = self._runstats["finished"]["summary"]
        except (KeyError, TypeError):
            pass

        if len(rval) == 0:
            rval = (
                "Nmap ended at {0} ; {1} IP addresses ({2} hosts up)"
                " scanned in {3} seconds".format(
                    self.endtimestr,
                    self.hosts_total,
                    self.hosts_up,
                    self.elapsed,
                )
            )
        return rval

    @property
    def elapsed(self):
        """
        Accessor returning the number of seconds the scan took

        :return: float (0 >= or -1)
        """
        rval = -1
        try:
            s_elapsed = self._runstats["finished"]["elapsed"]
            rval = float(s_elapsed)
        except (KeyError, TypeError, ValueError):
            rval = -1
        return rval

    @property
    def hosts_up(self):
        """
        Accessor returning the number of host detected
        as 'up' during the scan.

        :return: integer (0 >= or -1)
        """
        rval = -1
        try:
            s_up = self._runstats["hosts"]["up"]
            rval = int(s_up)
        except (KeyError, TypeError, ValueError):
            rval = -1
        return rval

    @property
    def hosts_down(self):
        """
        Accessor returning the number of host detected
        as 'down' during the scan.

        :return: integer (0 >= or -1)
        """
        rval = -1
        try:
            s_down = self._runstats["hosts"]["down"]
            rval = int(s_down)
        except (KeyError, TypeError, ValueError):
            rval = -1
        return rval

    @property
    def hosts_total(self):
        """
        Accessor returning the number of hosts scanned in total.

        :return: integer (0 >= or -1)
        """
        rval = -1
        try:
            s_total = self._runstats["hosts"]["total"]
            rval = int(s_total)
        except (KeyError, TypeError, ValueError):
            rval = -1
        return rval

    def get_raw_data(self):
        """
        Returns a dict representing the NmapReport object.

        :return: dict
        :todo: deprecate. get rid of this ugliness.
        """
        raw_data = {
            "_nmaprun": self._nmaprun,
            "_scaninfo": self._scaninfo,
            "_hosts": self._hosts,
            "_runstats": self._runstats,
        }
        return raw_data

    def __set_raw_data(self, raw_data):
        self._nmaprun = raw_data["_nmaprun"]
        self._scaninfo = raw_data["_scaninfo"]
        self._hosts = raw_data["_hosts"]
        self._runstats = raw_data["_runstats"]

    def is_consistent(self):
        """
        Checks if the report is consistent and can be diffed().

        This needs to be rewritten and removed: diff() should be generic.

        :return: boolean
        """
        rval = True
        rdata = self.get_raw_data()
        _consistent_keys = ["_nmaprun", "_scaninfo", "_hosts", "_runstats"]
        if set(_consistent_keys) != set(rdata):
            rval = False
        if None in rdata.values():
            rval = False
        return rval

    def get_dict(self):
        """
        Return a python dict representation of the NmapReport object.
        This is used to diff() NmapReport objects via NmapDiff.

        :return: dict
        """
        rdict = dict(
            [
                (
                    "{0}::{1}".format(_host.__class__.__name__, str(_host.id)),
                    hash(_host),
                )
                for _host in self.hosts
            ]
        )
        rdict.update(
            {
                "commandline": self.commandline,
                "version": self.version,
                "scan_type": self.scan_type,
                "elapsed": self.elapsed,
                "hosts_up": self.hosts_up,
                "hosts_down": self.hosts_down,
                "hosts_total": self.hosts_total,
            }
        )
        return rdict

    @property
    def id(self):
        """
        Dummy id() defined for reports.
        """
        return hash(1)

    def __eq__(self, other):
        """
        Compare eq NmapReport based on :

            - create a diff obj and check the result
            report are equal if added&changed&removed are empty

        :return: boolean
        """
        rval = False
        if self.__class__ == other.__class__ and self.id == other.id:
            diffobj = self.diff(other)
            rval = (
                len(diffobj.changed()) == 0
                and len(diffobj.added()) == 0
                and len(diffobj.removed()) == 0
            )
        return rval

    def __ne__(self, other):
        """
        Compare ne NmapReport based on:

            - create a diff obj and check the result
            report are ne if added|changed|removed are not empty

        :return: boolean
        """
        rval = True
        if self.__class__ == other.__class__ and self.id == other.id:
            diffobj = self.diff(other)
            rval = (
                len(diffobj.changed()) != 0
                or len(diffobj.added()) != 0
                or len(diffobj.removed()) != 0
            )
        return rval

    def __repr__(self):
        """
        Returns a string-based representation of the report

        :return: string
        """
        return "{0}: started at {1} hosts up {2}/{3}".format(
            self.__class__.__name__,
            self.started,
            self.hosts_up,
            self.hosts_total,
        )