<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>IPTables &#8211; SophieDogg</title>
	<atom:link href="https://sophiedogg.com/tag/iptables/feed/" rel="self" type="application/rss+xml" />
	<link>https://sophiedogg.com</link>
	<description>Dogg of all trades, Master of no one.</description>
	<lastBuildDate>Sat, 09 Oct 2021 12:02:53 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.1</generator>
	<item>
		<title>Blocking Services with Fail2Ban</title>
		<link>https://sophiedogg.com/blocking-services-with-fail2ban/</link>
					<comments>https://sophiedogg.com/blocking-services-with-fail2ban/#comments</comments>
		
		<dc:creator><![CDATA[SophieDogg]]></dc:creator>
		<pubDate>Tue, 17 Apr 2012 17:19:20 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Networking]]></category>
		<category><![CDATA[DNS]]></category>
		<category><![CDATA[IPTables]]></category>
		<category><![CDATA[Server]]></category>
		<guid isPermaLink="false">http://sophiedogg.com/?p=695</guid>

					<description><![CDATA[There are many different methods of securing a publicly accessible server, and one of the best things a system administrator can do is use fail2ban to dynamically block potential attackers before they can do any damage. First, you will want to install Fail2ban, so head over to Fail2Ban.org and follow their instructions for installing on [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>There are many different methods of securing a publicly accessible server, and one of the best things a system administrator can do is use fail2ban to dynamically block potential attackers before they can do any damage. <span id="more-695"></span></p>
<p>First, you will want to install Fail2ban, so head over to <a href="http://www.fail2ban.org">Fail2Ban.org</a> and follow their instructions for installing on your distribution.  On CentOS or RedHat, you will need to first install the <a href="http://fedoraproject.org/wiki/EPEL">EPEL</a> repository.  After doing so, fail2ban can be installed with the yum command <tt>yum install fail2ban</tt>.  That takes care of the installation! </p>
<p>Once you have Fail2Ban installed, you can start configuring it!  One of the problems that I run into with Fail2Ban is that it will try to load all the firewall rules too fast, resulting in errors like this:<br />
<tt>iptables -I INPUT -p all -j fail2ban-w00tw00t returned 400</tt><br />
You may also get some 100 or 200 errors as well.  Well, the fix for this is easy enough (and hopefully it will be incorporated into upcoming versions; as of version 0.8.4 is is not).  Simply edit the file <tt>/usr/bin/fail2ban-client</tt> and add the line <tt>time.sleep(0.1)</tt> into the <tt>__processCmd</tt> function.  This is added around line #145.  The first few lines of the function should look like this:</p>
<pre>def __processCmd(self, cmd, showRet = True):
    beautifier = Beautifier()
    for c in cmd:
        time.sleep(0.1)
        beautifier.setInputCmd(c)
        try:</pre>
<p>So, now that we have a working Fail2Ban setup, we can start configuring some rules!</p>
<p>First, take a look at <tt>/etc/fail2ban/fail2ban.conf</tt>.  This lets us specify some log options.  The only one that I am really concerned about is the logtarget, which I like to set to <tt>/var/log/fail2ban.log</tt>.</p>
<p>Next, let us look at the <tt>/etc/fail2ban/jail.conf</tt> file. Here you will want to configure your list of ignore IP&#8217;s, which should include localhost, and your trusted machine(s) or subnet (ex: <tt>ignoreip = 127.0.0.1 192.168.0.0/24 8.8.8.8</tt>).  You can also adjust the bantime, findtime, and maxretry options, all of which are explained within the file.</p>
<p>Next you can pick what jails to run, and override any global settings with specific settings set here.  Spend some time reading through the different jail configurations to get an idea for how to set yours up.  I&#8217;m always a little more paranoid about security; I don&#8217;t want any cats in my dogghouse, so I will usually completely ban any user that fits one of my rules.  You could optionally just ban them from using the service they are attempting to exploit, if you wish.</p>
<p>Lets take a look at a jail configuration that I wrote myself.</p>
<pre>[dns-root]
enabled  = true
filter   = dns-root
action   = iptables-allports[name=DNS-ROOT]
           sendmail-whois[name=dns-root, dest=jerp@herpandderp.com, sender=fail2ban@myserver.com]
logpath  = /var/named/chroot/var/log/bind.log
maxretry = 2
bantime  = 86400</pre>
<p>One of my nameservers was getting a large number of NS lookup requests for the root domains.  Well, our server isn&#8217;t authoritative for the root zone, so we respond with a REFUSED notice, however this didn&#8217;t keep the requests from coming.  At first I thought that it was some sort of lame DOS attack against one of my nameservers, but after investigation I found that this was likely a <a href="http://securityaffairs.co/wordpress/3184/cyber-crime/anonymous-dns-amplification-attacks-for-operation-global-blackout.html"> Root DNS Amplification Attack</a> against Facebook!  I would get approximately one request per minute for the root NS records, sending out one REFUSED response per minute to an IP address owned by Facebook.  In order to combat these annoyances, I created the <tt>[dns-root]</tt> jail for Fail2Ban.</p>
<p>Lets take a look at what each setting in this jail does.  First, the enabled line dictates whether or not the jail is active.  Next, the filter line specifies the name of the filter that will be used to match undesired behavior.  The action line in this case says to run the <tt>iptables-allports</tt> action, inserting an iptables rule into the chain named <tt>DNS-ROOT</tt>, which will drop traffic from the banned host on all ports.  Optionally, you can select to only have specific ports dropped, instead of banning the host from your entire server.  The <tt>sendmail-whois</tt> line lets us send an email, with whois inforation about the banned host, to whoever you like.  Finally the logpath specifies the actual log to be checked against, and the maxretry along with the bantime values will override the global variables you specified earlier in that file.</p>
<p>Next, take a look at the matching filter.  After removing some extra comments, we are left with this:</p>
<pre># Fail2Ban DNS-ROOT configuration file

[Definition]
failregex = ^.* security: info: client <HOST>#.*: query \(cache\) './(NS|A|AAAA|MX|CNAME)/IN' denied

ignoreregex =</pre>
<p>What we have are two variables.  The first is failregex, which is a regex formatted statement to match denied root zone lookup lines in our log files.  The next variable, ignoreregex, lets us specify something to search for, which will cause a matched line to be ignored.  Basically, if the failregex line is matched, the host is banned.  If both lines are matched, the host is not banned.</p>
<p>Now, once you have everything configured, you should be able to set the service to start at boot with the <tt>chkconfig fail2ban on</tt> command, and immediately make the service start with the <tt>service fail2ban start</tt> command.</p>
<p>Now you can finally have some piece of mind, knowing that those pesky internet cats wont be able to get into your dogghouse and steal your bone!</p>
<p>If you are interested in creating your own rules, check out <a href="http://regexpal.com/">Regexpal.com</a> for help in writing and debugging regex.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sophiedogg.com/blocking-services-with-fail2ban/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>A Better Linux Firewall</title>
		<link>https://sophiedogg.com/a-better-linux-firewall/</link>
					<comments>https://sophiedogg.com/a-better-linux-firewall/#comments</comments>
		
		<dc:creator><![CDATA[SophieDogg]]></dc:creator>
		<pubDate>Wed, 01 Jun 2011 19:49:03 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Networking]]></category>
		<category><![CDATA[IPTables]]></category>
		<guid isPermaLink="false">http://sophiedogg.com/?p=96</guid>

					<description><![CDATA[Since we recently learned about basic Linux firewalls, I figured that it would be good to cover some more advanced firewall topics. There are a lot of settings that we can use to allow or deny specific traffic from specific hosts. So, let&#8217;s jump right in and take a look! Credit to bit-tech.net for the [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Since we recently learned about basic Linux firewalls, I figured that it would be good to cover some more advanced firewall topics.  There are a lot of settings that we can use to allow or deny specific traffic from specific hosts.  So, let&#8217;s jump right in and take a look! <span id="more-96"></span></p>
<p><a href="https://sophiedogg.com/wp-content/uploads/2011/06/iptables1.gif"><img fetchpriority="high" decoding="async" src="https://sophiedogg.com/wp-content/uploads/2011/06/iptables1.gif" alt="" title="iptables" width="543" height="760" class="aligncenter size-full wp-image-103" srcset="https://sophiedogg.com/wp-content/uploads/2011/06/iptables1.gif 543w, https://sophiedogg.com/wp-content/uploads/2011/06/iptables1-214x300.gif 214w" sizes="(max-width: 543px) 100vw, 543px" /></a><center>Credit to <a href="http://www.bit-tech.net">bit-tech.net</a> for the image.</center></p>
<p>If you take a look at the image above, you can see how data flows through a typical Linux firewall.  Since we are currently only concerned with traffic destined for our Linux box, all data flows through the &#8220;Yes&#8221; side of the &#8220;Data for the firewall?&#8221; question.  In the future we will look at the other side when we configure a Linux router.</p>
<p>Let&#8217;s start with a description of what we are doing, and then I&#8217;ll show what it looks like.  Within IPTables there are different tables and chains, which are referenced in the picture above.  We want to look primarily at the Input chain in the Filter table.  The first thing we are going to do is create a new chain at the top of our input chain, which will accept all predetermined traffic that we know will always be safe.  This is data such as related or established traffic, traffic on the local loopback interface, and if you are running a DHCP server on your Linux machine, you would also want to accept UDP traffic on port 67.  DHCP requests come from source address 0.0.0.0, which we will be blocking later, so we need to accept it now!</p>
<p>After we accept this traffic, we want to block all traffic that we know is bad.  This is traffic from non-existent networks (<a href="http://www.team-cymru.org/Services/Bogons">bogons</a>), traffic with <a href="http://pikt.org/pikt/samples/iptables_tcp_flags_programs.cfg.html">bad TCP flags</a>, and anything else you specifically want to keep out.  We will create two chains for this; one to list the traffic to be blocked, and another to block and optionally log the traffic.  If you don&#8217;t wish to log the traffic, you can simply stick with one chain that will only block the undesired traffic.</p>
<p>Finally we can create another chain to accept traffic that we want to let in.  This can be access to a SSH server, DNS server, web server, or anything else you want!  After all of these chains, we can also optionally log any traffic that made it this far, before dropping it into oblivion.</p>
<p>Here is what the INPUT table looks like:</p>
<pre>-A INPUT -j Firewall-1-INPUT
-A INPUT -j Firewall-1-DROP
-A INPUT -j Firewall-2-INPUT
-A INPUT -m limit --limit 1/sec -j LOG --log-prefix "[INPUT] "</pre>
<p>Here the data comes in to the Firewall-1-INPUT chain first.  If it is not accepted (or blocked) by this chain, it then moves on to the Firewall-1-DROP chain.  If the data makes it past that chain it goes on to the Firewall-2-INPUT chain, and finally if it makes it the whole way through without getting accepted or blocked, it will be logged before performing the default policy, which in our case is to drop the traffic so it can&#8217;t come in.</p>
<p>The Firewall-1-INPUT chain looks like this:</p>
<pre>-A Firewall-1-INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A Firewall-1-INPUT -i lo -j ACCEPT
-A Firewall-1-INPUT -p udp -m udp --dport 67 -j ACCEPT</pre>
<p>The first line accepts any related or established traffic.  The second line accepts any traffic on the local loopback interface, and the third line accepts DHCP address request traffic on UDP port 67.  If you are not running a DHCP server, then you won&#8217;t need that last line.</p>
<p>Next we move on to the Firewall-1-DROP chain, which drops any specifically unwanted traffic.</p>
<pre>
-A Firewall-1-DROP -f -j Firewall-2-DROP
-A Firewall-1-DROP -p tcp ! --syn -m state --state NEW -j Firewall-2-DROP
-A Firewall-1-DROP -p tcp -m tcp --tcp-flags FIN,ACK FIN -j Firewall-2-DROP
-A Firewall-1-DROP -p tcp -m tcp --tcp-flags PSH,ACK PSH -j Firewall-2-DROP
-A Firewall-1-DROP -p tcp -m tcp --tcp-flags ACK,URG URG -j Firewall-2-DROP
-A Firewall-1-DROP -p tcp -m tcp --tcp-flags FIN,RST FIN,RST -j Firewall-2-DROP
-A Firewall-1-DROP -p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -j Firewall-2-DROP
-A Firewall-1-DROP -p tcp -m tcp --tcp-flags SYN,RST SYN,RST -j Firewall-2-DROP
-A Firewall-1-DROP -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,PSH,ACK,URG -j Firewall-2-DROP
-A Firewall-1-DROP -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j Firewall-2-DROP
-A Firewall-1-DROP -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -j Firewall-2-DROP
-A Firewall-1-DROP -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,PSH,URG -j Firewall-2-DROP
-A Firewall-1-DROP -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,ACK,URG -j Firewall-2-DROP</pre>
<pre>-A Firewall-1-DROP -s 0.0.0.0/8 -j Firewall-2-DROP
-A Firewall-1-DROP -d 0.0.0.0/8 -j Firewall-2-DROP
-A Firewall-1-DROP -s 10.0.0.0/8 -j Firewall-2-DROP
-A Firewall-1-DROP -d 10.0.0.0/8 -j Firewall-2-DROP
-A Firewall-1-DROP -s 127.0.0.0/8 -j Firewall-2-DROP
-A Firewall-1-DROP -d 127.0.0.0/8 -j Firewall-2-DROP
-A Firewall-1-DROP -s 169.254.0.0/16 -j Firewall-2-DROP
-A Firewall-1-DROP -d 169.254.0.0/16 -j Firewall-2-DROP
-A Firewall-1-DROP -s 172.16.0.0/12 -j Firewall-2-DROP
-A Firewall-1-DROP -d 172.16.0.0/12 -j Firewall-2-DROP
-A Firewall-1-DROP -s 192.0.0.0/24 -j Firewall-2-DROP
-A Firewall-1-DROP -d 192.0.0.0/24 -j Firewall-2-DROP
-A Firewall-1-DROP -s 192.0.2.0/24 -j Firewall-2-DROP
-A Firewall-1-DROP -d 192.0.2.0/24 -j Firewall-2-DROP
-A Firewall-1-DROP -s 192.168.1.0/24 -j Firewall-2-DROP
-A Firewall-1-DROP -d 192.168.1.0/24 -j Firewall-2-DROP
-A Firewall-1-DROP -s 192.168.2.0/23 -j Firewall-2-DROP
-A Firewall-1-DROP -d 192.168.2.0/23 -j Firewall-2-DROP
-A Firewall-1-DROP -s 192.168.4.0/22 -j Firewall-2-DROP
-A Firewall-1-DROP -d 192.168.4.0/22 -j Firewall-2-DROP
-A Firewall-1-DROP -s 192.168.8.0/21 -j Firewall-2-DROP
-A Firewall-1-DROP -d 192.168.8.0/21 -j Firewall-2-DROP
-A Firewall-1-DROP -s 162.168.16.0/20 -j Firewall-2-DROP
-A Firewall-1-DROP -d 162.168.16.0/20 -j Firewall-2-DROP
-A Firewall-1-DROP -s 192.168.32.0/19 -j Firewall-2-DROP
-A Firewall-1-DROP -d 192.168.32.0/19 -j Firewall-2-DROP
-A Firewall-1-DROP -s 192.168.64.0/18 -j Firewall-2-DROP
-A Firewall-1-DROP -d 192.168.64.0/18 -j Firewall-2-DROP
-A Firewall-1-DROP -s 192.168.128.0/17 -j Firewall-2-DROP
-A Firewall-1-DROP -d 192.168.128.0/17 -j Firewall-2-DROP
-A Firewall-1-DROP -s 198.18.0.0/15 -j Firewall-2-DROP
-A Firewall-1-DROP -d 198.18.0.0/15 -j Firewall-2-DROP
-A Firewall-1-DROP -s 198.51.100.0/24 -j Firewall-2-DROP
-A Firewall-1-DROP -d 198.51.100.0/24 -j Firewall-2-DROP
-A Firewall-1-DROP -s 203.0.113.0/24 -j Firewall-2-DROP
-A Firewall-1-DROP -d 203.0.113.0/24 -j Firewall-2-DROP
-A Firewall-1-DROP -s 224.0.0.0/3 -j Firewall-2-DROP
-A Firewall-1-DROP -d 224.0.0.0/3 -j Firewall-2-DROP</pre>
<p>The first set of lines blocks any incoming packets with fragments, new connections that aren&#8217;t SYN packets, and packets with invalid TCP connection flags.  This will help to prevent syn flood attacks on your machine.  The second set of lines blocks traffic coming from (-s) or going to (-d) addresses in the bogon list, which helps prevent spoof attacks.  It is important to note here that if your machine is on a <a href="http://en.wikipedia.org/wiki/Private_network">private network</a> using non-routable IP addresses, then you must remove the lines from the list above that correspond to your LAN IP addresses.  In the above example, traffic on the 192.168.0.x network is allowed, while all other traffic beginning with 192.168.x.x is blocked.  We will continue using this network in the rest of the examples.</p>
<p>If any traffic is caught by these rules, it is then sent to the Firewall-2-DROP chain, which will log the packet then drop it.  The Firewall-2-DROP chain will limit the amount of logging (to keep from filling our logs), and will also prefix the log so we know where the traffic was blocked.  Below are the lines from the Firewall-2-DROP chain:</p>
<pre>-A Firewall-2-DROP -m limit --limit 1/sec -j LOG --log-prefix "[INFW2] "
-A Firewall-2-DROP -j DROP</pre>
<p>Finally, if the traffic has made it this far and hasn&#8217;t been accepted or dropped yet, we can move on to the accept chain.</p>
<pre>-A Firewall-2-INPUT -p udp -m udp --dport 53 -j ACCEPT
-A Firewall-2-INPUT -p icmp -m icmp -m limit -s 192.168.0.0/24 --icmp-type 8 --limit 1/sec -j ACCEPT
-A Firewall-2-INPUT -p tcp -m tcp -m multiport -j ACCEPT --dports 22,53,80,443
-A Firewall-2-INPUT -p tcp -m tcp -m multiport -s 192.168.0.0/24 -j ACCEPT --dports 5000:5999
-A Firewall-2-INPUT -p udp -m udp -m multiport -s 192.168.0.0/24 -j ACCEPT --dports 69,70,71</pre>
<p>The first line will accept DNS lookup traffic from anyone (UDP port 53).  If you don&#8217;t run a DNS server, you won&#8217;t need this line.  The second line accepts <a href="http://www.iana.org/assignments/icmp-parameters">icmp-type</a> 8 traffic (ping request) from the local LAN.  This means that your machine will respond to ping requests from clients on the LAN, but not from clients on the internet.  The third line accepts TCP traffic on ports 22, 53, 80, and 443 (SSH, DNS, HTTP, and HTTPS respectively); again, if you&#8217;re not running these services then you don&#8217;t need these ports open.  The fourth line accepts traffic on ports 5000-5999; this is just an example to show how to open a range of ports.  The fifth line shows us accepting UDP traffic on ports 69, 70, and 71.  And finally, the last line will log any traffic that has made it this far, again limiting the amount of logs written to keep from flooding the log files, and prefixing the log lines so we know where it came from.</p>
<p>We also must have the default rule for our input chain, to determine what happens when traffic makes it all the way to the end of our rule list.  Here is our default input rule, to drop the traffic:</p>
<pre>:INPUT DROP [0:0]</pre>
<p>Hopefully this will help you to create a more advanced Linux firewall to fit your needs.  Sometime in the future I will show you how to use your Linux box as a router, to handle NAT and routing with your firewall.  For now, I&#8217;m going to go take a nap; it&#8217;s hard being a dogg!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sophiedogg.com/a-better-linux-firewall/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Setting up a Linux Firewall</title>
		<link>https://sophiedogg.com/setting-up-a-linux-firewall/</link>
					<comments>https://sophiedogg.com/setting-up-a-linux-firewall/#comments</comments>
		
		<dc:creator><![CDATA[SophieDogg]]></dc:creator>
		<pubDate>Sun, 17 Apr 2011 05:19:39 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Networking]]></category>
		<category><![CDATA[IPTables]]></category>
		<guid isPermaLink="false">http://sophiedogg.com/?p=68</guid>

					<description><![CDATA[One of the first tasks that should be accomplished when deploying a new server (and in reality, any new machine) is setting up a software firewall. On a Linux computer, this is accomplished using iptables. We can use the /sbin/iptables command to manipulate our firewall, or we can directly edit the /etc/sysconfig/iptables file. Each has [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>One of the first tasks that should be accomplished when deploying a new server (and in reality, any new machine) is setting up a software firewall.  On a Linux computer, this is accomplished using iptables.  We can use the <code>/sbin/iptables</code> command to manipulate our firewall, or we can directly edit the <code>/etc/sysconfig/iptables</code> file.  Each has their advantages, and it&#8217;s worthwhile to know how each works.</p>
<p>Lets start with the <code>iptables</code> command.  We can first <span id="more-68"></span>flush the firewall table, so that we have a blank set of rules to start with.</p>
<pre>[root@machine ~]# iptables -F</pre>
<p>Next we can start adding some rules.  First, lets create a rule to accept any traffic that is either already established, or traffic that is related to the established traffic.</p>
<pre>[root@machine ~]# iptables -I INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT</pre>
<p>The <code>-I</code> after our command means that we want to insert our rule.  We can optionally insert it into a specific location, which we would specify after the name of the chain, but we want it at the top so we can omit the location number.  The <code>INPUT</code> chain is our set of incoming firewall rules.  Next we will specify that we want to match the state of the connection with the <code>-m state</code> flag, followed by the state we wish to match, using <code>--state RELATED,ESTABLISHED</code>.  Finally we say that when these conditions are met, we will jump to another specific chain or target.  We could make this traffic jump to another chain that can do some extra processing, or we can just jump to one of the predefined targets.  In this case, we just jump to the <code>ACCEPT</code> chain, so that the traffic is accepted by the firewall, and further processing of the traffic (by iptables) is discontinued.</p>
<p>Next we will accept any traffic on the local interface, using the <code>-i lo</code> option.  It&#8217;s worth mentioning that we want to process our traffic efficiently, and these first two rules will accept the majority of traffic that we will see.  This will allow iptables to accept the traffic near the top of the INPUT chain, reducing the amount of processing necessary. We&#8217;ll append next rule to our current list of rules, by replacing the <code>-I</code> flag with the <code>-A</code> flag.</p>
<pre>[root@machine ~]# iptables -A INPUT -i lo -j ACCEPT</pre>
<p>Now we can actually start setting up some real rules to accept traffic from the outside.  What we can do for our basic server now is to accept a few ports, so that we will have outside access.  First, lets accept our SSH traffic with the following command:</p>
<pre>[root@machine ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT</pre>
<p>The <code>-p tcp</code> flag says that we want to match the TCP protocol, and the <code>--dport 22</code> tells the rule to accept traffic coming in with a destination of port 22.  This rule will allow tcp connections to the SSH server on port 22.  Next, let us accept two other ports.</p>
<pre>[root@machine ~]# iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -j ACCEPT</pre>
<p>Here we have accepted incoming traffic on TCP ports 80 and 443.  Notice that we also needed the <code>-m tcp</code> flag, in order to match our rule for the <code>-m multiport</code> flag.  We can also accept traffic from a specific source, providing an extra layer of security to protect any administrative ports.</p>
<pre>[root@machine ~]# iptables -A INPUT -p tcp -s 192.168.1.50/32 --dport 10000 -j ACCEPT</pre>
<p>This previous rule only accepts traffic on port 10000 if it has a source of 192.168.1.50/32.  Notice the <a href="http://en.wikipedia.org/wiki/CIDR">CIDR (Classes Inter-Domain Routing)</a> notation.  We can optionally specify a subnet to accept, with something like 192.168.1.0/24.  We can also omit the CIDR subnet notation if we are only allowing access to a single IP address.</p>
<p>Finally, we can set the default rule for the INPUT table.  This rule will be run on any traffic that hasn&#8217;t been matched by a previous rule in our chain.  In this case, we want to drop any other traffic, so we only allow what we have explicitly specified.  We should also drop any forwarded traffic, so nobody can try to relay traffic through us.</p>
<pre>[root@machine ~]# iptables -P INPUT DROP
[root@machine ~]# iptables -P FORWARD DROP</pre>
<p>Finally, we want to save our work, so that our rules are persistent on reboot.  On Red Hat/Fedora/CentOS, we can run the following command:</p>
<pre>[root@machine ~]# service iptables save</pre>
<p>The nice thing about using the <code>iptables</code> command, is that we can add or remove commands on the fly, allowing us to write scripts which can modify our rules, blocking potential threats on the in real time during an attack.</p>
<p>We can also directly edit the <code>/etc/sysconfig/iptables</code> file, so we can more easily create a set of default rules.  Lets take a quick look at the file, so open it in your favorite text editor.  It should look something like this:</p>
<pre># Generated by iptables-save v1.3.5 on Tue Mar 29 22:19:16 2011
*raw
:PREROUTING ACCEPT [320:29801]
:OUTPUT ACCEPT [334:46325]
COMMIT
# Completed on Tue Mar 29 22:19:16 2011
# Generated by iptables-save v1.3.5 on Tue Mar 29 22:19:16 2011
*nat
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [2:120]
:OUTPUT ACCEPT [2:120]
COMMIT
# Completed on Tue Mar 29 22:19:16 2011
# Generated by iptables-save v1.3.5 on Tue Mar 29 22:19:16 2011
*mangle
:PREROUTING ACCEPT [320:29801]
:INPUT ACCEPT [320:29801]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [334:46325]
:POSTROUTING ACCEPT [334:46325]
COMMIT
# Completed on Tue Mar 29 22:19:16 2011
# Generated by iptables-save v1.3.5 on Tue Mar 29 22:19:16 2011
*filter
:FORWARD DROP [0:0]
:INPUT DROP [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp -m multiport --dports 80,443 -j ACCEPT
-A INPUT -p tcp -s 192.168.1.50/32 --dport 10000 -j ACCEPT
COMMIT
# Completed on Tue Mar 29 22:19:16 2011</pre>
<p>The most important parts to notice are toward the end of the file.  We can see by the <code>:FORWARD DROP [0:0]</code> and the <code>:INPUT DROP [0:0]</code> lines that we are dropping the FORWARD and INPUT traffic by default.  There are also the lines for our specific rules, which process the traffic in the INPUT chain in the order that the rules are listed.  In the future we will take a look at some more advanced firewall rules, along with blocking potential threats on the fly, and making iptables do some NAT!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sophiedogg.com/setting-up-a-linux-firewall/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
