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 30KB

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