Comments
bruce.armstrong wrote: Somebody just said it better than I did, and with more chops to say it: Open Letter to Mark Zuckerberg, Sheryl Sandberg & Facebook Mobile
Cloud Expo on Google News


2008 West
DIAMOND SPONSOR:
Data Direct
SOA, WOA and Cloud Computing: The New Frontier for Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
GOLD SPONSORS:
Appsense
User Environment Management – The Third Layer of the Desktop
Cordys
Cloud Computing for Business Agility
EMC
CMIS: A Multi-Vendor Proposal for a Service-Based Content Management Interoperability Standard
Freedom OSS
Practical SOA” Max Yankelevich
Intel
Architecting an Enterprise Service Router (ESR) – A Cost-Effective Way to Scale SOA Across the Enterprise
Sensedia
Return on Assests: Bringing Visibility to your SOA Strategy
Symantec
Managing Hybrid Endpoint Environments
VMWare
Game-Changing Technology for Enterprise Clouds and Applications
Click For 2008 West
Event Webcasts

2008 West
PLATINUM SPONSORS:
Appcelerator
Get ‘Rich’ Quick: Rapid Prototyping for RIA with ZERO Server Code
Keynote Systems
Designing for and Managing Performance in the New Frontier of Rich Internet Applications
GOLD SPONSORS:
ICEsoft
How Can AJAX Improve Homeland Security?
Isomorphic
Beyond Widgets: What a RIA Platform Should Offer
Oracle
REAs: Rich Enterprise Applications
Click For 2008 Event Webcasts
SYS-CON.TV
Top Links You Must Click On


Building an Internet Gateway
Building an Internet Gateway

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.

About Frank Jennings
Frank Jennings works in the Communication Designs Group of Pramati Technologies

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

It should be "-i eth0". A capital "-I" means something else.

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.
One more thing when I run this command its show me error. I am using Red Hat linux 7.3 with default kernel.

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.


Your Feedback
josh wrote: It should be "-i eth0". A capital "-I" means something else.
Mohammad Shakir wrote: 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. One more thing when I run this command its show me error. I am using Red Hat linux 7.3 with default kernel. 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.
Enterprise Open Source Magazine Latest Stories . . .
Grid Dynamics, an eCommerce technology solutions company, and GridGain Systems, makers of an open source in-memory platform for Big Data processing, on Wednesday announced the expansion of their partnership which began in 2008. Grid Dynamics provides personalization and big data solut...
Before embarking on using open source cloud technology for your web property, a basic understanding of cloud, as it’s used in the industry, is essential. While there might be exceptions, here are the definitions. A software application delivered on the web instead of installing standa...
Private clouds solve many problems for enterprises and bring unique operational challenges along with them. There are dozens of companies of all sizes that will build you a private cloud and turn over the keys – then what? Trying to convert a traditional enterprise IT operations team t...
The networking industry has gone through different waves over last 30+ years. In the ’80s, the first wave was all about connecting and sharing; how to connect a computer to other peripheral devices and other computers. There were many players who developed technology and services to ad...
If your organization already uses virtualized infrastructure, you are well on your way to providing IT as a Service. But as businesses demand faster results in today’s competitive market, organizations look to gain more benefits from cloud computing than just virtualized infrastructure...
In this CTO Power Panel at the 10th International Cloud Expo, moderated by Cloud Expo Conference Chair Jeremy Geelan, industry-leading CTOs & VPs of Technology will discuss such topics as: Which do you think is the most important cloud computing standard still to tackle? Who should...
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON Featured Whitepapers
ADS BY GOOGLE