Ipchains Iptables Firewall
Ipchains Iptables Firewall
Ipchains Iptables Firewall
Jeff Muday
Instructional Technology Consultant Department of Biology, Wake Forest University
Must understand TCP/IP thoroughly to take full advantage to the utility. Used in concert with ipmasqadm to provide masqueraded port forwarding.
Kernel Options
CONFIG_FIREWALL CONFIG_IP_FIREWALL CONFIG_IP_MASQUERADE CONFIG_IP_MASQUERADE_ICMP CONFIG_IP_ALWAYS_DEFRAG CONFIG_IP_ADVANCED_ROUTER CONFIG_IP_ROUTE_VERBOSE CONFIG_IP_TRANSPARENT_PROXY
Problems
Not as flexible as iptables of Linux 2.4 Kernel Cannot resolve other network protocols
Such as:
AppleTalk IPX/SPX NetBeui
Security Advice: probably should only run non TCP protocols on private internal network isolated by a separate NIC
Using ipchains
3 Basic chains:
Input
Handles all inbound packets
Forward
Handles packets destined for other computers
Output
Handles packets processed and sent out by local machine
Using ipchains
Each chain is a set of rules Rules are processed sequentially from first to last User can create define custom chains, which must be called by the user.
In practice this is rarely used
Targets defined
If a packet matches the rule it encounters it then has a target action:
ACCEPT it is allowed to be processed DENY it is filtered out and forgotten REJECT it is filtered out and responds with an ICMP message back to the sender of its rejection MASQ the packet is allowed to be masqueraded for routing to internal or external networks REDIRECT the packet can be redirected to another port or user defined chain RETURN returns processing to the calling chain, much like a subroutine return
Syntax: ipchains
ipchains CMD [chain] [rule-spec|num] [options] Commands: -A (append) appends another rule to the end of the specified chain -D (delete) deletes rule in a list. If n is not supplied, then the first rule is assumed. -R (replace) replaces rule in a list. -I (insert) inserts a rule at position n. If n is not supplied, then the rule is placed at the start of the chain.
Commands contd
-L (list) Lists all the rules in a particular chain. If chainname is omitted, it lists all rules in all chains. -F (flush) Clears all rules in the named chain, or it chainname omitted, flushes all rules in all chains. -N (create) creates a user specified chain -X (delete user defined chain) -P policy (set the default policy)
10.1.0.0/16
(matches a class B network 10.1.0.1 to 10.1.255.254)
Masquerading
Kernel must be compiled to support it! Enable forwarding Echo 1 > /proc/sys/net/ipv4/ip_forward Modify /etc/sysconfig/network IP_FORWARD=yes Script:
#!/bin/sh MYNET=192.168.1.0/24 /sbin/ipchains A input i eth1 s $MYNET j ACCEPT /sbin/ipchains A forward s $MYNET j MASQ /sbin/ipchains A forward j DENY -l
Port forwarding
It is fairly easy to forward a port BEHIND the firewall to the outside world. All you need is the ipmasqadm command
You can easily forward ssh, Web, telnet ports to a machine behind the firewall. Note: FTP requires insmod of a masquerade module
# /sbin/ipmasqadm portfw a p tcp \ # L $MYIP $MYWEBPORT \ # R $SERVERIP $SERVERWEBPORT # example suppose your firewall internal IP is # 192.168.1.1 and your private web server is # 192.168.1.10 /sbin/ipmasqadm portfw a p tcp \ -L 192.168.1.1 80 R 192.168.1.10 80
You might choose to use the restore in the /etc/rc.local initialization sequence.
Summary: ipchains
Discussed the purpose of ipchains command Listed the kernel options required to make ipchains function Presented syntax of the command Wrote a simple firewall script Showed how masquerading can be established
Kernel/Network options
CONFIG_NETFILTER CONFIG_IP_NF_CONNTRACK CONFIG_IP_NF_FTP CONFIG_IP_NF_IPTABLES CONFIG_IP_NF_MATCH_STATE CONFIG_IP_NF_MATCH_LIMIT CONFIG_IP_NF_MATCH_UNCLEAN CONFIG_IP_NF_FILTER CONFIG_IP_NF_TARGET CONFIG_IP_NF_TARGET_REJECT CONFIG_IP_NF_NAT CONFIG_IP_NF_TARGET_MASQUERADE CONFIG_IP_NF_TARGET REDIRECT CONFIG_IP_NF_TARGET_LOG
iptables
Three major tables
Filter table, NAT table, Mangle table
Each table has a series of Rule chains The Filter table is analgous to the ipchains command. Like ipchains, it has three chains:
INPUT, FORWARD, and OUTPUT
iptables continued
NAT table
Handles network address translation Contains three chains:
PREROUTING, OUTPUT, and POSTROUTING
Mangle table
Used for packet mangling Has two chains:
PREROUTING and OUTPUT
Syntax: iptables
iptables [-t table] CMD [chain] [rule-spec|num] [options]
Simple firewall
/sbin/iptables t filter A INPUT m state \ --state ESTABLISHED, RELATED j ACCEPT /sbin/iptables t filter A INPUT p udp \ s $DNS source-port domain j ACCEPT
Rules
/sbin/iptables t nat A POSTROUTING o $EXTIF j MASQUERADE /sbin/iptables t nat A POSTROUTING o $EXTIF j SNAT to $MYIP
Example: enable port forwarding to our web server on the private network 192.168.1.10
/sbin/iptables t nat A PREROUTING \ p tcp d $MYIP dport http \ j DNAT to 192.168.1.10:80
Summary: iptables
iptables is a powerful packet filtering and routing tool Listed the kernel options required to make iptables function properly Studied the syntax and function of the iptables command Presented a how-to on simple port forwarding