Difference between revisions of "OPS335 Firewall Lab"
Line 129: | Line 129: | ||
===Best Practices=== | ===Best Practices=== | ||
− | + | <table border="1" cellspacing="0" cellpadding="5" style="margin-left:51px;"> | |
− | :*When you install Iptables in CentOS it already has some rules predefined. | + | <tr><td>'''Tip'''</td><td>'''Explanation'''<br>''(command)''</td></tr> |
− | :*You will always get the default rules if you restart the iptables service. | + | <tr> <td>'''Always start with the default iptables settings'''</td><td>#::*When you install Iptables in CentOS it already has some rules predefined. |
+ | :*You will always get the default rules if you restart the iptables service.</th></tr> | ||
+ | |||
+ | <tr> <td>Obtain Default Gateway</td><th>route -n</th></tr> | ||
+ | |||
+ | <tr> <td>Obtain DNS Server</td><td>nslookup</td></tr> | ||
+ | |||
+ | <tr> <td>Obtain Hostname</td><td>uname -n</td></tr> | ||
+ | |||
+ | </table> | ||
+ | |||
+ | |||
Revision as of 08:17, 23 January 2016
Contents
PACKET FILTERING USING IPTABLES
OVERVIEW & PREPARATION
In this lab, you will learn how to use iptables to build a simple Linux firewall on your servers.
iptables is a very complex topic. Fortunately, you are not required to become an "iptables expert", but by the end of the course, you should be able to use iptables to properly secure your servers.
You were exposed to iptables in your OPS235 course. You should refer to those notes or find and use documentation to learn how to complete these tasks. You can also ask your professor or lab assistant during the lab for help when using iptables. Some basic iptables commands are provided in this lab for reference, but it is also essential that you know how to obtain help (man pages and online) in order to become self-reliant.
iptables Resources
Some documentation to get started with (you'll need to find more):
- Week 3 Notes Recommended to review and understand prior to performing this lab.
- Overview A excellent concise overview of iptables (ignore diagram).
- CentOS Wiki Listing of basic commands (not all required to know).
How Firewalls (iptables) Relate to the Labs in this Course
We will use an example of setting up a firewall to secure a web server. You will be installing, configuring, protecting, and maintaining a web-server of one of your VMs in a later lab.
The diagram displayed below shows how iptables can be used with a web-server:
There are some important things to be aware of in terms of this diagram:
- There are two sets of IPtables rules (chains) that apply: OUTPUT/INPUT on the client and INPUT/OUTPUT on the server.
It is important to think about from the perspective from the client as well as the server.
- There are two sets of IPtables rules (chains) that apply: OUTPUT/INPUT on the client and INPUT/OUTPUT on the server.
- Outbound traffic from a server (in our case the web-server) is rarely blocked unless there is a security policy to prevent some kind of traffic.
Even in that case, that security policy is usually performed on a router (this is a topic which will be discussed later in this course).
- Outbound traffic from a server (in our case the web-server) is rarely blocked unless there is a security policy to prevent some kind of traffic.
- Inbound traffic is of two distinct types. Our diagram shows:
- New incoming connections (what you normally think of as inbound traffic): the web server receives a new incoming connection.
- Incoming data that's a response to a request: the web page that the server sent back in the diagram above.
- We normally don't want to do anything special for the response. It is safe to assume that a connection that was allowed to be established should be allowed to receive a response. This is accomplished with the following INPUT chain rule that should be there by default on your machines:
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
- We normally don't want to do anything special for the response. It is safe to assume that a connection that was allowed to be established should be allowed to receive a response. This is accomplished with the following INPUT chain rule that should be there by default on your machines:
- Rules are applied to: chains (e.g. input/output), protocols (e.g. tcp/udp/icmp), and ports (e.g. 22, 80, 443).
- For the request, the source port (sport) is 40112 and the destination port (dport) is 80
- For the response, the source port is 80 and the destination port is 40112
- Since the RELATED,ESTABLISHED rule already exists, we are only concerned about controlling the incoming traffic on the server, which in our example, the chain is: INPUT, the protocol is: tcp, and the destination is: port 80.
- Basically all other services that you learn to install, configure, and maintain will required the same knowledge just discussed.
Critical iptables Elements
This may seem like another task to perform, but it is an essential task! You need to "become one" with basic iptables and focus on these important elements on this section, since you will be troubleshooting MANY connection issues with MANY VMs for labs and assignments! You need to become comfortable when using iptables to not only set policy, but troubleshoot and fix mistakes when you set your firewall policies!
... the more you practice and get comfortable with iptables, the quicker you will be able to isolate and fix connection issues...
We don't expect you to become firewall experts, but there are some basics you need to become familiar for this and future labs:
- What is a chain?
- Which chain applies to which traffic?
- What's the default action for a chain and when that applies?
- What order the rules are executed in?
- Reading and/or creating a rule for a specific service. That includes a basic understanding of:
- Ports
- Protocols
- The best way to learn that is to practice.
Record steps, commands, and your observations from this section in your OPS335 lab log-book
INVESTIGATION 1: PREPARATION & GETTING TO KNOW IPTABLES COMMANDS
Confirming Existing Network Connections
Before proceeding with iptables, we should first verify that your host machine and vms can connect with one another. We can also take the opportunity to record some observations which could be used for future labs.
Perform the Following Steps:
- Find the MAC address of the virtual network device on the host and the IP address assigned to it. Record this information in your lab log book.
- Launch all three of your VMs.
- For each VM:
- Login as root.
- Find the MAC address of the virtual NIC and the IP address assigned to it. Record this information on your lab log book.
- Change to your host machine, open a terminal window, and perform the following connectivity tests for each vm:
ping -c 1 [ip-of-vm]
ssh [ip-of-vm]
Default vs Updated Firewall Rules for VMs
You should have learned in OPS235 how to view existing iptables rules with the command: iptables -L.Although you may assume that this listing of rules should be empty, they may not be!
There might be default rules that already exist, or that exist when the firewall is reset to default (i.e. iptables -F).
In fact, several rules were automatically added to your chains because you are using a virtual network. As an exercise, we will determine which of those rules when running a virtual network were added.
Perform the Following Steps:
- Run iptables -L but redirect the output to a text file called before.txt (you will be using this file later).
- Shutdown all of your VMs.
- Stop the libvirtd service
- Restart the iptables service
- Re-issue iptables -L commands making certain to redirect output to a second file (after.txt). This should provide a listing of the new state of your firewall settings.
- You now should have two text files representing the before and after states of your firewall. Compare differences between these two files using the diff command (You should have used this tool in ULI101).
- Run diff -u before.txt after.txt and figure out how to read the output.
TIP: You can also install a graphical tool that makes it much easier to see differences: kompare before.txt after.txt - You can use these tools to compare any two text files, they often come in handy. Note in your lab logbook the iptables rules that were added automatically by the libvirtd service.
Simple Rule Changes
We will run some iptables commands to practice and get a basic understanding of how to set rules.
- First, issue an iptables command to set the policy to disable all inbound traffic. Issue the iptables command to list rules for verification. The remaining tasks will relate to that same inbound traffic chain.
- Issue an iptables command to delete the default ssh rule, and issue another iptables command to verify.
- Issue an iptables command to insert the SSH rule in the beginning instead of appending to the bottom. Issue a command to verify, then delete that rule (by number), and verify that it was deleted.
- Issue an iptables command to append the SSH rule to the end of that chain, verify, delete that same rule, and verify.
- Issue an iptables command to delete the related,established rule. Test your network connectivity between your hosts and vms. What happened?
- Issue an iptables command to restore your firewall to its default settings.
Record steps, commands, and your observations in INVESTIGATION 1 in your OPS335 lab log-book
INVESTIGATION 2: BEST PRACTICES & CREATING CUSTOMIZED CHAINS
In this investigation, we will use shell scripting to help automate our firewalls, and create our own customized chains for packet filtering.
Best Practices
Tip | Explanation (command) |
Always start with the default iptables settings | #::*When you install Iptables in CentOS it already has some rules predefined.
|