Computer Science 530 - Lab Assignment #6 -- Fall 2025

Part 1 - Mechanics of Firewall Construction using nftables

Setting up this experiment

Scripts are used. A set of them is provided for each experiment. Please see the "Virtual Machine Usage" section of this document.

These scripts may be found in the appropriae directory (corresponding to your archiecture) as lab6scripts.windows or lab6scripts.linapp from the google drive folder for 2021 Lab 6.

A summary of the scripts' use and intended execution order follows.

"vmconfigure-populate" is first, creates machines
"vmconfigure-construct-network" is next, if it exists
"vmconfigure-guestOS-internal-settings" is next, it makes the internal settings after powering the machines on;
      so if this one exists "vmconfigure-poweron" is not needed
"vmconfigure-poweron" is next, if there is no "vmconfigure-guestOS-internal-settings"

Now you can use your VM(s). When you are finished:

"vmconfigure-poweroff"
"vmconfigure-destroy" if you want to delete the machines, but not if you plan to come back to them later

If you do come back to them later,

"vmconfigure-guestOS-internal-settings" should be repeated, if present
"vmconfigure-poweron" if there is not "vmconfigure-guestOS-internal-settings" present

 

In this exercise you will observe the effect of individual firewall rules created with the nft command. A firewall is a cumulative, multiple collection of such rules.

Preliminaries

First, start your two VirtualBox machines SERVER and CLIENT.
[ note fall 2022 - if you did the "sniffing" exercise previously, which uses identically named VMs, remove them (which can be done using the vmconfigure-destroy script from the previous lab) because they will clash with this exercise ]
Log in to each of them as user student. Then start a graphical interface on each by running:

startx

