| By Frank Jennings | Article Rating: |
|
| November 10, 2003 12:00 AM EST | Reads: |
16,119 |
After setting up a LAN for your company, the next step is to build a secure Internet gateway for sharing your Internet connection. Fortunately, you don't have to be a geek to set up a gateway and build firewall rules, as it involves minimal open-source software and minor kernel configurations. By setting up a gateway, you allow all the nodes in the subnet to access the Internet through a single secure point. And the gateway takes care of packet masquerading and filtering based on the Iptables rules you build.
Preparing Your System
For securing our gateway we'll use Iptables, an IP packet filter administration tool that comes with Linux kernel 2.4. If you are using the 2.4 kernel, chances are that the Iptables module gets loaded during startup. (Most distributions do that automatically.) If you have the 2.2 kernel and are comfortable with Ipchains, migrating to Iptables won't be a major problem. And if you have 2.0 kernel and are still using ipfwadm for building firewalls, please upgrade your kernel! For setting up a Web page-caching engine, you should download some of the myriad open-source caching software available. If you decide to use Squid, which comes bundled with most distributions, you can set up a transparent http-caching proxy server. However, for this article I'm going to use Smart Cache software, which is Java based. You can download the Java runtime for Linux from http://java.sun.com/linux.
Reconfiguring Your Kernel
If Iptables is not built into your kernel or the Iptables.o module is absent, then you can recompile the kernel with Iptables support. If you do not have the source for your Linux kernel, download the latest source from ftp://ftp.kernel.org. After downloading the source, uncompress and copy into /usr/src directory. You can learn the basics of compiling the kernel from many useful tutorials on the Net. Do a make menuconfig from the console, which will open a Curses based kernel configuration menu as shown in Figure 1. From the kernel option select Network Packet Filtering (Replace Ipchains).
There are two ways of building a kernel. The first way (and a better design) is to highly modularize your kernel, so that the kernel image itself is smaller and lightweight - remember, the image is loaded into RAM during startup. The second way, which I usually use, is to build all necessary options directly into the kernel. Now save your configuration and do make dep modules modules-install bzImage from the console. Once the kernel image and system map file are created, copy them into your /boot directory and modify your LILO configuration from /etc/lilo.conf.
Now the new kernel has Iptables enabled and can be invoked with the iptables command, or if configured as a module can be loaded first using the modprobe tool.
Juggling Packets
You must have already used Ipchains for creating firewall rules. Ipchains lacks many things. It does not boast of a stateful packet inspection like Iptables. From my LAN, I would like to allow all machines to ping others but block all ping packets from the outside world. How do I know if the ICMP Echo response entering the gateway is for the ICMP Echo request, which one of my machines in the LAN has sent? Iptables takes care of all this by examining the state of the traversing packets. And the other facet I love about Iptables is the clear-cut demarcation of packet filtering and packet masquerading. I do packet filtering purely as a firewall need. But I masquerade effectively to NAT the packet. Iptables treat source NAT (SNAT) as masquerading and destination NAT (DNAT) as forwarding. This is so elegant when it comes to writing a firewall script.
Let's start building a simple Iptables script, which can be loaded during startup. By default you can either block all packets in the INPUT, OUTPUT, and FORWARD tables and build rules to accept specific packets, or accept all packets and build rules to deny specific packets.
Here is my configuration script for the gateway:
#!/bin/bash
# Flushing and initializing the chains
iptables -F
iptables -X
iptables -Z
# By default we drop the packets.
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
# Disable response to ping.
echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_all
echo "Disabled Ping"
# Disable response to broadcasts.
echo "0" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
echo "Ignored Broadcasts"
# Enable Packet Forwarding
/bin/echo "1" > /proc/sys/net/ipv4/ip_forward
# Our Loopback device is exempted from Firewall rules
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
# Set our connection burst limit to prevent from SYN Flood
iptables -N syn-flood
iptables -A INPUT -i eth0 -p tcp --syn -j syn-flood
iptables -A syn-flood -m limit --limit 1/s --limit-burst 4 -j RETURN
iptables -A syn-flood -j DROP
# Allow outbound traffic to 80
iptables -A INPUT -i eth0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
# Allow HTTPS.
iptables -A INPUT -i eth0 -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
# ICMP
# We accept icmp in if it is related to other connections or it is part of an
# "established" connection
iptables -A INPUT -i eth0 -p icmp -m state --state ESTABLISHED,RELATED -j ACCEPT
# We always allow icmp out.
iptables -A OUTPUT -o $IFACE -p icmp -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
echo "Prevent Denial of Service Attack"
echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout
echo 1800 > /proc/sys/net/ipv4/tcp_keepalive_time
echo 1 > /proc/sys/net/ipv4/tcp_window_scaling
echo 0 > /proc/sys/net/ipv4/tcp_sack
echo 1280 > /proc/sys/net/ipv4/tcp_max_syn_backlog
echo "Allow Samba Share"
#iptables -A INPUT -p tcp --dport 137 -j ACCEPT
#iptables -A INPUT -p udp --dport 137 -j ACCEPT
#iptables -A INPUT -p tcp --dport 138 -j ACCEPT
#iptables -A INPUT -p udp --dport 138 -j ACCEPT
#iptables -A INPUT -p tcp --dport 139 -j ACCEPT
#iptables -A INPUT -p udp --dport 139 -j ACCEPT
# Drop inbound Port Scans
iptables -t nat -A PREROUTING -I eth0 -d 127.0.0.1 -m psd -j DROPLOG
# Drop packets from host with more than 8 active connections
iptables -t nat -A PREROUTING -I eth0 -p tcp -syn -d 127.0.0.1 -m iplimit \
--iplimit-above 16 -j DROPLOG
# Drop packets related to known viruses.Let us filter out CodeRed
iptables -t filter -A INPUT -I eth0 -p tcp -dport http -m string \
--string "/default.ida?" -j DROP
echo "Some ports should be denied and logged"
iptables -A INPUT -p tcp --dport 1433 -m limit -j LOG \
--log-prefix "Firewalled packet: MSSQL "
iptables -A INPUT -p tcp --dport 1433 -j DROP
iptables -A INPUT -p tcp --dport 6670 -m limit -j LOG \
--log-prefix "Firewalled packet: Deepthrt "
iptables -A INPUT -p tcp --dport 6670 -j DROP
iptables -A INPUT -p tcp --dport 6711 -m limit -j LOG \
--log-prefix "Firewalled packet: Sub7 "
iptables -A INPUT -p tcp --dport 6711 -j DROP
iptables -A INPUT -p tcp --dport 6712 -m limit -j LOG \
--log-prefix "Firewalled packet: Sub7 "
iptables -A INPUT -p tcp --dport 6712 -j DROP
iptables -A INPUT -p tcp --dport 6713 -m limit -j LOG \
--log-prefix "Firewalled packet: Sub7 "
iptables -A INPUT -p tcp --dport 6713 -j DROP
iptables -A INPUT -p tcp --dport 12345 -m limit -j LOG \
--log-prefix "Firewalled packet: Netbus "
iptables -A INPUT -p tcp --dport 12345 -j DROP
iptables -A INPUT -p tcp --dport 12346 -m limit -j LOG \
--log-prefix "Firewalled packet: Netbus "
iptables -A INPUT -p tcp --dport 12346 -j DROP
iptables -A INPUT -p tcp --dport 20034 -m limit -j LOG \
--log-prefix "Firewalled packet: Netbus "
iptables -A INPUT -p tcp --dport 20034 -j DROP
iptables -A INPUT -p tcp --dport 31337 -m limit -j LOG \
--log-prefix "Firewalled packet: BO "
iptables -A INPUT -p tcp --dport 31337 -j DROP
iptables -A INPUT -p tcp --dport 6000 -m limit -j LOG \
--log-prefix "Firewalled packet: XWin "
iptables -A INPUT -p tcp --dport 6000 -j DROP
echo "Forward packets to Smart cache"
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
echo "RULES APPLIED"
The above script is just a sample firewall rule. You can easily modify this to suit your LAN requirements.
Setting up a Caching Engine
If you have more computers in the LAN and would like to speed up browsing and enable offline browsing, you can set up a caching engine in the gateway. Squid is the choice for most system administrators because almost all the distributions come bundled with this caching proxy server. But I prefer Smart Cache, which does most of what Squid does and is written in Java. Smart Cache is open source and can be downloaded from http://freshmeat.net/projects/scache. As the project pages says, it can cache any page and make it available for offline browsing. Other features include a URL filter; cookie filter; ability to fake User-Agents, Referer, and Cookie headers; Web forwarder (accelerator) mode; background downloading; multiple logs; fast operation; and a very configurable garbage collection (the JVM does GC anyway). You can download the source and compile all the java files from the console:
$ $JAVA_HOME/bin/javac *.java
You need Java 2, Standard Edition, which you can get from http://java.sun.com/linux. Now create/modify the scache.cnf file and change the values for caching directory, caching level, and logging directory. You can also allow or deny Internet access for a set of IP addresses on the network. However, there is no feature by which you can authenticate the user with password or SOCKS support. Since the product is open source, I am coding the authentication module for smart Cache as a separate patch, which can be incorporated in the next release. Now that you have compiled the Java files and have the byte code files ready, you can run the program with java scache command. By default Smart Cache listens to port 8080 as Squid listens to 3128. Now we must instruct the kernel to forward all HTTP requests packet coming from the LAN to smart cache. You can do this with a simple command:
iptables -t nat -A PREROUTING -i eth0 -p tcp
--dport 80 -j REDIRECT --to-port 8080
Now the gateway is up and running with a caching server. With any Windows machine change the gateway settings and start browsing.
TCP Reflection
TCP Reflection is a technique by which packets coming to an IP address for a particular port can be redirected to some other port and IP address. Theoretically, I don't find much difference between a proxy server and a TCP reflector. If you have a small network and do not need a caching service running, you can use TCP Reflector instead of Smart Cache. Note: TCP Reflector does not masquerade the packets. It just redirects the packet. You can download the TCP Reflector from http://locutus.kingwoodcable.com/ jfd/java/tcp/tcp.html.
The major advantage of using this software is that the kernel forwards the TCP packet to the application, and the application has the advantage of logging the entire packet, though not ethical in a public network (see Figure 2). Again, TCP Reflector comes with Java source files. Compile it and run from the console:
$ ./reflect -port 8080 -server 12.120.34.23:3128
TCP Reflector will be started at 8080 and will forward all TCP packets to the specified IP address.
Now you know how easy building an Internet gateway is. If you have any problem in setting up, or if you are getting runtime errors/ Iptables errors, please post the error message.
Published November 10, 2003 Reads 16,119
Copyright © 2003 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Frank Jennings
Frank Jennings works in the Communication Designs Group of Pramati Technologies
![]() |
josh 05/16/04 04:32:50 AM EDT | |||
It should be "-i eth0". A capital "-I" means something else. |
||||
![]() |
Mohammad Shakir 11/11/03 06:03:01 AM EST | |||
I read this article and have some confusion, I hope author will solve my problem. In this article only port 80 request will forwarded to proxy/cache server whatever that is. But where will be other request will go like MSN Messenger and so on. iptables -t nat -A PREROUTING -I eth0 -p tcp -syn -d 127.0.0.1 -m iplimit –iplimit above 16 -j DROPLOG iptables v1.2.5: Can't use -I with -A Try `iptables -h' or 'iptables --help' for more information. |
||||
- Ubuntu-based Open Source Linux Mint Tests KDE Version
- Linux Virtualization and Tired Open Source Myths
- IGEL Supports Red Hat Enterprise Virtualization 3.0
- CloudLinux Announces Support for Atomia
- Amazon Kindle Fire Gets Its Own 'Personal Cloud Desktop' with AlwaysOnPC App Launch
- SPIRIT DSP Receives 2011 INTERNET TELEPHONY Product of the Year Award
- Hadoop Quickstart: Use Whirr to automate standup of your distributed cluster on Rackspace
- Jury Gets Novell Antitrust Case Against Microsoft
- The Utility Infrastructure Security Market 2012-2022: Cybersecurity & Smart Grids
- FORTUNE Magazine Names Rackspace Among “100 Best Companies to Work For”
- iFollowOffice Turns to Virtual Bridges and Savvis for On-Demand Virtual Desktop Services
- Convirture Reports Strong 2011 as Virtualization Management Takes Off
- i-Technology in 2012: Five Industry Predictions
- Ubuntu-based Open Source Linux Mint Tests KDE Version
- Amazon to Rent Out Supercomputers
- Amazon Émigré Starts Network Monitoring Firm
- HP’s Putting a Back Door in the Itanium Alamo
- Linux Virtualization and Tired Open Source Myths
- CloudLinux Announces Preferred Partner Program
- MapR Pushes the Hadoop Envelope
- Rightware Announces Gaming Performance Benchmark for OpenGL ES 3.0/Halti
- IGEL Supports Red Hat Enterprise Virtualization 3.0
- CloudLinux Announces Support for Atomia
- 3Dconnexion Announces its Newest 3D Mouse - the SpaceMouse Pro
- The i-Technology Right Stuff
- Linux.SYS-CON.com Exclusive: Linus Discloses *Real* Fathers of Linux
- After Ubuntu, Windows Looks Increasingly Bad, Increasingly Archaic, Increasingly Unfriendly
- A Closer Look at Damn Small Linux
- Linus' Top Ten SCO Barbs
- SCO CEO Posts Open Letter to the Open Source Community
- Netscape Co-Founder's 12 Reasons for Growth of Open Source
- Where Are RIA Technologies Headed in 2008?
- *POINT - COUNTERPOINT SPECIAL* What's Wrong with the Open Source Community?
- Introducing "Cooperative Linux" - Linux for Windows, No Less
- Linux.SYS-CON.com Exclusive: What Would UserLinux Look Like?
- Why Recovering a Deleted Ext3 File Is Difficult . . .





















