You will be setting up a firewall according to the specifications listed below.
$ ./submit.sh...it will make a tarball of all the relevant files and their permissions, including the firewall.sh script you have updated. You must copy or move the created tarball into your group directory, otherwise it will be lost upon swapout. Where's my group directory?! Due to the way DETERLab works, your homedirs are not secure, regardless of the permissions you set there. If you have files relating to homework that should be private, you should store them in your group directory, not your home directory.
WARNING: These scripts do not back up all arbitrary changes you may have made to the node (e.g., changing a peripheral configuration file), and it does not "merge" system files with submission files -- it only restores submission files copied by submit.sh. You shouldn't need to change anything else, but see submit.sh and restore.sh to see exactly what those scripts do, and do not delete any of your submission tarballs so that you can go back to an earlier version if need be.
$ ./restore.sh username-permissions-123123123.tar.gzYou will be asked if you want to automatically overwrite files, and if you want to selectively restore some files and not others. The options are self-explanatory. Finally, if you don't trust the scripts, you can always make your own backups into your group directory and restore them by hand if you prefer.
NOTE: You do not need to run the submit and restore scripts with sudo. However, if you use sudo to run submit.sh, your tarball will be named after the root user. This is OK -- just run sudo ./restore.sh root-permissions-2134234243.tar.gz.
$ iptables -t filter -A INPUT -m state --state NEW -p tcp -s 192.168.0.1 --dport 23 -j REJECTUpon closer inspection, iptables is revealed to be merely a command whose arguments define a single rule for packet filtering based on a number of possible criteria. iptables takes those arguments translates them one command at a time into priority-ordered filter rules in the Linux kernel. Thinking of iptables as a command with arguments can help demystify netfilter and the process of designing firewalls with iptables -- let's break down the above iptables command and translate it into English:
| iptables command arguments | |
| command/argument | translation |
| iptables | We're going to use the iptables tool to insert a new rule into netfilter. |
| -t filter | This rule is going to go in the filter table, which is the built-in packet filtering table. This rule will apply only to: |
| -A INPUT | packets that have been put into the INPUT chain either by the kernel or by some previous rule and which: |
| -m state --state NEW | represent a new connection, |
| -p tcp | are Transmission Control Protocol (TCP) packets, |
| -s 192.168.0.1 | are from the host 192.168.0.1, |
| --dport 23 | and are destined for port 23. |
| -j REJECT | Reject any matching packet. Processing of all packets matching this rule will instantly jump to the built-in target REJECT, which means that the packet will be rejected by the kernel with some kind of network error message. |
$ iptables -p tcp --syn --dport 23 -m connlimit --connlimit-above 2 -j REJECTThis rule (from man iptables) allows 2 telnet connections per client host. Note that this rule uses the connlimit matching module, and rejects additional connections.
$ iptables -A INPUT -i lo -j ACCEPT $ iptables -A OUTPUT -o lo -j ACCEPTThese rules accepts any inbound or outbound traffic on the internal loopback network device (an internal, logical network adapter the kernel uses for network communication internal to the computer) regardless of state, protocol, source, or destination address. The -i lo and -o lo arguments specify the "input interface" and "output interface" the packet arrived on.
$IPTABLES -t filter -A INPUT -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT $IPTABLES -t filter -A OUTPUT -m state --state NEW,RELATED,ESTABLISHED -j ACCEPTThese rules accept all INBOUND and OUTBOUND traffic regardless of interface, address, port or protocol. They use the state matching module, but accept all NEW, RELATED, and ESTABLISHED packets (which is basically all traffic). This rule is basically like having no firewall at all! NEW, RELATED, ESTABLISHED Think of your firewall as a security checkpoint in a big office building. There's usually two lines -- one for people with IDs, and one for people without IDs. If someone already has an ID, they can skip the long line, and go through. If they don't, they have to wait to get an ID card or visitor's pass. This is analogous to the distinction between NEW traffic versus RELATED or ESTABLISHED traffic (which you usually see together). Traffic marked NEW doesn't have an ID badge yet, because it is the first packet of a new stream of traffic. On the other hand, a packet of a RELATED or ESTABLISHED stream is part of something that by definition has already come through the firewall in the past. In other words, the firewall has already given that stream a "badge" (which is really an entry in an internal firewall data structure). Among other things, this means that firewalls are typically structured so that the first section passes all accepted RELATED,ESTABLISHED traffic first, and then carefully allows only certain kinds of NEW traffic. Why do it in that order? While this brief introduction to iptables should point you in the right direction, there are other features of iptables not included here that you may want to use for the exercise. There are many HOWTOs, tips, and tutorials online in addition to the iptables manpage; the exercise manual assumes that in order to complete the iptables exercise, you will need to do some research on your own.
$ sudo nmap yahoo.com Starting Nmap 4.20 ( http://insecure.org ) at 2007-09-22 21:33 PDT Warning: Hostname yahoo.com resolves to 2 IPs. Using 216.109.112.135. Interesting ports on w2.rc.vip.dcn.yahoo.com (216.109.112.135): Not shown: 1694 filtered ports PORT STATE SERVICE 25/tcp open smtp 80/tcp open http 443/tcp open https Nmap finished: 1 IP address (1 host up) scanned in 29.990 seconds $ sudo nmap www.somehost.edu -P0 Starting Nmap 4.20 ( http://insecure.org ) at 2007-09-22 21:34 PDT Interesting ports on dns.somehost.edu (33.xx.111.1): Not shown: 1677 filtered ports PORT STATE SERVICE 53/tcp open domain 80/tcp open http 443/tcp open https 2048/tcp open dls-monitor 2049/tcp closed nfs 2053/tcp open knetd 2064/tcp closed dnet-keyproxy 2065/tcp open dlsrpn 2067/tcp open dlswpn 2068/tcp open advocentkvm 2105/tcp open eklogin 2106/tcp open ekshell 2108/tcp open rkinit 2111/tcp open kx 2112/tcp open kip 2120/tcp open kauth 2121/tcp open ccproxy-ftp 2201/tcp open ats 2232/tcp open ivs-video 2241/tcp closed ivsd Nmap finished: 1 IP address (1 host up) scanned in 32.385 secondsSee the Nmap manpage or online documentation for advanced features.
$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:00:5A:00:01:B3
inet addr:64.81.0.256 Bcast:64.81.40.255 Mask:255.255.255.0
inet6 addr: fe80::200:5aff:fe00:1b3/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1826346 errors:0 dropped:0 overruns:0 frame:0
TX packets:1887951 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:691689933 (659.6 MiB) TX bytes:1037280707 (989.2 MiB)
Interrupt:58
eth1 Link encap:Ethernet HWaddr 00:13:D4:04:44:CA
inet addr:10.10.10.10 Bcast:10.10.10.255 Mask:255.255.0.0
inet6 addr: fe80::213:d4ff:fe04:44ca/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1165519 errors:0 dropped:0 overruns:0 frame:0
TX packets:1549057 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:428484191 (408.6 MiB) TX bytes:1780325755 (1.6 GiB)
Interrupt:50
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:5808042 errors:0 dropped:0 overruns:0 frame:0
TX packets:5808042 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:6895907043 (6.4 GiB) TX bytes:6895907043 (6.4 GiB)
Note that eth0's address is 64.81.0.256, while eth1's address is 10.10.10.10. This means that the two interfaces are on different networks.
$ telnet yahoo.com 80 Trying 66.94.234.13... Connected to yahoo.com. Escape character is '^]'. GET / ... <html><head> ...[web page data] ... </body> </html> Connection closed by foreign host.
$ telnet mailserver.net 25 Trying 216.0.1.1... Connected to mailserver.net. Escape character is '^]'. 220 mailserver.net ESMTP Postfix (Ubuntu) HELO sender.net 250 sender.net MAIL FROM: me@sender.net 250 Ok RCPT TO: me@mailserver.net 250 Ok DATA 354 End data with <CR><LF>.<CR><LF> Subject: test mail message test message . 250 Ok: queued as 152CFB8802A ^] telnet> Connection closed. You have mail in /var/mail/meFor this exercise, you will use telnet to test if a TCP port is open on a remote host. Telnetnetting to an IP and port (see above) should return a "connected" message if it is possible to connect to a running server.
There are too many cool uses of netcat to describe here. For the purposes of this exercise, we'll use netcat to create "fake" TCP or UDP servers that we can use to test firewall configurations.
$ sudo nc -l 80 # we need sudo because 80 is a privileged port... to start a listening TCP socket on port 80. Then, from the another host, you can either use telnet or nc to connect to the server you just started on the the first host. You should be able to type in one window and see output in the other if the network pipe is open. Testing UDP services is exactly the same -- you can use netcat for that, too. You need to start a listening UDP process on the receiving side, and a sending process on the sending side. If you are testing UDP traffic from a client to a server, you can do something like this:
[server]$ nc -u -l 10000 # listen for UDP traffic on port 10000Then on the client, do something like this:
[client]$ nc -u server 10000 # connect to server via UDP on port 10000After establishing the connection, enter some data from standard input (probably your keyboard). Input on the sender should appear on the receiving terminal. Hit ^C to close the programs. UDP is of course an unreliable network protocol, so it's possible that there will be errors in the text file. You can do any of this in reverse to test the connection from a server to a client. If you're able to transmit data, then the firewall is allowing communication.
/root/firewall/firewall.sh... as the root user or using sudo:
sudo /root/firewall/firewall.shThis will load the rules and start enforcing them. To make sure that you are removing all iptables rules, you should run extingui.sh in between every invocation of firewall.sh or rules might "stick around" which can be very confusing if you are trying to debug the system. This can be done like this as root (or with sudo as above):
/root/firewall/extingui.shIf you make the server inaccessible with broken rules, don't worry -- you can reboot the node in the DETER console, and since the firewall is not enabled by default, you can log in in order to fix it. Your files will still be on the experimental node as long as you don't swap out the experiment. (Of course, you can permanently save your files in your home directory.) Finally, only your final product is evaluated -- not the number of times you have to reboot the server. You should expect to lock yourself out a few times.
ETH="eth0"You can use this variable with the token $ETH -- the shell will substitute in its value at runtime. Use ifconfig to make sure that ETH is set to the right value for your experimental node (or update it). For example, use ETH in a hypothetical iptables command like this:
iptables -A INPUT -i $ETH -j ACCEPTThis way, you only need to update the ETH variable if your interface changes, rather than every iptables call that specifies an interface.
[client]$ telnet server 80 #testing apache Trying 10.1.1.3... Connected to server. Escape character is '^]'. ^] #hit control-] to escape telnet> quit Connection closed. [client]$ telnet server 3306 #testing mysql Trying 10.1.1.3... Connected to server. # if you see this, you're connected Escape character is '^]'. [possibly some garbage or the server may disconnect you automatically] ^] telnet> quit Connection closed.
Note: The MySQL server has its own internal permissions system -- so it may not like your client, or your IP address -- but if you see "Connected to..." or receive messages back from the MySQL server (even if the messages say "access denied" -- you are able to contact the server. iptables/netfilter does not send error messages to clients!
[client]$ telnet server 22 #testing ssh Trying 10.1.1.3... Connected to server. Escape character is '^]'. SSH-1.99-OpenSSH_4.2 ^] telnet> quit Connection closed.Testing ping is dead simple:
[client]$ ping server PING server-link0 (10.1.1.3) 56(84) bytes of data. 64 bytes from server-link0 (10.1.1.3): icmp_seq=0 ttl=64 time=1.14 ms ^C --- server-link0 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 1.140/1.140/1.140/0.000 ms, pipe 2 [client]$Of course, to test that in the other direction, just do ping client from the server. Testing outbound services from the server is a bit harder, because DETER is a secure testbed and won't let you actually get out to the Internet. But we can fake some services running on the client to test. See our discussion of netcat for more information.
If you have misconfigured your firewall, and it "locks you out," you can try to reboot your experimental nodes using the DETER interface. If that does not work, you will need to swap your nodes out and back in again, but beware that swapping your nodes out will destroy any work you have not backed up in your home directory.
Make sure you are using the environment variable to define the interface you are restricting -- this will help keep you from getting "locked out." See the tips and tricks section for more information.