/
clamav.py
350 lines (295 loc) · 12.9 KB
/
clamav.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
# 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,\
DELETE, Suspect, apply_template
import socket
import string
import os
import struct
import threading
import errno
threadLocal = threading.local()
# it's probably a good idea to re-establish the connection every now and then
MAX_SCANS_PER_SOCKET = 5000
class ClamavPlugin(ScannerPlugin):
"""This plugin passes suspects to a clam daemon.
Actions: This plugin will delete infected messages. If clamd is not reachable or times out, messages can be DEFERRED.
Prerequisites: You must have clamd installed (for performance reasons I recommend it to be on the same box, but this is not absoluely necessary)
Notes for developers:
Tags:
* sets ``virus['ClamAV']`` (boolean)
* sets ``ClamavPlugin.virus`` (list of strings) - virus names found in message
"""
def __init__(self, config, section=None):
ScannerPlugin.__init__(self, config, section)
self.requiredvars = {
'host': {
'default': 'localhost',
'description': 'hostname where clamd runs',
},
'port': {
'default': '3310',
'description': "tcp port number or path to clamd.sock for unix domain sockets\nexample /var/lib/clamav/clamd.sock or on ubuntu: /var/run/clamav/clamd.ctl ",
},
'timeout': {
'default': '30',
'description': 'socket timeout',
},
'pipelining': {
'default': '0',
'description': "*EXPERIMENTAL*: Perform multiple scans over the same connection. May improve performance on busy systems.",
},
'maxsize': {
'default': '22000000',
'description': "maximum message size, larger messages will not be scanned. \nshould match the 'StreamMaxLength' config option in clamd.conf ",
},
'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 "Clam AV"
def _problemcode(self):
retcode = string_to_actioncode(
self.config.get(self.section, 'problemaction'), self.config)
if retcode != None:
return retcode
else:
# in case of invalid problem action
return DEFER
def examine(self, suspect):
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 != None:
self.logger.info(
"Virus found in message from %s : %s" % (suspect.from_address, viruses))
suspect.tags['virus']['ClamAV'] = True
suspect.tags['ClamavPlugin.virus'] = viruses
suspect.debug('viruses found in message : %s' % viruses)
else:
suspect.tags['virus']['ClamAV'] = False
addheaderclean = self.config.get(self.section, 'addheaderclean')
suspect.set_tag('clam.clean', 'Clean')
if viruses != 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('clam.virus', firstvirusname)
return actioncode, message
return DUNNO
except Exception as e:
self.__invalidate_socket()
# don't warn the first time if it's just a broken pipe which
# can happen with the new pipelining protocol
if not (i == 0 and isinstance(e, socket.error) and e.errno == errno.EPIPE):
self.logger.warning("Error encountered while contacting clamd (try %s of %s): %s" % (
i + 1, self.config.getint(self.section, 'retries'), str(e)))
self.logger.error("Clamdscan failed after %s retries" %
self.config.getint(self.section, 'retries'))
content = None
return self._problemcode()
def scan_stream(self, buff):
"""
Scan byte buffer
return either :
- (dict) : {filename1: "virusname"}
- None if no virus found
- raises Exception if something went wrong
"""
pipelining = self.config.getboolean(self.section, 'pipelining')
s = self.__init_socket__(oneshot=not pipelining)
s.sendall('zINSTREAM\0')
default_chunk_size = 2048
remainingbytes = buff
while len(remainingbytes) > 0:
chunklength = min(default_chunk_size, len(remainingbytes))
#self.logger.debug('sending %s byte chunk' % chunklength)
chunkdata = remainingbytes[:chunklength]
remainingbytes = remainingbytes[chunklength:]
s.sendall(struct.pack('!L', chunklength))
s.sendall(chunkdata)
s.sendall(struct.pack('!L', 0))
dr = {}
result = self._read_until_delimiter(s).strip()
if result.startswith('INSTREAM size limit exceeded'):
raise Exception(
"Clamd size limit exeeded. Make sure fuglu's clamd maxsize config is not larger than clamd's StreamMaxLength")
if result.startswith('UNKNOWN'):
raise Exception(
"Clamd doesn't understand INSTREAM command. very old version?")
if pipelining:
try:
ans_id, filename, virusinfo = result.split(':', 2)
filename = filename.strip()
virusinfo = virusinfo.strip()
except:
raise Exception(
"Protocol error, could not parse result: %s" % result)
threadLocal.expectedID += 1
if threadLocal.expectedID != int(ans_id):
raise Exception(
"Commands out of sync - expected ID %s - got %s" % (threadLocal.expectedID, ans_id))
if virusinfo[-5:] == 'ERROR':
raise Exception(virusinfo)
elif virusinfo != 'OK':
dr[filename] = virusinfo.replace(" FOUND", '')
if threadLocal.expectedID >= MAX_SCANS_PER_SOCKET:
try:
s.sendall('zEND\0')
s.close()
finally:
self.__invalidate_socket()
else:
filename, virusinfo = result.split(':', 1)
filename = filename.strip()
virusinfo = virusinfo.strip()
if virusinfo[-5:] == 'ERROR':
raise Exception(virusinfo)
elif virusinfo != 'OK':
dr[filename] = virusinfo.replace(" FOUND", '')
s.close()
if dr == {}:
return None
else:
return dr
def _read_until_delimiter(self, socket):
data = ''
while True:
chunk = socket.recv(4096)
if len(chunk) == 0:
continue
data += chunk
if chunk.endswith('\0'):
break
if '\0' in chunk:
raise Exception(
"Protocol error: got unexpected additional data after delimiter")
return data[:-1] # remove \0 at the end
def __invalidate_socket(self):
threadLocal.clamdsocket = None
threadLocal.expectedID = 0
def __init_socket__(self, oneshot=False):
"""initialize a socket connection to clamd using host/port/file defined in the configuration
this connection is initialized with clamd's "IDSESSION" and cached per thread
set oneshot=True to get a socket without caching it and without initializing it with an IDSESSION
"""
existing_socket = getattr(threadLocal, 'clamdsocket', None)
socktimeout = self.config.getint(self.section, 'timeout')
if existing_socket != None and not oneshot:
existing_socket.settimeout(socktimeout)
return existing_socket
clamd_HOST = self.config.get(self.section, 'host')
unixsocket = False
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(socktimeout)
try:
s.connect(sock)
except socket.error:
raise Exception(
'Could not reach clamd using unix socket %s' % sock)
else:
clamd_PORT = int(self.config.get(self.section, 'port'))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(socktimeout)
try:
s.connect((clamd_HOST, clamd_PORT))
except socket.error:
raise Exception(
'Could not reach clamd using network (%s, %s)' % (clamd_HOST, clamd_PORT))
# initialize an IDSESSION
if not oneshot:
s.sendall('zIDSESSION\0')
threadLocal.clamdsocket = s
threadLocal.expectedID = 0
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_ping() and self.lint_eicar()
return allok
def lint_ping(self):
try:
s = self.__init_socket__(oneshot=True)
except Exception as e:
print("Could not contact clamd: %s" % (str(e)))
return False
s.sendall('PING')
result = s.recv(20000)
print("Got Pong: %s" % result)
if result.strip() != 'PONG':
print("Invalid PONG:" % result)
return True
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 == None:
print("EICAR Test virus not found!")
return False
print("Clamav found virus", result)
return True