In the GUI launch two terminal windows in each machine (click "Activities," right click on the terminal icon, choose "New Window ). Become root in each window:

sudo su -

Place the terminal windows in the upper left and lower right corners of the desktops. Your screen should look something like this:

 

You will interact two programs, "client4.c" and "server4.c." They are in directory /home/public/. Put copies of them in the root user's home directory. To do so execute the following command once on machine "server":

cp  /home/public/server4.c  /root

and this one once on machine "client":

cp  /home/public/client4.c  /root

(Don't overdo it. In each VM you have 2 windows open but you need only execute the above commands in one of them to get the job done on its machine.) Then go to root's home directory, in all 4 windows:

cd

We're going to run the server4 program on machine "server" and its client4 counterpart on machine "client". Compile the programs on their respective machines (you may get compiler warnings). On machine "server":

gcc  -o  server4  server4.c

and on machine "client":    

gcc  -o client4  client4.c

This exercise interacts the two programs on their two computers. Below, we adopt the color-coding convention that commands to be executed on machine "client" are shown in blue, those to be executed on machine "server" in red, like this:
 blue - to be performed on client
 red - to be performed on server
 black - to be performed on both


Clearing and examining any existing firewall

Our tool for building firewalls is nft, which presumably stands for net filter tables. Tables hold chains, which in turn hold rules. On both computers create a table and populate it with two chains, one for incoming the other for outgoing traffic. Separately on each computer:

nft list tables
nft add table ip mytable
nft list table mytable

nft 'add chain ip mytable myinputchain  { type filter   hook input     priority 1; policy accept; }'       single quotes are important to protect special characters from the shell
nft list table mytable

nft 'add chain ip mytable myoutputchain { type filter   hook output   priority 1; policy accept; }'
nft list table mytable

On both computers, make sure there are no firewall rules by flushing any that may exist (none should, on these virgin machines, so this is a conservative step for good measure or if you want to start over):

nft flush table ip mytable

Then list any rules, to confirm the flush:

nft list table mytable

Notice that no rules appear; also notice the policies shown, which you set to "accept."

We're going to start with an initial open state without any firewall barriers; the client and server will be able to freely interact. Then, we'll deliberately prevent interaction by erecting the limiting firewall rules necessary for blockage. Later, we'll start over with a closed system, and deliberately allow interaction by constructing the liberating firewall rules necessary for passage.

Prepare a test conversation to control with the firewall

The two VMs should have a network between them, to which they connect using their interfaces known to VirtualBox as "Adapter 1", which in linux appear named as "enp0s3". Client and server should have respective IP addresses 192.168.1.2 and 192.168.1.1.

Below you'll be asked to enter the client's and server's IP addresses a number of times. For convenience, let's record them in variables to avoid the possible confusion of repeatedly remembering them. In all 4 windows:

SERVERIP=192.168.1.1 ; CLIENTIP=192.168.1.2

(Why do we do this for all 4 windows, when there are only 2 machines? Because there are separate shells per window, and these environment variables are per shell. Not per machine.)

The program server4 runs on a port you can specify, by supplying the port number on server4's command line. client4 tries to communicate to an IP and port you dictate, both expressed on client4's command line. client4 sends a single character "R" to the designated IP and port. server4 increments it and sends the result, single character "S", back to the client. Let's test the programs.

First run tcpdump in the lower-right window of each machine:

tcpdump -nnti enp0s3 host <local machine's own IP>

(The interface names are supposed to be enp0s3 but just in case you are performing this experiment on a machine with a different interface name than that, replace "enp0s3" with the actual interface name. Use that name in these instructions  wherever I have used enp0s3, as in the above command for example. You can examing all the interfaces present with command "ifconfig -a".)

Now let's generate the traffic we want to study. On the server, in its upper-left window, tell server4 to listen on port number 2345:

./server4 2345

On the client, in its upper-left window, run client4, directing it to the IP of server4's machine, and server4's port :

./client4  $SERVERIP  2345

The client will see the letter "S" returned from the server. On both machines, look at the tcpdump (lower-right) windows to check the displayed packet traffic. Look closely at these, observing the addresses, ports, and traffic directions.

We're going to want an independent "second opinion" program, so that when we block client4/server4 we can see whether other things also get blocked or not. That is, we're interested in the precision of our blockage. For that, just have the client try to ping the server. In the client's program window (upper-left):

ping  -c 1  $SERVERIP

Make sure it works and observe the tcpdump output.


Blocking client4/server4 with nftables

We will obstruct the client4/server4 conversation 4 different ways. The conversation consists of 2 packet journeys. First, the client request travels from client to server. Thereafter, the server response travels from server to client. (You saw this in tcpdump.) So, there are 4 ways we could interfere: 

1. block request from leaving client, or
2. block request from entering server, or
3. block reply from leaving server, or
4. block reply from entering client

First, let's block the request from leaving the client. On the client machine (upper left) please execute:

nft add rule mytable myoutputchain oifname "enp0s3" tcp sport 1024-65535 tcp dport 2345 ip saddr $CLIENTIP ip daddr $SERVERIP drop

Examine the result:

nft list table mytable

Note that your just-created rule is shown. Test the programs again as detailed above (i.e., run the client), including the simultaneous tcpdump runs (tcpdump should remain up-and-running in the 2 right-hand windows). The conversation is blocked. Interrupt the client (ctrl-C) and run ping from there again. Is it blocked now too?

Undo the blockage by deleting the blocking rule. You must delete a rule by its "handle number." Find out what the handle number it by running:

nft list table mytable -a

and observing it. Then remove the rule by:

nft delete rule mytable myoutputchain handle <handle number>

Check that the blocking rule is gone:

nft list table mytable

Second, let's block the request from entering the server. On the server (upper left), interrupt server4 (ctrl-C) if it's running and then:

nft add rule mytable myinputchain    iifname "enp0s3" tcp sport 1024-65535 tcp dport 2345 ip saddr $CLIENTIP ip daddr $SERVERIP drop

Test (re-execute server4 in order to do so). The client4/server4 conversation is blocked again. But the tcpdump story is a little different. How? What about ping-- blocked or working?

Undo the blockage:

nft list table mytable -a
nft delete rule mytable myinputchain handle <handle number>

Third, let's block the reply from leaving the server. At the server (upper left):

nft add rule mytable myoutputchain oifname "enp0s3" tcp sport 2345 tcp dport 1024-65535 ip saddr $SERVERIP ip daddr $CLIENTIP drop

Test (re-execute server4 in order to do so). The client4/server4 conversation is blocked again. What's going on in the two tcpdump windows? What about ping? Run it again. Blocked or working?

Stop the server again and undo the blockage:

nft list table mytable -a
nft delete rule mytable myoutputchain handle <handle number>

Fourth, let's block the reply from entering the client (upper left):

nft add rule mytable myinputchain iifname "enp0s3" tcp sport 2345 tcp dport 1024-65535 ip saddr $SERVERIP ip daddr $CLIENTIP drop

Test (relaunch server4 in order to do so). And what about ping?

Undo the blockage:

nft list table mytable -a
nft delete rule mytable myinputchain handle <handle number>

Doing the opposite-- pessimistic instead of optimistic approach

Now we will largely repeat the exercise. Except, instead of using an open policy and selectively blocking our test program, we want to have a closed policy and selectively allow it.

Let's edit the chains' current "drop" policy with "accept" instead. On both machines:

nft chain ip mytable myinputchain    '{ policy drop; }'
nft chain ip mytable myoutputchain  '{ policy drop; }
'

At this point your two machines are in a state of total lock-down. None of the 4 necessary components of the client4/server4 conversation is possible. That is, the request can neither depart nor arrive, and same for the (hypothetical) reply. Fix each one of these issues and try running client4 after each command (of course server4 still runs continuously on the server machine-- if not, restart it-- so if everything eventually does get through the client will display the new letter). The conversation won't fully and finally succeed until after the 4th command since each blockage individually is sufficient to stop the whole show. Here are the commands (don't forget to adjust for the actual interfaces your machines may be using, if not enp0s3).

On client:

nft add rule mytable myoutputchain oifname "enp0s3" tcp sport 1024-65535 tcp dport 2345 ip saddr $CLIENTIP ip daddr $SERVERIP accept

Test client4-server4 interaction.

On server:

nft add rule mytable myinputchain iifname "enp0s3" tcp sport 1024-65535 tcp dport 2345 ip saddr $CLIENTIP ip daddr $SERVERIP accept

Test client4-server4 interaction.

nft add rule mytable myoutputchain oifname "enp0s3" tcp sport 2345 tcp dport 1024-65535 ip saddr $SERVERIP ip daddr $CLIENTIP accept

Test client4-server4 interaction.

on client again:

nft add rule mytable myinputchain iifname "enp0s3" tcp sport 2345 tcp dport 1024-65535 ip saddr $SERVERIP ip daddr $CLIENTIP accept

Test client4-server4 interaction.

After running all these, client4/server4 should work as intended. Until running them, all, client4/server4 should remain blocked. For reference, here are the resulting firewalls, shown on server node2 and client node3. Note the policies are all "DROP." Note that there are 4 rules, each to "accept" the client4-server4 traffic packets in accord with the interfaces they are traversing, the direction in which they are doing so, the machine (ip) addresses from and to which they designated themselves, and the application (port) addresses from and to which they designate themselves.

What about ping? In the optimistic scenario whenever we blocked the conversation between client4 and server4, ping kept working unaffected. But here, in the pessimistic scenario, can the client still ping the server?

ping  -c 1  $SERVERIP

Can the client at least ping itself?

ping  -c 1  $CLIENTIP
ping  -c 1  127.0.0.1

Why not?

Now that the experiment is over, if you wish to use the two machines any further you should do away with the chains you have used. On both machines:

nft list table mytable
nft flush ruleset
nft list table mytable
nft list ruleset

Your table, along with its contained chains and their contained rules, vanishes. Now inter-machine communication is again uninhibited, unregulated, unfirewalled.

Close the 4 terminal windows. In each, issue a series of "exit" command (or equivalently, ctrl-D keystrokes) till the window collapses.