What is NAT “Network Address Translation“ STEPS
- PREROUTING
- ROUTING
- POSTROUTING
- OUT
- LOCALPROCESS
- POSTROUTING
- OUT
- POSTROUTING
- POSTROUTING
- ROUTING
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$> echo "1" > /proc/sys/net/ipv4/ip_forward # Load various modules. Usually they are already loaded # (especially for newer kernels), in that case # the following commands are not needed. # Load iptables module: $> modprobe ip_tables # activate connection tracking # (connection's status are taken into account) $> modprobe ip_conntrack # Special features for IRC: $> modprobe ip_conntrack_irc # Special features for FTP: $> modprobe ip_conntrack_ftp |
Input Rules – INPUT ACCEPT or Reject
1 2 3 4 5 6 7 8 9 10 11 12 |
iptables -A INPUT -i eth0 -p tcp --dport 443 -j ACCEPT # Deleting - iptables -D INPUT -i eth0 -p tcp --dport 443 -j ACCEPT sudo iptables -t nat -D PREROUTING 1 # Add with ORDER - TABLE ROW COUNT iptables -I INPUT 1 -i eth0 -j ACCEPT # iptables -I INPUT 1 -i lo -j ACCEPT |
1 |
Listing
1 2 3 4 5 6 7 |
sudo iptables -t filter -L INPUT --line-numbers -n -v sudo iptables -t filter -L FORWARD --line-numbers -n -v sudo iptables -t filter -L INPUT --line-numbers -n sudo iptables -t filter -L OUTPUT --line-numbers -n sudo iptables -t filter -L FORWARD --line-numbers -n sudo iptables -t nat -L --line-numbers -n |
Delete Rules
1 2 3 4 |
iptables -D INPUT -s 127.0.0.1 -p tcp --dport 111 -j ACCEPT # This is iptables -D INPUT 4 Line Rule Id - their number and chain name: |
Forwarding
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
- Firstly Allow SYstem to do Forward sysctl -w net.ipv4.ip_forward=1 sysctl -p /etc/sysctl.conf # Example Rules iptables -A FORWARD -i eth1 -j ACCEPT iptables -A FORWARD -o eth1 -j ACCEPT iptables -A FORWARD -i eth0 -p tcp --dport 80 -d 172.31.0.23 -j ACCEPT sudo iptables -A INPUT -i eth0 -d 192.168.1.254 -j ACCEPT ## append rule to FORWARD chain ## sudo iptables -A FORWARD -o virbr0 -d 192.168.122.42 -j ACCEPT sudo iptables -A FORWARD -m state -s 192.168.2.0/24 -d 192.168.122.0/24 --state NEW,RELATED,ESTABLISHED -j ACCEPT |
Postrouting Masquareade
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE # add a rule: $> iptables -t nat -A chain [...] # list rules: $> iptables -t nat -L # remove user-defined chain with index 'myindex': $> iptables -t nat -D chain myindex # Remove all rules in chain 'chain': $> iptables -t nat -F chain # TCP packets from 192.168.1.2: $> iptables -t nat -A POSTROUTING -p tcp -s 192.168.1.2 [...] # UDP packets to 192.168.1.2: $> iptables -t nat -A POSTROUTING -p udp -d 192.168.1.2 [...] # all packets from 192.168.x.x arriving at eth0: $> iptables -t nat -A PREROUTING -s 192.168.0.0/16 -i eth0 [...] # all packets except TCP packets and except packets from 192.168.1.2: $> iptables -t nat -A PREROUTING -p ! tcp -s ! 192.168.1.2 [...] # packets leaving at eth1: $> iptables -t nat -A POSTROUTING -o eth1 [...] # TCP packets from 192.168.1.2, port 12345 to 12356 # to 123.123.123.123, Port 22 # (a backslash indicates contination at the next line) $> iptables -t nat -A POSTROUTING -p tcp -s 192.168.1.2 \ --sport 12345:12356 -d 123.123.123.123 --dport 22 [...] # Options for SNAT (abstract of manual page) --to-source <ipaddr>[-<ipaddr>][:port-port] # Options for DNAT (abstract of manual page) --to-destination <ipaddr>[-<ipaddr>][:port-port] $> iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-ports 8080 $> iptables -t nat -A PREROUTING -p tcp --dport 5000 -j REDIRECT --to-ports 22 # redirect port 5001 to port 110 (POP3) at 123.123.123.123: $> iptables -t nat -A PREROUTING -p tcp --dport 5001 \ -j DNAT --to-destination 123.123.123.123:110 # Change sender to redirecting machine: $> iptables -t nat -A POSTROUTING -p tcp --dport 110 \ -j MASQUERADE $> iptables -t nat -A OUTPUT -p tcp --dport 80 \ -j DNAT --to-destination 111.111.111.111:5002 $> iptables -t nat -A PREROUTING -p tcp -i eth1 --dport 80 -j DNAT --to 192.168.1.2 |
Pre Routing
1 2 3 4 |
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT \ --to 172.31.0.23:80 iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT \ --to-destination 10.0.4.2:80 |
- Deny User
- Allow port for a user.
Great http://www.thegeekstuff.com/2011/06/iptables-rules-examples/ Kutay Zorlu Introduction Iptables is a firewall that plays an essential role in network security for most Linux systems. While many iptables tutorials will teach you how to create firewall rules to secure your server, this one will focus on a different aspect of firewall management: listing and deleting rules. In this tutorial, we will cover how to do the following iptables tasks: List rules Clear Packet and Byte Counters Delete rules Flush chains (delete all rules in a chain) Flush all chains and tables, delete all chains, and accept all traffic Note: When working with firewalls, take care not to lock yourself out of your own server by blocking SSH traffic (port 22, by default). If you lose access due to your firewall settings, you may need to connect to it via the console to fix your access. Once you are connected via the console, you can change your firewall rules to allow SSH access (or allow all traffic). If your saved firewall rules allow SSH access, another method is to reboot your server. Prerequisites Before you start using this tutorial, you should have a separate, non-root superuser account—a user with sudo privileges—set up on your server. If you need to set this up, follow the appropriate guide: Initial Server Setup with Ubuntu 14.04 Initial Server Setup with CentOS 6 Let’s look at how to list rules first. There are two different ways to view your active iptables rules: in a table or as a list of rule specifications. Both methods provide roughly the same information in different formats. List Rules by Specification To list out all of the active iptables rules by specification, run the iptables command with the -S option: sudo iptables -S Example: Rule Specification Listing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
-P INPUT DROP -P FORWARD DROP -P OUTPUT ACCEPT -N ICMP -N TCP -N UDP -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m conntrack --ctstate INVALID -j DROP -A INPUT -p udp -m conntrack --ctstate NEW -j UDP -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m conntrack --ctstate NEW -j TCP -A INPUT -p icmp -m conntrack --ctstate NEW -j ICMP -A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable -A INPUT -p tcp -j REJECT --reject-with tcp-reset -A INPUT -j REJECT --reject-with icmp-proto-unreachable -A TCP -p tcp -m tcp --dport 22 -j ACCEPT |
As you can see, the output looks just like the commands that were used to create them, without the preceding iptables command. This will also look similar to the iptables rules configuration files, if you’ve ever used iptables-persistent or iptables save. List Specific Chain If you want to limit the output to a specific chain (INPUT, OUTPUT, TCP, etc.), you can specify the chain name directly after the -S option. For example, to show all of the rule specifications in the TCP chain, you would run this command: sudo iptables -S TCP Example: TCP Chain Rule Specification Listing -N TCP -A TCP -p tcp -m tcp –dport 22 -j ACCEPT Let’s take a look at the alternative way to view the active iptables rules, as a table of rules. List Rules as Tables Listing the iptables rules in the table view can be useful for comparing different rules against each other, To output all of the active iptables rules in a table, run the iptables command with the -L option: sudo iptables -L This will output all of current rules sorted by chain. If you want to limit the output to a specific chain (INPUT, OUTPUT, TCP, etc.), you can specify the chain name directly after the -L option. Let’s take a look at an example INPUT chain: sudo iptables -L INPUT Example: Input Chain Rule Table Listing Chain INPUT (policy DROP) target prot opt source destination ACCEPT all — anywhere anywhere ctstate RELATED,ESTABLISHED ACCEPT all — anywhere anywhere DROP all — anywhere anywhere ctstate INVALID UDP udp — anywhere anywhere ctstate NEW TCP tcp — anywhere anywhere tcp flags:FIN,SYN,RST,ACK/SYN ctstate NEW ICMP icmp — anywhere anywhere ctstate NEW REJECT udp — anywhere anywhere reject-with icmp-port-unreachable REJECT tcp — anywhere anywhere reject-with tcp-reset REJECT all — anywhere anywhere reject-with icmp-proto-unreachable The first line of output indicates the chain name (INPUT, in this case), followed by its default policy (DROP). The next line consists of the headers of each column in the table, and is followed by the chain’s rules. Let’s go over what each header indicates: target: If a packet matches the rule, the target specifies what should be done with it. For example, a packet can be accepted, dropped, logged, or sent to another chain to be compared against more rules prot: The protocol, such as tcp, udp, icmp, or all opt: Rarely used, this column indicates IP options source: The source IP address or subnet of the traffic, or anywhere destination: The destination IP address or subnet of the traffic, or anywhere The last column, which is not labeled, indicates the options of a rule. That is, any part of the rule that isn’t indicated by the previous columns. This could be anything from source and destination ports, to the connection state of the packet. Show Packet Counts and Aggregate Size When listing iptables rules, it is also possible to show the number of packets, and the aggregate size of the packets in bytes, that matched each particular rule. This is often useful when trying to get a rough idea of which rules are matching against packets. To do so, simply use the -L and -v option together. For example, let’s look at the INPUT chain again, with the -v option: sudo iptables -L INPUT -v Example: Verbose Listing Chain INPUT (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 284K 42M ACCEPT all — any any anywhere anywhere ctstate RELATED,ESTABLISHED 0 0 ACCEPT all — lo any anywhere anywhere 0 0 DROP all — any any anywhere anywhere ctstate INVALID 396 63275 UDP udp — any any anywhere anywhere ctstate NEW 17067 1005K TCP tcp — any any anywhere anywhere tcp flags:FIN,SYN,RST,ACK/SYN ctstate NEW 2410 154K ICMP icmp — any any anywhere anywhere ctstate NEW 396 63275 REJECT udp — any any anywhere anywhere reject-with icmp-port-unreachable 2916 179K REJECT all — any any anywhere anywhere reject-with icmp-proto-unreachable 0 0 ACCEPT tcp — any any anywhere anywhere tcp dpt:ssh ctstate NEW,ESTABLISHED Note that the listing now has two additional columns, pkts and bytes. Now that you know how to list the active firewall rules in a variety of ways, let’s look at how you can reset the packet and byte counters. Reset Packet Counts and Aggregate Size If you want to clear, or zero, the packet and byte counters for your rules, use the -Z option. They also reset if a reboot occurs. This is useful if you want to see if your server is receiving new traffic that matches your existing rules. To clear the counters for all chains and rules, use the -Z option by itself: sudo iptables -Z To clear the counters for all rules in a specific chain, use the -Z option and specify the chain. For example, to clear the INPUT chain counters run this command: sudo iptables -Z INPUT If you want to clear the counters for a specific rule, specify the chain name and the rule number. For example, to zero the counters for the 1st rule in the INPUT chain, run this: sudo iptables -Z INPUT 1 Now that you know how to reset the iptables packet and byte counters, let’s look at the two methods that can be used to delete them. Delete Rule by Specification One of the ways to delete iptables rules is by rule specification. To do so, you can run the iptables command with the -D option followed by the rule specification. If you want to delete rules using this method, you can use the output of the rules list, iptables -S, for some help. For example, if you want to delete the rule that drops invalid incoming packets (-A INPUT -m conntrack –ctstate INVALID -j DROP), you could run this command: sudo iptables -D INPUT -m conntrack –ctstate INVALID -j DROP Note that the -A option, which is used to indicate the rule position at creation time, should be excluded here. Delete Rule by Chain and Number The other way to delete iptables rules is by its chain and line number. To determine a rule’s line number, list the rules in the table format and add the –line-numbers option: sudo iptables -L –line-numbers [secondary_output Example Output: Rules with Line Numbers] Chain INPUT (policy DROP) num target prot opt source destination 1 ACCEPT all — anywhere anywhere ctstate RELATED,ESTABLISHED 2 ACCEPT all — anywhere anywhere 3 DROP all — anywhere anywhere ctstate INVALID 4 UDP udp — anywhere anywhere ctstate NEW 5 TCP tcp — anywhere anywhere tcp flags:FIN,SYN,RST,ACK/SYN ctstate NEW 6 ICMP icmp — anywhere anywhere ctstate NEW 7 REJECT udp — anywhere anywhere reject-with icmp-port-unreachable 8 REJECT tcp — anywhere anywhere reject-with tcp-reset 9 REJECT all — anywhere anywhere reject-with icmp-proto-unreachable 10 ACCEPT tcp — anywhere anywhere tcp dpt:ssh ctstate NEW,ESTABLISHED … This adds the line number to each rule row, indicated by the num header. Once you know which rule you want to delete, note the chain and line number of the rule. Then run the iptables -D command followed by the chain and rule number. For example, if we want to delete the input rule that drops invalid packets, we can see that it’s rule 3 of the INPUT chain. So we should run this command: sudo iptables -D INPUT 3 Now that you know how to delete individual firewall rules, let’s go over how you can flush chains of rules. Flush Chains Iptables offers a way to delete all rules in a chain, or flush a chain. This section will cover the variety of ways to do this. Note: Be careful to not lock yourself out of your server, via SSH, by flushing a chain with a default policy of drop or deny. If you do, you may need to connect to it via the console to fix your access. Flush a Single Chain To flush a specific chain, which will delete all of the rules in the chain, you may use the -F, or the equivalent –flush, option and the name of the chain to flush. For example, to delete all of the rules in the INPUT chain, run this command:
1 |
sudo iptables -F INPUT |
Flush All Chains To flush all chains, which will delete all of the firewall rules, you may use the -F, or the equivalent –flush, option by itself: sudo iptables -F Flush All Rules, Delete All Chains, and Accept All This section will show you how to flush all of your firewall rules, tables, and chains, and allow all network traffic. Note: This will effectively disable your firewall. You should only follow this section if you want to start over the configuration of your firewall. First, set the default policies for each of the built-in chains to ACCEPT. The main reason to do this is to ensure that you won’t be locked out from your server via SSH:
1 2 3 |
sudo iptables -P INPUT ACCEPT sudo iptables -P FORWARD ACCEPT sudo iptables -P OUTPUT ACCEPT |
Then flush the nat and mangle tables, flush all chains (-F), and delete all non-default chains (-X):
1 2 3 4 |
sudo iptables -t nat -F sudo iptables -t mangle -F sudo iptables -F sudo iptables -X |
Your firewall will now allow all network traffic. If you list your rules now, you will will see there are none, and only the three default chains (INPUT, FORWARD, and OUTPUT) remain.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
IPtables is a stateful firewall * You can understand what the below commands doing. iptables -I INPUT -p tcp -m tcp -s 1.1.1.1 --dport 22 -j ACCEPT iptables -I INPUT -p tcp -m tcp -s 1.1.1.1/29 --dport 22 -j ACCEPT iptables -I INPUT -p tcp -m tcp -s 0.0.0.0/0 --dport 22 -j DROP * Please save the configuration what you did to the /etc/sysconfig/iptables. # iptables-save > /etc/sysconfig/iptables -A INPUT -p tcp -m tcp -s 1.1.1.1 --dport 22 -j ACCEPT -A INPUT -p tcp -m tcp -s 1.1.1.1/29 --dport 22 -j ACCEPT -A INPUT -p tcp -m tcp -s 0.0.0.0/0 --dport 22 -j DROP # BLOCK all others |
# PRE ROutING and OTHERS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# KZCOM -> Exempt udp dns of connection tracking iptables -t raw -A PREROUTING -p udp --sport 53 -j NOTRACK iptables -t raw -A PREROUTING -p udp --dport 53 -j NOTRACK iptables -t raw -A OUTPUT -p udp --sport 53 -j NOTRACK iptables -t raw -A OUTPUT -p udp --dport 53 -j NOTRACK #KZCOM -> allow related, established and untracked packets iptables -A INPUT -m conntrack --ctstate UNTRACKED -j ACCEPT iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT # KZCOM -> allow new tcp dns packets iptables -A INPUT -p tcp --dport 53 -m conntrack --ctstate NEW -j ACCEPT iptables -A INPUT -p tcp --sport 53 -m conntrack --ctstate NEW -j ACCEPT iptables -A INPUT -p tcp --dport 53 -j REJECT iptables -A INPUT -p tcp --sport 53 -j REJECT # in case you do not use the UNTRACKED ACCEPT rule above because you # want to be more restrictive, use this instead # iptables -A INPUT -p udp --dport 53 -j ACCEPT # iptables -A INPUT -p udp --sport 53 -j ACCEPT #KZCOM -> allow new ssh packets iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -j ACCEPT iptables -A INPUT -p tcp --dport 22 -j REJECT #KZCOM -> allow new HTTP packets Apache or nginx ! iptables -A INPUT -p tcp --dport 80 -m conntrack --ctstate NEW -j ACCEPT iptables -A INPUT -p tcp --dport 80 -j REJECT # drop all invalid packets iptables -A INPUT -m conntrack --ctstate INVALID -j DROP |