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

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