Networking

These posts discuss articles that are related to networking

How to open a port using iptables – the secure way.

Enterprises often need servers to serve their clients across the world such as websites , databases or remote access servers. For example when you are viewing this site you are accessing my server’s public IP address on a specific port , most likely port 80. Therefore we are in desperate need to ensure we are server is secure and administrated in safe manner. The best way I can describe having port 80 open on a public IP address is like putting a nice donut on the open road but that donut is covered and secured by a glass box and you can only see it through a tiny hole. That tiny hole is port 80. Imagine how much people would try to break that glass and take that tasty donut especially in less safe areas. Therefore this article will teach you the most secure way to open a port using iptables.

What is a firewall?

A firewall is a network security device that monitors, filters, and controls incoming and outgoing network traffic[1]. The main purpose is to act as barrier between external networks and internal networks or any network in fact. It is often placed on critical areas such as between networks or routers. Moreover another type of firewall is a host-based firewall which is a software application installed directly on a single host to monitor and filter its inbound and outbound network traffic rather than a whole network [2]. Therefore the point is to simply prevent unauthorized incoming and outgoing traffic on hosts and networks to help secure them further. It is kind of like a border you do not need everyone coming in , you must be able to dicatate who comes in and who comes out to have a safe country , the same idea applies to a network. Iptables is a host based firewall application based on Linux systems. It has direct access to the net filter Linux kernel which is a separate post on its own which I intend to complete in the near future. For this post we will simply demonstrate how to secure a webserver using iptables leaving out the theory on

Practical application

It is best practice for every host to ensure that only authorized traffic is allowed on both incoming and outgoing connections. That means that iptables should default be on drop meaning all ports are closed by default and you open strictly the ports that are needed. For iptables we have three chains , the first chain deals with incoming connections this means connections that are entering your system. The second chain is the output chain which deals with all connections that are destined for machines on the outside. Lastly the the forward chain deals with any packet that is not destined to the host machine itself.

Setting up our webserver

First we must update our kali linux machine using the command:

sudo apt update -y

Secondly we must install the software which we will be using apache2

sudo apt install apache2

By default it is disabled meaning it is not running nor accessible to anyone to enable it run the command:

sudo systemctl enable apache2

Opening ports

Now from experience I know a webserver on apache2 uses port 80 and if configured it can also use port 443 to serve it’s clients on a secure connection. Therefore for this lab we will be using only port 80 and will be opening only this port. To allow others to access port 80 on our IP address we can use the following command

sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

sudo: Runs the command with root (administrator) privileges.
iptables: The built-in Linux administration tool for IPv4 packet filtering.
-A INPUT: Appends the new rule to the end of the specified chain (e.g., INPUT).
-p tcp: Matches packets using the TCP protocol.
--dport 80: Matches traffic targeting destination port 80 (standard HTTP).
-j ACCEPT: Tells the firewall to jump to the ACCEPT target, letting the traffic pass through.

Great! Now others can access our webserver on port 80 , however our default policy for iptables is to allow all traffic this can be quite dangerous if we have other services running like SSH that we did not intend the public to access.

Securing our webserver

It is best to understand your application to know what port it is using on both the input and output chain. We can use Wireshark ( a packet sniffing tool) to conduct such testing. We can know access it from another machine while sniffing for packets.

One important detail to analyze is that our webserver uses port 80 to send packets used to facilitate connections between the client and itself. This is because the client operating system The client operating system chooses a random ephemeral port between 49152 and 65535 for outbound connections. So our webserver will use port 80 to reply to our client. Now we can set our iptables to default drop all connections but allow only:

  1. Connections coming to our webserver via port 80
  2. Connections coming from our webserver to reply to the client on source port 80

We have already applied the 1st firewall rule , now we can apply the second one on the output chain and change it to source port instead of destination port.

sudo iptables -A OUTPUT -p tcp --sport 80 -j ACCEPT

We can set our iptables default policy to drop all connections , this means only authorized traffic will be allowed into our machine and our webserver will also work , it is also good to allow localhost to work to prevent glitches , we can do this by:

iptables -A INPUT -i lo -j ACCEPT

Great our website works and it safe from all of those outsiders. Thanks for reading and have a great day.

[1] https://www.fortinet.com/resources/cyberglossary/firewall

[2] https://nordvpn.com/cybersecurity/glossary/host-based-firewall/?srsltid=AfmBOopFRnsXjgOc5Rf4EWoJpt8NLQoAH2EjmkjZ1le1_lRNEzUEa0Hc

What is a router ? – practically explained

A Router is a networking device that forwards packets to the intended IP destination on layer 3. The problem with computers is that we want to go all over the place but we need to travel through many networks to reach our destination. This where routers come into play. Routers allow us to connect and traverse through many different networks in a timely manner via packet forwarding.

Routers consult a routing table to know where to send packets that are not intended within its network For example we have the following topology:

PC0 would like to communicate with PC1 via sending an ICMP packet to ping PC1 however they are on two different networks . A router is mandatory for them to communicate because they are on different networks regardless if we were to even establish a direct connection via ethernet because they are on two separate networks on the layer 3. The router will look at the destination address and see its intended for a IP on its network and forward the packet accordingly.

Routers use a routing table to know where to send packets that are not intended within its network when sending packets across router a route must be added. This can be done via routing protocols like OSPF which automatically update the routing table or be added manually. The syntax to add a route within Linux is as follows:

ip route add <network>/<prefix> via <gateway> dev <interface>

You add the network you would like to be able to reach and the gateway that has a link there most likely via physical connection. I have noted an example down below.

ip route add 192.168.200.0/24 via 192.168.100.1

The best analogy to routing is like a relay race which requires passing the baton to sprint.

Creating your own router

The best way to understanding routers is to build your own router. Actually any computer has the ability to become a router it just has to be able to :

  1. Forward packets that are not destined to itself but other hosts
  2. Be able to masquerade and conduct NAT ( network address translation). # NAT can be an entire article btw.

We will demonstrate this using a simple linux machine that has two interfaces one connected to the internet and one connected to the local area network.

  1. # enable IP forwarding : echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf && sysctl -p
  2. # add a firewall rule to enable masquerading and network adddress translation which can be found below.
iptables -t nat -A POSTROUTING -o ens33 -j MASQUERADE

Now any machine on the local network can add our router’s IP address as a gateway and gain access to the world wide web 🌐

Security risks with routers

Routers are very sensitive devices because they often have many devices connected to them and often act as the bridge between the wide area network and the local area network. Therefore routers must be secured with the utmost care. Here are best practices for securing routers.

  1. Ensure all ports are closed for incoming connections
  2. Update Firmware
  3. Change Default Credentials
  4. Enable an IDS/IPS

In conclusion routers are network devices that allow devices to communicate across networks essentially providing them with access to the internet and world wide web. Therefore they should be secure with utmost care to prevent unauthorized access to local area networks.