Linux Containers Authors: Liz McMillan, Elizabeth White, Zakia Bouachraoui, Pat Romanski, Stefana Muller

Related Topics: Linux Containers

Linux Containers: Article

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:

# 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

# 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 -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 -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


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

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.

More Stories By Frank Jennings

Frank Jennings works in the Communication Designs Group of Pramati Technologies

Comments (2)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

IoT & Smart Cities Stories
Moroccanoil®, the global leader in oil-infused beauty, is thrilled to announce the NEW Moroccanoil Color Depositing Masks, a collection of dual-benefit hair masks that deposit pure pigments while providing the treatment benefits of a deep conditioning mask. The collection consists of seven curated shades for commitment-free, beautifully-colored hair that looks and feels healthy.
The textured-hair category is inarguably the hottest in the haircare space today. This has been driven by the proliferation of founder brands started by curly and coily consumers and savvy consumers who increasingly want products specifically for their texture type. This trend is underscored by the latest insights from NaturallyCurly's 2018 TextureTrends report, released today. According to the 2018 TextureTrends Report, more than 80 percent of women with curly and coily hair say they purcha...
The textured-hair category is inarguably the hottest in the haircare space today. This has been driven by the proliferation of founder brands started by curly and coily consumers and savvy consumers who increasingly want products specifically for their texture type. This trend is underscored by the latest insights from NaturallyCurly's 2018 TextureTrends report, released today. According to the 2018 TextureTrends Report, more than 80 percent of women with curly and coily hair say they purcha...
We all love the many benefits of natural plant oils, used as a deap treatment before shampooing, at home or at the beach, but is there an all-in-one solution for everyday intensive nutrition and modern styling?I am passionate about the benefits of natural extracts with tried-and-tested results, which I have used to develop my own brand (lemon for its acid ph, wheat germ for its fortifying action…). I wanted a product which combined caring and styling effects, and which could be used after shampo...
The platform combines the strengths of Singtel's extensive, intelligent network capabilities with Microsoft's cloud expertise to create a unique solution that sets new standards for IoT applications," said Mr Diomedes Kastanis, Head of IoT at Singtel. "Our solution provides speed, transparency and flexibility, paving the way for a more pervasive use of IoT to accelerate enterprises' digitalisation efforts. AI-powered intelligent connectivity over Microsoft Azure will be the fastest connected pat...
There are many examples of disruption in consumer space – Uber disrupting the cab industry, Airbnb disrupting the hospitality industry and so on; but have you wondered who is disrupting support and operations? AISERA helps make businesses and customers successful by offering consumer-like user experience for support and operations. We have built the world’s first AI-driven IT / HR / Cloud / Customer Support and Operations solution.
Codete accelerates their clients growth through technological expertise and experience. Codite team works with organizations to meet the challenges that digitalization presents. Their clients include digital start-ups as well as established enterprises in the IT industry. To stay competitive in a highly innovative IT industry, strong R&D departments and bold spin-off initiatives is a must. Codete Data Science and Software Architects teams help corporate clients to stay up to date with the mod...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Druva is the global leader in Cloud Data Protection and Management, delivering the industry's first data management-as-a-service solution that aggregates data from endpoints, servers and cloud applications and leverages the public cloud to offer a single pane of glass to enable data protection, governance and intelligence-dramatically increasing the availability and visibility of business critical information, while reducing the risk, cost and complexity of managing and protecting it. Druva's...
BMC has unmatched experience in IT management, supporting 92 of the Forbes Global 100, and earning recognition as an ITSM Gartner Magic Quadrant Leader for five years running. Our solutions offer speed, agility, and efficiency to tackle business challenges in the areas of service management, automation, operations, and the mainframe.