| 1 |
--[[ |
| 2 |
Copyright (c) 2022, Vsevolod Stakhov <[email protected]> |
| 3 |
Copyright (c) 2019, 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 icap |
| 20 |
This module contains icap access functions. |
| 21 |
Currently tested with |
| 22 |
- C-ICAP Squidclamav / echo |
| 23 |
- Checkpoint Sandblast |
| 24 |
- F-Secure Internet Gatekeeper |
| 25 |
- Kaspersky Web Traffic Security |
| 26 |
- Kaspersky Scan Engine 2.0 |
| 27 |
- McAfee Web Gateway 9/10/11 |
| 28 |
- Sophos Savdi |
| 29 |
- Symantec (Rspamd <3.2, >=3.2 untested) |
| 30 |
- Trend Micro IWSVA 6.0 |
| 31 |
- Trend Micro Web Gateway |
| 32 |
|
| 33 |
@TODO |
| 34 |
- Preview / Continue |
| 35 |
- Reqmod URL's |
| 36 |
- Content-Type / Filename |
| 37 |
]] -- |
| 38 |
|
| 39 |
--[[ |
| 40 |
Configuration Notes: |
| 41 |
|
| 42 |
C-ICAP Squidclamav |
| 43 |
scheme = "squidclamav"; |
| 44 |
|
| 45 |
Checkpoint Sandblast example: |
| 46 |
scheme = "sandblast"; |
| 47 |
|
| 48 |
ESET Gateway Security / Antivirus for Linux example: |
| 49 |
scheme = "scan"; |
| 50 |
|
| 51 |
F-Secure Internet Gatekeeper example: |
| 52 |
scheme = "respmod"; |
| 53 |
x_client_header = true; |
| 54 |
x_rcpt_header = true; |
| 55 |
x_from_header = true; |
| 56 |
|
| 57 |
Kaspersky Web Traffic Security example: |
| 58 |
scheme = "av/respmod"; |
| 59 |
x_client_header = true; |
| 60 |
|
| 61 |
Kaspersky Web Traffic Security (as configured in kavicapd.xml): |
| 62 |
scheme = "resp"; |
| 63 |
x_client_header = true; |
| 64 |
|
| 65 |
McAfee Web Gateway 10/11 (Headers must be activated with personal extra Rules) |
| 66 |
scheme = "respmod"; |
| 67 |
x_client_header = true; |
| 68 |
|
| 69 |
Sophos SAVDI example: |
| 70 |
# scheme as configured in savdi.conf (name option in service section) |
| 71 |
scheme = "respmod"; |
| 72 |
|
| 73 |
Symantec example: |
| 74 |
scheme = "avscan"; |
| 75 |
|
| 76 |
Trend Micro IWSVA example (X-Virus-ID/X-Infection-Found headers must be activated): |
| 77 |
scheme = "avscan"; |
| 78 |
x_client_header = true; |
| 79 |
|
| 80 |
Trend Micro Web Gateway example (X-Virus-ID/X-Infection-Found headers must be activated): |
| 81 |
scheme = "interscan"; |
| 82 |
x_client_header = true; |
| 83 |
]] -- |
| 84 |
|
| 85 |
|
| 86 |
local lua_util = require "lua_util" |
| 87 |
local tcp = require "rspamd_tcp" |
| 88 |
local upstream_list = require "rspamd_upstream_list" |
| 89 |
local rspamd_logger = require "rspamd_logger" |
| 90 |
local common = require "lua_scanners/common" |
| 91 |
local rspamd_util = require "rspamd_util" |
| 92 |
local rspamd_version = rspamd_version |
| 93 |
|
| 94 |
local N = 'icap' |
| 95 |
|
| 96 |
local function icap_config(opts) |
| 97 |
|
| 98 |
local icap_conf = { |
| 99 |
name = N, |
| 100 |
scan_mime_parts = true, |
| 101 |
scan_all_mime_parts = true, |
| 102 |
scan_text_mime = false, |
| 103 |
scan_image_mime = false, |
| 104 |
scheme = "scan", |
| 105 |
default_port = 1344, |
| 106 |
ssl = false, |
| 107 |
no_ssl_verify = false, |
| 108 |
timeout = 10.0, |
| 109 |
log_clean = false, |
| 110 |
retransmits = 2, |
| 111 |
cache_expire = 7200, -- expire redis in one hour |
| 112 |
message = '${SCANNER}: threat found with icap scanner: "${VIRUS}"', |
| 113 |
detection_category = "virus", |
| 114 |
default_score = 1, |
| 115 |
action = false, |
| 116 |
dynamic_scan = false, |
| 117 |
user_agent = "Rspamd", |
| 118 |
x_client_header = false, |
| 119 |
x_rcpt_header = false, |
| 120 |
x_from_header = false, |
| 121 |
req_headers_enabled = true, |
| 122 |
req_fake_url = "http://127.0.0.1/mail", |
| 123 |
http_headers_enabled = true, |
| 124 |
use_http_result_header = true, |
| 125 |
use_http_3xx_as_threat = false, |
| 126 |
use_specific_content_type = false, -- Use content type from a part where possible |
| 127 |
} |
| 128 |
|
| 129 |
icap_conf = lua_util.override_defaults(icap_conf, opts) |
| 130 |
|
| 131 |
if not icap_conf.prefix then |
| 132 |
icap_conf.prefix = 'rs_' .. icap_conf.name .. '_' |
| 133 |
end |
| 134 |
|
| 135 |
if not icap_conf.log_prefix then |
| 136 |
icap_conf.log_prefix = icap_conf.name .. ' (' .. icap_conf.type .. ')' |
| 137 |
end |
| 138 |
|
| 139 |
if not icap_conf.log_prefix then |
| 140 |
if icap_conf.name:lower() == icap_conf.type:lower() then |
| 141 |
icap_conf.log_prefix = icap_conf.name |
| 142 |
else |
| 143 |
icap_conf.log_prefix = icap_conf.name .. ' (' .. icap_conf.type .. ')' |
| 144 |
end |
| 145 |
end |
| 146 |
|
| 147 |
if not icap_conf.servers then |
| 148 |
rspamd_logger.errx(rspamd_config, 'no servers defined') |
| 149 |
|
| 150 |
return nil |
| 151 |
end |
| 152 |
|
| 153 |
icap_conf.upstreams = upstream_list.create(rspamd_config, |
| 154 |
icap_conf.servers, |
| 155 |
icap_conf.default_port) |
| 156 |
|
| 157 |
if icap_conf.upstreams then |
| 158 |
lua_util.add_debug_alias('external_services', icap_conf.name) |
| 159 |
return icap_conf |
| 160 |
end |
| 161 |
|
| 162 |
rspamd_logger.errx(rspamd_config, 'cannot parse servers %s', |
| 163 |
icap_conf.servers) |
| 164 |
return nil |
| 165 |
end |
| 166 |
|
| 167 |
local function icap_check(task, content, digest, rule, maybe_part) |
| 168 |
local function icap_check_uncached () |
| 169 |
local upstream = rule.upstreams:get_upstream_round_robin() |
| 170 |
local addr = upstream:get_addr() |
| 171 |
local retransmits = rule.retransmits |
| 172 |
local http_headers = {} |
| 173 |
local req_headers = {} |
| 174 |
local tcp_options = {} |
| 175 |
local threat_table = {} |
| 176 |
|
| 177 |
-- Build extended User Agent |
| 178 |
if rule.user_agent == "extended" then |
| 179 |
rule.user_agent = string.format("Rspamd/%s-%s (%s/%s)", |
| 180 |
rspamd_version('main'), |
| 181 |
rspamd_version('id'), |
| 182 |
rspamd_util.get_hostname(), |
| 183 |
string.sub(task:get_uid(), 1, 6)) |
| 184 |
end |
| 185 |
|
| 186 |
-- Build the icap queries |
| 187 |
local options_request = { |
| 188 |
string.format("OPTIONS icap://%s/%s ICAP/1.0\r\n", addr:to_string(), rule.scheme), |
| 189 |
string.format('Host: %s\r\n', addr:to_string()), |
| 190 |
string.format("User-Agent: %s\r\n", rule.user_agent), |
| 191 |
"Connection: keep-alive\r\n", |
| 192 |
"Encapsulated: null-body=0\r\n\r\n", |
| 193 |
} |
| 194 |
if rule.user_agent == "none" then |
| 195 |
table.remove(options_request, 3) |
| 196 |
end |
| 197 |
|
| 198 |
local respond_headers = { |
| 199 |
-- Add main RESPMOD header before any other |
| 200 |
string.format('RESPMOD icap://%s/%s ICAP/1.0\r\n', addr:to_string(), rule.scheme), |
| 201 |
string.format('Host: %s\r\n', addr:to_string()), |
| 202 |
} |
| 203 |
|
| 204 |
local size = tonumber(#content) |
| 205 |
local chunked_size = string.format("%x", size) |
| 206 |
|
| 207 |
local function icap_callback(err, conn) |
| 208 |
|
| 209 |
local function icap_requery(err_m, info) |
| 210 |
-- retry with another upstream until retransmits exceeds |
| 211 |
if retransmits > 0 then |
| 212 |
|
| 213 |
retransmits = retransmits - 1 |
| 214 |
|
| 215 |
lua_util.debugm(rule.name, task, |
| 216 |
'%s: %s Request Error: %s - retries left: %s', |
| 217 |
rule.log_prefix, info, err_m, retransmits) |
| 218 |
|
| 219 |
-- Select a different upstream! |
| 220 |
upstream = rule.upstreams:get_upstream_round_robin() |
| 221 |
addr = upstream:get_addr() |
| 222 |
|
| 223 |
lua_util.debugm(rule.name, task, '%s: retry IP: %s:%s', |
| 224 |
rule.log_prefix, addr, addr:get_port()) |
| 225 |
|
| 226 |
tcp_options.host = addr:to_string() |
| 227 |
tcp_options.port = addr:get_port() |
| 228 |
tcp_options.callback = icap_callback |
| 229 |
tcp_options.data = options_request |
| 230 |
tcp_options.upstream = upstream |
| 231 |
|
| 232 |
tcp.request(tcp_options) |
| 233 |
|
| 234 |
else |
| 235 |
rspamd_logger.errx(task, '%s: failed to scan, maximum retransmits ' .. |
| 236 |
'exceed - error: %s', rule.log_prefix, err_m or '') |
| 237 |
common.yield_result(task, rule, string.format('failed - error: %s', err_m), |
| 238 |
0.0, 'fail', maybe_part) |
| 239 |
end |
| 240 |
end |
| 241 |
|
| 242 |
local function get_req_headers() |
| 243 |
local in_client_ip = task:get_from_ip() |
| 244 |
local in_client_ip_str = in_client_ip:to_string() |
| 245 |
local req_hlen = 2 |
| 246 |
if in_client_ip:get_version() == 6 then |
| 247 |
in_client_ip_str = "ip6-" .. string.gsub(in_client_ip_str, ":", "-") |
| 248 |
end |
| 249 |
if maybe_part then |
| 250 |
table.insert(req_headers, |
| 251 |
string.format('GET http://%s/%s HTTP/1.0\r\n', in_client_ip_str, lua_util.url_encode_string(maybe_part:get_filename()))) |
| 252 |
if rule.use_specific_content_type then |
| 253 |
table.insert(http_headers, string.format('Content-Type: %s/%s\r\n', maybe_part:get_detected_type())) |
| 254 |
--else |
| 255 |
-- To test: what content type is better for icap servers? |
| 256 |
--table.insert(http_headers, 'Content-Type: text/plain\r\n') |
| 257 |
end |
| 258 |
else |
| 259 |
table.insert(req_headers, string.format('GET %s HTTP/1.0\r\n', rule.req_fake_url)) |
| 260 |
table.insert(http_headers, string.format('Content-Type: application/octet-stream\r\n')) |
| 261 |
end |
| 262 |
table.insert(req_headers, string.format('Date: %s\r\n', rspamd_util.time_to_string(rspamd_util.get_time()))) |
| 263 |
if rule.user_agent ~= "none" then |
| 264 |
table.insert(req_headers, string.format("User-Agent: %s\r\n", rule.user_agent)) |
| 265 |
end |
| 266 |
|
| 267 |
for _, h in ipairs(req_headers) do |
| 268 |
req_hlen = req_hlen + tonumber(#h) |
| 269 |
end |
| 270 |
|
| 271 |
return req_hlen, req_headers |
| 272 |
|
| 273 |
end |
| 274 |
|
| 275 |
local function get_http_headers() |
| 276 |
local http_hlen = 2 |
| 277 |
table.insert(http_headers, 'HTTP/1.0 200 OK\r\n') |
| 278 |
table.insert(http_headers, string.format('Date: %s\r\n', rspamd_util.time_to_string(rspamd_util.get_time()))) |
| 279 |
table.insert(http_headers, string.format('Server: %s\r\n', 'Apache/2.4')) |
| 280 |
if rule.user_agent ~= "none" then |
| 281 |
table.insert(http_headers, string.format("User-Agent: %s\r\n", rule.user_agent)) |
| 282 |
end |
| 283 |
--table.insert(http_headers, string.format('Content-Type: %s\r\n', 'text/html')) |
| 284 |
table.insert(http_headers, string.format('Content-Length: %s\r\n', size)) |
| 285 |
|
| 286 |
for _, h in ipairs(http_headers) do |
| 287 |
http_hlen = http_hlen + tonumber(#h) |
| 288 |
end |
| 289 |
|
| 290 |
return http_hlen, http_headers |
| 291 |
|
| 292 |
end |
| 293 |
|
| 294 |
local function get_respond_query() |
| 295 |
local req_hlen = 0 |
| 296 |
local resp_req_headers |
| 297 |
local http_hlen = 0 |
| 298 |
local resp_http_headers |
| 299 |
|
| 300 |
-- Append all extra headers |
| 301 |
if rule.user_agent ~= "none" then |
| 302 |
table.insert(respond_headers, |
| 303 |
string.format("User-Agent: %s\r\n", rule.user_agent)) |
| 304 |
end |
| 305 |
|
| 306 |
if rule.req_headers_enabled then |
| 307 |
req_hlen, resp_req_headers = get_req_headers() |
| 308 |
end |
| 309 |
if rule.http_headers_enabled then |
| 310 |
http_hlen, resp_http_headers = get_http_headers() |
| 311 |
end |
| 312 |
|
| 313 |
if rule.req_headers_enabled and rule.http_headers_enabled then |
| 314 |
local res_body_hlen = req_hlen + http_hlen |
| 315 |
table.insert(respond_headers, |
| 316 |
string.format('Encapsulated: req-hdr=0, res-hdr=%s, res-body=%s\r\n', |
| 317 |
req_hlen, res_body_hlen)) |
| 318 |
elseif rule.http_headers_enabled then |
| 319 |
table.insert(respond_headers, |
| 320 |
string.format('Encapsulated: res-hdr=0, res-body=%s\r\n', |
| 321 |
http_hlen)) |
| 322 |
else |
| 323 |
table.insert(respond_headers, 'Encapsulated: res-body=0\r\n') |
| 324 |
end |
| 325 |
|
| 326 |
table.insert(respond_headers, '\r\n') |
| 327 |
for _, h in ipairs(resp_req_headers) do |
| 328 |
table.insert(respond_headers, h) |
| 329 |
end |
| 330 |
table.insert(respond_headers, '\r\n') |
| 331 |
for _, h in ipairs(resp_http_headers) do |
| 332 |
table.insert(respond_headers, h) |
| 333 |
end |
| 334 |
table.insert(respond_headers, '\r\n') |
| 335 |
table.insert(respond_headers, chunked_size .. '\r\n') |
| 336 |
table.insert(respond_headers, content) |
| 337 |
table.insert(respond_headers, '\r\n0\r\n\r\n') |
| 338 |
return respond_headers |
| 339 |
end |
| 340 |
|
| 341 |
local function add_respond_header(name, value) |
| 342 |
if name and value then |
| 343 |
table.insert(respond_headers, string.format('%s: %s\r\n', name, value)) |
| 344 |
end |
| 345 |
end |
| 346 |
|
| 347 |
local function result_header_table(result) |
| 348 |
local icap_headers = {} |
| 349 |
for s in result:gmatch("[^\r\n]+") do |
| 350 |
if string.find(s, '^ICAP') then |
| 351 |
icap_headers['icap'] = tostring(s) |
| 352 |
elseif string.find(s, '^HTTP') then |
| 353 |
icap_headers['http'] = tostring(s) |
| 354 |
elseif string.find(s, '[%a%d-+]-:') then |
| 355 |
local _, _, key, value = tostring(s):find("([%a%d-+]-):%s?(.+)") |
| 356 |
if key ~= nil then |
| 357 |
icap_headers[key:lower()] = tostring(value) |
| 358 |
end |
| 359 |
end |
| 360 |
end |
| 361 |
lua_util.debugm(rule.name, task, '%s: icap_headers: %s', |
| 362 |
rule.log_prefix, icap_headers) |
| 363 |
return icap_headers |
| 364 |
end |
| 365 |
|
| 366 |
local function threat_table_add(icap_threat, maybe_split) |
| 367 |
|
| 368 |
if maybe_split and string.find(icap_threat, ',') then |
| 369 |
local threats = lua_util.str_split(string.gsub(icap_threat, "%s", ""), ',') or {} |
| 370 |
|
| 371 |
for _, v in ipairs(threats) do |
| 372 |
table.insert(threat_table, v) |
| 373 |
end |
| 374 |
else |
| 375 |
table.insert(threat_table, icap_threat) |
| 376 |
end |
| 377 |
return true |
| 378 |
end |
| 379 |
|
| 380 |
local function icap_parse_result(headers) |
| 381 |
|
| 382 |
--[[ |
| 383 |
@ToDo: handle type in response |
| 384 |
|
| 385 |
Generic Strings: |
| 386 |
icap: X-Infection-Found: Type=0; Resolution=2; Threat=Troj/DocDl-OYC; |
| 387 |
icap: X-Infection-Found: Type=0; Resolution=2; Threat=W97M.Downloader; |
| 388 |
|
| 389 |
Symantec String: |
| 390 |
icap: X-Infection-Found: Type=2; Resolution=2; Threat=Container size violation |
| 391 |
icap: X-Infection-Found: Type=2; Resolution=2; Threat=Encrypted container violation; |
| 392 |
|
| 393 |
Sophos Strings: |
| 394 |
icap: X-Virus-ID: Troj/DocDl-OYC |
| 395 |
http: X-Blocked: Virus found during virus scan |
| 396 |
http: X-Blocked-By: Sophos Anti-Virus |
| 397 |
|
| 398 |
Kaspersky Web Traffic Security Strings: |
| 399 |
icap: X-Virus-ID: HEUR:Backdoor.Java.QRat.gen |
| 400 |
icap: X-Response-Info: blocked |
| 401 |
icap: X-Virus-ID: no threats |
| 402 |
icap: X-Response-Info: blocked |
| 403 |
icap: X-Response-Info: passed |
| 404 |
http: HTTP/1.1 403 Forbidden |
| 405 |
|
| 406 |
Kaspersky Scan Engine 2.0 (ICAP mode) |
| 407 |
icap: X-Virus-ID: EICAR-Test-File |
| 408 |
http: HTTP/1.0 403 Forbidden |
| 409 |
|
| 410 |
Trend Micro Strings: |
| 411 |
icap: X-Virus-ID: Trojan.W97M.POWLOAD.SMTHF1 |
| 412 |
icap: X-Infection-Found: Type=0; Resolution=2; Threat=Trojan.W97M.POWLOAD.SMTHF1; |
| 413 |
http: HTTP/1.1 403 Forbidden (TMWS Blocked) |
| 414 |
http: HTTP/1.1 403 Forbidden |
| 415 |
|
| 416 |
F-Secure Internet Gatekeeper Strings: |
| 417 |
icap: X-FSecure-Scan-Result: infected |
| 418 |
icap: X-FSecure-Infection-Name: "Malware.W97M/Agent.32584203" |
| 419 |
icap: X-FSecure-Infected-Filename: "virus.doc" |
| 420 |
|
| 421 |
ESET File Security for Linux 7.0 |
| 422 |
icap: X-Infection-Found: Type=0; Resolution=0; Threat=VBA/TrojanDownloader.Agent.JOA; |
| 423 |
icap: X-Virus-ID: Trojaner |
| 424 |
icap: X-Response-Info: Blocked |
| 425 |
|
| 426 |
McAfee Web Gateway 10/11 (Headers must be activated with personal extra Rules) |
| 427 |
icap: X-Virus-ID: EICAR test file |
| 428 |
icap: X-Media-Type: text/plain |
| 429 |
icap: X-Block-Result: 80 |
| 430 |
icap: X-Block-Reason: Malware found |
| 431 |
icap: X-Block-Reason: Archive not supported |
| 432 |
icap: X-Block-Reason: Media Type (Block List) |
| 433 |
http: HTTP/1.0 403 VirusFound |
| 434 |
|
| 435 |
C-ICAP Squidclamav |
| 436 |
icap/http: X-Infection-Found: Type=0; Resolution=2; Threat={HEX}EICAR.TEST.3.UNOFFICIAL; |
| 437 |
icap/http: X-Virus-ID: {HEX}EICAR.TEST.3.UNOFFICIAL |
| 438 |
http: HTTP/1.0 307 Temporary Redirect |
| 439 |
]] -- |
| 440 |
|
| 441 |
-- Generic ICAP Headers |
| 442 |
if headers['x-infection-found'] then |
| 443 |
local _, _, icap_type, _, icap_threat = headers['x-infection-found']:find("Type=(.-); Resolution=(.-); Threat=(.-);$") |
| 444 |
|
| 445 |
-- Type=2 is typical for scan error returns |
| 446 |
if icap_type and icap_type == '2' then |
| 447 |
lua_util.debugm(rule.name, task, |
| 448 |
'%s: icap error X-Infection-Found: %s', rule.log_prefix, icap_threat) |
| 449 |
common.yield_result(task, rule, icap_threat, 0, |
| 450 |
'fail', maybe_part) |
| 451 |
return true |
| 452 |
elseif icap_threat ~= nil then |
| 453 |
lua_util.debugm(rule.name, task, |
| 454 |
'%s: icap X-Infection-Found: %s', rule.log_prefix, icap_threat) |
| 455 |
threat_table_add(icap_threat, false) |
| 456 |
-- stupid workaround for unuseable x-infection-found header |
| 457 |
-- but also x-virus-name set (McAfee Web Gateway 9) |
| 458 |
elseif not icap_threat and headers['x-virus-name'] then |
| 459 |
threat_table_add(headers['x-virus-name'], true) |
| 460 |
else |
| 461 |
threat_table_add(headers['x-infection-found'], true) |
| 462 |
end |
| 463 |
elseif headers['x-virus-name'] and headers['x-virus-name'] ~= "no threats" then |
| 464 |
lua_util.debugm(rule.name, task, |
| 465 |
'%s: icap X-Virus-Name: %s', rule.log_prefix, headers['x-virus-name']) |
| 466 |
threat_table_add(headers['x-virus-name'], true) |
| 467 |
elseif headers['x-virus-id'] and headers['x-virus-id'] ~= "no threats" then |
| 468 |
lua_util.debugm(rule.name, task, |
| 469 |
'%s: icap X-Virus-ID: %s', rule.log_prefix, headers['x-virus-id']) |
| 470 |
threat_table_add(headers['x-virus-id'], true) |
| 471 |
-- FSecure X-Headers |
| 472 |
elseif headers['x-fsecure-scan-result'] and headers['x-fsecure-scan-result'] ~= "clean" then |
| 473 |
|
| 474 |
local infected_filename = "" |
| 475 |
local infection_name = "-unknown-" |
| 476 |
|
| 477 |
if headers['x-fsecure-infected-filename'] then |
| 478 |
infected_filename = string.gsub(headers['x-fsecure-infected-filename'], '[%s"]', '') |
| 479 |
end |
| 480 |
if headers['x-fsecure-infection-name'] then |
| 481 |
infection_name = string.gsub(headers['x-fsecure-infection-name'], '[%s"]', '') |
| 482 |
end |
| 483 |
|
| 484 |
lua_util.debugm(rule.name, task, |
| 485 |
'%s: icap X-FSecure-Infection-Name (X-FSecure-Infected-Filename): %s (%s)', |
| 486 |
rule.log_prefix, infection_name, infected_filename) |
| 487 |
|
| 488 |
threat_table_add(infection_name, true) |
| 489 |
-- McAfee Web Gateway manual extra headers |
| 490 |
elseif headers['x-mwg-block-reason'] and headers['x-mwg-block-reason'] ~= "" then |
| 491 |
threat_table_add(headers['x-mwg-block-reason'], false) |
| 492 |
-- Sophos SAVDI special http headers |
| 493 |
elseif headers['x-blocked'] and headers['x-blocked'] ~= "" then |
| 494 |
threat_table_add(headers['x-blocked'], false) |
| 495 |
elseif headers['x-block-reason'] and headers['x-block-reason'] ~= "" then |
| 496 |
threat_table_add(headers['x-block-reason'], false) |
| 497 |
-- last try HTTP [4]xx return |
| 498 |
elseif headers.http and string.find(headers.http, '^HTTP%/[12]%.. [4]%d%d') then |
| 499 |
threat_table_add( |
| 500 |
string.format("pseudo-virus (blocked): %s", string.gsub(headers.http, 'HTTP%/[12]%.. ', '')), false) |
| 501 |
elseif rule.use_http_3xx_as_threat and |
| 502 |
headers.http and |
| 503 |
string.find(headers.http, '^HTTP%/[12]%.. [3]%d%d') |
| 504 |
then |
| 505 |
threat_table_add( |
| 506 |
string.format("pseudo-virus (redirect): %s", |
| 507 |
string.gsub(headers.http, 'HTTP%/[12]%.. ', '')), false) |
| 508 |
end |
| 509 |
|
| 510 |
if #threat_table > 0 then |
| 511 |
common.yield_result(task, rule, threat_table, rule.default_score, nil, maybe_part) |
| 512 |
common.save_cache(task, digest, rule, threat_table, rule.default_score, maybe_part) |
| 513 |
return true |
| 514 |
else |
| 515 |
return false |
| 516 |
end |
| 517 |
end |
| 518 |
|
| 519 |
local function icap_r_respond_http_cb(err_m, data, connection) |
| 520 |
if err_m or connection == nil then |
| 521 |
icap_requery(err_m, "icap_r_respond_http_cb") |
| 522 |
else |
| 523 |
local result = tostring(data) |
| 524 |
|
| 525 |
local icap_http_headers = result_header_table(result) or {} |
| 526 |
-- Find HTTP/[12].x [234]xx response |
| 527 |
if icap_http_headers.http and string.find(icap_http_headers.http, 'HTTP%/[12]%.. [234]%d%d') then |
| 528 |
local icap_http_header_result = icap_parse_result(icap_http_headers) |
| 529 |
if icap_http_header_result then |
| 530 |
-- Threat found - close connection |
| 531 |
connection:close() |
| 532 |
else |
| 533 |
common.save_cache(task, digest, rule, 'OK', 0, maybe_part) |
| 534 |
common.log_clean(task, rule) |
| 535 |
end |
| 536 |
else |
| 537 |
rspamd_logger.errx(task, '%s: unhandled response |%s|', |
| 538 |
rule.log_prefix, string.gsub(result, "\r\n", ", ")) |
| 539 |
common.yield_result(task, rule, string.format('unhandled icap response: %s', icap_http_headers.icap), |
| 540 |
0.0, 'fail', maybe_part) |
| 541 |
end |
| 542 |
end |
| 543 |
end |
| 544 |
|
| 545 |
local function icap_r_respond_cb(err_m, data, connection) |
| 546 |
if err_m or connection == nil then |
| 547 |
icap_requery(err_m, "icap_r_respond_cb") |
| 548 |
else |
| 549 |
local result = tostring(data) |
| 550 |
|
| 551 |
local icap_headers = result_header_table(result) or {} |
| 552 |
-- Find ICAP/1.x 2xx response |
| 553 |
if icap_headers.icap and string.find(icap_headers.icap, 'ICAP%/1%.. 2%d%d') then |
| 554 |
local icap_header_result = icap_parse_result(icap_headers) |
| 555 |
if icap_header_result then |
| 556 |
-- Threat found - close connection |
| 557 |
connection:close() |
| 558 |
elseif not icap_header_result |
| 559 |
and rule.use_http_result_header |
| 560 |
and icap_headers.encapsulated |
| 561 |
and not string.find(icap_headers.encapsulated, 'null%-body=0') |
| 562 |
then |
| 563 |
-- Try to read encapsulated HTTP Headers |
| 564 |
lua_util.debugm(rule.name, task, '%s: no ICAP virus header found - try HTTP headers', |
| 565 |
rule.log_prefix) |
| 566 |
connection:add_read(icap_r_respond_http_cb, '\r\n\r\n') |
| 567 |
else |
| 568 |
connection:close() |
| 569 |
common.save_cache(task, digest, rule, 'OK', 0, maybe_part) |
| 570 |
common.log_clean(task, rule) |
| 571 |
end |
| 572 |
elseif icap_headers.icap and string.find(icap_headers.icap, 'ICAP%/1%.. [45]%d%d') then |
| 573 |
-- Find ICAP/1.x 5/4xx response |
| 574 |
--[[ |
| 575 |
Symantec String: |
| 576 |
ICAP/1.0 539 Aborted - No AV scanning license |
| 577 |
SquidClamAV/C-ICAP: |
| 578 |
ICAP/1.0 500 Server error |
| 579 |
Eset: |
| 580 |
ICAP/1.0 405 Forbidden |
| 581 |
TrendMicro: |
| 582 |
ICAP/1.0 400 Bad request |
| 583 |
McAfee: |
| 584 |
ICAP/1.0 418 Bad composition |
| 585 |
]]-- |
| 586 |
rspamd_logger.errx(task, '%s: ICAP ERROR: %s', rule.log_prefix, icap_headers.icap) |
| 587 |
common.yield_result(task, rule, icap_headers.icap, 0.0, |
| 588 |
'fail', maybe_part) |
| 589 |
return false |
| 590 |
else |
| 591 |
rspamd_logger.errx(task, '%s: unhandled response |%s|', |
| 592 |
rule.log_prefix, string.gsub(result, "\r\n", ", ")) |
| 593 |
common.yield_result(task, rule, string.format('unhandled icap response: %s', icap_headers.icap), |
| 594 |
0.0, 'fail', maybe_part) |
| 595 |
end |
| 596 |
end |
| 597 |
end |
| 598 |
|
| 599 |
local function icap_w_respond_cb(err_m, connection) |
| 600 |
if err_m or connection == nil then |
| 601 |
icap_requery(err_m, "icap_w_respond_cb") |
| 602 |
else |
| 603 |
connection:add_read(icap_r_respond_cb, '\r\n\r\n') |
| 604 |
end |
| 605 |
end |
| 606 |
|
| 607 |
local function icap_r_options_cb(err_m, data, connection) |
| 608 |
if err_m or connection == nil then |
| 609 |
icap_requery(err_m, "icap_r_options_cb") |
| 610 |
else |
| 611 |
local icap_headers = result_header_table(tostring(data)) |
| 612 |
|
| 613 |
if icap_headers.icap and string.find(icap_headers.icap, 'ICAP%/1%.. 2%d%d') then |
| 614 |
if icap_headers['methods'] and string.find(icap_headers['methods'], 'RESPMOD') then |
| 615 |
-- Allow "204 No Content" responses |
| 616 |
-- https://datatracker.ietf.org/doc/html/rfc3507#section-4.6 |
| 617 |
if icap_headers['allow'] and string.find(icap_headers['allow'], '204') then |
| 618 |
add_respond_header('Allow', '204') |
| 619 |
end |
| 620 |
|
| 621 |
if rule.x_client_header then |
| 622 |
local client = task:get_from_ip() |
| 623 |
if client then |
| 624 |
add_respond_header('X-Client-IP', client:to_string()) |
| 625 |
end |
| 626 |
end |
| 627 |
|
| 628 |
-- F-Secure extra headers |
| 629 |
if icap_headers['server'] and string.find(icap_headers['server'], 'f-secure icap server') then |
| 630 |
|
| 631 |
if rule.x_rcpt_header then |
| 632 |
local rcpt_to = task:get_principal_recipient() |
| 633 |
if rcpt_to then |
| 634 |
add_respond_header('X-Rcpt-To', rcpt_to) |
| 635 |
end |
| 636 |
end |
| 637 |
|
| 638 |
if rule.x_from_header then |
| 639 |
local mail_from = task:get_principal_recipient() |
| 640 |
if mail_from and mail_from[1] then |
| 641 |
add_respond_header('X-Rcpt-To', mail_from[1].addr) |
| 642 |
end |
| 643 |
end |
| 644 |
|
| 645 |
end |
| 646 |
|
| 647 |
if icap_headers.connection and icap_headers.connection:lower() == 'close' then |
| 648 |
lua_util.debugm(rule.name, task, '%s: OPTIONS request Connection: %s - using new connection', |
| 649 |
rule.log_prefix, icap_headers.connection) |
| 650 |
connection:close() |
| 651 |
tcp_options.callback = icap_w_respond_cb |
| 652 |
tcp_options.data = get_respond_query() |
| 653 |
tcp.request(tcp_options) |
| 654 |
else |
| 655 |
connection:add_write(icap_w_respond_cb, get_respond_query()) |
| 656 |
end |
| 657 |
|
| 658 |
else |
| 659 |
rspamd_logger.errx(task, '%s: RESPMOD method not advertised: Methods: %s', |
| 660 |
rule.log_prefix, icap_headers['methods']) |
| 661 |
common.yield_result(task, rule, 'NO RESPMOD', 0.0, |
| 662 |
'fail', maybe_part) |
| 663 |
end |
| 664 |
else |
| 665 |
rspamd_logger.errx(task, '%s: OPTIONS query failed: %s', |
| 666 |
rule.log_prefix, icap_headers.icap or "-") |
| 667 |
common.yield_result(task, rule, 'OPTIONS query failed', 0.0, |
| 668 |
'fail', maybe_part) |
| 669 |
end |
| 670 |
end |
| 671 |
end |
| 672 |
|
| 673 |
if err or conn == nil then |
| 674 |
icap_requery(err, "options_request") |
| 675 |
else |
| 676 |
conn:add_read(icap_r_options_cb, '\r\n\r\n') |
| 677 |
end |
| 678 |
end |
| 679 |
|
| 680 |
tcp_options.task = task |
| 681 |
tcp_options.stop_pattern = '\r\n' |
| 682 |
tcp_options.read = false |
| 683 |
tcp_options.timeout = rule.timeout |
| 684 |
tcp_options.callback = icap_callback |
| 685 |
tcp_options.data = options_request |
| 686 |
|
| 687 |
if rule.ssl then |
| 688 |
tcp_options.ssl = true |
| 689 |
if rule.no_ssl_verify then |
| 690 |
tcp_options.no_ssl_verify = true |
| 691 |
end |
| 692 |
end |
| 693 |
|
| 694 |
tcp_options.host = addr:to_string() |
| 695 |
tcp_options.port = addr:get_port() |
| 696 |
tcp_options.upstream = upstream |
| 697 |
|
| 698 |
tcp.request(tcp_options) |
| 699 |
end |
| 700 |
|
| 701 |
if common.condition_check_and_continue(task, content, rule, digest, |
| 702 |
icap_check_uncached, maybe_part) then |
| 703 |
return |
| 704 |
else |
| 705 |
icap_check_uncached() |
| 706 |
end |
| 707 |
|
| 708 |
end |
| 709 |
|
| 710 |
return { |
| 711 |
type = { N, 'virus', 'virus', 'scanner' }, |
| 712 |
description = 'generic icap antivirus', |
| 713 |
configure = icap_config, |
| 714 |
check = icap_check, |
| 715 |
name = N |
| 716 |
} |