You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fprot.lua 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. --[[
  2. Copyright (c) 2022, Vsevolod Stakhov <vsevolod@rspamd.com>
  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 fprot
  15. -- This module contains fprot 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 = "fprot"
  23. local default_message = '${SCANNER}: virus found: "${VIRUS}"'
  24. local function fprot_config(opts)
  25. local fprot_conf = {
  26. name = N,
  27. scan_mime_parts = true,
  28. scan_text_mime = false,
  29. scan_image_mime = false,
  30. default_port = 10200,
  31. timeout = 5.0, -- FIXME: this will break task_timeout!
  32. log_clean = false,
  33. detection_category = "virus",
  34. retransmits = 2,
  35. cache_expire = 3600, -- expire redis in one hour
  36. message = default_message,
  37. }
  38. fprot_conf = lua_util.override_defaults(fprot_conf, opts)
  39. if not fprot_conf.prefix then
  40. fprot_conf.prefix = 'rs_' .. fprot_conf.name .. '_'
  41. end
  42. if not fprot_conf.log_prefix then
  43. if fprot_conf.name:lower() == fprot_conf.type:lower() then
  44. fprot_conf.log_prefix = fprot_conf.name
  45. else
  46. fprot_conf.log_prefix = fprot_conf.name .. ' (' .. fprot_conf.type .. ')'
  47. end
  48. end
  49. if not fprot_conf['servers'] then
  50. rspamd_logger.errx(rspamd_config, 'no servers defined')
  51. return nil
  52. end
  53. fprot_conf['upstreams'] = upstream_list.create(rspamd_config,
  54. fprot_conf['servers'],
  55. fprot_conf.default_port)
  56. if fprot_conf['upstreams'] then
  57. lua_util.add_debug_alias('antivirus', fprot_conf.name)
  58. return fprot_conf
  59. end
  60. rspamd_logger.errx(rspamd_config, 'cannot parse servers %s',
  61. fprot_conf['servers'])
  62. return nil
  63. end
  64. local function fprot_check(task, content, digest, rule, maybe_part)
  65. local function fprot_check_uncached ()
  66. local upstream = rule.upstreams:get_upstream_round_robin()
  67. local addr = upstream:get_addr()
  68. local retransmits = rule.retransmits
  69. local scan_id = task:get_queue_id()
  70. if not scan_id then
  71. scan_id = task:get_uid()
  72. end
  73. local header = string.format('SCAN STREAM %s SIZE %d\n', scan_id,
  74. #content)
  75. local footer = '\n'
  76. local function fprot_callback(err, data)
  77. if err then
  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, '%s: error: %s; retry IP: %s; retries left: %s',
  85. rule.log_prefix, err, addr, retransmits)
  86. tcp.request({
  87. task = task,
  88. host = addr:to_string(),
  89. port = addr:get_port(),
  90. upstream = upstream,
  91. timeout = rule['timeout'],
  92. callback = fprot_callback,
  93. data = { header, content, footer },
  94. stop_pattern = '\n'
  95. })
  96. else
  97. rspamd_logger.errx(task,
  98. '%s [%s]: failed to scan, maximum retransmits exceed',
  99. rule['symbol'], rule['type'])
  100. common.yield_result(task, rule, 'failed to scan and retransmits exceed',
  101. 0.0, 'fail', maybe_part)
  102. end
  103. else
  104. upstream:ok()
  105. data = tostring(data)
  106. local cached
  107. local clean = string.match(data, '^0 <clean>')
  108. if clean then
  109. cached = 'OK'
  110. if rule['log_clean'] then
  111. rspamd_logger.infox(task,
  112. '%s [%s]: message or mime_part is clean',
  113. rule['symbol'], rule['type'])
  114. end
  115. else
  116. -- returncodes: 1: infected, 2: suspicious, 3: both, 4-255: some error occurred
  117. -- see http://www.f-prot.com/support/helpfiles/unix/appendix_c.html for more detail
  118. local vname = string.match(data, '^[1-3] <[%w%s]-: (.-)>')
  119. if not vname then
  120. rspamd_logger.errx(task, 'Unhandled response: %s', data)
  121. else
  122. common.yield_result(task, rule, vname, 1.0, nil, maybe_part)
  123. cached = vname
  124. end
  125. end
  126. if cached then
  127. common.save_cache(task, digest, rule, cached, 1.0, maybe_part)
  128. end
  129. end
  130. end
  131. tcp.request({
  132. task = task,
  133. host = addr:to_string(),
  134. port = addr:get_port(),
  135. upstream = upstream,
  136. timeout = rule['timeout'],
  137. callback = fprot_callback,
  138. data = { header, content, footer },
  139. stop_pattern = '\n'
  140. })
  141. end
  142. if common.condition_check_and_continue(task, content, rule, digest,
  143. fprot_check_uncached, maybe_part) then
  144. return
  145. else
  146. fprot_check_uncached()
  147. end
  148. end
  149. return {
  150. type = 'antivirus',
  151. description = 'fprot antivirus',
  152. configure = fprot_config,
  153. check = fprot_check,
  154. name = N
  155. }