Logo

dev-resources.site

for different kinds of informations.

Homemade application firewall for Linux

Published at
10/15/2024
Categories
linux
firewall
container
Author
henrybarreto
Categories
3 categories in total
linux
open
firewall
open
container
open
Author
12 person written this
henrybarreto
open
Homemade application firewall for Linux

Firewalls on Linux normally work on network interfaces, managing and controlling the networking traffic basing on defined rules. If you want to block any request of goes on the port 80, for example, a simple configuration could be done. No UDP allowed; no problem at all. However, how to block traffic for only one application? The Application Firewall shows up.

In a simple summarize, An Application Firewall blocks or limits the application to receiving or sending traffic to/from a destination. It can have plenty of utilities, since Servers' applications to Desktops' one, what I was looking for.

I was working on an old game; trying to understand its Network protocol, and how the binary behavior when something on the connections goes wrong, and something comes to my mind: "What if I could block the traffic to this server only for this process?" what brings me to OpenSnitch.

OpenSnitch (n.d.). OpenSnitch allows you to create rules for which apps to allow to access the internet and which to block. Retrieved from It's Foss. Nothing bad to say about it, but I thought it would be too much for my use case, so I have continued questing.

Some days after, a light came to my mind: "Should Linux namespaces fit for it?" I have read about it, but never applied directly, so my theory was: Could I create a namespace for the application, use UFW or IP Tables to build my rules, and have a simpler version of the Application Firewall? The answer is Yes!

The steps to make this test were:

On the host machine, I created a P2P interfaces...

sudo ip link add veth0 type veth peer name veth1
Enter fullscreen mode Exit fullscreen mode

Have configured the IP address...

sudo ip addr add 10.0.0.1/24 dev veth0
Enter fullscreen mode Exit fullscreen mode

And started the network interface.

sudo ip link set veth0 up
Enter fullscreen mode Exit fullscreen mode

Enable IP forwarding.

sudo sysctl -w net.ipv4.ip_forward=1
Enter fullscreen mode Exit fullscreen mode

With the interface started, we need to create the namespace, isolating the
network stack, what can be done using the unshare command.

sudo unshare --net /bin/bash
Enter fullscreen mode Exit fullscreen mode

Shows the namespace's PID.

echo $$
Enter fullscreen mode Exit fullscreen mode

Sends the interface veth1 to the namespace.

This command should be run in the Host environment.

sudo ip link set veth1 netns <PID> 
Enter fullscreen mode Exit fullscreen mode

Have configured the IP address...

sudo ip addr add 10.0.0.2/24 dev veth1
Enter fullscreen mode Exit fullscreen mode

Start the network interface...

sudo ip link set veth1 up
Enter fullscreen mode Exit fullscreen mode

And configure the default route to the host machine.

ip route add default via 10.0.0.1
Enter fullscreen mode Exit fullscreen mode

To complete the setup, on the host environment too, one extra step is necessary:
allow IP routing to the external interface.

sudo iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o <INTERFACE> -j MASQUERADE
Enter fullscreen mode Exit fullscreen mode

After this configuration process, the bash initialized with unshare could be used to set UWF rules, for example, to block the desirable traffic, essentially blocking only the application/applications that runs inside this bash instance.

It's a straightforward approach, but it works! Thanks for reading, and I hope
this helps.

container Article's
30 articles in total
Favicon
How to run a Nginx-web server
Favicon
Docker Basics
Favicon
What is Kubernetes Vs Terraform
Favicon
It is time to express your intention ,before you really code
Favicon
Docker Hands-on: Learn Docker Volume and Bind Mounts with Sample Projects using NGINX
Favicon
Can I start and stop Docker Desktop using CLI?
Favicon
The Power of Containers: Why Docker is Essential in Cloud, AI, Software Engineering and DevOps
Favicon
Docker Tutorial and Easy Guide to Master Dockerfile, Images, Containers, Commands, Volume, Network, and Compose
Favicon
Mastering the Container-Presenter Pattern in Angular: A Deep Dive
Favicon
Terraform: Use Template file for AWS CodeDeploy AppSpec file
Favicon
Building a PSR-11 Compatible Dependency Injection Container with PHP 8.4 Lazy Objects
Favicon
PnR: Configuration-Intention Driven Container Orchestration with Go's Platform Abstraction
Favicon
Why Rootless Containers Matter: A Security Perspective
Favicon
How to Install Tailscale in a Proxmox CE 8.2 LXC Container (AlmaLinux 9)
Favicon
Create a container using the Ubuntu image in Docker.
Favicon
Kubernetes คืออะไร? แบบ Dev เห็นภาพ
Favicon
A brief breakdown of Kubernetes architecture
Favicon
Docker Image Optimization: Reducing Size for Faster Deployments
Favicon
Docker
Favicon
Dockerfile Anti-Patterns: What Not to Do
Favicon
Docker Layer Caching Explained: Tips to Improve Build Times
Favicon
Homemade application firewall for Linux
Favicon
Kubernetes: Introduction
Favicon
Pod Security with K8Studio
Favicon
Docker ARG vs ENV: Understanding Build-time and Runtime Variables
Favicon
Containerize Rust Application in 2 Minutes using Docker Init
Favicon
What is a Container Registry
Favicon
How to Use Docker to Improve Your Development Workflow: A Complete Guide
Favicon
Effortlessly Dockerize Your Vite-React Application
Favicon
Docker Networking every developer should know

Featured ones: