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
|
--[[
Copyright (c) 2018, Vsevolod Stakhov <vsevolod@highsecure.ru>
Copyright (c) 2018, Carsten Rosenberg <c.rosenberg@heinlein-support.de>
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.
]]--
--[[[
-- @module dcc
-- This module contains dcc access functions
--]]
local lua_util = require "lua_util"
local tcp = require "rspamd_tcp"
local upstream_list = require "rspamd_upstream_list"
local rspamd_logger = require "rspamd_logger"
local common = require "lua_scanners/common"
local fun = require "fun"
local N = 'dcc'
local function dcc_config(opts)
local dcc_conf = {
name = N,
default_port = 10045,
timeout = 5.0,
log_clean = false,
retransmits = 2,
cache_expire = 7200, -- expire redis in 2h
message = '${SCANNER}: bulk message found: "${VIRUS}"',
detection_category = "hash",
default_score = 1,
action = false,
client = '0.0.0.0',
symbol_fail = 'DCC_FAIL',
symbol = 'DCC_REJECT',
symbol_bulk = 'DCC_BULK',
body_max = 999999,
fuz1_max = 999999,
fuz2_max = 999999,
}
dcc_conf = lua_util.override_defaults(dcc_conf, opts)
if not dcc_conf.prefix then
dcc_conf.prefix = 'rs_' .. dcc_conf.name .. '_'
end
if not dcc_conf.log_prefix then
dcc_conf.log_prefix = dcc_conf.name
end
if not dcc_conf.servers and dcc_conf.socket then
dcc_conf.servers = dcc_conf.socket
end
if not dcc_conf.servers then
rspamd_logger.errx(rspamd_config, 'no servers defined')
return nil
end
dcc_conf.upstreams = upstream_list.create(rspamd_config,
dcc_conf.servers,
dcc_conf.default_port)
if dcc_conf.upstreams then
lua_util.add_debug_alias('external_services', dcc_conf.name)
return dcc_conf
end
rspamd_logger.errx(rspamd_config, 'cannot parse servers %s',
dcc_conf['servers'])
return nil
end
local function dcc_check(task, content, digest, rule)
local function dcc_check_uncached ()
local upstream = rule.upstreams:get_upstream_round_robin()
local addr = upstream:get_addr()
local retransmits = rule.retransmits
local client = rule.client
local client_ip = task:get_from_ip()
if client_ip and client_ip:is_valid() then
client = client_ip:to_string()
end
local client_host = task:get_hostname()
if client_host then
client = client .. "\r" .. client_host
end
-- HELO
local helo = task:get_helo() or ''
-- Envelope From
local ef = task:get_from()
local envfrom = 'test@example.com'
if ef and ef[1] then
envfrom = ef[1]['addr']
end
-- Envelope To
local envrcpt = 'test@example.com'
local rcpts = task:get_recipients();
if rcpts then
local dcc_recipients = table.concat(fun.totable(fun.map(function(rcpt)
return rcpt['addr'] end,
rcpts)), '\n')
if dcc_recipients then
envrcpt = dcc_recipients
end
end
-- Build the DCC query
-- https://www.dcc-servers.net/dcc/dcc-tree/dccifd.html#Protocol
local request_data = {
"header\n",
client .. "\n",
helo .. "\n",
envfrom .. "\n",
envrcpt .. "\n",
"\n",
content
}
local function dcc_callback(err, data, conn)
local function dcc_requery()
-- set current upstream to fail because an error occurred
upstream:fail()
-- retry with another upstream until retransmits exceeds
if retransmits > 0 then
retransmits = retransmits - 1
lua_util.debugm(rule.name, task, '%s: Request Error: %s - retries left: %s',
rule.log_prefix, err, retransmits)
-- Select a different upstream!
upstream = rule.upstreams:get_upstream_round_robin()
addr = upstream:get_addr()
lua_util.debugm(rule.name, task, '%s: retry IP: %s:%s',
rule.log_prefix, addr, addr:get_port())
tcp.request({
task = task,
host = addr:to_string(),
port = addr:get_port(),
timeout = rule.timeout or 2.0,
shutdown = true,
data = request_data,
callback = dcc_callback,
body_max = 999999,
fuz1_max = 999999,
fuz2_max = 999999,
})
else
rspamd_logger.errx(task, '%s: failed to scan, maximum retransmits '..
'exceed', rule.log_prefix)
common.yield_result(task, rule, 'failed to scan and retransmits exceed', 0.0, 'fail')
end
end
if err then
dcc_requery()
else
-- Parse the response
if upstream then upstream:ok() end
local _,_,result,disposition,header = tostring(data):find("(.-)\n(.-)\n(.-)$")
lua_util.debugm(rule.name, task, 'DCC result=%1 disposition=%2 header="%3"',
result, disposition, header)
if header then
-- Unfold header
header = header:gsub('\r?\n%s*', ' ')
local _,_,info = header:find("; (.-)$")
if (result == 'R') then
-- Reject
common.yield_result(task, rule, info, rule.default_score)
common.save_cache(task, digest, rule, info, rule.default_score)
elseif (result == 'T') then
-- Temporary failure
rspamd_logger.warnx(task, 'DCC returned a temporary failure result: %s', result)
dcc_requery()
elseif result == 'A' then
local opts = {}
local score = 0.0
info = info:lower()
local rep = info:match('rep=([^=%s]+)')
-- Adjust reputation if available
if rep then rep = tonumber(rep) end
if not rep then
rep = 1.0
end
local function check_threshold(what, num, lim)
local rnum
if num == 'many' then
rnum = lim
else
rnum = tonumber(num)
end
if rnum and rnum >= lim then
opts[#opts + 1] = string.format('%s=%s', what, num)
score = score + rep / 3.0
end
end
info = info:lower()
local body = info:match('body=([^=%s]+)')
if body then
check_threshold('body', body, rule.body_max)
end
local fuz1 = info:match('fuz1=([^=%s]+)')
if fuz1 then
check_threshold('fuz1', fuz1, rule.fuz1_max)
end
local fuz2 = info:match('fuz2=([^=%s]+)')
if fuz2 then
check_threshold('fuz2', fuz2, rule.fuz2_max)
end
if #opts > 0 and score > 0 then
task:insert_result(rule.symbol_bulk,
score,
opts)
common.save_cache(task, digest, rule, opts, score)
else
common.save_cache(task, digest, rule, 'OK')
if rule.log_clean then
rspamd_logger.infox(task, '%s: clean, returned result A - info: %s',
rule.log_prefix, info)
else
lua_util.debugm(rule.name, task, '%s: returned result A - info: %s',
rule.log_prefix, info)
end
end
elseif result == 'G' then
-- do nothing
common.save_cache(task, digest, rule, 'OK')
if rule.log_clean then
rspamd_logger.infox(task, '%s: clean, returned result G - info: %s', rule.log_prefix, info)
else
lua_util.debugm(rule.name, task, '%s: returned result G - info: %s', rule.log_prefix, info)
end
elseif result == 'S' then
-- do nothing
common.save_cache(task, digest, rule, 'OK')
if rule.log_clean then
rspamd_logger.infox(task, '%s: clean, returned result S - info: %s', rule.log_prefix, info)
else
lua_util.debugm(rule.name, task, '%s: returned result S - info: %s', rule.log_prefix, info)
end
else
-- Unknown result
rspamd_logger.warnx(task, '%s: result error: %1', rule.log_prefix, result);
common.yield_result(task, rule, 'error: ' .. result, 0.0, 'fail')
end
end
end
end
tcp.request({
task = task,
host = addr:to_string(),
port = addr:get_port(),
timeout = rule.timeout or 2.0,
shutdown = true,
data = request_data,
callback = dcc_callback,
body_max = 999999,
fuz1_max = 999999,
fuz2_max = 999999,
})
end
if common.condition_check_and_continue(task, content, rule, digest, dcc_check_uncached) then
return
else
dcc_check_uncached()
end
end
return {
type = {'dcc','bulk', 'hash', 'scanner'},
description = 'dcc bulk scanner',
configure = dcc_config,
check = dcc_check,
name = N
}
|