Debian Codegraph @

lualib/lua_scanners/dcc.lua

raw
1 --[[
2 Copyright (c) 2022, Vsevolod Stakhov <[email protected]>
3 Copyright (c) 2018, Carsten Rosenberg <[email protected]>
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ]]--
17
18 --[[[
19 -- @module dcc
20 -- This module contains dcc access functions
21 --]]
22
23 local lua_util = require "lua_util"
24 local tcp = require "rspamd_tcp"
25 local upstream_list = require "rspamd_upstream_list"
26 local rspamd_logger = require "rspamd_logger"
27 local common = require "lua_scanners/common"
28 local fun = require "fun"
29
30 local N = 'dcc'
31
32 local function dcc_config(opts)
33
34 local dcc_conf = {
35 name = N,
36 default_port = 10045,
37 timeout = 5.0,
38 log_clean = false,
39 retransmits = 2,
40 cache_expire = 7200, -- expire redis in 2h
41 message = '${SCANNER}: bulk message found: "${VIRUS}"',
42 detection_category = "hash",
43 default_score = 1,
44 client = '0.0.0.0',
45 symbol_fail = 'DCC_FAIL',
46 symbol = 'DCC_REJECT',
47 symbol_bulk = 'DCC_BULK',
48 body_max = 999999,
49 fuz1_max = 999999,
50 fuz2_max = 999999,
51 }
52
53 dcc_conf = lua_util.override_defaults(dcc_conf, opts)
54
55 if not dcc_conf.prefix then
56 dcc_conf.prefix = 'rs_' .. dcc_conf.name .. '_'
57 end
58
59 if not dcc_conf.log_prefix then
60 dcc_conf.log_prefix = dcc_conf.name
61 end
62
63 if not dcc_conf.servers and dcc_conf.socket then
64 dcc_conf.servers = dcc_conf.socket
65 end
66
67 if not dcc_conf.servers then
68 rspamd_logger.errx(rspamd_config, 'no servers defined')
69
70 return nil
71 end
72
73 dcc_conf.upstreams = upstream_list.create(rspamd_config,
74 dcc_conf.servers,
75 dcc_conf.default_port)
76
77 if dcc_conf.upstreams then
78 lua_util.add_debug_alias('external_services', dcc_conf.name)
79 return dcc_conf
80 end
81
82 rspamd_logger.errx(rspamd_config, 'cannot parse servers %s',
83 dcc_conf['servers'])
84 return nil
85 end
86
87 local function dcc_check(task, content, digest, rule)
88 local upstream = rule.upstreams:get_upstream_round_robin()
89 local addr = upstream:get_addr()
90 local retransmits = rule.retransmits
91 local client = rule.client
92
93 local client_ip = task:get_from_ip()
94 if client_ip and client_ip:is_valid() then
95 client = client_ip:to_string()
96 end
97 local client_host = task:get_hostname()
98 if client_host then
99 client = client .. "\r" .. client_host
100 end
101
102 -- HELO
103 local helo = task:get_helo() or ''
104
105 -- Envelope From
106 local ef = task:get_from()
107 local envfrom = '[email protected]'
108 if ef and ef[1] then
109 envfrom = ef[1]['addr']
110 end
111
112 -- Envelope To
113 local envrcpt = '[email protected]'
114 local rcpts = task:get_recipients();
115 if rcpts then
116 local dcc_recipients = table.concat(fun.totable(fun.map(function(rcpt)
117 return rcpt['addr']
118 end,
119 rcpts)), '\n')
120 if dcc_recipients then
121 envrcpt = dcc_recipients
122 end
123 end
124
125 -- Build the DCC query
126 -- https://www.dcc-servers.net/dcc/dcc-tree/dccifd.html#Protocol
127 local request_data = {
128 "header grey-off\n",
129 client .. "\n",
130 helo .. "\n",
131 envfrom .. "\n",
132 envrcpt .. "\n",
133 "\n",
134 content
135 }
136
137 local function dcc_callback(err, data, conn)
138
139 local function dcc_requery()
140 -- retry with another upstream until retransmits exceeds
141 if retransmits > 0 then
142
143 retransmits = retransmits - 1
144
145 -- Select a different upstream!
146 upstream = rule.upstreams:get_upstream_round_robin()
147 addr = upstream:get_addr()
148
149 lua_util.debugm(rule.name, task, '%s: error: %s; retry IP: %s; retries left: %s',
150 rule.log_prefix, err, addr, retransmits)
151
152 tcp.request({
153 task = task,
154 host = addr:to_string(),
155 port = addr:get_port(),
156 timeout = rule.timeout or 2.0,
157 upstream = upstream,
158 shutdown = true,
159 data = request_data,
160 callback = dcc_callback,
161 })
162 else
163 rspamd_logger.errx(task, '%s: failed to scan, maximum retransmits ' ..
164 'exceed', rule.log_prefix)
165 common.yield_result(task, rule, 'failed to scan and retransmits exceed', 0.0, 'fail')
166 end
167 end
168
169 if err then
170
171 dcc_requery()
172
173 else
174 -- Parse the response
175 local _, _, result, disposition, header = tostring(data):find("(.-)\n(.-)\n(.-)$")
176 lua_util.debugm(rule.name, task, 'DCC result=%1 disposition=%2 header="%3"',
177 result, disposition, header)
178
179 if header then
180 -- Unfold header
181 header = header:gsub('\r?\n%s*', ' ')
182 local _, _, info = header:find("; (.-)$")
183 if (result == 'R') then
184 -- Reject
185 common.yield_result(task, rule, info, rule.default_score)
186 elseif (result == 'T') then
187 -- Temporary failure
188 rspamd_logger.warnx(task, 'DCC returned a temporary failure result: %s', result)
189 dcc_requery()
190 elseif result == 'A' or result == 'S' then
191 -- Accept for all or some recipients, return a dynamic score
192 local opts = {}
193 local score = 0.0
194 local rep_orig = nil
195 if info then
196 info = info:lower()
197 local rep = info:match('rep=(%d+)')
198 rep_orig = rep
199
200 -- Adjust reputation if available
201 if rep then
202 rep = (tonumber(rep) or 100.0) / 100.0
203
204 if rep > 1.0 then
205 rep = 1.0
206 elseif rep < 0.0 then
207 rep = 0.0
208 end
209 else
210 rep = 1.0
211 end
212
213 local function check_threshold(what, num, lim)
214 local rnum
215 if num == 'many' then
216 rnum = lim
217 else
218 rnum = tonumber(num) or lim
219 end
220
221 if rnum and rnum >= lim then
222 opts[#opts + 1] = string.format('%s=%s', what, num)
223 score = score + (rule.default_score * rep / 3.0)
224 end
225 end
226
227 local body = info:match('body=([^=%s]+)')
228
229 if body then
230 check_threshold('body', body, rule.body_max)
231 end
232
233 local fuz1 = info:match('fuz1=([^=%s]+)')
234
235 if fuz1 then
236 check_threshold('fuz1', fuz1, rule.fuz1_max)
237 end
238
239 local fuz2 = info:match('fuz2=([^=%s]+)')
240
241 if fuz2 then
242 check_threshold('fuz2', fuz2, rule.fuz2_max)
243 end
244 end
245
246 if #opts > 0 and score > 0 then
247 if rep_orig then
248 opts[#opts + 1] = string.format('%s=%s', "rep", rep_orig .. "%")
249 end
250 task:insert_result(rule.symbol_bulk,
251 score,
252 opts)
253 else
254 if rule.log_clean then
255 rspamd_logger.infox(task, '%s: clean, returned result A - info: %s',
256 rule.log_prefix, info)
257 else
258 lua_util.debugm(rule.name, task, '%s: returned result A - info: %s',
259 rule.log_prefix, info)
260 end
261 end
262 else
263 -- Unexpected result
264 rspamd_logger.warnx(task, '%1: Unexpected result. Result: %2, info: %3', rule.log_prefix, result, info);
265 common.yield_result(task, rule, 'error: ' .. result, 0.0, 'fail')
266 end
267 end
268 end
269 end
270
271 tcp.request({
272 task = task,
273 host = addr:to_string(),
274 port = addr:get_port(),
275 timeout = rule.timeout or 2.0,
276 shutdown = true,
277 upstream = upstream,
278 data = request_data,
279 callback = dcc_callback,
280 })
281 end
282
283 return {
284 type = { 'dcc', 'bulk', 'hash', 'scanner' },
285 description = 'dcc bulk scanner',
286 configure = dcc_config,
287 check = dcc_check,
288 name = N
289 }