[{"data":1,"prerenderedAt":474},["ShallowReactive",2],{"article-20210416-rspamd-forwarding-spam":3},{"article":4,"surround":463},{"id":5,"title":6,"authorKey":7,"body":8,"codeLinks":417,"createdAt":418,"description":419,"excerpt":420,"extension":450,"image":451,"meta":452,"navigation":453,"path":454,"published":455,"readingTime":267,"revisedAt":417,"seo":456,"stem":457,"tags":458,"__hash__":462},"articles\u002Farticles\u002F20210416-rspamd-forwarding-spam.md","Block email forwarding spam with Rspamd","philip",{"type":9,"value":10,"toc":412},"minimark",[11,48,65,77,82,85,92,117,121,124,172,175,179,190,200,214,271,274,286,375,378,405,408],[12,13,14,15,22,23,27,28,31,32,34,35,37,38,43,44,47],"p",{},"At ",[16,17,21],"a",{"href":18,"rel":19},"https:\u002F\u002Fwww.onlime.ch\u002F",[20],"nofollow","Onlime GmbH"," we have a mail infrastructure that consists of 3 mail servers: ",[24,25,26],"code",{},"mx1","  acts as primary MX server and provides SMTP as outgoing mail gateway for our customers. ",[24,29,30],{},"mx2"," acts as secondary MX and fallback incoming mailserver. Incoming email from ",[24,33,30],{}," is forwarded to ",[24,36,26],{}," which does spam\u002Fantivirus filtering with ",[16,39,42],{"href":40,"rel":41},"https:\u002F\u002Frspamd.com\u002F",[20],"Rspamd",". Finally, the 3rd mailserver which is simply called ",[24,45,46],{},"mail"," acts as IMAP server and outgoing mailserver.",[12,49,50,51,53,54,57,58,60,61,64],{},"We allow customers to set up email forwardings on their domains. A forwarding address could have another customer email address as destination, but could also directly or indirectly (through another forwarding) point to an external email address. Spam filtering is done on ",[24,52,26],{},", but Rspamd actually just flags the email as ham\u002Fspam with a spam score and adds the ",[24,55,56],{},"X-Spamd-Result"," header with all symbols. The actual action is done on the final mailserver ",[24,59,46],{}," in the recipients mailbox via Sieve rule. The reason for this is that we want to let the customer define his own spam score (going from \"minimal\" to \"radical\" which maps to a spam score treshold) and what should happen with an email that got classified as spam (store it in ",[24,62,63],{},"Spam"," box or discard it directly).",[12,66,67,68,73,74,76],{},"So, how can we block an incoming spam email that would get forwarded to some external address, without introducing any ",[16,69,72],{"href":70,"rel":71},"https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FBackscatter_(email)",[20],"Backscattering","? We need to block incoming email already radically on the first\u002Fincoming mail server ",[24,75,26],{}," if it will be forwarded to some external email address.",[78,79,81],"h2",{"id":80},"mail-infrastructure-recap","Mail Infrastructure Recap",[12,83,84],{},"If you didn't quite get the description of our mailserver infrastructure from my intro above, I try to explain in some more visual way:",[12,86,87],{},[88,89],"img",{"alt":90,"src":91},"2021-mail-infra-overview","\u002Fimages\u002Farticles\u002F20210416-rspamd-forwarding-spam\u002F2021-mail-infra-overview.png",[12,93,94,95,97,98,100,101,106,107,110,111,113,114,116],{},"So, again, Rspamd on ",[24,96,26],{}," just flags the messages with a spam score, but the actual blocking of spam is done on ",[24,99,46],{}," through a Sieve rule in the final mailbox (on ",[16,102,105],{"href":103,"rel":104},"https:\u002F\u002Fdovecot.org\u002F",[20],"Dovecot IMAP server","). If we would block an outgoing forwarding spam mail there, that would result in backscattering, as the email was not rejected with a ",[24,108,109],{},"5xx"," status code on the first mailserver ",[24,112,26],{},". How can we block it already on ",[24,115,26],{}," without introducing any added complexity in our mail infrastructure? See the problem?",[78,118,120],{"id":119},"the-idea","The Idea",[12,122,123],{},"Proposed solution:",[125,126,127,134,153],"ul",{},[128,129,130,131],"li",{},"Deploy a list of forwarding emails (that point directly or indirectly to some external address) to a Rspamd map ",[24,132,133],{},"forwardings.inc.local",[128,135,136,137,142,143,146,147,149,150],{},"Use ",[16,138,141],{"href":139,"rel":140},"https:\u002F\u002Frspamd.com\u002Fdoc\u002Fmodules\u002Fmultimap.html",[20],"Rspamd mutimap module"," to tag all emails with ",[24,144,145],{},"rcpt"," (map type) in ",[24,148,133],{}," with symbol ",[24,151,152],{},"IS_FORWARDING",[128,154,136,155,160,161,164,165,167,168,171],{},[16,156,159],{"href":157,"rel":158},"https:\u002F\u002Frspamd.com\u002Fdoc\u002Fmodules\u002Fforce_actions.html",[20],"Rspamd force actions"," to override default action with ",[24,162,163],{},"reject"," if the ",[24,166,152],{}," symbol is set and ",[24,169,170],{},"X-Spam: Yes"," header was set",[12,173,174],{},"Like this, we would reject directly on mx1\u002Fmx2\u002Fmailman as frontend mailservers, not generating any bounces \u002F backscattering!",[78,176,178],{"id":177},"rspamd-force-action","Rspamd force action",[12,180,181,182,185,186,189],{},"That solution was quite easy to implement. The only tricky part was to generate a list of forwarding email addresses that pointed directly or indirectly (potentially recursively over multiple forwardings) to some external address. This is your job on application level. I put this list into Rspamd's ",[24,183,184],{},"local.d\u002Fmaps.d\u002Fforwardings.inc.local"," and keep it updated through our controlpanel, deploying it via an agent\u002Fmicroservice on the frontend mailservers. Whenever a customer creates a new forwarding that points to some external email address, this list is regenerated and deployed to the frontend mailservers ",[24,187,188],{},"mx1\u002Fmx2\u002Fmailman",". It contains one address per line:",[191,192,198],"pre",{"className":193,"code":195,"filename":184,"language":196,"meta":197},[194],"language-text","forwarding1@example.com\nforwarding2@example.com\n...\n","text","",[24,199,195],{"__ignoreMap":197},[12,201,202,203,207,208,210,211,213],{},"Rspamd ",[16,204,206],{"href":139,"rel":205},[20],"multimap module"," now adds symbol ",[24,209,152],{}," (which should be unterstood as \"is forwarding to some external recipient\") if the recipient was found in ",[24,212,133],{},":",[191,215,220],{"className":216,"code":217,"filename":218,"language":219,"meta":197,"style":197},"language-ruby shiki shiki-themes github-light-default github-light-default github-dark-default","IS_FORWARDING {\n    type = \"rcpt\";\n    map = \"\u002Fetc\u002Frspamd\u002Flocal.d\u002Fmaps.d\u002Fforwardings.inc.local\";\n}\n","local.d\u002Fmultimap.conf","ruby",[24,221,222,234,252,265],{"__ignoreMap":197},[223,224,227,230],"span",{"class":225,"line":226},"line",1,[223,228,152],{"class":229},"sDkKp",[223,231,233],{"class":232},"s_bOF"," {\n",[223,235,237,241,245,249],{"class":225,"line":236},2,[223,238,240],{"class":239},"sQJ0q","    type",[223,242,244],{"class":243},"s5nWI"," =",[223,246,248],{"class":247},"s-BGO"," \"rcpt\"",[223,250,251],{"class":232},";\n",[223,253,255,258,260,263],{"class":225,"line":254},3,[223,256,257],{"class":239},"    map",[223,259,244],{"class":243},[223,261,262],{"class":247}," \"\u002Fetc\u002Frspamd\u002Flocal.d\u002Fmaps.d\u002Fforwardings.inc.local\"",[223,264,251],{"class":232},[223,266,268],{"class":225,"line":267},4,[223,269,270],{"class":232},"}\n",[12,272,273],{},"In case you were wondering: No, it is not required to restart\u002Freload Rspamd when this map content changes!",[12,275,276,277,281,282,285],{},"Finally, we configure the Rspamd ",[16,278,280],{"href":157,"rel":279},[20],"force actions module"," in ",[24,283,284],{},"force_actions.conf"," as follows:",[191,287,290],{"className":216,"code":288,"filename":289,"language":219,"meta":197,"style":197},"rules {\n  SPAMMY_FORWARDING {\n    action = \"reject\";\n    expression = \"IS_FORWARDING\";\n    # require_action setting defines actions that will be overridden\n    require_action = [\"add header\"];\n    # default message: \"Spam message rejected\"\n    #message = \"This message cannot be sent because it looks like spam.\"\n  }\n}\n","local.d\u002Fforce_actions.conf",[24,291,292,297,304,316,328,335,352,358,364,370],{"__ignoreMap":197},[223,293,294],{"class":225,"line":226},[223,295,296],{"class":232},"rules {\n",[223,298,299,302],{"class":225,"line":236},[223,300,301],{"class":229},"  SPAMMY_FORWARDING",[223,303,233],{"class":232},[223,305,306,309,311,314],{"class":225,"line":254},[223,307,308],{"class":239},"    action",[223,310,244],{"class":243},[223,312,313],{"class":247}," \"reject\"",[223,315,251],{"class":232},[223,317,318,321,323,326],{"class":225,"line":267},[223,319,320],{"class":239},"    expression",[223,322,244],{"class":243},[223,324,325],{"class":247}," \"IS_FORWARDING\"",[223,327,251],{"class":232},[223,329,331],{"class":225,"line":330},5,[223,332,334],{"class":333},"s9BHP","    # require_action setting defines actions that will be overridden\n",[223,336,338,341,343,346,349],{"class":225,"line":337},6,[223,339,340],{"class":239},"    require_action",[223,342,244],{"class":243},[223,344,345],{"class":232}," [",[223,347,348],{"class":247},"\"add header\"",[223,350,351],{"class":232},"];\n",[223,353,355],{"class":225,"line":354},7,[223,356,357],{"class":333},"    # default message: \"Spam message rejected\"\n",[223,359,361],{"class":225,"line":360},8,[223,362,363],{"class":333},"    #message = \"This message cannot be sent because it looks like spam.\"\n",[223,365,367],{"class":225,"line":366},9,[223,368,369],{"class":232},"  }\n",[223,371,373],{"class":225,"line":372},10,[223,374,270],{"class":232},[12,376,377],{},"You could also override the default message \"Spam message rejected\" there, see my comment.",[379,380,381],"blockquote",{},[12,382,383,387,388,390,391,394,395,397,398,400,401,404],{},[384,385,386],"strong",{},"Further explanation:"," The ",[24,389,170],{}," header is set by Rspamd if the calculated spam score was above the default ",[24,392,393],{},"add_header = 6.0"," action configuration. Above force action only comes into play, if that header was set AND the ",[24,396,152],{}," symol is there. And because we didn't define any score for ",[24,399,152],{}," symbol, it will just be ",[24,402,403],{},"0.00",", not influencing the whole spam score calculation. Think of this symbol just as a flag, not as any spam\u002Fham symbol.",[12,406,407],{},"That's the whole trick!",[409,410,411],"style",{},"html pre.shiki code .sDkKp, html code.shiki .sDkKp{--shiki-light:#0550AE;--shiki-default:#0550AE;--shiki-dark:#79C0FF}html pre.shiki code .s_bOF, html code.shiki .s_bOF{--shiki-light:#1F2328;--shiki-default:#1F2328;--shiki-dark:#E6EDF3}html pre.shiki code .sQJ0q, html code.shiki .sQJ0q{--shiki-light:#953800;--shiki-default:#953800;--shiki-dark:#FFA657}html pre.shiki code .s5nWI, html code.shiki .s5nWI{--shiki-light:#CF222E;--shiki-default:#CF222E;--shiki-dark:#FF7B72}html pre.shiki code .s-BGO, html code.shiki .s-BGO{--shiki-light:#0A3069;--shiki-default:#0A3069;--shiki-dark:#A5D6FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s9BHP, html code.shiki .s9BHP{--shiki-light:#6E7781;--shiki-default:#6E7781;--shiki-dark:#8B949E}",{"title":197,"searchDepth":236,"depth":254,"links":413},[414,415,416],{"id":80,"depth":236,"text":81},{"id":119,"depth":236,"text":120},{"id":177,"depth":236,"text":178},null,"2021-04-16","Apply spam filtering to outgoing email forwardings with Rspamd.",{"type":9,"value":421},[422,440],[12,423,14,424,22,427,27,429,31,431,34,433,37,435,43,438,47],{},[16,425,21],{"href":18,"rel":426},[20],[24,428,26],{},[24,430,30],{},[24,432,30],{},[24,434,26],{},[16,436,42],{"href":40,"rel":437},[20],[24,439,46],{},[12,441,50,442,53,444,57,446,60,448,64],{},[24,443,26],{},[24,445,56],{},[24,447,46],{},[24,449,63],{},"md","\u002Fimages\u002Fcovers\u002Fspam-fries.jpg",{},true,"\u002Farticles\u002F20210416-rspamd-forwarding-spam",false,{"title":6,"description":419},"articles\u002F20210416-rspamd-forwarding-spam",[459,460,461],"rspamd","email","sysadmin","PsFNyNXixoOn-yW536b6sQeK9ARQuQI258AftryMOTQ",[464,469],{"title":465,"path":466,"stem":467,"createdAt":468,"children":-1},"MySQL MyISAM to InnoDB Conversion","\u002Farticles\u002F20201222-mysql-myisam-to-innodb","articles\u002F20201222-mysql-myisam-to-innodb","2020-12-22",{"title":470,"path":471,"stem":472,"createdAt":473,"children":-1},"Fail2ban persistent banning","\u002Farticles\u002F20210518-fail2ban-persistent-banning","articles\u002F20210518-fail2ban-persistent-banning","2021-05-18",1794961391446]