| 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 razor |
| 20 |
-- This module contains razor 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 |
|
| 29 |
local N = 'razor' |
| 30 |
|
| 31 |
local function razor_config(opts) |
| 32 |
|
| 33 |
local razor_conf = { |
| 34 |
name = N, |
| 35 |
default_port = 11342, |
| 36 |
timeout = 5.0, |
| 37 |
log_clean = false, |
| 38 |
retransmits = 2, |
| 39 |
cache_expire = 7200, -- expire redis in 2h |
| 40 |
message = '${SCANNER}: spam message found: "${VIRUS}"', |
| 41 |
detection_category = "hash", |
| 42 |
default_score = 1, |
| 43 |
action = false, |
| 44 |
dynamic_scan = false, |
| 45 |
symbol_fail = 'RAZOR_FAIL', |
| 46 |
symbol = 'RAZOR', |
| 47 |
} |
| 48 |
|
| 49 |
razor_conf = lua_util.override_defaults(razor_conf, opts) |
| 50 |
|
| 51 |
if not razor_conf.prefix then |
| 52 |
razor_conf.prefix = 'rs_' .. razor_conf.name .. '_' |
| 53 |
end |
| 54 |
|
| 55 |
if not razor_conf.log_prefix then |
| 56 |
razor_conf.log_prefix = razor_conf.name |
| 57 |
end |
| 58 |
|
| 59 |
if not razor_conf.servers and razor_conf.socket then |
| 60 |
razor_conf.servers = razor_conf.socket |
| 61 |
end |
| 62 |
|
| 63 |
if not razor_conf.servers then |
| 64 |
rspamd_logger.errx(rspamd_config, 'no servers defined') |
| 65 |
|
| 66 |
return nil |
| 67 |
end |
| 68 |
|
| 69 |
razor_conf.upstreams = upstream_list.create(rspamd_config, |
| 70 |
razor_conf.servers, |
| 71 |
razor_conf.default_port) |
| 72 |
|
| 73 |
if razor_conf.upstreams then |
| 74 |
lua_util.add_debug_alias('external_services', razor_conf.name) |
| 75 |
return razor_conf |
| 76 |
end |
| 77 |
|
| 78 |
rspamd_logger.errx(rspamd_config, 'cannot parse servers %s', |
| 79 |
razor_conf['servers']) |
| 80 |
return nil |
| 81 |
end |
| 82 |
|
| 83 |
local function razor_check(task, content, digest, rule) |
| 84 |
local function razor_check_uncached () |
| 85 |
local upstream = rule.upstreams:get_upstream_round_robin() |
| 86 |
local addr = upstream:get_addr() |
| 87 |
local retransmits = rule.retransmits |
| 88 |
|
| 89 |
local function razor_callback(err, data, conn) |
| 90 |
|
| 91 |
local function razor_requery() |
| 92 |
-- retry with another upstream until retransmits exceeds |
| 93 |
if retransmits > 0 then |
| 94 |
|
| 95 |
retransmits = retransmits - 1 |
| 96 |
|
| 97 |
lua_util.debugm(rule.name, task, '%s: Request Error: %s - retries left: %s', |
| 98 |
rule.log_prefix, err, retransmits) |
| 99 |
|
| 100 |
-- Select a different upstream! |
| 101 |
upstream = rule.upstreams:get_upstream_round_robin() |
| 102 |
addr = upstream:get_addr() |
| 103 |
|
| 104 |
lua_util.debugm(rule.name, task, '%s: retry IP: %s:%s', |
| 105 |
rule.log_prefix, addr, addr:get_port()) |
| 106 |
|
| 107 |
tcp.request({ |
| 108 |
task = task, |
| 109 |
host = addr:to_string(), |
| 110 |
port = addr:get_port(), |
| 111 |
upstream = upstream, |
| 112 |
timeout = rule.timeout or 2.0, |
| 113 |
shutdown = true, |
| 114 |
data = content, |
| 115 |
callback = razor_callback, |
| 116 |
}) |
| 117 |
else |
| 118 |
rspamd_logger.errx(task, '%s: failed to scan, maximum retransmits ' .. |
| 119 |
'exceed', rule.log_prefix) |
| 120 |
common.yield_result(task, rule, 'failed to scan and retransmits exceed', 0.0, 'fail') |
| 121 |
end |
| 122 |
end |
| 123 |
|
| 124 |
if err then |
| 125 |
|
| 126 |
razor_requery() |
| 127 |
|
| 128 |
else |
| 129 |
--[[ |
| 130 |
@todo: Razorsocket currently only returns ham or spam. When the wrapper is fixed we should add dynamic scores here. |
| 131 |
Maybe check spamassassin implementation. |
| 132 |
|
| 133 |
This implementation is based on https://github.com/cgt/rspamd-plugins |
| 134 |
Thanks @cgt! |
| 135 |
]] -- |
| 136 |
|
| 137 |
local threat_string = tostring(data) |
| 138 |
if threat_string == "spam" then |
| 139 |
lua_util.debugm(N, task, '%s: returned result is spam', rule['symbol'], rule['type']) |
| 140 |
common.yield_result(task, rule, threat_string, rule.default_score) |
| 141 |
common.save_cache(task, digest, rule, threat_string, rule.default_score) |
| 142 |
elseif threat_string == "ham" then |
| 143 |
if rule.log_clean then |
| 144 |
rspamd_logger.infox(task, '%s: returned result is ham', rule['symbol'], rule['type']) |
| 145 |
else |
| 146 |
lua_util.debugm(N, task, '%s: returned result is ham', rule['symbol'], rule['type']) |
| 147 |
end |
| 148 |
common.save_cache(task, digest, rule, 'OK', rule.default_score) |
| 149 |
else |
| 150 |
rspamd_logger.errx(task, "%s - unknown response from razorfy: %s", addr:to_string(), threat_string) |
| 151 |
end |
| 152 |
|
| 153 |
end |
| 154 |
end |
| 155 |
|
| 156 |
tcp.request({ |
| 157 |
task = task, |
| 158 |
host = addr:to_string(), |
| 159 |
port = addr:get_port(), |
| 160 |
upstream = upstream, |
| 161 |
timeout = rule.timeout or 2.0, |
| 162 |
shutdown = true, |
| 163 |
data = content, |
| 164 |
callback = razor_callback, |
| 165 |
}) |
| 166 |
end |
| 167 |
|
| 168 |
if common.condition_check_and_continue(task, content, rule, digest, razor_check_uncached) then |
| 169 |
return |
| 170 |
else |
| 171 |
razor_check_uncached() |
| 172 |
end |
| 173 |
end |
| 174 |
|
| 175 |
return { |
| 176 |
type = { 'razor', 'spam', 'hash', 'scanner' }, |
| 177 |
description = 'razor bulk scanner', |
| 178 |
configure = razor_config, |
| 179 |
check = razor_check, |
| 180 |
name = N |
| 181 |
} |