You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

headers_checks.lua 30KB

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