Is it possible to block the (outgoing) network access of a single process?
|
With Linux 2.6.24+ (considered experimental until 2.6.29), you can use network namespaces for that. You need to have the 'network namespaces' enabled in your kernel ( Then, starting a process without network access is as simple as:
This creates an empty network namespace for the process. That is, it is run with no network interfaces, including no loopback. In below example we add -r to run the program only after the current effective user and group IDs have been mapped to the superuser ones (avoid sudo):
If your app needs a network interface you can set a new one up:
Note that this will create a new, local loopback. That is, the spawned process won't be able to access open ports of the host's If you need to gain access to the original networking inside the namespace, you can use The following example runs
|
|||||||||||||||||||||
|
|
Linux has a feature called network namespaces which allow you to essentially have multiple network stacks on the same machine, and assign one to a program when running it. This is a feature typically used for containers, but you can also use it to accomplish what you want. The
Now, if you switch in to that namespace, you can configure it fairly easily. You'll probably want to bring up lo in it, and that's it:
Now when you want to run your command with no network, you just run it in that jail:
The network is, as desired, unreachable. (You can do all kinds of interesting things as a separate network stack includes |
|||||
|
|
Yes, with customized apparmor profile, i.e
But in that way, you will need to generate a list of allowed files to access as well, the whole procedure can be a bit of complicated. And see the help document here |
|||
|
|
|
You could use iptables and move that process into a cgroup:
|
|||||
|
|
You can use firejail sandbox (should work on kernels with seccomp feature). To use it just do
I do believe that most distros do provide package already but even if they don't firejail has virtually no dependencies other than build toolchain and namespace/seccomp enabled kernel. There are some other nice features of firejail that can be shown with |
|||
|
|
|
You cannot do it with iptables alone. This feature briefly existed, but couldn't be made to work reliably and was abandoned. If you can run the process as a dedicated user ID, iptables can do it with the
See examples in Iptables: matching outgoing traffic with conntrack and owner. Works with strange drops, iptables/pf rule to only allow XY application/user? If you can run the process in its own container, you can firewall that container independently (even make it completely disconnected from the network). A security module can filter a process's access to networking features. warl0ck's answer gives an example with AppArmor. |
||||
|
|
|
It depends on what distro you're using but that's a feature usually included in the OS's MAC system. As stated previously Ubuntu or SuSE's AppArmor can do this. If you're using RHEL you can configure SELinux to either allow or deny access to a particular port number based on the executing process's label. This is all I could find after some fast googling but there are probably more in depth resources online if you look harder and it gives you the general idea. |
||||
|
|
|
You can use seccomp-bpf to block some system calls. For example might want to block the I wrote an example of this approcah which prevents the
This does not need root privilegese. A complete sandbox is much more complex however so you should not use this un order to block non-cooperative/malicious programs. |
|||||
|
|
You could use a command line program called "proxychains" and try one of the following possibilities: Set it up that it uses...
I have not tested it myself so I do not know if it works... |
|||||
|
localhost(to the same machine) to do their jobs. – vonbrand Mar 24 '13 at 2:13