| 1 |
--[[ |
| 2 |
Copyright (c) 2022, Vsevolod Stakhov <[email protected]> |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
]]-- |
| 16 |
|
| 17 |
--[[[ |
| 18 |
-- @module avast |
| 19 |
-- This module contains avast av access functions |
| 20 |
--]] |
| 21 |
|
| 22 |
local lua_util = require "lua_util" |
| 23 |
local rspamd_util = require "rspamd_util" |
| 24 |
local tcp = require "rspamd_tcp" |
| 25 |
local upstream_list = require "rspamd_upstream_list" |
| 26 |
local rspamd_regexp = require "rspamd_regexp" |
| 27 |
local rspamd_logger = require "rspamd_logger" |
| 28 |
local common = require "lua_scanners/common" |
| 29 |
|
| 30 |
local N = "avast" |
| 31 |
|
| 32 |
local default_message = '${SCANNER}: virus found: "${VIRUS}"' |
| 33 |
|
| 34 |
local function avast_config(opts) |
| 35 |
local avast_conf = { |
| 36 |
name = N, |
| 37 |
scan_mime_parts = true, |
| 38 |
scan_text_mime = false, |
| 39 |
scan_image_mime = false, |
| 40 |
timeout = 4.0, -- FIXME: this will break task_timeout! |
| 41 |
log_clean = false, |
| 42 |
detection_category = "virus", |
| 43 |
retransmits = 1, |
| 44 |
servers = nil, -- e.g. /var/run/avast/scan.sock |
| 45 |
cache_expire = 3600, -- expire redis in one hour |
| 46 |
message = default_message, |
| 47 |
tmpdir = '/tmp', |
| 48 |
} |
| 49 |
|
| 50 |
avast_conf = lua_util.override_defaults(avast_conf, opts) |
| 51 |
|
| 52 |
if not avast_conf.prefix then |
| 53 |
avast_conf.prefix = 'rs_' .. avast_conf.name .. '_' |
| 54 |
end |
| 55 |
|
| 56 |
if not avast_conf.log_prefix then |
| 57 |
if avast_conf.name:lower() == avast_conf.type:lower() then |
| 58 |
avast_conf.log_prefix = avast_conf.name |
| 59 |
else |
| 60 |
avast_conf.log_prefix = avast_conf.name .. ' (' .. avast_conf.type .. ')' |
| 61 |
end |
| 62 |
end |
| 63 |
|
| 64 |
if not avast_conf['servers'] then |
| 65 |
rspamd_logger.errx(rspamd_config, 'no servers/unix socket defined') |
| 66 |
|
| 67 |
return nil |
| 68 |
end |
| 69 |
|
| 70 |
avast_conf['upstreams'] = upstream_list.create(rspamd_config, |
| 71 |
avast_conf['servers'], |
| 72 |
0) |
| 73 |
|
| 74 |
if avast_conf['upstreams'] then |
| 75 |
lua_util.add_debug_alias('antivirus', avast_conf.name) |
| 76 |
return avast_conf |
| 77 |
end |
| 78 |
|
| 79 |
rspamd_logger.errx(rspamd_config, 'cannot parse servers %s', |
| 80 |
avast_conf['servers']) |
| 81 |
return nil |
| 82 |
end |
| 83 |
|
| 84 |
local function avast_check(task, content, digest, rule, maybe_part) |
| 85 |
local function avast_check_uncached () |
| 86 |
local upstream = rule.upstreams:get_upstream_round_robin() |
| 87 |
local addr = upstream:get_addr() |
| 88 |
local retransmits = rule.retransmits |
| 89 |
local CRLF = '\r\n' |
| 90 |
|
| 91 |
-- Common tcp options |
| 92 |
local tcp_opts = { |
| 93 |
stop_pattern = CRLF, |
| 94 |
host = addr:to_string(), |
| 95 |
port = addr:get_port(), |
| 96 |
upstream = upstream, |
| 97 |
timeout = rule.timeout, |
| 98 |
task = task |
| 99 |
} |
| 100 |
|
| 101 |
-- Regexps to process reply from avast |
| 102 |
local clean_re = rspamd_regexp.create_cached( |
| 103 |
[=[(?!\\)\t\[\+\]]=] |
| 104 |
) |
| 105 |
local virus_re = rspamd_regexp.create_cached( |
| 106 |
[[(?!\\)\t\[L\]\d\.\d\t\d\s(.*)]] |
| 107 |
) |
| 108 |
local error_re = rspamd_regexp.create_cached( |
| 109 |
[[(?!\\)\t\[E\]\d+\.0\tError\s\d+\s(.*)]] |
| 110 |
) |
| 111 |
|
| 112 |
-- Used to make a dialog |
| 113 |
local tcp_conn |
| 114 |
|
| 115 |
-- Save content in file as avast can work with files only |
| 116 |
local fname = string.format('%s/%s.avtmp', |
| 117 |
rule.tmpdir, rspamd_util.random_hex(32)) |
| 118 |
local message_fd = rspamd_util.create_file(fname) |
| 119 |
|
| 120 |
if not message_fd then |
| 121 |
rspamd_logger.errx(task, 'cannot store file for avast scan: %s', fname) |
| 122 |
return |
| 123 |
end |
| 124 |
|
| 125 |
if type(content) == 'string' then |
| 126 |
-- Create rspamd_text |
| 127 |
local rspamd_text = require "rspamd_text" |
| 128 |
content = rspamd_text.fromstring(content) |
| 129 |
end |
| 130 |
content:save_in_file(message_fd) |
| 131 |
|
| 132 |
-- Ensure file cleanup on task processed |
| 133 |
task:get_mempool():add_destructor(function() |
| 134 |
os.remove(fname) |
| 135 |
rspamd_util.close_file(message_fd) |
| 136 |
end) |
| 137 |
|
| 138 |
-- Dialog stages closures |
| 139 |
local avast_helo_cb |
| 140 |
local avast_scan_cb |
| 141 |
local avast_scan_done_cb |
| 142 |
|
| 143 |
-- Utility closures |
| 144 |
local function maybe_retransmit() |
| 145 |
if retransmits > 0 then |
| 146 |
retransmits = retransmits - 1 |
| 147 |
else |
| 148 |
rspamd_logger.errx(task, |
| 149 |
'%s [%s]: failed to scan, maximum retransmits exceed', |
| 150 |
rule['symbol'], rule['type']) |
| 151 |
common.yield_result(task, rule, 'failed to scan and retransmits exceed', |
| 152 |
0.0, 'fail', maybe_part) |
| 153 |
|
| 154 |
return |
| 155 |
end |
| 156 |
|
| 157 |
upstream = rule.upstreams:get_upstream_round_robin() |
| 158 |
addr = upstream:get_addr() |
| 159 |
tcp_opts.upstream = upstream |
| 160 |
tcp_opts.callback = avast_helo_cb |
| 161 |
|
| 162 |
local is_succ, err = tcp.request(tcp_opts) |
| 163 |
|
| 164 |
if not is_succ then |
| 165 |
rspamd_logger.infox(task, 'cannot create connection to avast server: %s (%s)', |
| 166 |
addr:to_string(true), err) |
| 167 |
else |
| 168 |
lua_util.debugm(rule.log_prefix, task, 'established connection to %s; retransmits=%s', |
| 169 |
addr:to_string(true), retransmits) |
| 170 |
end |
| 171 |
end |
| 172 |
|
| 173 |
local function no_connection_error(err) |
| 174 |
if err then |
| 175 |
if tcp_conn then |
| 176 |
tcp_conn:close() |
| 177 |
tcp_conn = nil |
| 178 |
|
| 179 |
rspamd_logger.infox(task, 'failed to request to avast (%s): %s', |
| 180 |
addr:to_string(true), err) |
| 181 |
maybe_retransmit() |
| 182 |
end |
| 183 |
|
| 184 |
return false |
| 185 |
end |
| 186 |
|
| 187 |
return true |
| 188 |
end |
| 189 |
|
| 190 |
|
| 191 |
-- Define callbacks |
| 192 |
avast_helo_cb = function(merr, mdata, conn) |
| 193 |
-- Called when we have established a connection but not read anything |
| 194 |
tcp_conn = conn |
| 195 |
|
| 196 |
if no_connection_error(merr) then |
| 197 |
-- Check mdata to ensure that it starts with 220 |
| 198 |
if #mdata > 3 and tostring(mdata:span(1, 3)) == '220' then |
| 199 |
tcp_conn:add_write(avast_scan_cb, string.format( |
| 200 |
'SCAN %s%s', fname, CRLF)) |
| 201 |
else |
| 202 |
rspamd_logger.errx(task, 'Unhandled response: %s', mdata) |
| 203 |
end |
| 204 |
end |
| 205 |
end |
| 206 |
|
| 207 |
avast_scan_cb = function(merr) |
| 208 |
-- Called when we have send request to avast and are waiting for reply |
| 209 |
if no_connection_error(merr) then |
| 210 |
tcp_conn:add_read(avast_scan_done_cb, CRLF) |
| 211 |
end |
| 212 |
end |
| 213 |
|
| 214 |
avast_scan_done_cb = function(merr, mdata) |
| 215 |
if no_connection_error(merr) then |
| 216 |
lua_util.debugm(rule.log_prefix, task, 'got reply from avast: %s', |
| 217 |
mdata) |
| 218 |
if #mdata > 4 then |
| 219 |
local beg = tostring(mdata:span(1, 3)) |
| 220 |
|
| 221 |
if beg == '210' then |
| 222 |
-- Ignore 210, fire another read |
| 223 |
if tcp_conn then |
| 224 |
tcp_conn:add_read(avast_scan_done_cb, CRLF) |
| 225 |
end |
| 226 |
elseif beg == '200' then |
| 227 |
-- Final line |
| 228 |
if tcp_conn then |
| 229 |
tcp_conn:close() |
| 230 |
tcp_conn = nil |
| 231 |
end |
| 232 |
else |
| 233 |
-- Check line using regular expressions |
| 234 |
local cached |
| 235 |
local ret = clean_re:search(mdata, false, true) |
| 236 |
|
| 237 |
if ret then |
| 238 |
cached = 'OK' |
| 239 |
if rule.log_clean then |
| 240 |
rspamd_logger.infox(task, |
| 241 |
'%s [%s]: message or mime_part is clean', |
| 242 |
rule.symbol, rule.type) |
| 243 |
end |
| 244 |
end |
| 245 |
|
| 246 |
if not cached then |
| 247 |
ret = virus_re:search(mdata, false, true) |
| 248 |
|
| 249 |
if ret then |
| 250 |
local vname = ret[1][2] |
| 251 |
|
| 252 |
if vname then |
| 253 |
vname = vname:gsub('\\ ', ' '):gsub('\\\\', '\\') |
| 254 |
common.yield_result(task, rule, vname, 1.0, nil, maybe_part) |
| 255 |
cached = vname |
| 256 |
end |
| 257 |
end |
| 258 |
end |
| 259 |
|
| 260 |
if not cached then |
| 261 |
ret = error_re:search(mdata, false, true) |
| 262 |
|
| 263 |
if ret then |
| 264 |
rspamd_logger.errx(task, '%s: error: %s', rule.log_prefix, ret[1][2]) |
| 265 |
common.yield_result(task, rule, 'error:' .. ret[1][2], |
| 266 |
0.0, 'fail', maybe_part) |
| 267 |
end |
| 268 |
end |
| 269 |
|
| 270 |
if cached then |
| 271 |
common.save_cache(task, digest, rule, cached, 1.0, maybe_part) |
| 272 |
else |
| 273 |
-- Unexpected reply |
| 274 |
rspamd_logger.errx(task, '%s: unexpected reply: %s', rule.log_prefix, mdata) |
| 275 |
end |
| 276 |
-- Read more |
| 277 |
if tcp_conn then |
| 278 |
tcp_conn:add_read(avast_scan_done_cb, CRLF) |
| 279 |
end |
| 280 |
end |
| 281 |
end |
| 282 |
end |
| 283 |
end |
| 284 |
|
| 285 |
-- Send the real request |
| 286 |
maybe_retransmit() |
| 287 |
end |
| 288 |
|
| 289 |
if common.condition_check_and_continue(task, content, rule, digest, |
| 290 |
avast_check_uncached, maybe_part) then |
| 291 |
return |
| 292 |
else |
| 293 |
avast_check_uncached() |
| 294 |
end |
| 295 |
|
| 296 |
end |
| 297 |
|
| 298 |
return { |
| 299 |
type = 'antivirus', |
| 300 |
description = 'Avast antivirus', |
| 301 |
configure = avast_config, |
| 302 |
check = avast_check, |
| 303 |
name = N |
| 304 |
} |