forked from savon-noir/python-libnmap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.py
More file actions
205 lines (164 loc) · 6.09 KB
/
service.py
File metadata and controls
205 lines (164 loc) · 6.09 KB
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
#!/usr/bin/env python
from libnmap.diff import NmapDiff
class NmapService(object):
"""
NmapService represents a nmap scanned service. Its id() is comprised
of the protocol and the port.
Depending on the scanning options, some additional details might be
available or not. Like banner or extra datas from NSE (nmap scripts).
"""
def __init__(self, portid, protocol='tcp', state=None,
service=None, service_extras=None):
"""
Constructor
:param portid: port number
:type portid: string
:param protocol: protocol of port scanned (tcp, udp)
:type protocol: string
:param state: python dict describing the service status
:type state: python dict
:param service: python dict describing the service name and banner
:type service: python dict
:param service_extras: additional info about the tested service
like scripts' data
"""
try:
self._portid = int(portid or -1)
except (ValueError, TypeError):
raise
if self._portid < 0 or self._portid > 65535:
raise ValueError
self._protocol = protocol
self._state = state if state is not None else {}
self._service = service if service is not None else {}
self._service_extras = []
if service_extras is not None:
self._service_extras = service_extras
def __eq__(self, other):
"""
Compares two NmapService objects to see if they are the same or
if one of them changed.
:param other: NmapService
:return: boolean
"""
rval = False
if(self.__class__ == other.__class__ and self.id == other.id):
rval = (self.changed(other) == 0)
return rval
def __ne__(self, other):
"""
Compares two NmapService objects to see if they are different
if one of them changed.
:param other: NmapService
:return: boolean
"""
rval = True
if(self.__class__ == other.__class__ and self.id == other.id):
rval = (self.changed(other) > 0)
return rval
def __repr__(self):
return "{0}: [{1} {2}/{3} {4} ({5})]".format(self.__class__.__name__,
self.state,
str(self.port),
self.protocol,
self.service,
self.banner)
def __hash__(self):
return (hash(self.port) ^ hash(self.protocol) ^ hash(self.state) ^
hash(self.service) ^ hash(self.banner))
def changed(self, other):
"""
Checks if a NmapService is different from another.
:param other: NmapService
:return: boolean
"""
return len(self.diff(other).changed())
@property
def port(self):
"""
Accessor for port.
:return: integer or -1
"""
return self._portid
@property
def protocol(self):
"""
Accessor for protocol
:return: string
"""
return self._protocol
@property
def state(self):
"""
Accessor for service's state (open, filtered, closed,...)
:return: string
"""
return self._state['state'] if 'state' in self._state else None
@property
def service(self):
"""
Accessor for service dictionnary.
:return: dict or None
"""
return self._service['name'] if 'name' in self._service else None
def open(self):
"""
Tells if the port was open or not
:return: boolean
"""
return 'state' in self._state and self._state['state'] == 'open'
@property
def banner(self):
"""
Accessor for the service's banner. Only available
if the nmap option -sV or similar was used.
:return: string
"""
notrelevant = ['name', 'method', 'conf']
b = ''
if 'method' in self._service and self._service['method'] == "probed":
b = " ".join([k + ": " + self._service[k]
for k in self._service.keys()
if k not in notrelevant])
return b
def scripts_results(self):
"""
Gives a python dictionary of the nse scripts results.
The dict key is the name (id) of the nse script and
the value is the output of the script.
:return: dict
"""
scripts_dict = None
try:
scripts_dict = dict([(bdct['id'], bdct['output'])
for bdct in self._service_extras])
except (KeyError, TypeError):
pass
return scripts_dict
@property
def id(self):
"""
Accessor for the id() of the NmapService.
This is used for diff()ing NmapService object via NmapDiff.
:return: tuple
"""
return "{0}.{1}".format(self.protocol, self.port)
def get_dict(self):
"""
Return a python dict representation of the NmapService object.
This is used to diff() NmapService objects via NmapDiff.
:return: dict
"""
return ({'id': str(self.id), 'port': str(self.port),
'protocol': self.protocol, 'banner': self.banner,
'service': self.service, 'state': self.state})
def diff(self, other):
"""
Calls NmapDiff to check the difference between self and
another NmapService object.
Will return a NmapDiff object.
This objects return python set() of keys describing the elements
which have changed, were added, removed or kept unchanged.
:return: NmapDiff object
"""
return NmapDiff(self, other)