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

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