您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

sophos.lua 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. --[[
  2. Copyright (c) 2018, Vsevolod Stakhov <vsevolod@highsecure.ru>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ]]--
  13. --[[[
  14. -- @module savapi
  15. -- This module contains avira savapi antivirus access functions
  16. --]]
  17. local lua_util = require "lua_util"
  18. local tcp = require "rspamd_tcp"
  19. local upstream_list = require "rspamd_upstream_list"
  20. local rspamd_logger = require "rspamd_logger"
  21. local common = require "lua_scanners/common"
  22. local N = "sophos"
  23. local default_message = '${SCANNER}: virus found: "${VIRUS}"'
  24. local function sophos_config(opts)
  25. local sophos_conf = {
  26. name = N,
  27. scan_mime_parts = true,
  28. scan_text_mime = false,
  29. scan_image_mime = false,
  30. default_port = 4010,
  31. timeout = 15.0,
  32. log_clean = false,
  33. retransmits = 2,
  34. cache_expire = 3600, -- expire redis in one hour
  35. message = default_message,
  36. savdi_report_encrypted = false,
  37. detection_category = "virus",
  38. savdi_report_oversize = false,
  39. }
  40. sophos_conf = lua_util.override_defaults(sophos_conf, opts)
  41. if not sophos_conf.prefix then
  42. sophos_conf.prefix = 'rs_' .. sophos_conf.name .. '_'
  43. end
  44. if not sophos_conf.log_prefix then
  45. if sophos_conf.name:lower() == sophos_conf.type:lower() then
  46. sophos_conf.log_prefix = sophos_conf.name
  47. else
  48. sophos_conf.log_prefix = sophos_conf.name .. ' (' .. sophos_conf.type .. ')'
  49. end
  50. end
  51. if not sophos_conf['servers'] then
  52. rspamd_logger.errx(rspamd_config, 'no servers defined')
  53. return nil
  54. end
  55. sophos_conf['upstreams'] = upstream_list.create(rspamd_config,
  56. sophos_conf['servers'],
  57. sophos_conf.default_port)
  58. if sophos_conf['upstreams'] then
  59. lua_util.add_debug_alias('antivirus', sophos_conf.name)
  60. return sophos_conf
  61. end
  62. rspamd_logger.errx(rspamd_config, 'cannot parse servers %s',
  63. sophos_conf['servers'])
  64. return nil
  65. end
  66. local function sophos_check(task, content, digest, rule)
  67. local function sophos_check_uncached ()
  68. local upstream = rule.upstreams:get_upstream_round_robin()
  69. local addr = upstream:get_addr()
  70. local retransmits = rule.retransmits
  71. local protocol = 'SSSP/1.0\n'
  72. local streamsize = string.format('SCANDATA %d\n', #content)
  73. local bye = 'BYE\n'
  74. local function sophos_callback(err, data, conn)
  75. if err then
  76. -- set current upstream to fail because an error occurred
  77. upstream:fail()
  78. -- retry with another upstream until retransmits exceeds
  79. if retransmits > 0 then
  80. retransmits = retransmits - 1
  81. -- Select a different upstream!
  82. upstream = rule.upstreams:get_upstream_round_robin()
  83. addr = upstream:get_addr()
  84. lua_util.debugm(rule.name, task,
  85. '%s [%s]: retry IP: %s', rule['symbol'], rule['type'], addr)
  86. tcp.request({
  87. task = task,
  88. host = addr:to_string(),
  89. port = addr:get_port(),
  90. timeout = rule['timeout'],
  91. callback = sophos_callback,
  92. data = { protocol, streamsize, content, bye }
  93. })
  94. else
  95. rspamd_logger.errx(task, '%s [%s]: failed to scan, maximum retransmits exceed', rule['symbol'], rule['type'])
  96. common.yield_result(task, rule, 'failed to scan and retransmits exceed', 0.0, 'fail')
  97. end
  98. else
  99. upstream:ok()
  100. data = tostring(data)
  101. lua_util.debugm(rule.name, task,
  102. '%s [%s]: got reply: %s', rule['symbol'], rule['type'], data)
  103. local vname = string.match(data, 'VIRUS (%S+) ')
  104. if vname then
  105. common.yield_result(task, rule, vname)
  106. common.save_av_cache(task, digest, rule, vname)
  107. else
  108. if string.find(data, 'DONE OK') then
  109. if rule['log_clean'] then
  110. rspamd_logger.infox(task, '%s: message or mime_part is clean', rule.log_prefix)
  111. else
  112. lua_util.debugm(rule.name, task,
  113. '%s: message or mime_part is clean', rule.log_prefix)
  114. end
  115. common.save_av_cache(task, digest, rule, 'OK')
  116. -- not finished - continue
  117. elseif string.find(data, 'ACC') or string.find(data, 'OK SSSP') then
  118. conn:add_read(sophos_callback)
  119. elseif string.find(data, 'FAIL 0212') then
  120. rspamd_logger.warnx(task, 'Message is encrypted (FAIL 0212): %s', data)
  121. common.yield_result(task, rule, 'SAVDI: Message is encrypted (FAIL 0212)', 0.0, 'fail')
  122. elseif string.find(data, 'REJ 4') then
  123. rspamd_logger.warnx(task, 'Message is oversized (REJ 4): %s', data)
  124. common.yield_result(task, rule, 'SAVDI: Message oversized (REJ 4)', 0.0, 'fail')
  125. -- excplicitly set REJ1 message when SAVDIreports a protocol error
  126. elseif string.find(data, 'REJ 1') then
  127. rspamd_logger.errx(task, 'SAVDI (Protocol error (REJ 1)): %s', data)
  128. common.yield_result(task, rule, 'SAVDI: Protocol error (REJ 1)', 0.0, 'fail')
  129. else
  130. rspamd_logger.errx(task, 'unhandled response: %s', data)
  131. common.yield_result(task, rule, 'unhandled response: ' .. data, 0.0, 'fail')
  132. end
  133. end
  134. end
  135. end
  136. tcp.request({
  137. task = task,
  138. host = addr:to_string(),
  139. port = addr:get_port(),
  140. timeout = rule['timeout'],
  141. callback = sophos_callback,
  142. data = { protocol, streamsize, content, bye }
  143. })
  144. end
  145. if common.need_av_check(task, content, rule) then
  146. if common.check_av_cache(task, digest, rule, sophos_check_uncached) then
  147. return
  148. else
  149. sophos_check_uncached()
  150. end
  151. end
  152. end
  153. return {
  154. type = 'antivirus',
  155. description = 'sophos antivirus',
  156. configure = sophos_config,
  157. check = sophos_check,
  158. name = N
  159. }