summaryrefslogtreecommitdiffstats
path: root/lualib/lua_scanners/clamav.lua
blob: 1ff3cdc4fdeae1c522f32a8a58cf04f90f54bb07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
--[[
Copyright (c) 2018, Vsevolod Stakhov <vsevolod@highsecure.ru>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]--

--[[[
-- @module clamav
-- This module contains clamav access functions
--]]

local lua_util = require "lua_util"
local tcp = require "rspamd_tcp"
local upstream_list = require "rspamd_upstream_list"
local rspamd_util = require "rspamd_util"
local rspamd_logger = require "rspamd_logger"
local common = require "lua_scanners/common"

local N = "clamav"

local default_message = '${SCANNER}: virus found: "${VIRUS}"'

local function clamav_config(opts)
  local clamav_conf = {
    name = N,
    scan_mime_parts = true,
    scan_text_mime = false,
    scan_image_mime = false,
    default_port = 3310,
    log_clean = false,
    timeout = 5.0, -- FIXME: this will break task_timeout!
    detection_category = "virus",
    retransmits = 2,
    cache_expire = 3600, -- expire redis in one hour
    message = default_message,
  }

  clamav_conf = lua_util.override_defaults(clamav_conf, opts)

  if not clamav_conf.prefix then
    clamav_conf.prefix = 'rs_' .. clamav_conf.name .. '_'
  end

  if not clamav_conf.log_prefix then
    if clamav_conf.name:lower() == clamav_conf.type:lower() then
      clamav_conf.log_prefix = clamav_conf.name
    else
      clamav_conf.log_prefix = clamav_conf.name .. ' (' .. clamav_conf.type .. ')'
    end
  end

  if not clamav_conf['servers'] then
    rspamd_logger.errx(rspamd_config, 'no servers defined')

    return nil
  end

  clamav_conf['upstreams'] = upstream_list.create(rspamd_config,
      clamav_conf['servers'],
      clamav_conf.default_port)

  if clamav_conf['upstreams'] then
    lua_util.add_debug_alias('antivirus', clamav_conf.name)
    return clamav_conf
  end

  rspamd_logger.errx(rspamd_config, 'cannot parse servers %s',
      clamav_conf['servers'])
  return nil
end

local function clamav_check(task, content, digest, rule)
  local function clamav_check_uncached ()
    local upstream = rule.upstreams:get_upstream_round_robin()
    local addr = upstream:get_addr()
    local retransmits = rule.retransmits
    local header = rspamd_util.pack("c9 c1 >I4", "zINSTREAM", "\0",
        #content)
    local footer = rspamd_util.pack(">I4", 0)

    local function clamav_callback(err, data)
      if err then

        -- set current upstream to fail because an error occurred
        upstream:fail()

        -- retry with another upstream until retransmits exceeds
        if retransmits > 0 then

          retransmits = retransmits - 1

          -- Select a different upstream!
          upstream = rule.upstreams:get_upstream_round_robin()
          addr = upstream:get_addr()

          lua_util.debugm(rule.name, task, '%s: retry IP: %s',
              rule.log_prefix, addr)

          tcp.request({
            task = task,
            host = addr:to_string(),
            port = addr:get_port(),
            timeout = rule['timeout'],
            callback = clamav_callback,
            data = { header, content, footer },
            stop_pattern = '\0'
          })
        else
          rspamd_logger.errx(task, '%s: failed to scan, maximum retransmits exceed', rule.log_prefix)
          common.yield_result(task, rule, 'failed to scan and retransmits exceed', 0.0, 'fail')
        end

      else
        upstream:ok()
        data = tostring(data)
        local cached
        lua_util.debugm(rule.name, task, '%s: got reply: %s',
            rule.log_prefix, data)
        if data == 'stream: OK' then
          cached = 'OK'
          if rule['log_clean'] then
            rspamd_logger.infox(task, '%s: message or mime_part is clean',
                rule.log_prefix)
          else
            lua_util.debugm(rule.name, task, '%s: message or mime_part is clean', rule.log_prefix)
          end
        else
          local vname = string.match(data, 'stream: (.+) FOUND')
          if string.find(vname, '^Heuristics%.Encrypted') then
            rspamd_logger.errx(task, '%s: File is encrypted', rule.log_prefix)
            common.yield_result(task, rule, 'File is encrypted: '.. vname, 0.0, 'encrypted')
          elseif string.find(vname, '^Heuristics%.Limits%.Exceeded') then
            rspamd_logger.errx(task, '%s: ClamAV Limits Exceeded', rule.log_prefix)
            common.yield_result(task, rule, 'Limits Exceeded: '.. vname, 0.0, 'fail')
          elseif vname then
            common.yield_result(task, rule, vname)
            cached = vname
          else
            rspamd_logger.errx(task, '%s: unhandled response: %s', rule.log_prefix, data)
            common.yield_result(task, rule, 'unhandled response:' .. vname, 0.0, 'fail')
          end
        end
        if cached then
          common.save_av_cache(task, digest, rule, cached)
        end
      end
    end

    tcp.request({
      task = task,
      host = addr:to_string(),
      port = addr:get_port(),
      timeout = rule['timeout'],
      callback = clamav_callback,
      data = { header, content, footer },
      stop_pattern = '\0'
    })
  end

  if common.need_av_check(task, content, rule) then
    if common.check_av_cache(task, digest, rule, clamav_check_uncached) then
      return
    else
      clamav_check_uncached()
    end
  end
end

return {
  type = 'antivirus',
  description = 'clamav antivirus',
  configure = clamav_config,
  check = clamav_check,
  name = N
}