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

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