Solution I: Preloading a specific library
App-Route-Jail: use ld_preload to force the interface gateway (great idea but require root or marks capabilities) usage is detailed on notes bellow
Proxybound: use ld_preload to force a proxy to a specific application (this is using proxy instead of interface)
Force-Bind: have a lot of features but the bind leaks (not reliable)
Bind-Interface-IP: too simple and leak connections (not reliable)
Bind-IP: way too simple and leak connections (not reliable)
Solution II: Linux userspace
Classic linux user space ip-netns: great solution but require root and interface can only exist on one single user space
Firejail: Firejail can force an application to use a specific network, but the compatibility is limited (example it is not compatible with tun interfaces). firejail does not require root firejail --dns=8.8.8.8 --noprofile --net=eth0 --ip=192.168.1.1 app-command
Firejail with netns: Firejail can force an application to use a specific user space that was created separately, this let us name spaces without root firejail --dns=8.8.8.8 --noprofile --netns=nameOfyourNS app-command
Firejail with masquerade and bridge: Firejail can force an application to use a specific interface with iptables masquerade, this is great and does not require root but this require ip_forward and could imply security impact firejail --net=br0 firefox
Solution III: Linux iptables
Iptables could be used for this purpose but this require ip_forward and could imply security impact if its not correctly configured, example 1, example 2, example 3, example 4
Solutions (I, II & III) notes:
Wireguard
If you are using a VPN (especially wireguard) and you want to apply this solution to a wireguard interface (wireguard with user space) you can follow the linked instructed to create a user space containing a wg interface (and thus limited to a vpn interface) also this can be combined with firejail --netns=container
to be able to use the user space without root.
How to find the interface gateway
There are many solution to find the gateway here are some commands that permit to find the used gateway
$ route
$ route -n
$ ip rule list
$ ip route show
$ netstat -rn
$ cat /etc/network/interfaces
$ cat /etc/sysconfig/network-scripts/ifcfg-eth0
$ traceroute www.google.com
$ ip route show 0.0.0.0/0 dev eth0
How to use App-Route-Jail
git clone https://github.com/Intika-Linux-Network/App-Route-Jail.git
cd Approute-Root-Jail
chmod 755 make.sh
./make.sh
- Add a route for the future marked packets (for the jailed application) in this example
192.168.1.1
is used as the forced gateway, this route rule wont affect other applications, this manipulation have to be done only once at the system boot for instance if you want to use this solution daily
ip rule add fwmark 10 table 100
ip route add default via 192.168.1.1 table 100
- Start the application that you want to jail
MARK=10 LD_PRELOAD=./mark.so firefox
- Testing the wan IP address
MARK=10 LD_PRELOAD=./mark.so wget -qO- ifconfig.me