{"id":668,"date":"2017-01-23T09:27:33","date_gmt":"2017-01-23T07:27:33","guid":{"rendered":"http:\/\/helia.ee\/koolitus\/?page_id=668"},"modified":"2017-01-23T09:27:33","modified_gmt":"2017-01-23T07:27:33","slug":"debian-8-postfix-blocking-sender-ips-in-postfix","status":"publish","type":"page","link":"https:\/\/helia.ee\/koolitus\/?page_id=668","title":{"rendered":"Debian 8 &#8211; Postfix &#8211; Blocking sender IPs in Postfix"},"content":{"rendered":"<header class=\"entry-header\">\n<h1 class=\"entry-title\">Blocking sender IPs in Postfix<\/h1>\n<\/header>\n<div class=\"entry-content\">\n<p>Despite all I\u2019ve done to filter junk mail, I recently noticed one consistent spammer who was bypassing all of my safeguards. Notably, this source has a host who\u2019s tolerant enough that the spammer\u00a0went so far as to set SPF headers, to give their messages some \u201ccredibility.\u201d<\/p>\n<p><span id=\"more-5970\"><\/span>The sender\u2019s IPs were, fortunately, confined to a single <code>\/24<\/code>.\u00a0Thanks to <a href=\"https:\/\/ethitter.com\/2016\/02\/why-i-host-my-own-email\/\">hosting my own email<\/a>, I\u2019m able to block their entire range until spam filters catch up.<\/p>\n<p>It\u2019s important to note that I confirmed, using IP lookup\u00a0services from the appropriate <a href=\"https:\/\/en.wikipedia.org\/wiki\/Regional_Internet_registry\">regional IP registries<\/a>, that the IPs and ranges I blocked were specific enough to not reject\u00a0otherwise-innocuous messages. One could easily ban\u00a0too-broad a subnet and lose\u00a0many legitimate emails.<\/p>\n<h2 id=\"client-checks\">IP Blacklist<\/h2>\n<p>First, create a <code>client_checks<\/code> file in <code>\/etc\/postfix<\/code>. To it, add one or more of the following\u00a0(depending on your needs)\u00a0replacing the IPs or ranges with what you\u2019ve observed in your logs, email headers, etc.:<\/p>\n<div>\n<div id=\"highlighter_358069\" class=\"syntaxhighlighter  plain\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"plain plain\">123.456.789.123\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 REJECT Your IP is spam<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"plain plain\">123.456.789.0\/24\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 REJECT Your IP range is spam<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Domains and subdomains could also be specified, as noted in the first tutorial linked in the\u00a0<a href=\"https:\/\/ethitter.com\/2016\/03\/blocking-sender-ips-in-postfix\/#references\"><em>References<\/em><\/a>, but for my purposes, domains are rarely consistent enough to bother filtering that way.<\/p>\n<h2 id=\"postmap\">Hash the blacklist<\/h2>\n<p>Next, the <code>client_checks<\/code> file must be converted to a database that Postfix can read. This must be done <em>every time<\/em> <code>client_checks<\/code> is updated<sup class=\"footnote\"><a id=\"fnref-5970-1\" href=\"https:\/\/ethitter.com\/2016\/03\/blocking-sender-ips-in-postfix\/#fn-5970-1\">1<\/a><\/sup>.<\/p>\n<div>\n<div id=\"highlighter_477082\" class=\"syntaxhighlighter  plain\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"plain plain\">postmap \/etc\/postfix\/client_checks<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2 id=\"smtpd_recipient_restrictions\"><code>smtpd_recipient_restrictions<\/code><\/h2>\n<p>After that, update <code>smtpd_recipient_restrictions<\/code> in <code>\/etc\/postfix\/main.cf<\/code>so that Postfix is aware of\u00a0the block list.<\/p>\n<div>\n<div id=\"highlighter_253779\" class=\"syntaxhighlighter  plain \">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"plain plain\">smtpd_recipient_restrictions =<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"plain spaces\">\u00a0\u00a0\u00a0<\/code><code class=\"plain plain\">check_client_access hash:\/etc\/postfix\/client_checks,<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"plain spaces\">\u00a0\u00a0\u00a0<\/code><code class=\"plain plain\">...<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Placing the block list at the beginning\u00a0of the <code>smtpd_recipient_restrictions<\/code> parameter ensures that the IP blocks are obeyed above all other processing,\u00a0but just as\u00a0importantly, prevents\u00a0more-expensive operations,\u00a0such as virus scanning and spam\u00a0scoring, from running when the outcome is predetermined.<\/p>\n<h2 id=\"conclusion\">Wrapping up<\/h2>\n<p>Lastly, restart Postfix and check <code>mail.log<\/code> to confirm that there weren\u2019t any errors reading the new configuration. Regardless of if spam volume subsides,\u00a0revisit the log to confirm that the changes had the intended effect and aren\u2019t blocking legitimate messages.<\/p>\n<p><strong id=\"references\">References:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/www.linuxlasse.net\/linux\/howtos\/Blacklist_and_Whitelist_with_Postfix\">Blacklist &amp; Whitelist with Postfix<\/a><\/li>\n<li><a href=\"http:\/\/www.cyberciti.biz\/faq\/howto-blacklist-reject-sender-email-address\/\">Block specific sending email addresses<\/a> (not covered here, but noted for reference)<\/li>\n<\/ul>\n<div id=\"footnotes-5970\" class=\"footnotes\">\n<div class=\"footnotedivider\"><\/div>\n<ol>\n<li id=\"fn-5970-1\">Postfix also needs to be reloaded after each subsequent rehashing of <code>client_checks<\/code>. <span class=\"footnotereverse\"><a href=\"https:\/\/ethitter.com\/2016\/03\/blocking-sender-ips-in-postfix\/#fnref-5970-1\">\u21a9<\/a><\/span><\/li>\n<\/ol>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Blocking sender IPs in Postfix Despite all I\u2019ve done to filter junk mail, I recently noticed one consistent spammer who was bypassing all of my safeguards. Notably, this source has a host who\u2019s tolerant enough that the spammer\u00a0went so far as to set SPF headers, to give their messages some \u201ccredibility.\u201d The sender\u2019s IPs were, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":621,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"class_list":["post-668","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/pages\/668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=668"}],"version-history":[{"count":1,"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/pages\/668\/revisions"}],"predecessor-version":[{"id":669,"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/pages\/668\/revisions\/669"}],"up":[{"embeddable":true,"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/pages\/621"}],"wp:attachment":[{"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}