Commit graph

66 commits

Author SHA1 Message Date
Gustavo Iñiguez Goia
0b0255ea4f improved setting proc monitor method from cli
This change is part of the last commit.
2021-09-04 23:09:04 +02:00
Gustavo Iñiguez Goia
a354ae22e1 statistics: fixed missed connections
Previous behaviour:

 1) Before version 1.0.0b the daemon kept a list of processes that had
 established connections. The list was displayed on the GUI as is, so
 the maximum number of connections displayed were 100 (hardcoded).

 2) When the intercepted connections reached 100, the last entry of the
    list was removed, and a new one was inserted on the top.

After v1.0.0 we started saving connections to a DB on the GUI side, to
get rid of the hardcoded connections limit. However, the point 2) was
still present that caused some problems:
 - When the backlog was full we kept inserting and deleting connections
   from it continuously, one by one.
 - If there was a connections burst we could end up missing some
   connections.

New behaviour:
 - The statisics are deleted from the daemon everytime we send them to
   the GUI, because we don't need them on the daemon anymore.
 - If the GUI is not connected, the connections will be added to the
   backlog as in the point 2).
 - When the backlog reaches the limit, it'll keep deleting the last
   one in order to insert a new one.
 - The number of connections to keep on the backlog is configurable.
 - If the statistics configuration is missing, default values will be
   150 (maxEvents) and 25 (maxStats).

Notes:
 If the GUI is saving the data to memory (default), there won't be
 any noticeable side effect.
 If the GUI is configured to save the connections to a DB on disk, and
 the daemon sends all the backlog at once, the GUI may experience a
 delay and a high CPU spike. This can occur on connecting to the daemon
 (because the backlog will be full), or when an app sends too many
 connections per second (like nmap).
2021-08-13 12:18:10 +02:00
Gustavo Iñiguez Goia
5a6dd28072 cache of PIDs: added new tests, fixed rare crash
- New tests to validate deleion of PIDs.
- Fixed potential out-of-bounds when deleting PIDs from cache.
2021-08-11 14:19:43 +02:00
Gustavo Iñiguez Goia
ba7c4e1878 allow to configure what firewall to use
Before this change, we tried to determine what firewall to use based on
the version of iptables (if -V legacy -> nftables, otherwise iptables).

