-
Notifications
You must be signed in to change notification settings - Fork 0
/
fprot.py
executable file
·277 lines (236 loc) · 10.1 KB
/
fprot.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
# 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, DELETE, DUNNO, DEFER, string_to_actioncode, apply_template
import socket
import time
import re
import os
class FprotPlugin(ScannerPlugin):
""" This plugin passes suspects to a f-prot scan daemon
Prerequisites: f-protd must be installed and running, not necessarily on the same box as fuglu though.
Notes for developers:
Tags:
* sets ``virus['F-Prot']`` (boolean)
* sets ``FprotPlugin.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 fpscand runs',
},
'port': {
'default': '10200',
'description': "fpscand port",
},
'timeout': {
'default': '30',
'description': "network timeout",
},
'networkmode': {
'default': '0',
'description': "if fpscand runs on a different host than fuglu, set this to 1 to send the message over the network instead of just the filename",
},
'scanoptions': {
'default': '',
'description': 'additional scan options (see `man fpscand` -> SCANNING OPTIONS for possible values)',
},
'maxsize': {
'default': '10485000',
'description': "maximum message size to scan",
},
'retries': {
'default': '3',
'description': "maximum retries on failed connections",
},
'virusaction': {
'default': 'DEFAULTVIRUSACTION',
'description': "plugin action if threat is detected",
},
'problemaction': {
'default': 'DEFER',
'description': "plugin action if scan fails",
},
'rejectmessage': {
'default': 'threat detected: ${virusname}',
'description': "reject message template if running in pre-queue mode and virusaction=REJECT",
},
}
self.pattern = re.compile('^(\d)+ <(.+)> (.+)$')
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):
starttime = time.time()
if suspect.size > self.config.getint(self.section, 'maxsize'):
self._logger().info('Not scanning - message too big (message %s bytes > config %s bytes )' %
(suspect.size, self.config.getint(self.section, 'maxsize')))
return DUNNO
content = suspect.get_message_rep().as_string()
for i in range(0, self.config.getint(self.section, 'retries')):
try:
headerscannername = '-' + self.config.get(self.section, 'headerscannername')
if headerscannername == '-':
headerscannername = ''
if self.config.getboolean(self.section, 'networkmode'):
viruses = self.scan_stream(content)
else:
viruses = self.scan_file(suspect.tempfile)
if viruses != None:
self._logger().info("Virus found in message from %s : %s" %
(suspect.from_address, viruses))
suspect.tags['virus']['F-Prot'] = True
suspect.tags['FprotPlugin.virus'] = viruses
suspect.debug('Viruses found in message : %s' % viruses)
else:
suspect.tags['virus']['F-Prot'] = False
addheaderclean = self.config.get(self.section, 'addheaderclean')
suspect.set_tag('fprot.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('fprot.virus', firstvirusname)
return actioncode, message
else:
return DUNNO
except Exception as e:
self._logger().warning("Error encountered while contacting fpscand (try %s of %s): %s" %
(i + 1, self.config.getint(self.section, 'retries'), str(e)))
self._logger().error("fpscand failed after %s retries" %
self.config.getint(self.section, 'retries'))
content = None
return self._problemcode()
def _parse_result(self, result):
dr = {}
for line in result.strip().split('\n'):
m = self.pattern.match(line)
if m == None:
self._logger().error(
'Could not parse line from f-prot: %s' % line)
raise Exception('f-prot: Unparseable answer: %s' % result)
status = m.group(1)
text = m.group(2)
details = m.group(3)
status = int(status)
self._logger().debug("f-prot scan status: %s" % status)
self._logger().debug("f-prot scan text: %s" % text)
if status == 0:
continue
if status > 3:
self._logger().warning(
"f-prot: got unusual status %s" % status)
# http://www.f-prot.com/support/helpfiles/unix/appendix_c.html
if status & 1 == 1 or status & 2 == 2:
# we have a infection
if text[0:10] == "infected: ":
text = text[10:]
elif text[0:27] == "contains infected objects: ":
text = text[27:]
else:
self._logger().warn(
"Unexpected reply from f-prot: %s" % text)
continue
dr[details] = text
if len(dr) == 0:
return None
else:
return dr
def scan_file(self, filename):
filename = os.path.abspath(filename)
s = self.__init_socket__()
s.sendall('SCAN %s FILE %s' %
(self.config.get(self.section, 'scanoptions'), filename))
s.sendall('\n')
result = s.recv(20000)
if len(result) < 1:
self._logger().error('Got no reply from fpscand')
s.close()
return self._parse_result(result)
def scan_stream(self, buffer):
"""
Scan a buffer
buffer (string) : buffer to scan
return either :
- (dict) : {filename1: "virusname"}
- None if no virus found
"""
s = self.__init_socket__()
buflen = len(buffer)
s.sendall('SCAN %s STREAM fu_stream SIZE %s' %
(self.config.get(self.section, 'scanoptions'), buflen))
s.sendall('\n')
self._logger().debug(
'Sending buffer (length=%s) to fpscand...' % buflen)
s.sendall(buffer)
self._logger().debug(
'Sent %s bytes to fpscand, waiting for scan result' % buflen)
result = s.recv(20000)
if len(result) < 1:
self._logger().error('Got no reply from fpscand')
s.close()
return self._parse_result(result)
def __init_socket__(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(self.config.getint(self.section, 'timeout'))
try:
s.connect((self.config.get(self.section, 'host'),
self.config.getint(self.section, 'port')))
except socket.error:
raise Exception('Could not reach fpscand using network (%s, %s)' % (
self.config.get(self.section, 'host'), self.config.getint(self.section, 'port')))
return s
def __str__(self):
return 'F-Prot AV'
def lint(self):
allok = (self.checkConfig() and self.lint_eicar())
return allok
def lint_eicar(self):
stream = """Date: Mon, 08 Sep 2008 17:33:54 +0200
From: [email protected]
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("F-Prot found virus", result)
return True