/
sssp.py
409 lines (315 loc) · 11.9 KB
/
sssp.py
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
# Copyright 2009-2017 Oli Schacher
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
from fuglu.shared import ScannerPlugin, string_to_actioncode, DEFER, DUNNO, actioncode_to_string, apply_template
import socket
import os
#
# sssp_utils.py Some utility functions used by the SSSP python sampl apps.
#
# Copyright (c) 2006 Sophos Plc, www.sophos.com.
#
import re
# Regular Expressions defining some messages from the server.
acceptsyntax = re.compile("^ACC\s+(.*?)\s*$")
optionsyntax = re.compile("^(\w+):\s*(.*?)\s*$")
virussyntax = re.compile("^VIRUS\s+(\S+)\s+(.*)")
typesyntax = re.compile("^TYPE\s+(\w+)")
donesyntax = re.compile("^DONE\s+(\w+)\s+(\w+)\s+(.*?)\s*$")
eventsyntax = re.compile("^([A-Z]+)\s+(\w+)")
# Receives a line of text from the socket
# \r chars are discarded
# The line is terminated by a \n
# NUL chars indicate a broken socket
def receiveline(s):
line = ''
done = 0
while not done:
c = s.recv(1)
if c == '':
return ''
done = (c == '\n')
if not done and c != '\r':
line = line + c
return line
# Receives a whole message. Messages are terminated by
# a blank line
def receivemsg(s):
response = []
finished = 0
while not finished:
msg = receiveline(s)
finished = (len(msg) == 0)
if not finished:
response.append(msg)
return response
# Receives the ACC message which is a single line
# conforming to the acceptsyntax RE.
def accepted(s):
acc = receiveline(s)
return acceptsyntax.match(acc)
# Reads a message which should be a list of options
# and transforms them into a dictionary
def readoptions(s):
resp = receivemsg(s)
opts = {}
for l in resp:
parts = optionsyntax.findall(l)
for p in parts:
if p[0] not in opts:
opts[p[0]] = []
opts[p[0]].append(p[1])
return opts
# Performs the initial exchange of messages.
def exchangeGreetings(s):
line = receiveline(s)
if not line.startswith('OK SSSP/1.0'):
return 0
s.send('SSSP/1.0\n')
if not accepted(s):
print("Greeting Rejected!!")
return 0
return 1
# performs the final exchange of messages
def sayGoodbye(s):
s.send('BYE\n')
receiveline(s)
class SSSPPlugin(ScannerPlugin):
""" This plugin scans the suspect using the sophos SSSP protocol.
Prerequisites: Requires a running sophos daemon with dynamic interface (SAVDI)
"""
def __init__(self, config, section=None):
ScannerPlugin.__init__(self, config, section)
self.requiredvars = {
'host': {
'default': 'localhost',
'description': 'hostname where the SSSP server runs',
},
'port': {
'default': '4010',
'description': "tcp port or path to unix socket",
},
'timeout': {
'default': '30',
'description': 'socket timeout',
},
'maxsize': {
'default': '22000000',
'description': "maximum message size, larger messages will not be scanned. ",
},
'retries': {
'default': '3',
'description': 'how often should fuglu retry the connection before giving up',
},
'virusaction': {
'default': 'DEFAULTVIRUSACTION',
'description': "action if infection is detected (DUNNO, REJECT, DELETE)",
},
'problemaction': {
'default': 'DEFER',
'description': "action if there is a problem (DUNNO, DEFER)",
},
'rejectmessage': {
'default': 'threat detected: ${virusname}',
'description': "reject message template if running in pre-queue mode and virusaction=REJECT",
},
}
self.logger = self._logger()
def __str__(self):
return "Sophos"
def _problemcode(self):
retcode = string_to_actioncode(
self.config.get(self.section, 'problemaction'), self.config)
if retcode is not None:
return retcode
else:
# in case of invalid problem action
return DEFER
def examine(self, suspect):
enginename = 'sophos'
if suspect.size > self.config.getint(self.section, 'maxsize'):
self.logger.info('Not scanning - message too big')
return
content = suspect.get_source()
for i in range(0, self.config.getint(self.section, 'retries')):
try:
viruses = self.scan_stream(content)
headerscannername = '-' + self.config.get(self.section, 'headerscannername')
if headerscannername == '-':
headerscannername = ''
if viruses is not None:
self.logger.info(
"Virus found in message from %s : %s" % (suspect.from_address, viruses))
suspect.tags['virus'][enginename] = True
suspect.tags['%s.virus' % enginename] = viruses
suspect.debug('viruses found in message : %s' % viruses)
else:
suspect.tags['virus'][enginename] = False
addheaderclean = self.config.get(self.section, 'addheaderclean')
suspect.set_tag('sssp.clean', 'Clean')
if viruses is not None:
virusaction = self.config.get(self.section, 'virusaction')
actioncode = string_to_actioncode(virusaction, self.config)
addheaderinfected = self.config.get(self.section, 'addheaderinfected')
firstinfected, firstvirusname = list(viruses.items())[0]
values = dict(
infectedfile=firstinfected, virusname=firstvirusname)
message = apply_template(
self.config.get(self.section, 'rejectmessage'), suspect, values)
suspect.set_tag('sssp.virus', firstvirusname)
return actioncode, message
return DUNNO
except Exception as e:
self.logger.warning("Error encountered while contacting SSSP server (try %s of %s): %s" % (
i + 1, self.config.getint(self.section, 'retries'), str(e)))
self.logger.error("SSSP scan failed after %s retries" %
self.config.getint(self.section, 'retries'))
return self._problemcode()
def scan_stream(self, buf):
"""
Scan a buffer
buffer (string) : buffer to scan
return either :
- (dict) : {filename1: "virusname"}
- None if no virus found
"""
s = self.__init_socket__()
dr = {}
# Read the welcome message
if not exchangeGreetings(s):
raise Exception("SSSP Greeting failed")
# QUERY to discover the maxclassificationsize
s.send('SSSP/1.0 QUERY\n')
if not accepted(s):
raise Exception("SSSP Query rejected")
options = readoptions(s)
# Set the options for classification
enableoptions = [
"TnefAttachmentHandling",
"ActiveMimeHandling",
"Mime",
"ZipDecompression",
"DynamicDecompression",
]
enablegroups = [
'GrpExecutable',
'GrpArchiveUnpack',
'GrpSelfExtract',
'GrpInternet',
'GrpSuper',
'GrpMisc',
]
sendbuf = "OPTIONS\nreport:all\n"
for opt in enableoptions:
sendbuf += "savists: %s 1\n" % opt
for grp in enablegroups:
sendbuf += "savigrp: %s 1\n" % grp
# all sent, add aditional newline
sendbuf += "\n"
s.send(sendbuf)
if not accepted(s):
raise Exception("SSSP Options not accepted")
resp = receivemsg(s)
for l in resp:
if donesyntax.match(l):
parts = donesyntax.findall(l)
if parts[0][0] != 'OK':
raise Exception("SSSP Options failed")
break
# Send the SCAN request
s.send('SCANDATA ' + str(len(buf)) + '\n')
if not accepted(s):
raise Exception("SSSP Scan rejected")
s.sendall(buf)
# and read the result
events = receivemsg(s)
for l in events:
if virussyntax.match(l):
parts = virussyntax.findall(l)
virus = parts[0][0]
filename = parts[0][1]
dr[filename] = virus
try:
sayGoodbye(s)
s.shutdown(socket.SHUT_RDWR)
except socket.error as e:
self.logger.warning('Error terminating connection: %s', str(e))
finally:
s.close()
if dr == {}:
return None
else:
return dr
def __init_socket__(self):
sssp_HOST = self.config.get(self.section, 'host')
unixsocket = False
iport = None
try:
iport = int(self.config.get(self.section, 'port'))
except ValueError:
unixsocket = True
if unixsocket:
sock = self.config.get(self.section, 'port')
if not os.path.exists(sock):
raise Exception("unix socket %s not found" % sock)
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.settimeout(self.config.getint(self.section, 'timeout'))
try:
s.connect(sock)
except socket.error:
raise Exception(
'Could not reach SSSP server using unix socket %s' % sock)
else:
sssp_PORT = iport
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(self.config.getint(self.section, 'timeout'))
try:
s.connect((sssp_HOST, sssp_PORT))
except socket.error:
raise Exception(
'Could not reach SSSP server using network (%s, %s)' % (sssp_HOST, sssp_PORT))
return s
def lint(self):
viract = self.config.get(self.section, 'virusaction')
print("Virusaction: %s" % actioncode_to_string(
string_to_actioncode(viract, self.config)))
allok = self.checkConfig() and self.lint_eicar()
return allok
def lint_eicar(self):
stream = """Date: Mon, 08 Sep 2008 17:33:54 +0200
To: oli@unittests.fuglu.org
From: oli@unittests.fuglu.org
Subject: test eicar attachment
X-Mailer: swaks v20061116.0 jetmore.org/john/code/#swaks
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="----=_MIME_BOUNDARY_000_12140"
------=_MIME_BOUNDARY_000_12140
Content-Type: text/plain
Eicar test
------=_MIME_BOUNDARY_000_12140
Content-Type: application/octet-stream
Content-Transfer-Encoding: BASE64
Content-Disposition: attachment
UEsDBAoAAAAAAGQ7WyUjS4psRgAAAEYAAAAJAAAAZWljYXIuY29tWDVPIVAlQEFQWzRcUFpYNTQo
UF4pN0NDKTd9JEVJQ0FSLVNUQU5EQVJELUFOVElWSVJVUy1URVNULUZJTEUhJEgrSCoNClBLAQIU
AAoAAAAAAGQ7WyUjS4psRgAAAEYAAAAJAAAAAAAAAAEAIAD/gQAAAABlaWNhci5jb21QSwUGAAAA
AAEAAQA3AAAAbQAAAAAA
------=_MIME_BOUNDARY_000_12140--"""
result = self.scan_stream(stream)
if result is None:
print("EICAR Test virus not found!")
return False
print("SSSP server found virus", result)
return True