{"id":1091,"date":"2015-02-09T01:34:26","date_gmt":"2015-02-09T01:34:26","guid":{"rendered":"https:\/\/trouble.org\/?p=1091"},"modified":"2015-02-09T01:34:26","modified_gmt":"2015-02-09T01:34:26","slug":"really-really-really-nuke-iptables","status":"publish","type":"post","link":"https:\/\/trouble.org\/?p=1091","title":{"rendered":"really, really, really nuke iptables"},"content":{"rendered":"<figure id=\"attachment_1094\" aria-describedby=\"caption-attachment-1094\" style=\"width: 150px\" class=\"wp-caption alignright\"><a href=\"https:\/\/trouble.org\/uploads\/2015\/02\/nuke.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-thumbnail wp-image-1094\" src=\"https:\/\/trouble.org\/uploads\/2015\/02\/nuke-150x150.jpg\" alt=\"nukem from orbit, it's the only way to be sure\" width=\"150\" height=\"150\" srcset=\"https:\/\/trouble.org\/wp-content\/uploads\/2015\/02\/nuke-150x150.jpg 150w, https:\/\/trouble.org\/wp-content\/uploads\/2015\/02\/nuke-96x96.jpg 96w, https:\/\/trouble.org\/wp-content\/uploads\/2015\/02\/nuke-24x24.jpg 24w, https:\/\/trouble.org\/wp-content\/uploads\/2015\/02\/nuke-36x36.jpg 36w, https:\/\/trouble.org\/wp-content\/uploads\/2015\/02\/nuke-48x48.jpg 48w, https:\/\/trouble.org\/wp-content\/uploads\/2015\/02\/nuke-64x64.jpg 64w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a><figcaption id=\"caption-attachment-1094\" class=\"wp-caption-text\">nuke it from orbit, it&#8217;s the only way to be sure<\/figcaption><\/figure>\n<p>I think this is the way to really clear out all the stuff in iptables, the arcane packet filtering thing for Linux. At least&#8230; I think. My take on it, at least. For somewhat modern Linuxes at the time of this writing, IPv4 only.<\/p>\n<p>Basic method: loop over all the types of tables, flushing&#8230; then loop over all the builtin tables for the various types, reset the policies&#8230; then erase all chains not in tables.<\/p>\n<p>All this joy in a shell script below.<\/p>\n<div style=\"clear:both\"><\/div>\n<div class=\"codecolorer-container bash blackboard\" style=\"overflow:auto;white-space:nowrap;height:800px;\"><div class=\"bash codecolorer\"><span class=\"co0\">#!\/bin\/bash<\/span><br \/>\n<br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"co0\"># Usage: $0<\/span><br \/>\n<span class=\"co0\">#<\/span><br \/>\n<br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"co0\"># so many tables, so little time... this simply nukes everything in iptables.<\/span><br \/>\n<span class=\"co0\"># At least... I think... who really knows?<\/span><br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"co0\"># IPv4 ONLY<\/span><br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"co0\"># Perhaps you can define your own, I'd never see them. Kernels can have<\/span><br \/>\n<span class=\"co0\"># others in or out... can't find any command to just list the tables,<\/span><br \/>\n<span class=\"co0\"># probably something simple.<\/span><br \/>\n<span class=\"co0\">#<\/span><br \/>\n<br \/>\n<span class=\"co0\"># Anyway. Basic method - loop over all the types of tables, flushing... <\/span><br \/>\n<span class=\"co0\"># then loop over and, based on the builtin tables, reset the policies...<\/span><br \/>\n<span class=\"co0\"># then erase all chains not in tables.<\/span><br \/>\n<br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st_h\">'killing off routes'<\/span><br \/>\n<br \/>\n<span class=\"co0\"># kill routes<\/span><br \/>\n<span class=\"kw2\">ip route<\/span> flush cache<br \/>\n<br \/>\n<span class=\"co0\"># who knew there were so many? &quot;filter&quot; is the default table<\/span><br \/>\n<span class=\"re2\">types_o_tables<\/span>=<span class=\"br0\">&#40;<\/span><span class=\"st_h\">'-t filter'<\/span> <span class=\"st_h\">'-t nat'<\/span> <span class=\"st_h\">'-t mangle'<\/span> <span class=\"st_h\">'-t raw'<\/span> <span class=\"st_h\">'-t security'<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"co0\"># from the man page:<\/span><br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp;filter:<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;This is the default table (if no -t option is passed). It contains<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;the built-in chains INPUT (for packets destined to local sockets),<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;FORWARD (for packets being routed through the box), and OUTPUT<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;(for locally-generated packets).<\/span><br \/>\n<span class=\"re2\">filter<\/span>=<span class=\"br0\">&#40;<\/span><span class=\"st_h\">'INPUT'<\/span> <span class=\"st_h\">'FORWARD'<\/span> <span class=\"st_h\">'OUTPUT'<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp;<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp;nat:<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;This table is consulted when a packet that creates a new connection<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;is encountered. &nbsp;It consists of three &nbsp;built-ins: &nbsp;PREROUTING (for<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;altering &nbsp;packets as soon as they come in), OUTPUT (for altering<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;locally-generated packets before routing), and POSTROUTING (for<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;altering packets as they are about to go out). &nbsp;IPv6 NAT support is<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;available since kernel 3.7.<\/span><br \/>\n<span class=\"re2\">nat<\/span>=<span class=\"br0\">&#40;<\/span><span class=\"st_h\">'PREROUTING'<\/span> <span class=\"st_h\">'OUTPUT'<\/span> <span class=\"st_h\">'POSTROUTING'<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp;<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp;mangle:<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;This table is used for specialized packet alteration. &nbsp;Until kernel<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;2.4.17 it had two built-in chains: PREROUTING &nbsp;(for &nbsp;altering incoming<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;packets before routing) and OUTPUT (for altering locally-generated<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;packets before routing). &nbsp;Since kernel 2.4.18, three other built-in<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;chains are also supported: INPUT (for packets coming into the box<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;itself), FORWARD &nbsp;(for &nbsp;altering &nbsp;packets &nbsp;being routed through the<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;box), and POSTROUTING (for altering packets as they are about to<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;go out).<\/span><br \/>\n<span class=\"re2\">mangle<\/span>=<span class=\"br0\">&#40;<\/span><span class=\"st_h\">'PREROUTING'<\/span> <span class=\"st_h\">'OUTPUT'<\/span> <span class=\"st_h\">'INPUT'<\/span> <span class=\"st_h\">'FORWARD'<\/span> <span class=\"st_h\">'POSTROUTING'<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp;<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp;raw:<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;This &nbsp;table is used mainly for configuring exemptions from connection<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;tracking in combination with the NOTRACK target. &nbsp;It registers at<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;the netfilter hooks with higher priority and is thus called before<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;ip_conntrack, or any other IP tables. &nbsp;It provides the following<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;built-in &nbsp;chains: &nbsp;PREROUTING &nbsp;(for packets arriving via any network<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;interface) OUTPUT (for packets generated by local processes)<\/span><br \/>\n<span class=\"re2\">raw<\/span>=<span class=\"br0\">&#40;<\/span><span class=\"st_h\">'PREROUTING'<\/span> <span class=\"st_h\">'OUTPUT'<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp;<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp;security:<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;This table is used for Mandatory Access Control (MAC) networking<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;rules, such as those enabled by the SECMARK and CONNSECMARK targets.<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;Mandatory Access Control is implemented by Linux Security Modules<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;such as SELinux. &nbsp;The security table is called after the filter<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;table, allowing any Discretionary Access Control (DAC) rules in the<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;filter table to take effect before &nbsp;MAC &nbsp;rules. &nbsp; This table provides<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;the following built-in chains: INPUT (for packets coming into the<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;box itself), OUTPUT (for altering locally-generated packets before<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;routing), and FORWARD (for altering packets being routed through<\/span><br \/>\n<span class=\"co0\"># &nbsp; &nbsp; &nbsp; &nbsp;the box).<\/span><br \/>\n<span class=\"re2\">security<\/span>=<span class=\"br0\">&#40;<\/span><span class=\"st_h\">'INPUT'<\/span> <span class=\"st_h\">'OUTPUT'<\/span> <span class=\"st_h\">'FORWARD'<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"co0\"># flush the tables<\/span><br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"kw3\">echo<\/span> flushing tables....<br \/>\n<span class=\"kw1\">for<\/span> type_o <span class=\"kw1\">in<\/span> <span class=\"st0\">&quot;<span class=\"es3\">${types_o_tables[@]}<\/span>&quot;<\/span>; <span class=\"kw1\">do<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw3\">echo<\/span> <span class=\"re5\">-e<\/span> <span class=\"st0\">&quot;<span class=\"es1\">\\t<\/span><span class=\"es2\">$type_o<\/span>&quot;<\/span><br \/>\n&nbsp; &nbsp; iptables <span class=\"re1\">$type_o<\/span> <span class=\"re5\">-F<\/span><br \/>\n<span class=\"kw1\">done<\/span><br \/>\n<br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"co0\"># reset all the policies<\/span><br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"kw3\">echo<\/span> reset policies<br \/>\n<span class=\"kw1\">for<\/span> type_o <span class=\"kw1\">in<\/span> <span class=\"st0\">&quot;<span class=\"es3\">${types_o_tables[@]}<\/span>&quot;<\/span>; <span class=\"kw1\">do<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw3\">echo<\/span> <span class=\"re5\">-e<\/span> <span class=\"st0\">&quot;<span class=\"es1\">\\t<\/span><span class=\"es2\">$type_o<\/span>&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"co0\"># have to tear out the -t<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re2\">pol<\/span>=$<span class=\"br0\">&#40;<\/span><span class=\"kw3\">echo<\/span> <span class=\"re1\">$type_o<\/span><span class=\"sy0\">|<\/span><span class=\"kw2\">awk<\/span> <span class=\"st_h\">'{print $2}'<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"co0\"># echo -e &quot;\\t--&gt; $a&quot;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"co0\"># it's a bit convoluted... so building this up one step at a time<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re2\">e<\/span>=$<span class=\"br0\">&#40;<\/span><span class=\"kw3\">echo<\/span> \\<span class=\"co1\">${$pol[\\*]}<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"co0\"># loop over arrays of tables<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">for<\/span> p <span class=\"kw1\">in<\/span> $<span class=\"br0\">&#40;<\/span><span class=\"kw3\">eval<\/span> <span class=\"st0\">&quot;echo <span class=\"es2\">$e<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span>; <span class=\"kw1\">do<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">echo<\/span> <span class=\"re5\">-e<\/span> <span class=\"st0\">&quot;<span class=\"es1\">\\t<\/span><span class=\"es1\">\\t<\/span><span class=\"es2\">$p<\/span>&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">done<\/span><br \/>\n<span class=\"kw1\">done<\/span><br \/>\n<br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"co0\"># erase all chains not in table<\/span><br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"kw3\">echo<\/span> erasing chains...<br \/>\n<span class=\"kw1\">for<\/span> type_o <span class=\"kw1\">in<\/span> <span class=\"st0\">&quot;<span class=\"es3\">${types_o_tables[@]}<\/span>&quot;<\/span>; <span class=\"kw1\">do<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw3\">echo<\/span> <span class=\"re5\">-e<\/span> <span class=\"st0\">&quot;<span class=\"es1\">\\t<\/span><span class=\"es2\">$type_o<\/span>&quot;<\/span><br \/>\n&nbsp; &nbsp; iptables <span class=\"re1\">$type_o<\/span> <span class=\"re5\">-X<\/span><br \/>\n<span class=\"kw1\">done<\/span><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I think this is the way to really clear out all the stuff in iptables, the arcane packet filtering thing for Linux. At least&#8230; I think. My take on it, at least. For somewhat modern Linuxes at the time of this writing, IPv4 only. Basic method: loop over all the types of tables, flushing&#8230; then [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,4,6],"tags":[303,300,298,301,299,302],"class_list":["post-1091","post","type-post","status-publish","format-standard","hentry","category-code","category-security","category-tech","tag-die-die-die","tag-flush","tag-iptables","tag-kill","tag-nuke","tag-stab"],"_links":{"self":[{"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/1091","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1091"}],"version-history":[{"count":12,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/1091\/revisions"}],"predecessor-version":[{"id":1104,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/1091\/revisions\/1104"}],"wp:attachment":[{"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}