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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 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)
  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 scan_id = task:get_uid() end
  71. local header = string.format('SCAN STREAM %s SIZE %d\n', scan_id,
  72. #content)
  73. local footer = '\n'
  74. local function fprot_callback(err, data)
  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, '%s [%s]: retry IP: %s', rule['symbol'], rule['type'], addr)
  85. tcp.request({
  86. task = task,
  87. host = addr:to_string(),
  88. port = addr:get_port(),
  89. timeout = rule['timeout'],
  90. callback = fprot_callback,
  91. data = { header, content, footer },
  92. stop_pattern = '\n'
  93. })
  94. else
  95. rspamd_logger.errx(task,
  96. '%s [%s]: failed to scan, maximum retransmits exceed',
  97. rule['symbol'], rule['type'])
  98. common.yield_result(task, rule, 'failed to scan and retransmits exceed', 0.0, 'fail')
  99. end
  100. else
  101. upstream:ok()
  102. data = tostring(data)
  103. local cached
  104. local clean = string.match(data, '^0 <clean>')
  105. if clean then
  106. cached = 'OK'
  107. if rule['log_clean'] then
  108. rspamd_logger.infox(task,
  109. '%s [%s]: message or mime_part is clean',
  110. rule['symbol'], rule['type'])
  111. end
  112. else
  113. -- returncodes: 1: infected, 2: suspicious, 3: both, 4-255: some error occured
  114. -- see http://www.f-prot.com/support/helpfiles/unix/appendix_c.html for more detail
  115. local vname = string.match(data, '^[1-3] <[%w%s]-: (.-)>')
  116. if not vname then
  117. rspamd_logger.errx(task, 'Unhandled response: %s', data)
  118. else
  119. common.yield_result(task, rule, vname)
  120. cached = vname
  121. end
  122. end
  123. if cached then
  124. common.save_cache(task, digest, rule, cached)
  125. end
  126. end
  127. end
  128. tcp.request({
  129. task = task,
  130. host = addr:to_string(),
  131. port = addr:get_port(),
  132. timeout = rule['timeout'],
  133. callback = fprot_callback,
  134. data = { header, content, footer },
  135. stop_pattern = '\n'
  136. })
  137. end
  138. if common.need_check(task, content, rule, digest, fprot_check_uncached) then
  139. return
  140. else
  141. fprot_check_uncached()
  142. end
  143. end
  144. return {
  145. type = 'antivirus',
  146. description = 'fprot antivirus',
  147. configure = fprot_config,
  148. check = fprot_check,
  149. name = N
  150. }