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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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. local parts = task:get_parts()
  327. if not parts then return false end
  328. -- Make sure there is a MIME-Version header
  329. local mv = task:get_header('MIME-Version')
  330. local missing_mime = false
  331. if (not mv) then
  332. missing_mime = true
  333. end
  334. local found_ma = false
  335. local found_plain = false
  336. local found_html = false
  337. local cte_7bit = false
  338. for _,p in ipairs(parts) do
  339. local mtype,subtype = p:get_type()
  340. local ctype = mtype:lower() .. '/' .. subtype:lower()
  341. if (ctype == 'multipart/alternative') then
  342. found_ma = true
  343. end
  344. if (ctype == 'text/plain') then
  345. if p:get_cte() == '7bit' then
  346. cte_7bit = true
  347. end
  348. found_plain = true
  349. end
  350. if (ctype == 'text/html') then
  351. if p:get_cte() == '7bit' then
  352. cte_7bit = true
  353. end
  354. found_html = true
  355. end
  356. end
  357. if missing_mime then
  358. if not (not found_ma and ((found_plain or found_html) and cte_7bit)) then
  359. task:insert_result('MISSING_MIME_VERSION', 1.0)
  360. end
  361. end
  362. if (found_ma) then
  363. if (not found_plain) then
  364. task:insert_result('MIME_MA_MISSING_TEXT', 1.0)
  365. end
  366. if (not found_html) then
  367. task:insert_result('MIME_MA_MISSING_HTML', 1.0)
  368. end
  369. end
  370. end
  371. }
  372. rspamd_config:register_symbol{
  373. name = 'MISSING_MIME_VERSION',
  374. score = 2.0,
  375. parent = check_mime_id,
  376. type = 'virtual',
  377. description = 'MIME-Version header is missing',
  378. group = 'headers',
  379. }
  380. rspamd_config:register_symbol{
  381. name = 'MIME_MA_MISSING_TEXT',
  382. score = 2.0,
  383. parent = check_mime_id,
  384. type = 'virtual',
  385. description = 'MIME multipart/alternative missing text/plain part',
  386. group = 'headers',
  387. }
  388. rspamd_config:register_symbol{
  389. name = 'MIME_MA_MISSING_HTML',
  390. score = 1.0,
  391. parent = check_mime_id,
  392. type = 'virtual',
  393. description = 'MIME multipart/alternative missing text/html part',
  394. group = 'headers',
  395. }
  396. -- Used to be called IS_LIST
  397. rspamd_config.PREVIOUSLY_DELIVERED = {
  398. callback = function(task)
  399. if not task:has_recipients(2) then return false end
  400. local to = task:get_recipients(2)
  401. local rcvds = task:get_header_full('Received')
  402. if not rcvds then return false end
  403. for _, rcvd in ipairs(rcvds) do
  404. local _,_,addr = rcvd['decoded']:lower():find("%sfor%s<(.-)>")
  405. if addr then
  406. for _, toa in ipairs(to) do
  407. if toa and toa.addr:lower() == addr then
  408. return true, addr
  409. end
  410. end
  411. return false
  412. end
  413. end
  414. end,
  415. description = 'Message either to a list or was forwarded',
  416. group = 'headers',
  417. score = 0.0
  418. }
  419. rspamd_config.BROKEN_HEADERS = {
  420. callback = function(task)
  421. return task:has_flag('broken_headers')
  422. end,
  423. score = 10.0,
  424. group = 'headers',
  425. description = 'Headers structure is likely broken'
  426. }
  427. rspamd_config.BROKEN_CONTENT_TYPE = {
  428. callback = function(task)
  429. return fun.any(function(p) return p:is_broken() end,
  430. task:get_parts())
  431. end,
  432. score = 1.5,
  433. group = 'headers',
  434. description = 'Message has part with broken content type'
  435. }
  436. rspamd_config.HEADER_RCONFIRM_MISMATCH = {
  437. callback = function (task)
  438. local header_from = nil
  439. local cread = task:get_header('X-Confirm-Reading-To')
  440. if task:has_from('mime') then
  441. header_from = task:get_from('mime')[1]
  442. end
  443. local header_cread = nil
  444. if cread then
  445. local headers_cread = util.parse_mail_address(cread, task:get_mempool())
  446. if headers_cread then header_cread = headers_cread[1] end
  447. end
  448. if header_from and header_cread then
  449. if not string.find(header_from['addr'], header_cread['addr']) then
  450. return true
  451. end
  452. end
  453. return false
  454. end,
  455. score = 2.0,
  456. group = 'headers',
  457. description = 'Read confirmation address is different to from address'
  458. }
  459. rspamd_config.HEADER_FORGED_MDN = {
  460. callback = function (task)
  461. local mdn = task:get_header('Disposition-Notification-To')
  462. if not mdn then return false end
  463. local header_rp = nil
  464. if task:has_from('smtp') then
  465. header_rp = task:get_from('smtp')[1]
  466. end
  467. -- Parse mail addr
  468. local headers_mdn = util.parse_mail_address(mdn, task:get_mempool())
  469. if headers_mdn and not header_rp then return true end
  470. if header_rp and not headers_mdn then return false end
  471. if not headers_mdn and not header_rp then return false end
  472. local found_match = false
  473. for _, h in ipairs(headers_mdn) do
  474. if util.strequal_caseless(h['addr'], header_rp['addr']) then
  475. found_match = true
  476. break
  477. end
  478. end
  479. return (not found_match)
  480. end,
  481. score = 2.0,
  482. group = 'headers',
  483. description = 'Read confirmation address is different to return path'
  484. }
  485. local headers_unique = {
  486. ['Content-Type'] = 1.0,
  487. ['Content-Transfer-Encoding'] = 1.0,
  488. -- https://tools.ietf.org/html/rfc5322#section-3.6
  489. ['Date'] = 0.1,
  490. ['From'] = 1.0,
  491. ['Sender'] = 1.0,
  492. ['Reply-To'] = 1.0,
  493. ['To'] = 0.2,
  494. ['Cc'] = 0.1,
  495. ['Bcc'] = 0.1,
  496. ['Message-ID'] = 0.7,
  497. ['In-Reply-To'] = 0.7,
  498. ['References'] = 0.3,
  499. ['Subject'] = 0.7
  500. }
  501. rspamd_config.MULTIPLE_UNIQUE_HEADERS = {
  502. callback = function(task)
  503. local res = 0
  504. local max_mult = 0.0
  505. local res_tbl = {}
  506. for hdr,mult in pairs(headers_unique) do
  507. local hc = task:get_header_count(hdr)
  508. if hc > 1 then
  509. res = res + 1
  510. table.insert(res_tbl, hdr)
  511. if max_mult < mult then
  512. max_mult = mult
  513. end
  514. end
  515. end
  516. if res > 0 then
  517. return true,max_mult,table.concat(res_tbl, ',')
  518. end
  519. return false
  520. end,
  521. score = 7.0,
  522. group = 'headers',
  523. one_shot = true,
  524. description = 'Repeated unique headers'
  525. }
  526. rspamd_config.MISSING_FROM = {
  527. callback = function(task)
  528. local from = task:get_header('From')
  529. if from == nil or from == '' then
  530. return true
  531. end
  532. return false
  533. end,
  534. score = 2.0,
  535. group = 'headers',
  536. description = 'Missing From: header'
  537. }
  538. rspamd_config.MULTIPLE_FROM = {
  539. callback = function(task)
  540. local from = task:get_from('mime')
  541. if from and from[1] then
  542. if #from > 1 then
  543. return true,1.0,table.concat(
  544. fun.totable(
  545. fun.map(function(a) return a.addr end,
  546. fun.filter(function(a) return a.addr and a.addr ~= '' end,
  547. from))),
  548. ',')
  549. end
  550. end
  551. return false
  552. end,
  553. score = 9.0,
  554. group = 'headers',
  555. description = 'Multiple addresses in From'
  556. }
  557. rspamd_config.MV_CASE = {
  558. callback = function (task)
  559. local mv = task:get_header('Mime-Version', true)
  560. if (mv) then return true end
  561. end,
  562. description = 'Mime-Version .vs. MIME-Version',
  563. score = 0.5,
  564. group = 'headers'
  565. }
  566. rspamd_config.FAKE_REPLY = {
  567. callback = function (task)
  568. local subject = task:get_header('Subject')
  569. if (subject and subject:lower():find('^re:')) then
  570. local ref = task:get_header('References')
  571. local rt = task:get_header('In-Reply-To')
  572. if (not (ref or rt)) then return true end
  573. end
  574. return false
  575. end,
  576. description = 'Fake reply',
  577. score = 1.0,
  578. group = 'headers'
  579. }
  580. local check_from_id = rspamd_config:register_symbol{
  581. name = 'CHECK_FROM',
  582. type = 'callback',
  583. score = 0.0,
  584. group = 'headers',
  585. callback = function(task)
  586. local envfrom = task:get_from(1)
  587. local from = task:get_from(2)
  588. if (from and from[1] and (from[1].name == nil or from[1].name == '' )) then
  589. task:insert_result('FROM_NO_DN', 1.0)
  590. elseif (from and from[1] and from[1].name and
  591. util.strequal_caseless(from[1].name, from[1].addr)) then
  592. task:insert_result('FROM_DN_EQ_ADDR', 1.0)
  593. elseif (from and from[1] and from[1].name and from[1].name ~= '') then
  594. task:insert_result('FROM_HAS_DN', 1.0)
  595. -- Look for Mr/Mrs/Dr titles
  596. local n = from[1].name:lower()
  597. local match, match_end
  598. match, match_end = n:find('^mrs?[%.%s]')
  599. if match then
  600. task:insert_result('FROM_NAME_HAS_TITLE', 1.0, n:sub(match, match_end-1))
  601. end
  602. match, match_end = n:find('^dr[%.%s]')
  603. if match then
  604. task:insert_result('FROM_NAME_HAS_TITLE', 1.0, n:sub(match, match_end-1))
  605. end
  606. -- Check for excess spaces
  607. if n:find('%s%s') then
  608. task:insert_result('FROM_NAME_EXCESS_SPACE', 1.0)
  609. end
  610. end
  611. if (envfrom and from and envfrom[1] and from[1] and
  612. util.strequal_caseless(envfrom[1].addr, from[1].addr))
  613. then
  614. task:insert_result('FROM_EQ_ENVFROM', 1.0)
  615. elseif (envfrom and envfrom[1] and envfrom[1].addr) then
  616. task:insert_result('FROM_NEQ_ENVFROM', 1.0, ((from or E)[1] or E).addr or '', envfrom[1].addr)
  617. end
  618. local to = task:get_recipients(2)
  619. if not (to and to[1] and #to == 1 and from and from[1]) then return false end
  620. -- Check if FROM == TO
  621. if (util.strequal_caseless(to[1].addr, from[1].addr)) then
  622. task:insert_result('TO_EQ_FROM', 1.0)
  623. elseif (to[1].domain and from[1].domain and
  624. util.strequal_caseless(to[1].domain, from[1].domain))
  625. then
  626. task:insert_result('TO_DOM_EQ_FROM_DOM', 1.0)
  627. end
  628. end
  629. }
  630. rspamd_config:register_symbol{
  631. name = 'FROM_NO_DN',
  632. score = 0.0,
  633. group = 'headers',
  634. parent = check_from_id,
  635. type = 'virtual',
  636. description = 'From header does not have a display name',
  637. }
  638. rspamd_config:register_symbol{
  639. name = 'FROM_DN_EQ_ADDR',
  640. score = 1.0,
  641. group = 'headers',
  642. parent = check_from_id,
  643. type = 'virtual',
  644. description = 'From header display name is the same as the address',
  645. }
  646. rspamd_config:register_symbol{
  647. name = 'FROM_HAS_DN',
  648. score = 0.0,
  649. group = 'headers',
  650. parent = check_from_id,
  651. type = 'virtual',
  652. description = 'From header has a display name',
  653. }
  654. rspamd_config:register_symbol{
  655. name = 'FROM_NAME_EXCESS_SPACE',
  656. score = 1.0,
  657. group = 'headers',
  658. parent = check_from_id,
  659. type = 'virtual',
  660. description = 'From header display name contains excess whitespace',
  661. }
  662. rspamd_config:register_symbol{
  663. name = 'FROM_NAME_HAS_TITLE',
  664. score = 1.0,
  665. group = 'headers',
  666. parent = check_from_id,
  667. type = 'virtual',
  668. description = 'From header display name has a title (Mr/Mrs/Dr)',
  669. }
  670. rspamd_config:register_symbol{
  671. name = 'FROM_EQ_ENVFROM',
  672. score = 0.0,
  673. group = 'headers',
  674. parent = check_from_id,
  675. type = 'virtual',
  676. description = 'From address is the same as the envelope',
  677. }
  678. rspamd_config:register_symbol{
  679. name = 'FROM_NEQ_ENVFROM',
  680. score = 0.0,
  681. group = 'headers',
  682. parent = check_from_id,
  683. type = 'virtual',
  684. description = 'From address is different to the envelope',
  685. }
  686. rspamd_config:register_symbol{
  687. name = 'TO_EQ_FROM',
  688. score = 0.0,
  689. group = 'headers',
  690. parent = check_from_id,
  691. type = 'virtual',
  692. description = 'To address matches the From address',
  693. }
  694. rspamd_config:register_symbol{
  695. name = 'TO_DOM_EQ_FROM_DOM',
  696. score = 0.0,
  697. group = 'headers',
  698. parent = check_from_id,
  699. type = 'virtual',
  700. description = 'To domain is the same as the From domain',
  701. }
  702. local check_to_cc_id = rspamd_config:register_symbol{
  703. name = 'CHECK_TO_CC',
  704. type = 'callback',
  705. score = 0.0,
  706. group = 'headers,mime',
  707. callback = function(task)
  708. local rcpts = task:get_recipients(1)
  709. local to = task:get_recipients(2)
  710. local to_match_envrcpt = 0
  711. local cnts = {
  712. [1] = 'ONE',
  713. [2] = 'TWO',
  714. [3] = 'THREE',
  715. [5] = 'FIVE',
  716. [7] = 'SEVEN',
  717. [12] = 'TWELVE',
  718. [50] = 'GT_50'
  719. }
  720. local def = 'ZERO'
  721. if (not to) then return false end
  722. -- Add symbol for recipient count
  723. local nrcpt = #to
  724. for k,v in pairs(cnts) do
  725. if nrcpt >= tonumber(k) then
  726. def = v
  727. end
  728. end
  729. task:insert_result('RCPT_COUNT_' .. def, 1.0, tostring(nrcpt))
  730. -- Check for display names
  731. local to_dn_count = 0
  732. local to_dn_eq_addr_count = 0
  733. for _, toa in ipairs(to) do
  734. -- To: Recipients <noreply@dropbox.com>
  735. if (toa['name'] and (toa['name']:lower() == 'recipient'
  736. or toa['name']:lower() == 'recipients')) then
  737. task:insert_result('TO_DN_RECIPIENTS', 1.0)
  738. end
  739. if (toa['name'] and util.strequal_caseless(toa['name'], toa['addr'])) then
  740. to_dn_eq_addr_count = to_dn_eq_addr_count + 1
  741. elseif (toa['name'] and toa['name'] ~= '') then
  742. to_dn_count = to_dn_count + 1
  743. end
  744. -- See if header recipients match envrcpts
  745. if (rcpts) then
  746. for _, rcpt in ipairs(rcpts) do
  747. if (toa and toa['addr'] and rcpt and rcpt['addr'] and
  748. util.strequal_caseless(rcpt['addr'], toa['addr']))
  749. then
  750. to_match_envrcpt = to_match_envrcpt + 1
  751. end
  752. end
  753. end
  754. end
  755. if (to_dn_count == 0 and to_dn_eq_addr_count == 0) then
  756. task:insert_result('TO_DN_NONE', 1.0)
  757. elseif (to_dn_count == #to) then
  758. task:insert_result('TO_DN_ALL', 1.0)
  759. elseif (to_dn_count > 0) then
  760. task:insert_result('TO_DN_SOME', 1.0)
  761. end
  762. if (to_dn_eq_addr_count == #to) then
  763. task:insert_result('TO_DN_EQ_ADDR_ALL', 1.0)
  764. elseif (to_dn_eq_addr_count > 0) then
  765. task:insert_result('TO_DN_EQ_ADDR_SOME', 1.0)
  766. end
  767. -- See if header recipients match envelope recipients
  768. if (to_match_envrcpt == #to) then
  769. task:insert_result('TO_MATCH_ENVRCPT_ALL', 1.0)
  770. elseif (to_match_envrcpt > 0) then
  771. task:insert_result('TO_MATCH_ENVRCPT_SOME', 1.0)
  772. end
  773. end
  774. }
  775. rspamd_config:register_symbol{
  776. name = 'RCPT_COUNT_ZERO',
  777. score = 0.0,
  778. parent = check_to_cc_id,
  779. type = 'virtual',
  780. description = 'No recipients',
  781. group = 'headers',
  782. }
  783. rspamd_config:register_symbol{
  784. name = 'RCPT_COUNT_ONE',
  785. score = 0.0,
  786. parent = check_to_cc_id,
  787. type = 'virtual',
  788. description = 'One recipient',
  789. group = 'headers',
  790. }
  791. rspamd_config:register_symbol{
  792. name = 'RCPT_COUNT_TWO',
  793. score = 0.0,
  794. parent = check_to_cc_id,
  795. type = 'virtual',
  796. description = 'Two recipients',
  797. group = 'headers',
  798. }
  799. rspamd_config:register_symbol{
  800. name = 'RCPT_COUNT_THREE',
  801. score = 0.0,
  802. parent = check_to_cc_id,
  803. type = 'virtual',
  804. description = '3-5 recipients',
  805. group = 'headers',
  806. }
  807. rspamd_config:register_symbol{
  808. name = 'RCPT_COUNT_FIVE',
  809. score = 0.0,
  810. parent = check_to_cc_id,
  811. type = 'virtual',
  812. description = '5-7 recipients',
  813. group = 'headers',
  814. }
  815. rspamd_config:register_symbol{
  816. name = 'RCPT_COUNT_SEVEN',
  817. score = 0.0,
  818. parent = check_to_cc_id,
  819. type = 'virtual',
  820. description = '7-11 recipients',
  821. group = 'headers',
  822. }
  823. rspamd_config:register_symbol{
  824. name = 'RCPT_COUNT_TWELVE',
  825. score = 0.0,
  826. parent = check_to_cc_id,
  827. type = 'virtual',
  828. description = '12-50 recipients',
  829. group = 'headers',
  830. }
  831. rspamd_config:register_symbol{
  832. name = 'RCPT_COUNT_GT_50',
  833. score = 0.0,
  834. parent = check_to_cc_id,
  835. type = 'virtual',
  836. description = '50+ recipients',
  837. group = 'headers',
  838. }
  839. rspamd_config:register_symbol{
  840. name = 'TO_DN_RECIPIENTS',
  841. score = 2.0,
  842. group = 'headers',
  843. parent = check_to_cc_id,
  844. type = 'virtual',
  845. description = 'To header display name is "Recipients"',
  846. }
  847. rspamd_config:register_symbol{
  848. name = 'TO_DN_NONE',
  849. score = 0.0,
  850. group = 'headers',
  851. parent = check_to_cc_id,
  852. type = 'virtual',
  853. description = 'None of the recipients have display names',
  854. }
  855. rspamd_config:register_symbol{
  856. name = 'TO_DN_ALL',
  857. score = 0.0,
  858. group = 'headers',
  859. parent = check_to_cc_id,
  860. type = 'virtual',
  861. description = 'All the recipients have display names',
  862. }
  863. rspamd_config:register_symbol{
  864. name = 'TO_DN_SOME',
  865. score = 0.0,
  866. group = 'headers',
  867. parent = check_to_cc_id,
  868. type = 'virtual',
  869. description = 'Some of the recipients have display names',
  870. }
  871. rspamd_config:register_symbol{
  872. name = 'TO_DN_EQ_ADDR_ALL',
  873. score = 0.0,
  874. group = 'headers',
  875. parent = check_to_cc_id,
  876. type = 'virtual',
  877. description = 'All of the recipients have display names that are the same as their address',
  878. }
  879. rspamd_config:register_symbol{
  880. name = 'TO_DN_EQ_ADDR_SOME',
  881. score = 0.0,
  882. group = 'headers',
  883. parent = check_to_cc_id,
  884. type = 'virtual',
  885. description = 'Some of the recipients have display names that are the same as their address',
  886. }
  887. rspamd_config:register_symbol{
  888. name = 'TO_MATCH_ENVRCPT_ALL',
  889. score = 0.0,
  890. group = 'headers',
  891. parent = check_to_cc_id,
  892. type = 'virtual',
  893. description = 'All of the recipients match the envelope',
  894. }
  895. rspamd_config:register_symbol{
  896. name = 'TO_MATCH_ENVRCPT_SOME',
  897. score = 0.0,
  898. group = 'headers',
  899. parent = check_to_cc_id,
  900. type = 'virtual',
  901. description = 'Some of the recipients match the envelope',
  902. }
  903. -- TODO: rewrite this rule, it should not touch headers directly
  904. rspamd_config.CTYPE_MISSING_DISPOSITION = {
  905. callback = function(task)
  906. local parts = task:get_parts()
  907. if (not parts) or (parts and #parts < 1) then return false end
  908. for _,p in ipairs(parts) do
  909. local ct = p:get_header('Content-Type')
  910. if (ct and ct:lower():match('^application/octet%-stream') ~= nil) then
  911. local cd = p:get_header('Content-Disposition')
  912. if (not cd) or (cd and cd:lower():find('^attachment') == nil) then
  913. local ci = p:get_header('Content-ID')
  914. if ci or (#parts > 1 and (cd and cd:find('filename=.+%.asc') ~= nil))
  915. then
  916. return false
  917. end
  918. local parent = p:get_parent()
  919. if parent then
  920. local t,st = parent:get_type()
  921. if t == 'multipart' and st == 'encrypted' then
  922. -- Special case
  923. return false
  924. end
  925. end
  926. return true
  927. end
  928. end
  929. end
  930. return false
  931. end,
  932. description = 'Binary content-type not specified as an attachment',
  933. score = 4.0,
  934. group = 'mime'
  935. }
  936. rspamd_config.CTYPE_MIXED_BOGUS = {
  937. callback = function(task)
  938. local ct = task:get_header('Content-Type')
  939. if (not ct) then return false end
  940. local parts = task:get_parts()
  941. if (not parts) then return false end
  942. if (not ct:lower():match('^multipart/mixed')) then return false end
  943. local found = false
  944. -- Check each part and look for a part that isn't multipart/* or text/plain or text/html
  945. for _,p in ipairs(parts) do
  946. local pct = p:get_header('Content-Type')
  947. if (pct) then
  948. pct = pct:lower()
  949. if not ((pct:match('^multipart/') or
  950. pct:match('^text/plain') or
  951. pct:match('^text/html'))) then
  952. found = true
  953. end
  954. end
  955. end
  956. if (not found) then return true end
  957. return false
  958. end,
  959. description = 'multipart/mixed without non-textual part',
  960. score = 1.0,
  961. group = 'mime'
  962. }
  963. local function check_for_base64_text(part)
  964. local ct = part:get_header('Content-Type')
  965. if (not ct) then return false end
  966. ct = ct:lower()
  967. if (ct:match('^text')) then
  968. -- Check encoding
  969. local cte = part:get_header('Content-Transfer-Encoding')
  970. if (cte and cte:lower():match('^base64')) then
  971. return true
  972. end
  973. end
  974. return false
  975. end
  976. rspamd_config.MIME_BASE64_TEXT = {
  977. callback = function(task)
  978. -- Check outer part
  979. if (check_for_base64_text(task)) then
  980. return true
  981. else
  982. local parts = task:get_parts()
  983. if (not parts) then return false end
  984. -- Check each part and look for base64 encoded text parts
  985. for _, part in ipairs(parts) do
  986. if (check_for_base64_text(part)) then
  987. return true
  988. end
  989. end
  990. end
  991. return false
  992. end,
  993. description = 'Has text part encoded in base64',
  994. score = 0.1,
  995. group = 'mime'
  996. }
  997. rspamd_config.MIME_BASE64_TEXT_BOGUS = {
  998. callback = function(task)
  999. local parts = task:get_text_parts()
  1000. if (not parts) then return false end
  1001. -- Check each part and look for base64 encoded text parts
  1002. -- where the part does not have any 8bit characters within it
  1003. for _, part in ipairs(parts) do
  1004. local mimepart = part:get_mimepart();
  1005. if (check_for_base64_text(mimepart) and not part:has_8bit()) then
  1006. return true
  1007. end
  1008. end
  1009. return false
  1010. end,
  1011. description = 'Has text part encoded in base64 that does not contain any 8bit characters',
  1012. score = 1.0,
  1013. group = 'mime'
  1014. }
  1015. local function is_8bit_addr(addr)
  1016. if addr.flags and addr.flags['8bit'] then
  1017. return true
  1018. end
  1019. return false;
  1020. end
  1021. rspamd_config.INVALID_FROM_8BIT = {
  1022. callback = function(task)
  1023. local from = (task:get_from('mime') or {})[1] or {}
  1024. if is_8bit_addr(from) then
  1025. return true
  1026. end
  1027. return false
  1028. end,
  1029. description = 'Invalid 8bit character in From header',
  1030. score = 6.0,
  1031. group = 'headers'
  1032. }
  1033. rspamd_config.INVALID_RCPT_8BIT = {
  1034. callback = function(task)
  1035. local rcpts = task:get_recipients('mime') or {}
  1036. return fun.any(function(rcpt)
  1037. if is_8bit_addr(rcpt) then
  1038. return true
  1039. end
  1040. return false
  1041. end, rcpts)
  1042. end,
  1043. description = 'Invalid 8bit character in recipients headers',
  1044. score = 6.0,
  1045. group = 'headers'
  1046. }
  1047. rspamd_config.XM_CASE = {
  1048. callback = function (task)
  1049. local xm = task:get_header('X-mailer', true)
  1050. if (xm) then return true end
  1051. end,
  1052. description = 'X-mailer .vs. X-Mailer',
  1053. score = 0.5,
  1054. group = 'headers'
  1055. }