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.

headers_checks.lua 28KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. --[[
  2. Copyright (c) 2017, 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. local util = require "rspamd_util"
  14. local ipairs = ipairs
  15. local pairs = pairs
  16. local table = table
  17. local tostring = tostring
  18. local tonumber = tonumber
  19. local fun = require "fun"
  20. local E = {}
  21. local rcvd_cb_id = rspamd_config:register_symbol{
  22. name = 'CHECK_RECEIVED',
  23. type = 'callback',
  24. score = 0.0,
  25. group = 'headers',
  26. callback = function(task)
  27. local cnts = {
  28. [1] = 'ONE',
  29. [2] = 'TWO',
  30. [3] = 'THREE',
  31. [5] = 'FIVE',
  32. [7] = 'SEVEN',
  33. [12] = 'TWELVE'
  34. }
  35. local def = 'ZERO'
  36. local received = task:get_received_headers()
  37. local nreceived = fun.reduce(function(acc, rcvd)
  38. return acc + 1
  39. end, 0, fun.filter(function(h)
  40. return not h['artificial']
  41. end, received))
  42. for k,v in pairs(cnts) do
  43. if nreceived >= tonumber(k) then
  44. def = v
  45. end
  46. end
  47. task:insert_result('RCVD_COUNT_' .. def, 1.0, tostring(nreceived))
  48. end
  49. }
  50. rspamd_config:register_symbol{
  51. name = 'RCVD_COUNT_ZERO',
  52. score = 0.0,
  53. parent = rcvd_cb_id,
  54. type = 'virtual',
  55. description = 'No received',
  56. group = 'headers',
  57. }
  58. rspamd_config:register_symbol{
  59. name = 'RCVD_COUNT_ONE',
  60. score = 0.0,
  61. parent = rcvd_cb_id,
  62. type = 'virtual',
  63. description = 'One received',
  64. group = 'headers',
  65. }
  66. rspamd_config:register_symbol{
  67. name = 'RCVD_COUNT_TWO',
  68. score = 0.0,
  69. parent = rcvd_cb_id,
  70. type = 'virtual',
  71. description = 'Two received',
  72. group = 'headers',
  73. }
  74. rspamd_config:register_symbol{
  75. name = 'RCVD_COUNT_THREE',
  76. score = 0.0,
  77. parent = rcvd_cb_id,
  78. type = 'virtual',
  79. description = '3-5 received',
  80. group = 'headers',
  81. }
  82. rspamd_config:register_symbol{
  83. name = 'RCVD_COUNT_FIVE',
  84. score = 0.0,
  85. parent = rcvd_cb_id,
  86. type = 'virtual',
  87. description = '5-7 received',
  88. group = 'headers',
  89. }
  90. rspamd_config:register_symbol{
  91. name = 'RCVD_COUNT_SEVEN',
  92. score = 0.0,
  93. parent = rcvd_cb_id,
  94. type = 'virtual',
  95. description = '7-11 received',
  96. group = 'headers',
  97. }
  98. rspamd_config:register_symbol{
  99. name = 'RCVD_COUNT_TWELVE',
  100. score = 0.0,
  101. parent = rcvd_cb_id,
  102. type = 'virtual',
  103. description = '12+ received',
  104. group = 'headers',
  105. }
  106. local prio_cb_id = rspamd_config:register_symbol {
  107. name = 'HAS_X_PRIO',
  108. type = 'callback',
  109. score = 0.0,
  110. group = 'headers',
  111. callback = function (task)
  112. local cnts = {
  113. [1] = 'ONE',
  114. [2] = 'TWO',
  115. [3] = 'THREE',
  116. [5] = 'FIVE',
  117. }
  118. local def = 'ZERO'
  119. local xprio = task:get_header('X-Priority');
  120. if not xprio then return false end
  121. local _,_,x = xprio:find('^%s?(%d+)');
  122. if (x) then
  123. x = tonumber(x)
  124. for k,v in pairs(cnts) do
  125. if x >= tonumber(k) then
  126. def = v
  127. end
  128. end
  129. task:insert_result('HAS_X_PRIO_' .. def, 1.0, tostring(x))
  130. end
  131. end
  132. }
  133. rspamd_config:register_symbol{
  134. name = 'HAS_X_PRIO_ZERO',
  135. score = 0.0,
  136. parent = prio_cb_id,
  137. type = 'virtual',
  138. description = 'Priority 0',
  139. group = 'headers',
  140. }
  141. rspamd_config:register_symbol{
  142. name = 'HAS_X_PRIO_ONE',
  143. score = 0.0,
  144. parent = prio_cb_id,
  145. type = 'virtual',
  146. description = 'Priority 1',
  147. group = 'headers',
  148. }
  149. rspamd_config:register_symbol{
  150. name = 'HAS_X_PRIO_TWO',
  151. score = 0.0,
  152. parent = prio_cb_id,
  153. type = 'virtual',
  154. description = 'Priority 2',
  155. group = 'headers',
  156. }
  157. rspamd_config:register_symbol{
  158. name = 'HAS_X_PRIO_THREE',
  159. score = 0.0,
  160. parent = prio_cb_id,
  161. type = 'virtual',
  162. description = 'Priority 3-4',
  163. group = 'headers',
  164. }
  165. rspamd_config:register_symbol{
  166. name = 'HAS_X_PRIO_FIVE',
  167. score = 0.0,
  168. parent = prio_cb_id,
  169. type = 'virtual',
  170. description = 'Priority 5+',
  171. group = 'headers',
  172. }
  173. local function get_raw_header(task, name)
  174. return ((task:get_header_full(name) or {})[1] or {})['value']
  175. end
  176. local check_replyto_id = rspamd_config:register_symbol({
  177. type = 'callback',
  178. name = 'CHECK_REPLYTO',
  179. score = 0.0,
  180. group = 'headers',
  181. callback = function(task)
  182. local replyto = get_raw_header(task, 'Reply-To')
  183. if not replyto then
  184. return false
  185. end
  186. local rt = util.parse_mail_address(replyto, task:get_mempool())
  187. if not (rt and rt[1] and (string.len(rt[1].addr) > 0)) then
  188. task:insert_result('REPLYTO_UNPARSEABLE', 1.0)
  189. return false
  190. else
  191. local rta = rt[1].addr
  192. task:insert_result('HAS_REPLYTO', 1.0, rta)
  193. -- Check if Reply-To address starts with title seen in display name
  194. local sym = task:get_symbol('FROM_NAME_HAS_TITLE')
  195. local title = (((sym or E)[1] or E).options or E)[1]
  196. if title then
  197. rta = rta:lower()
  198. if rta:find('^' .. title) then
  199. task:insert_result('REPLYTO_EMAIL_HAS_TITLE', 1.0)
  200. end
  201. end
  202. end
  203. -- See if Reply-To matches From in some way
  204. local from = task:get_from(2)
  205. local from_h = get_raw_header(task, 'From')
  206. if not (from and from[1]) then
  207. return false
  208. end
  209. if (from_h and from_h == replyto) then
  210. -- From and Reply-To are identical
  211. task:insert_result('REPLYTO_EQ_FROM', 1.0)
  212. else
  213. if (from and from[1]) then
  214. -- See if From and Reply-To addresses match
  215. if (util.strequal_caseless(from[1].addr, rt[1].addr)) then
  216. task:insert_result('REPLYTO_ADDR_EQ_FROM', 1.0)
  217. elseif from[1].domain and rt[1].domain then
  218. if (util.strequal_caseless(from[1].domain, rt[1].domain)) then
  219. task:insert_result('REPLYTO_DOM_EQ_FROM_DOM', 1.0)
  220. else
  221. -- See if Reply-To matches the To address
  222. local to = task:get_recipients(2)
  223. if (to and to[1] and to[1].addr:lower() == rt[1].addr:lower()) then
  224. -- Ignore this for mailing-lists and automatic submissions
  225. if (not (task:get_header('List-Unsubscribe') or
  226. task:get_header('X-To-Get-Off-This-List') or
  227. task:get_header('X-List') or
  228. task:get_header('Auto-Submitted')))
  229. then
  230. task:insert_result('REPLYTO_EQ_TO_ADDR', 1.0)
  231. end
  232. else
  233. task:insert_result('REPLYTO_DOM_NEQ_FROM_DOM', 1.0)
  234. end
  235. end
  236. end
  237. -- See if the Display Names match
  238. if (from[1].name and rt[1].name and
  239. util.strequal_caseless(from[1].name, rt[1].name)) then
  240. task:insert_result('REPLYTO_DN_EQ_FROM_DN', 1.0)
  241. end
  242. end
  243. end
  244. end
  245. })
  246. rspamd_config:register_symbol{
  247. name = 'REPLYTO_UNPARSEABLE',
  248. score = 1.0,
  249. parent = check_replyto_id,
  250. type = 'virtual',
  251. description = 'Reply-To header could not be parsed',
  252. group = 'headers',
  253. }
  254. rspamd_config:register_symbol{
  255. name = 'HAS_REPLYTO',
  256. score = 0.0,
  257. parent = check_replyto_id,
  258. type = 'virtual',
  259. description = 'Has Reply-To header',
  260. group = 'headers',
  261. }
  262. rspamd_config:register_symbol{
  263. name = 'REPLYTO_EQ_FROM',
  264. score = 0.0,
  265. parent = check_replyto_id,
  266. type = 'virtual',
  267. description = 'Reply-To header is identical to From header',
  268. group = 'headers',
  269. }
  270. rspamd_config:register_symbol{
  271. name = 'REPLYTO_ADDR_EQ_FROM',
  272. score = 0.0,
  273. parent = check_replyto_id,
  274. type = 'virtual',
  275. description = 'Reply-To header is identical to SMTP From',
  276. group = 'headers',
  277. }
  278. rspamd_config:register_symbol{
  279. name = 'REPLYTO_DOM_EQ_FROM_DOM',
  280. score = 0.0,
  281. parent = check_replyto_id,
  282. type = 'virtual',
  283. description = 'Reply-To domain matches the From domain',
  284. group = 'headers',
  285. }
  286. rspamd_config:register_symbol{
  287. name = 'REPLYTO_DOM_NEQ_FROM_DOM',
  288. score = 0.0,
  289. parent = check_replyto_id,
  290. type = 'virtual',
  291. description = 'Reply-To domain does not match the From domain',
  292. group = 'headers',
  293. }
  294. rspamd_config:register_symbol{
  295. name = 'REPLYTO_DN_EQ_FROM_DN',
  296. score = 0.0,
  297. parent = check_replyto_id,
  298. type = 'virtual',
  299. description = 'Reply-To display name matches From',
  300. group = 'headers',
  301. }
  302. rspamd_config:register_symbol{
  303. name = 'REPLYTO_EMAIL_HAS_TITLE',
  304. score = 2.0,
  305. parent = check_replyto_id,
  306. type = 'virtual',
  307. description = 'Reply-To header has title',
  308. group = 'headers',
  309. }
  310. rspamd_config:register_symbol{
  311. name = 'REPLYTO_EQ_TO_ADDR',
  312. score = 5.0,
  313. parent = check_replyto_id,
  314. type = 'virtual',
  315. description = 'Reply-To is the same as the To address',
  316. group = 'headers',
  317. }
  318. rspamd_config:register_dependency('CHECK_REPLYTO', 'CHECK_FROM')
  319. local check_mime_id = rspamd_config:register_symbol{
  320. name = 'CHECK_MIME',
  321. type = 'callback',
  322. group = 'headers',
  323. score = 0.0,
  324. callback = function(task)
  325. local parts = task:get_parts()
  326. if not parts then return false end
  327. -- Make sure there is a MIME-Version header
  328. local mv = task:get_header('MIME-Version')
  329. local missing_mime = false
  330. if (not mv) then
  331. missing_mime = true
  332. end
  333. local found_ma = false
  334. local found_plain = false
  335. local found_html = false
  336. local cte_7bit = false
  337. for _,p in ipairs(parts) do
  338. local mtype,subtype = p:get_type()
  339. local ctype = mtype:lower() .. '/' .. subtype:lower()
  340. if (ctype == 'multipart/alternative') then
  341. found_ma = true
  342. end
  343. if (ctype == 'text/plain') then
  344. if p:get_cte() == '7bit' then
  345. cte_7bit = true
  346. end
  347. found_plain = true
  348. end
  349. if (ctype == 'text/html') then
  350. if p:get_cte() == '7bit' then
  351. cte_7bit = true
  352. end
  353. found_html = true
  354. end
  355. end
  356. if missing_mime then
  357. if not (not found_ma and ((found_plain or found_html) and cte_7bit)) then
  358. task:insert_result('MISSING_MIME_VERSION', 1.0)
  359. end
  360. end
  361. if (found_ma) then
  362. if (not found_plain) then
  363. task:insert_result('MIME_MA_MISSING_TEXT', 1.0)
  364. end
  365. if (not found_html) then
  366. task:insert_result('MIME_MA_MISSING_HTML', 1.0)
  367. end
  368. end
  369. end
  370. }
  371. rspamd_config:register_symbol{
  372. name = 'MISSING_MIME_VERSION',
  373. score = 2.0,
  374. parent = check_mime_id,
  375. type = 'virtual',
  376. description = 'MIME-Version header is missing',
  377. group = 'headers',
  378. }
  379. rspamd_config:register_symbol{
  380. name = 'MIME_MA_MISSING_TEXT',
  381. score = 2.0,
  382. parent = check_mime_id,
  383. type = 'virtual',
  384. description = 'MIME multipart/alternative missing text/plain part',
  385. group = 'headers',
  386. }
  387. rspamd_config:register_symbol{
  388. name = 'MIME_MA_MISSING_HTML',
  389. score = 1.0,
  390. parent = check_mime_id,
  391. type = 'virtual',
  392. description = 'MIME multipart/alternative missing text/html part',
  393. group = 'headers',
  394. }
  395. -- Used to be called IS_LIST
  396. rspamd_config.PREVIOUSLY_DELIVERED = {
  397. callback = function(task)
  398. if not task:has_recipients(2) then return false end
  399. local to = task:get_recipients(2)
  400. local rcvds = task:get_header_full('Received')
  401. if not rcvds then return false end
  402. for _, rcvd in ipairs(rcvds) do
  403. local _,_,addr = rcvd['decoded']:lower():find("%sfor%s<(.-)>")
  404. if addr then
  405. for _, toa in ipairs(to) do
  406. if toa and toa.addr:lower() == addr then
  407. return true, addr
  408. end
  409. end
  410. return false
  411. end
  412. end
  413. end,
  414. description = 'Message either to a list or was forwarded',
  415. group = 'headers',
  416. score = 0.0
  417. }
  418. rspamd_config.BROKEN_HEADERS = {
  419. callback = function(task)
  420. return task:has_flag('broken_headers')
  421. end,
  422. score = 10.0,
  423. group = 'headers',
  424. description = 'Headers structure is likely broken'
  425. }
  426. rspamd_config.BROKEN_CONTENT_TYPE = {
  427. callback = function(task)
  428. return fun.any(function(p) return p:is_broken() end,
  429. task:get_parts())
  430. end,
  431. score = 1.5,
  432. group = 'headers',
  433. description = 'Message has part with broken content type'
  434. }
  435. rspamd_config.HEADER_RCONFIRM_MISMATCH = {
  436. callback = function (task)
  437. local header_from = nil
  438. local cread = task:get_header('X-Confirm-Reading-To')
  439. if task:has_from('mime') then
  440. header_from = task:get_from('mime')[1]
  441. end
  442. local header_cread = nil
  443. if cread then
  444. local headers_cread = util.parse_mail_address(cread, task:get_mempool())
  445. if headers_cread then header_cread = headers_cread[1] end
  446. end
  447. if header_from and header_cread then
  448. if not string.find(header_from['addr'], header_cread['addr']) then
  449. return true
  450. end
  451. end
  452. return false
  453. end,
  454. score = 2.0,
  455. group = 'headers',
  456. description = 'Read confirmation address is different to from address'
  457. }
  458. rspamd_config.HEADER_FORGED_MDN = {
  459. callback = function (task)
  460. local mdn = task:get_header('Disposition-Notification-To')
  461. if not mdn then return false end
  462. local header_rp = nil
  463. if task:has_from('smtp') then
  464. header_rp = task:get_from('smtp')[1]
  465. end
  466. -- Parse mail addr
  467. local headers_mdn = util.parse_mail_address(mdn, task:get_mempool())
  468. if headers_mdn and not header_rp then return true end
  469. if header_rp and not headers_mdn then return false end
  470. if not headers_mdn and not header_rp then return false end
  471. local found_match = false
  472. for _, h in ipairs(headers_mdn) do
  473. if util.strequal_caseless(h['addr'], header_rp['addr']) then
  474. found_match = true
  475. break
  476. end
  477. end
  478. return (not found_match)
  479. end,
  480. score = 2.0,
  481. group = 'headers',
  482. description = 'Read confirmation address is different to return path'
  483. }
  484. local headers_unique = {
  485. ['Content-Type'] = 1.0,
  486. ['Content-Transfer-Encoding'] = 1.0,
  487. -- https://tools.ietf.org/html/rfc5322#section-3.6
  488. ['Date'] = 0.1,
  489. ['From'] = 1.0,
  490. ['Sender'] = 1.0,
  491. ['Reply-To'] = 1.0,
  492. ['To'] = 0.2,
  493. ['Cc'] = 0.1,
  494. ['Bcc'] = 0.1,
  495. ['Message-ID'] = 0.7,
  496. ['In-Reply-To'] = 0.7,
  497. ['References'] = 0.3,
  498. ['Subject'] = 0.7
  499. }
  500. rspamd_config.MULTIPLE_UNIQUE_HEADERS = {
  501. callback = function(task)
  502. local res = 0
  503. local max_mult = 0.0
  504. local res_tbl = {}
  505. for hdr,mult in pairs(headers_unique) do
  506. local hc = task:get_header_count(hdr)
  507. if hc > 1 then
  508. res = res + 1
  509. table.insert(res_tbl, hdr)
  510. if max_mult < mult then
  511. max_mult = mult
  512. end
  513. end
  514. end
  515. if res > 0 then
  516. return true,max_mult,table.concat(res_tbl, ',')
  517. end
  518. return false
  519. end,
  520. score = 7.0,
  521. group = 'headers',
  522. one_shot = true,
  523. description = 'Repeated unique headers'
  524. }
  525. rspamd_config.MISSING_FROM = {
  526. callback = function(task)
  527. local from = task:get_header('From')
  528. if from == nil or from == '' then
  529. return true
  530. end
  531. return false
  532. end,
  533. score = 2.0,
  534. group = 'headers',
  535. description = 'Missing From: header'
  536. }
  537. rspamd_config.MULTIPLE_FROM = {
  538. callback = function(task)
  539. local from = task:get_from('mime')
  540. if from and from[1] then
  541. if #from > 1 then
  542. return true,1.0,table.concat(
  543. fun.totable(
  544. fun.map(function(a) return a.addr end,
  545. fun.filter(function(a) return a.addr and a.addr ~= '' end,
  546. from))),
  547. ',')
  548. end
  549. end
  550. return false
  551. end,
  552. score = 9.0,
  553. group = 'headers',
  554. description = 'Multiple addresses in From'
  555. }
  556. rspamd_config.MV_CASE = {
  557. callback = function (task)
  558. local mv = task:get_header('Mime-Version', true)
  559. if (mv) then return true end
  560. end,
  561. description = 'Mime-Version .vs. MIME-Version',
  562. score = 0.5,
  563. group = 'headers',
  564. }
  565. rspamd_config.FAKE_REPLY = {
  566. callback = function (task)
  567. local subject = task:get_header('Subject')
  568. if (subject and subject:lower():find('^re:')) then
  569. local ref = task:get_header('References')
  570. local rt = task:get_header('In-Reply-To')
  571. if (not (ref or rt)) then return true end
  572. end
  573. return false
  574. end,
  575. description = 'Fake reply',
  576. score = 1.0,
  577. group = 'headers'
  578. }
  579. local check_from_id = rspamd_config:register_symbol{
  580. name = 'CHECK_FROM',
  581. type = 'callback',
  582. score = 0.0,
  583. group = 'headers',
  584. callback = function(task)
  585. local envfrom = task:get_from(1)
  586. local from = task:get_from(2)
  587. if (from and from[1] and (from[1].name == nil or from[1].name == '' )) then
  588. task:insert_result('FROM_NO_DN', 1.0)
  589. elseif (from and from[1] and from[1].name and
  590. util.strequal_caseless(from[1].name, from[1].addr)) then
  591. task:insert_result('FROM_DN_EQ_ADDR', 1.0)
  592. elseif (from and from[1] and from[1].name and from[1].name ~= '') then
  593. task:insert_result('FROM_HAS_DN', 1.0)
  594. -- Look for Mr/Mrs/Dr titles
  595. local n = from[1].name:lower()
  596. local match, match_end
  597. match, match_end = n:find('^mrs?[%.%s]')
  598. if match then
  599. task:insert_result('FROM_NAME_HAS_TITLE', 1.0, n:sub(match, match_end-1))
  600. end
  601. match, match_end = n:find('^dr[%.%s]')
  602. if match then
  603. task:insert_result('FROM_NAME_HAS_TITLE', 1.0, n:sub(match, match_end-1))
  604. end
  605. -- Check for excess spaces
  606. if n:find('%s%s') then
  607. task:insert_result('FROM_NAME_EXCESS_SPACE', 1.0)
  608. end
  609. end
  610. if (envfrom and from and envfrom[1] and from[1] and
  611. util.strequal_caseless(envfrom[1].addr, from[1].addr))
  612. then
  613. task:insert_result('FROM_EQ_ENVFROM', 1.0)
  614. elseif (envfrom and envfrom[1] and envfrom[1].addr) then
  615. task:insert_result('FROM_NEQ_ENVFROM', 1.0, ((from or E)[1] or E).addr or '', envfrom[1].addr)
  616. end
  617. local to = task:get_recipients(2)
  618. if not (to and to[1] and #to == 1 and from and from[1]) then return false end
  619. -- Check if FROM == TO
  620. if (util.strequal_caseless(to[1].addr, from[1].addr)) then
  621. task:insert_result('TO_EQ_FROM', 1.0)
  622. elseif (to[1].domain and from[1].domain and
  623. util.strequal_caseless(to[1].domain, from[1].domain))
  624. then
  625. task:insert_result('TO_DOM_EQ_FROM_DOM', 1.0)
  626. end
  627. end
  628. }
  629. rspamd_config:register_symbol{
  630. name = 'FROM_NO_DN',
  631. score = 0,
  632. group = 'headers',
  633. parent = check_from_id,
  634. type = 'virtual',
  635. description = 'From header does not have a display name',
  636. }
  637. rspamd_config:register_symbol{
  638. name = 'FROM_DN_EQ_ADDR',
  639. score = 1.0,
  640. group = 'headers',
  641. parent = check_from_id,
  642. type = 'virtual',
  643. description = 'From header display name is the same as the address',
  644. }
  645. rspamd_config:register_symbol{
  646. name = 'FROM_HAS_DN',
  647. score = 0.0,
  648. group = 'headers',
  649. parent = check_from_id,
  650. type = 'virtual',
  651. description = 'From header has a display name',
  652. }
  653. rspamd_config:register_symbol{
  654. name = 'FROM_NAME_EXCESS_SPACE',
  655. score = 1.0,
  656. group = 'headers',
  657. parent = check_from_id,
  658. type = 'virtual',
  659. description = 'From header display name contains excess whitespace',
  660. }
  661. rspamd_config:register_symbol{
  662. name = 'FROM_NAME_HAS_TITLE',
  663. score = 1.0,
  664. group = 'headers',
  665. parent = check_from_id,
  666. type = 'virtual',
  667. description = 'From header display name has a title (Mr/Mrs/Dr)',
  668. }
  669. rspamd_config:register_symbol{
  670. name = 'FROM_EQ_ENVFROM',
  671. score = 0.0,
  672. group = 'headers',
  673. parent = check_from_id,
  674. type = 'virtual',
  675. description = 'From address is the same as the envelope',
  676. }
  677. rspamd_config:register_symbol{
  678. name = 'FROM_NEQ_ENVFROM',
  679. score = 0.0,
  680. group = 'headers',
  681. parent = check_from_id,
  682. type = 'virtual',
  683. description = 'From address is different to the envelope',
  684. }
  685. rspamd_config:register_symbol{
  686. name = 'TO_EQ_FROM',
  687. score = 0.0,
  688. group = 'headers',
  689. parent = check_from_id,
  690. type = 'virtual',
  691. description = 'To address matches the From address',
  692. }
  693. rspamd_config:register_symbol{
  694. name = 'TO_DOM_EQ_FROM_DOM',
  695. score = 0.0,
  696. group = 'headers',
  697. parent = check_from_id,
  698. type = 'virtual',
  699. description = 'To domain is the same as the From domain',
  700. }
  701. local check_to_cc_id = rspamd_config:register_symbol{
  702. name = 'CHECK_TO_CC',
  703. type = 'callback',
  704. score = 0.0,
  705. group = 'headers',
  706. callback = function(task)
  707. local rcpts = task:get_recipients(1)
  708. local to = task:get_recipients(2)
  709. local to_match_envrcpt = 0
  710. local cnts = {
  711. [1] = 'ONE',
  712. [2] = 'TWO',
  713. [3] = 'THREE',
  714. [5] = 'FIVE',
  715. [7] = 'SEVEN',
  716. [12] = 'TWELVE',
  717. [50] = 'GT_50'
  718. }
  719. local def = 'ZERO'
  720. if (not to) then return false end
  721. -- Add symbol for recipient count
  722. local nrcpt = #to
  723. for k,v in pairs(cnts) do
  724. if nrcpt >= tonumber(k) then
  725. def = v
  726. end
  727. end
  728. task:insert_result('RCPT_COUNT_' .. def, 1.0, tostring(nrcpt))
  729. -- Check for display names
  730. local to_dn_count = 0
  731. local to_dn_eq_addr_count = 0
  732. for _, toa in ipairs(to) do
  733. -- To: Recipients <noreply@dropbox.com>
  734. if (toa['name'] and (toa['name']:lower() == 'recipient'
  735. or toa['name']:lower() == 'recipients')) then
  736. task:insert_result('TO_DN_RECIPIENTS', 1.0)
  737. end
  738. if (toa['name'] and util.strequal_caseless(toa['name'], toa['addr'])) then
  739. to_dn_eq_addr_count = to_dn_eq_addr_count + 1
  740. elseif (toa['name'] and toa['name'] ~= '') then
  741. to_dn_count = to_dn_count + 1
  742. end
  743. -- See if header recipients match envrcpts
  744. if (rcpts) then
  745. for _, rcpt in ipairs(rcpts) do
  746. if (toa and toa['addr'] and rcpt and rcpt['addr'] and
  747. util.strequal_caseless(rcpt['addr'], toa['addr']))
  748. then
  749. to_match_envrcpt = to_match_envrcpt + 1
  750. end
  751. end
  752. end
  753. end
  754. if (to_dn_count == 0 and to_dn_eq_addr_count == 0) then
  755. task:insert_result('TO_DN_NONE', 1.0)
  756. elseif (to_dn_count == #to) then
  757. task:insert_result('TO_DN_ALL', 1.0)
  758. elseif (to_dn_count > 0) then
  759. task:insert_result('TO_DN_SOME', 1.0)
  760. end
  761. if (to_dn_eq_addr_count == #to) then
  762. task:insert_result('TO_DN_EQ_ADDR_ALL', 1.0)
  763. elseif (to_dn_eq_addr_count > 0) then
  764. task:insert_result('TO_DN_EQ_ADDR_SOME', 1.0)
  765. end
  766. -- See if header recipients match envelope recipients
  767. if (to_match_envrcpt == #to) then
  768. task:insert_result('TO_MATCH_ENVRCPT_ALL', 1.0)
  769. elseif (to_match_envrcpt > 0) then
  770. task:insert_result('TO_MATCH_ENVRCPT_SOME', 1.0)
  771. end
  772. end
  773. }
  774. rspamd_config:register_symbol{
  775. name = 'RCPT_COUNT_ZERO',
  776. score = 0.0,
  777. parent = check_to_cc_id,
  778. type = 'virtual',
  779. description = 'No recipients',
  780. group = 'headers',
  781. }
  782. rspamd_config:register_symbol{
  783. name = 'RCPT_COUNT_ONE',
  784. score = 0.0,
  785. parent = check_to_cc_id,
  786. type = 'virtual',
  787. description = 'One recipient',
  788. group = 'headers',
  789. }
  790. rspamd_config:register_symbol{
  791. name = 'RCPT_COUNT_TWO',
  792. score = 0.0,
  793. parent = check_to_cc_id,
  794. type = 'virtual',
  795. description = 'Two recipients',
  796. group = 'headers',
  797. }
  798. rspamd_config:register_symbol{
  799. name = 'RCPT_COUNT_THREE',
  800. score = 0.0,
  801. parent = check_to_cc_id,
  802. type = 'virtual',
  803. description = '3-5 recipients',
  804. group = 'headers',
  805. }
  806. rspamd_config:register_symbol{
  807. name = 'RCPT_COUNT_FIVE',
  808. score = 0.0,
  809. parent = check_to_cc_id,
  810. type = 'virtual',
  811. description = '5-7 recipients',
  812. group = 'headers',
  813. }
  814. rspamd_config:register_symbol{
  815. name = 'RCPT_COUNT_SEVEN',
  816. score = 0.0,
  817. parent = check_to_cc_id,
  818. type = 'virtual',
  819. description = '7-11 recipients',
  820. group = 'headers',
  821. }
  822. rspamd_config:register_symbol{
  823. name = 'RCPT_COUNT_TWELVE',
  824. score = 0.0,
  825. parent = check_to_cc_id,
  826. type = 'virtual',
  827. description = '12-50 recipients',
  828. group = 'headers',
  829. }
  830. rspamd_config:register_symbol{
  831. name = 'RCPT_COUNT_GT_50',
  832. score = 0.0,
  833. parent = check_to_cc_id,
  834. type = 'virtual',
  835. description = '50+ recipients',
  836. group = 'headers',
  837. }
  838. rspamd_config:register_symbol{
  839. name = 'TO_DN_RECIPIENTS',
  840. score = 2.0,
  841. group = 'headers',
  842. parent = check_to_cc_id,
  843. type = 'virtual',
  844. description = 'To header display name is "Recipients"',
  845. }
  846. rspamd_config:register_symbol{
  847. name = 'TO_DN_NONE',
  848. score = 0.0,
  849. group = 'headers',
  850. parent = check_to_cc_id,
  851. type = 'virtual',
  852. description = 'None of the recipients have display names',
  853. }
  854. rspamd_config:register_symbol{
  855. name = 'TO_DN_ALL',
  856. score = 0.0,
  857. group = 'headers',
  858. parent = check_to_cc_id,
  859. type = 'virtual',
  860. description = 'All the recipients have display names',
  861. }
  862. rspamd_config:register_symbol{
  863. name = 'TO_DN_SOME',
  864. score = 0.0,
  865. group = 'headers',
  866. parent = check_to_cc_id,
  867. type = 'virtual',
  868. description = 'Some of the recipients have display names',
  869. }
  870. rspamd_config:register_symbol{
  871. name = 'TO_DN_EQ_ADDR_ALL',
  872. score = 0.0,
  873. group = 'headers',
  874. parent = check_to_cc_id,
  875. type = 'virtual',
  876. description = 'All of the recipients have display names that are the same as their address',
  877. }
  878. rspamd_config:register_symbol{
  879. name = 'TO_DN_EQ_ADDR_SOME',
  880. score = 0.0,
  881. group = 'headers',
  882. parent = check_to_cc_id,
  883. type = 'virtual',
  884. description = 'Some of the recipients have display names that are the same as their address',
  885. }
  886. rspamd_config:register_symbol{
  887. name = 'TO_MATCH_ENVRCPT_ALL',
  888. score = 0.0,
  889. group = 'headers',
  890. parent = check_to_cc_id,
  891. type = 'virtual',
  892. description = 'All of the recipients match the envelope',
  893. }
  894. rspamd_config:register_symbol{
  895. name = 'TO_MATCH_ENVRCPT_SOME',
  896. score = 0.0,
  897. group = 'headers',
  898. parent = check_to_cc_id,
  899. type = 'virtual',
  900. description = 'Some of the recipients match the envelope',
  901. }
  902. rspamd_config.CTYPE_MISSING_DISPOSITION = {
  903. callback = function(task)
  904. local parts = task:get_parts()
  905. if (not parts) or (parts and #parts < 1) then return false end
  906. for _,p in ipairs(parts) do
  907. local ct = p:get_header('Content-Type')
  908. if (ct and ct:lower():match('^application/octet%-stream') ~= nil) then
  909. local cd = p:get_header('Content-Disposition')
  910. if (not cd) or (cd and cd:lower():find('^attachment') == nil) then
  911. local ci = p:get_header('Content-ID')
  912. if ci or (#parts > 1 and (cd and cd:find('filename=.+%.asc') ~= nil))
  913. then
  914. return false
  915. end
  916. return true
  917. end
  918. end
  919. end
  920. return false
  921. end,
  922. description = 'Binary content-type not specified as an attachment',
  923. score = 4.0,
  924. group = 'headers'
  925. }
  926. rspamd_config.CTYPE_MIXED_BOGUS = {
  927. callback = function(task)
  928. local ct = task:get_header('Content-Type')
  929. if (not ct) then return false end
  930. local parts = task:get_parts()
  931. if (not parts) then return false end
  932. if (not ct:lower():match('^multipart/mixed')) then return false end
  933. local found = false
  934. -- Check each part and look for a part that isn't multipart/* or text/plain or text/html
  935. for _,p in ipairs(parts) do
  936. local pct = p:get_header('Content-Type')
  937. if (pct) then
  938. pct = pct:lower()
  939. if not ((pct:match('^multipart/') or
  940. pct:match('^text/plain') or
  941. pct:match('^text/html'))) then
  942. found = true
  943. end
  944. end
  945. end
  946. if (not found) then return true end
  947. return false
  948. end,
  949. description = 'multipart/mixed without non-textual part',
  950. score = 1.0,
  951. group = 'headers'
  952. }
  953. local function check_for_base64_text(part)
  954. local ct = part:get_header('Content-Type')
  955. if (not ct) then return false end
  956. ct = ct:lower()
  957. if (ct:match('^text')) then
  958. -- Check encoding
  959. local cte = part:get_header('Content-Transfer-Encoding')
  960. if (cte and cte:lower():match('^base64')) then
  961. return true
  962. end
  963. end
  964. return false
  965. end
  966. rspamd_config.MIME_BASE64_TEXT = {
  967. callback = function(task)
  968. -- Check outer part
  969. if (check_for_base64_text(task)) then
  970. return true
  971. else
  972. local parts = task:get_parts()
  973. if (not parts) then return false end
  974. -- Check each part and look for base64 encoded text parts
  975. for _, part in ipairs(parts) do
  976. if (check_for_base64_text(part)) then
  977. return true
  978. end
  979. end
  980. end
  981. return false
  982. end,
  983. description = 'Has text part encoded in base64',
  984. score = 0.1,
  985. group = 'headers'
  986. }
  987. local function is_8bit_addr(addr)
  988. if addr.flags and addr.flags['8bit'] then
  989. return true
  990. end
  991. return false;
  992. end
  993. rspamd_config.INVALID_FROM_8BIT = {
  994. callback = function(task)
  995. local from = (task:get_from('mime') or {})[1] or {}
  996. if is_8bit_addr(from) then
  997. return true
  998. end
  999. return false
  1000. end,
  1001. description = 'Invalid 8bit character in From header',
  1002. score = 6.0,
  1003. group = 'headers'
  1004. }
  1005. rspamd_config.INVALID_RCPT_8BIT = {
  1006. callback = function(task)
  1007. local rcpts = task:get_recipients('mime') or {}
  1008. return fun.any(function(rcpt)
  1009. if is_8bit_addr(rcpt) then
  1010. return true
  1011. end
  1012. return false
  1013. end, rcpts)
  1014. end,
  1015. description = 'Invalid 8bit character in recipients headers',
  1016. score = 6.0,
  1017. group = 'headers'
  1018. }
  1019. rspamd_config.XM_CASE = {
  1020. callback = function (task)
  1021. local xm = task:get_header('X-mailer', true)
  1022. if (xm) then return true end
  1023. end,
  1024. description = 'X-mailer .vs. X-Mailer',
  1025. score = 0.5,
  1026. group = 'headers',
  1027. }