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

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