This caused problems (#455), and as there's no support yet for nftables
system firewall rules, it can't be configured to workaround these
errors.

Now the default firewall to use will be iptables.
If it's not available (installed), can't be used or the configuration
option is empty/missing, we'll use nftables.
2021-08-09 00:32:27 +02:00
Gustavo Iñiguez Goia
da23c827f7 added nftables support
Added basic nftables support, which adds the needed rules to intercept
outgoing network traffic and DNS responses. System rules will be added
soon.

What netfilter subsystem to use is determined based on the following:
- nftables: if the _iptables_ binary is not present in the system, or
  if the iptables version (iptables -V) is
  "iptables vX.Y.Z (nf_tables)".
- iptables: in the rest of the cases.
2021-06-07 01:32:05 +02:00
Gustavo Iñiguez Goia
966342cbbc ui: allow to stop/start interception from systray
- removed unused import time.
- allow to pause/start interception from tray contextual menu.
- improved case when the daemon is in paused state, and the GUI is
  launched.

There's more work yet to do to improve the states when there're several
nodes connected.

closes #398
2021-04-17 22:09:16 +02:00
themighty1
9497cf8394
Use ebpf program to find PID of new connections. (#397)
* Use ebpf program to find PID of new connections.

    before running the branch you have to compile ebpf_prog/opensnitch.c
    opensnitch.c is an eBPF program. Compilation requires getting kernel source.

    cd opensnitch
    wget https://github.com/torvalds/linux/archive/v5.8.tar.gz
    tar -xf v5.8.tar.gz
    patch linux-5.8/tools/lib/bpf/bpf_helpers.h < ebpf_prog/file.patch
    cp ebpf_prog/opensnitch.c ebpf_prog/Makefile linux-5.8/samples/bpf
    cd linux-5.8 && yes "" | make oldconfig && make prepare && make headers_install # (1 min)
    cd samples/bpf && make
    objdump -h opensnitch.o #you should see many section, number 1 should be called kprobe/tcp_v4_connect
    llvm-strip -g opensnitch.o #remove debug info
    sudo cp opensnitch.o /etc/opensnitchd
    cd ../../../daemon

    --opensnitchd expects to find opensnitch.o in /etc/opensnitchd/
    --start opensnitchd with:

    opensnitchd -rules-path /etc/opensnitchd/rules -process-monitor-method ebpf

Co-authored-by: themighty1 <you@example.com>
Co-authored-by: Gustavo Iñiguez Goia <gooffy1@gmail.com>
2021-04-05 11:28:16 +02:00
Gustavo Iñiguez Goia
7b9a57b788 added -version cli option
closes #374
2021-03-22 17:51:31 +01:00
Gustavo Iñiguez Goia
0b978c0c1f
Merge pull request #361 from themighty1/master
clean up the obsolete "drop marked"
2021-02-28 10:01:18 +01:00
themighty1
b5bf4edec7 clean up the obsolete "drop marked" 2021-02-28 11:30:26 +03:00
themighty1
53bf4f7925 do not block connection processing when GUI popup is active. 2021-02-18 19:43:27 +03:00
themighty1
c9ae47fe01 fix logging when flag is passed on command line 2021-02-16 21:19:54 +03:00
themighty1
41172b65e5 minor fixes to prevent go vet from complaining 2021-02-13 19:18:38 +03:00
themighty1
ff6ede0557 preserve nfmark when NF_ACCEPT'ing packets
Mullvad VPN uses an nfmark on packets when establishing a connection with their VPN servers.
If we don't preserve the nfmark, the connection will never be established.
2021-01-25 11:50:57 +03:00
Gustavo Iñiguez Goia
e13015ce67 updated import paths 2020-12-09 18:18:42 +01:00
Gustavo Iñiguez Goia
831ab347e3 Merge branch 'priority-rules' into main
Added option to let the users define iptables rules.

The system rules are added in the file /etc/opensnitchd/system-fw.json
with this format:
```
{
    "SystemRules": [
        {
            "Rule": {
                "Description": "Allow pptp VPN",
                "Table": "mangle",
                "Chain": "OUTPUT",
                "Parameters": "-p gre",
                "Target": "ACCEPT",
                "TargetParameters": ""
            }
        }
    ]
}
```
On the mangle table, OUTPUT chain, these rules are added before
the NFQUEUE interception rule, so any rule you add there bypasses the
interception. Useful to allow traffic you don't want to intercept.

This feature solves in some way the issue some users have connecting to
VPNs when the Default Action configured in the daemon is Deny.

For example:
- OpenVPN when keepalive is configured and ICMP is used.
- PPTP because the GRE routing protocol is blocked.
- probably others like IPSEC.

(regarding WireGuard, as far as I can tell it works just fine, see #61).

closes #47
2020-11-13 00:14:39 +01:00
Gustavo Iñiguez Goia
ff5c1ff4c2 cli logging parameters overwrite config options
Parameters passed by command line must overwrite the options configured
in the config file.

closes #82
2020-11-02 01:37:35 +01:00
Gustavo Iñiguez Goia
0d1e9f5b47 set server address and log file from the default config
The server address and log file were hardcoded into the
opensnitchd.service file, making it almost impossible to change.

Soon we'll be able to change it from the UI.
2020-10-26 23:16:27 +01:00
Gustavo Iñiguez Goia
df9e781051 Do not panic if we can't insert fw rules
Some systems has the IPV6 protocol disabled, so we failed starting up
with the error "Address family not supported by protocol" (#52).

Now we don't exist even if we can't insert the needed rules, we'll just
log the error.
2020-07-30 01:10:53 +02:00
Gustavo Iñiguez Goia
b547067f61 close nfqueue descriptors gracefully
When the daemon is stopped, we need to close opened netfilter recurses.
Otherwise we can fall into a situation where we leave NFQUEUE queues
opened, which causes opensnitch to not run anymore until system restart
or a manual intervention, because there's a NFQUEUE queue already created
with the same ID.

This is what was happening as a collateral effect of #41.
2020-07-17 01:29:58 +02:00
Gustavo Iñiguez Goia
b03bbf0506 fixed race conditions setting log level and monitor methods 2020-06-14 20:14:24 +02:00
Gustavo Iñiguez Goia
78c0da83c0 increase default timeout to ask for a rule
Explained here: https://github.com/gustavo-iniguez-goya/opensnitch/issues/28#issuecomment-637484501
2020-06-04 00:38:11 +02:00
Gustavo Iñiguez Goia
d8b2f41e08 Apply default action if a rule is disabled. 2020-05-10 17:08:08 +02:00
Gustavo Iñiguez Goia
83ffaaad44 decrease log level of denied connections
Denied connections were logged with Warning level.
However if you had a rule that denied a particular noisy connection, it
generated too much logs that didn't add value.

In the future it would be nice to log as warning, denied connections
that do not have a rule created by a user.

Discussion: https://github.com/gustavo-iniguez-goya/opensnitch/issues/10#issuecomment-615854975
2020-04-20 01:00:16 +02:00
Gustavo Iñiguez Goia
6ee80b1640 Allow to change settings from the UI
(1/2)
We start receiving notifications from the UI, which allow us to change
configurations and perform actions on the daemon.

The concept of Node has also been introduced, which identifies every
daemon (client) connected to the UI (server).

These options has been added:
- Enable/Disable firewall interception (for all nodes)
- Change daemons (clients) configuration. globally or per node.
- Change prompt dialog options.

We have fixed some bugs along the way:
- Close audit client connection gracefully.
- Exclude our own connections from being intercepted.
- Better handling of client connection status with the UI.

We probably has also introduced some other bugs (not listed here).
2020-04-19 20:13:31 +02:00
Gustavo Iñiguez Goia
e85a41ca0f allow to configure process monitor method in daemon config
Added ProcMonitorMethod, which can be "proc", "ftrace" or "audit".

Parameters passed by command line take prevalence over default
configuration.

breaking changes: config options changed from xx_yy to XxYy.

Config example:
{
    "DefaultAction": "allow",
    "DefaultDuration": "once",
    "InterceptUnknown": true,
    "ProcMonitorMethod": "audit"
}
2020-03-16 01:37:33 +01:00
Gustavo Iñiguez Goia
dbb86d7df8 avoid to crash if opensnitchd is already running 2020-03-13 12:47:04 +01:00
Gustavo Iñiguez Goia
3d6beb5ec3 Added new processes monitor method: audit
Use auditd events to keep a list of PIDs which open sockets, reading
them from the audisp af_unix plugin.

- Install auditd and audisp-plugins
- Enable the af_unix plugin (/etc/audisp-plugin/af_unix, active = yes)
- Start opensnitch with -process-monitor-method audit.

If the choosen method is audit but it's not active or not installed,
it'll fallback to /proc anyway.

If it's properly configured, a debug trace will be written to the logs:
"PID found via audit events ..."
2020-03-03 23:51:25 +01:00
Gustavo Iñiguez Goia
6a82cdbac1 Allow to select which log level to use
Let the user choose which level log to use. For now it must be set
manually and when starting the daemon.
2020-02-28 10:24:08 +01:00
Gustavo Iñiguez Goia
c5a3fb5132 Allow to select what process search method to use
Until now OpenSnitch used ftrace(debugfs) to search for running
processes (PIDs) and obtain the process path.

On some systems, this filesystem is not mounted or available, so we have
to rely on /proc.

After several weeks of use, I think that it's faster and more accurate
the 2nd method, search pids/cmdlines in /proc. So we offer the user to
choose which one to use.
2020-02-26 19:13:42 +01:00
Gustavo Iñiguez Goia
2c7472f06d firewall: check rules every 5s
Every 5s check if our rules are loaded, and if they aren't, add them
again.
2020-02-22 00:27:35 +01:00
Gustavo Iñiguez Goia
9207465d58 Do not panic if we can't parse a Regex type rule
If for some reason a Regex type rule can not be parsed, opensnitchd
panics and exit. We drop regex.MustCompile() in favor of
regex.Compile(), and in case of failure we just drop the packet.

In either case, the daemon should not panic but it should not received
an invalid rule either, specially from the UI.

Closes #4
2020-02-14 23:15:14 +01:00
Gustavo Iñiguez Goia
1e2f9aa124 Find pid of a process in /proc if debugfs is unavailable
debugfs is not always available due to different reasons:
https://github.com/evilsocket/opensnitch/issues/214
https://github.com/evilsocket/opensnitch/issues/276

Fallback to /proc parsing, although procfs could also be not available.

Easily testable by unmounting debugfs (umount debugfs) and launch
opensnitchd. It should work as expected.
2020-02-13 23:08:58 +01:00
Gustavo Iñiguez Goia
912f85f5d1 Added more timeout options (30s, 5m, 15m, 30m, 1h)
Added 30s, 5m, 15m, 30m and 1h timeout options to deny or allow
connections.

It helps to avoid repetitive new connection dialogs, while at the same
time it allows you to deny/allow a connection for a short period of time,
and apply the default for the rest.
2019-11-09 01:35:13 +01:00
Gustavo Iñiguez Goia
7eec749498 allow to configure unknown conns interception
/etc/opensnitchd/default-config.json can now contain
"intercept_unknown": true|false
2019-11-01 01:00:10 +01:00
Gustavo Iñiguez Goia
5690d37d52 Merge branch 'clean_fw_rules_before_start' into main 2019-10-21 19:25:42 +02:00
Gustavo Iñiguez Goia
8922d445af Clean firewall rules before start
If opensnitchd dies unexpectedly, firewall rules are not cleaned up.
2019-10-21 19:23:29 +02:00
Gustavo Iñiguez Goia
ad70655698 removed useless trace log 2019-10-21 00:40:26 +02:00
Gustavo Iñiguez Goia
dedd009204 Merge branch 'daemon_default_config' into main 2019-10-21 00:02:25 +02:00
Gustavo Iñiguez Goia
a7e9b5072f project import paths changed 2019-10-20 21:51:35 +02:00
Gustavo Iñiguez Goia
3bc83efd10 apply default action to connections that can not be parsed
If a connection can not be parsed for some reason, apply the default
action configured.
2019-07-31 01:44:41 +02:00
evilsocket
461e6b678e
using ftrace in order to track pids in realtime 2018-04-17 18:08:03 +02:00
evilsocket
a80f41a147
misc: small fix or general refactoring i did not bother commenting 2018-04-16 17:51:54 +02:00
evilsocket
4560219b92
parsing process env (ref #152) 2018-04-15 15:47:08 +02:00
evilsocket
b014a4069e
misc: small fix or general refactoring i did not bother commenting 2018-04-15 15:39:43 +02:00
evilsocket
e3db27dfc3
misc: small fix or general refactoring i did not bother commenting 2018-04-10 13:15:30 +02:00
evilsocket
01e5134139
misc: small fix or general refactoring i did not bother commenting 2018-04-10 13:11:39 +02:00
evilsocket
fc97f5b431
misc: small fix or general refactoring i did not bother commenting 2018-04-10 13:06:02 +02:00
evilsocket
de1d7cd414
misc: small fix or general refactoring i did not bother commenting 2018-04-10 12:52:01 +02:00
evilsocket
7e474a0514
refactored netfilter code, implemented SetVerdictAndMark method 2018-04-08 20:13:35 +02:00