| 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 kaspersky_se |
| 19 |
-- This module contains Kaspersky Scan Engine integration support |
| 20 |
-- https://www.kaspersky.com/scan-engine |
| 21 |
--]] |
| 22 |
|
| 23 |
local lua_util = require "lua_util" |
| 24 |
local rspamd_util = require "rspamd_util" |
| 25 |
local http = require "rspamd_http" |
| 26 |
local upstream_list = require "rspamd_upstream_list" |
| 27 |
local rspamd_logger = require "rspamd_logger" |
| 28 |
local common = require "lua_scanners/common" |
| 29 |
|
| 30 |
local N = 'kaspersky_se' |
| 31 |
|
| 32 |
local function kaspersky_se_config(opts) |
| 33 |
|
| 34 |
local default_conf = { |
| 35 |
name = N, |
| 36 |
default_port = 9999, |
| 37 |
use_https = false, |
| 38 |
use_files = false, |
| 39 |
timeout = 5.0, |
| 40 |
log_clean = false, |
| 41 |
tmpdir = '/tmp', |
| 42 |
retransmits = 1, |
| 43 |
cache_expire = 7200, -- expire redis in 2h |
| 44 |
message = '${SCANNER}: spam message found: "${VIRUS}"', |
| 45 |
detection_category = "virus", |
| 46 |
default_score = 1, |
| 47 |
action = false, |
| 48 |
scan_mime_parts = true, |
| 49 |
scan_text_mime = false, |
| 50 |
scan_image_mime = false, |
| 51 |
keepalive = true, |
| 52 |
auth_string = nil |
| 53 |
} |
| 54 |
|
| 55 |
default_conf = lua_util.override_defaults(default_conf, opts) |
| 56 |
|
| 57 |
if not default_conf.prefix then |
| 58 |
default_conf.prefix = 'rs_' .. default_conf.name .. '_' |
| 59 |
end |
| 60 |
|
| 61 |
if not default_conf.log_prefix then |
| 62 |
if default_conf.name:lower() == default_conf.type:lower() then |
| 63 |
default_conf.log_prefix = default_conf.name |
| 64 |
else |
| 65 |
default_conf.log_prefix = default_conf.name .. ' (' .. default_conf.type .. ')' |
| 66 |
end |
| 67 |
end |
| 68 |
|
| 69 |
if not default_conf.servers and default_conf.socket then |
| 70 |
default_conf.servers = default_conf.socket |
| 71 |
end |
| 72 |
|
| 73 |
if not default_conf.servers then |
| 74 |
rspamd_logger.errx(rspamd_config, 'no servers defined') |
| 75 |
|
| 76 |
return nil |
| 77 |
end |
| 78 |
|
| 79 |
default_conf.upstreams = upstream_list.create(rspamd_config, |
| 80 |
default_conf.servers, |
| 81 |
default_conf.default_port) |
| 82 |
|
| 83 |
if default_conf.upstreams then |
| 84 |
lua_util.add_debug_alias('external_services', default_conf.name) |
| 85 |
return default_conf |
| 86 |
end |
| 87 |
|
| 88 |
rspamd_logger.errx(rspamd_config, 'cannot parse servers %s', |
| 89 |
default_conf['servers']) |
| 90 |
return nil |
| 91 |
end |
| 92 |
|
| 93 |
local function kaspersky_se_check(task, content, digest, rule, maybe_part) |
| 94 |
local function kaspersky_se_check_uncached() |
| 95 |
local function make_url(addr) |
| 96 |
local url |
| 97 |
local suffix = '/scanmemory' |
| 98 |
|
| 99 |
if rule.use_files then |
| 100 |
suffix = '/scanfile' |
| 101 |
end |
| 102 |
if rule.use_https then |
| 103 |
url = string.format('https://%s:%d%s', tostring(addr), |
| 104 |
addr:get_port(), suffix) |
| 105 |
else |
| 106 |
url = string.format('http://%s:%d%s', tostring(addr), |
| 107 |
addr:get_port(), suffix) |
| 108 |
end |
| 109 |
|
| 110 |
return url |
| 111 |
end |
| 112 |
|
| 113 |
local upstream = rule.upstreams:get_upstream_round_robin() |
| 114 |
local addr = upstream:get_addr() |
| 115 |
local retransmits = rule.retransmits |
| 116 |
|
| 117 |
local url = make_url(addr) |
| 118 |
local hdrs = { |
| 119 |
['X-KAV-ProtocolVersion'] = '1', |
| 120 |
['X-KAV-Timeout'] = tostring(rule.timeout * 1000), |
| 121 |
} |
| 122 |
|
| 123 |
local ip = task:get_from_ip() |
| 124 |
if ip and ip:is_valid() then |
| 125 |
hdrs['X-KAV-HostIP'] = tostring(ip) |
| 126 |
end |
| 127 |
|
| 128 |
if rule.auth_string then |
| 129 |
hdrs['Authorization'] = rule.auth_string |
| 130 |
end |
| 131 |
|
| 132 |
if task:has_from() then |
| 133 |
hdrs['X-KAV-ObjectURL'] = string.format('[from:%s]', task:get_from()[1].addr) |
| 134 |
end |
| 135 |
|
| 136 |
local req_body |
| 137 |
|
| 138 |
if rule.use_files then |
| 139 |
local fname = string.format('%s/%s.tmp', |
| 140 |
rule.tmpdir, rspamd_util.random_hex(32)) |
| 141 |
local message_fd = rspamd_util.create_file(fname) |
| 142 |
|
| 143 |
if not message_fd then |
| 144 |
rspamd_logger.errx(task, 'cannot store file for kaspersky_se scan: %s', fname) |
| 145 |
return |
| 146 |
end |
| 147 |
|
| 148 |
if type(content) == 'string' then |
| 149 |
-- Create rspamd_text |
| 150 |
local rspamd_text = require "rspamd_text" |
| 151 |
content = rspamd_text.fromstring(content) |
| 152 |
end |
| 153 |
content:save_in_file(message_fd) |
| 154 |
|
| 155 |
-- Ensure cleanup |
| 156 |
task:get_mempool():add_destructor(function() |
| 157 |
os.remove(fname) |
| 158 |
rspamd_util.close_file(message_fd) |
| 159 |
end) |
| 160 |
|
| 161 |
req_body = fname |
| 162 |
else |
| 163 |
req_body = content |
| 164 |
end |
| 165 |
|
| 166 |
local request_data = { |
| 167 |
task = task, |
| 168 |
url = url, |
| 169 |
body = req_body, |
| 170 |
headers = hdrs, |
| 171 |
timeout = rule.timeout, |
| 172 |
keepalive = rule.keepalive, |
| 173 |
} |
| 174 |
|
| 175 |
local function kas_callback(http_err, code, body, headers) |
| 176 |
|
| 177 |
local function requery() |
| 178 |
-- set current upstream to fail because an error occurred |
| 179 |
upstream:fail() |
| 180 |
|
| 181 |
-- retry with another upstream until retransmits exceeds |
| 182 |
if retransmits > 0 then |
| 183 |
|
| 184 |
retransmits = retransmits - 1 |
| 185 |
|
| 186 |
lua_util.debugm(rule.name, task, |
| 187 |
'%s: Request Error: %s - retries left: %s', |
| 188 |
rule.log_prefix, http_err, retransmits) |
| 189 |
|
| 190 |
-- Select a different upstream! |
| 191 |
upstream = rule.upstreams:get_upstream_round_robin() |
| 192 |
addr = upstream:get_addr() |
| 193 |
url = make_url(addr) |
| 194 |
|
| 195 |
lua_util.debugm(rule.name, task, '%s: retry IP: %s:%s', |
| 196 |
rule.log_prefix, addr, addr:get_port()) |
| 197 |
request_data.url = url |
| 198 |
request_data.upstream = upstream |
| 199 |
|
| 200 |
http.request(request_data) |
| 201 |
else |
| 202 |
rspamd_logger.errx(task, '%s: failed to scan, maximum retransmits ' .. |
| 203 |
'exceed', rule.log_prefix) |
| 204 |
task:insert_result(rule['symbol_fail'], 0.0, 'failed to scan and ' .. |
| 205 |
'retransmits exceed') |
| 206 |
end |
| 207 |
end |
| 208 |
|
| 209 |
if http_err then |
| 210 |
requery() |
| 211 |
else |
| 212 |
-- Parse the response |
| 213 |
if upstream then |
| 214 |
upstream:ok() |
| 215 |
end |
| 216 |
if code ~= 200 then |
| 217 |
rspamd_logger.errx(task, 'invalid HTTP code: %s, body: %s, headers: %s', code, body, headers) |
| 218 |
task:insert_result(rule.symbol_fail, 1.0, 'Bad HTTP code: ' .. code) |
| 219 |
return |
| 220 |
end |
| 221 |
local data = string.gsub(tostring(body), '[\r\n%s]$', '') |
| 222 |
local cached |
| 223 |
lua_util.debugm(rule.name, task, '%s: got reply data: "%s"', |
| 224 |
rule.log_prefix, data) |
| 225 |
|
| 226 |
if data:find('^CLEAN') then |
| 227 |
-- Handle CLEAN replies |
| 228 |
if data == 'CLEAN' then |
| 229 |
cached = 'OK' |
| 230 |
if rule['log_clean'] then |
| 231 |
rspamd_logger.infox(task, '%s: message or mime_part is clean', |
| 232 |
rule.log_prefix) |
| 233 |
else |
| 234 |
lua_util.debugm(rule.name, task, '%s: message or mime_part is clean', |
| 235 |
rule.log_prefix) |
| 236 |
end |
| 237 |
elseif data == 'CLEAN AND CONTAINS OFFICE MACRO' then |
| 238 |
common.yield_result(task, rule, 'File contains macros', |
| 239 |
0.0, 'macro', maybe_part) |
| 240 |
cached = 'MACRO' |
| 241 |
else |
| 242 |
rspamd_logger.errx(task, '%s: unhandled clean response: %s', rule.log_prefix, data) |
| 243 |
common.yield_result(task, rule, 'unhandled response:' .. data, |
| 244 |
0.0, 'fail', maybe_part) |
| 245 |
end |
| 246 |
elseif data == 'SERVER_ERROR' then |
| 247 |
rspamd_logger.errx(task, '%s: error: %s', rule.log_prefix, data) |
| 248 |
common.yield_result(task, rule, 'error:' .. data, |
| 249 |
0.0, 'fail', maybe_part) |
| 250 |
elseif string.match(data, 'DETECT (.+)') then |
| 251 |
local vname = string.match(data, 'DETECT (.+)') |
| 252 |
common.yield_result(task, rule, vname, 1.0, nil, maybe_part) |
| 253 |
cached = vname |
| 254 |
elseif string.match(data, 'NON_SCANNED %((.+)%)') then |
| 255 |
local why = string.match(data, 'NON_SCANNED %((.+)%)') |
| 256 |
|
| 257 |
if why == 'PASSWORD PROTECTED' then |
| 258 |
rspamd_logger.errx(task, '%s: File is encrypted', rule.log_prefix) |
| 259 |
common.yield_result(task, rule, 'File is encrypted: ' .. why, |
| 260 |
0.0, 'encrypted', maybe_part) |
| 261 |
cached = 'ENCRYPTED' |
| 262 |
else |
| 263 |
common.yield_result(task, rule, 'unhandled response:' .. data, |
| 264 |
0.0, 'fail', maybe_part) |
| 265 |
end |
| 266 |
else |
| 267 |
rspamd_logger.errx(task, '%s: unhandled response: %s', rule.log_prefix, data) |
| 268 |
common.yield_result(task, rule, 'unhandled response:' .. data, |
| 269 |
0.0, 'fail', maybe_part) |
| 270 |
end |
| 271 |
|
| 272 |
if cached then |
| 273 |
common.save_cache(task, digest, rule, cached, 1.0, maybe_part) |
| 274 |
end |
| 275 |
|
| 276 |
end |
| 277 |
end |
| 278 |
|
| 279 |
request_data.callback = kas_callback |
| 280 |
http.request(request_data) |
| 281 |
end |
| 282 |
|
| 283 |
if common.condition_check_and_continue(task, content, rule, digest, |
| 284 |
kaspersky_se_check_uncached, maybe_part) then |
| 285 |
return |
| 286 |
else |
| 287 |
|
| 288 |
kaspersky_se_check_uncached() |
| 289 |
end |
| 290 |
|
| 291 |
end |
| 292 |
|
| 293 |
return { |
| 294 |
type = 'antivirus', |
| 295 |
description = 'Kaspersky Scan Engine interface', |
| 296 |
configure = kaspersky_se_config, |
| 297 |
check = kaspersky_se_check, |
| 298 |
name = N |
| 299 |
} |