Commit graph

147 commits

Author SHA1 Message Date
Gustavo Iñiguez Goia
481dc1cd53
avoid to send empty stats to the GUI (server)
By default, every second we send to the GUI the collected statistics by
the daemon: metrics + events (connections).

This call has a hard-coded timeout of one second, and if it reaches the
deadline, a warning message is printed to the log.

After several timeouts, the daemon closes the connection and tries to
reconnect again.

The daemon invokes this call even if there're no statistics to send.
With one daemon and one GUI, it's not much of a problem. But if there're
n nodes, the GUI will receive a minimum of n connections every second.
In many scenarios, these empty calls are unnecessary.

So, in order to optimize this behaviour, if the daemon has not collected
new statistics, we won't invoke this call. This will slightly reduce the
load on the GUI (server) when managing multiple nodes, and will save
some CPU cycles on the daemon side.

The connection status between the daemon and the GUI is managed with the
following gRPC options:

 - The server and the daemon configures a 5-seconds keepalive to keep
   the channel open:
   'grpc.keepalive_time_ms', 5000
 - If after 5 seconds the keepalive msg is not received, then after 20
   seconds the connection with the daemon will be closed if the server
   doesn't receive a response:
   'grpc.keepalive_timeout_ms', 20000

So after about 30 seconds, if the daemon can't communicate with the GUI
(server), it'll close the connection and retry it every second.
2025-12-11 12:07:10 +01:00
Gustavo Iñiguez Goia
9b33f7bdc8 discard notification replies if channel is closed
Sometimes, when closing the notifications channel, we could end up
trying to reply to a notification after the channel was already
closed. For example when changing the server address of a node.

In that scenario, the goroutine never finished and attempted to
reply to the notification.
2025-12-09 23:35:32 +01:00
Gustavo Iñiguez Goia
5b67e2e53e stop notifications at the server's request 2025-12-07 13:14:50 +01:00
Gustavo Iñiguez Goia
8270be1278
check TaskMgr validity 2025-12-05 23:12:51 +01:00
Gustavo Iñiguez Goia
fefbf385f1
lower ping() log level to DEBUG
When sending the events to the GUI (server), there's a hardcoded timeout
of one second. If the call takes longer, we were writing a warning to
the logs for each call, which in some cases could be excessive.

Hitting the timeout can be normal under some circumstances, such as when
connecting to a GUI with multiple nodes and a lot of activity.

More info: #1453
2025-12-05 22:45:42 +01:00
Gustavo Iñiguez Goia
7a0c96c17e initiate Ping() timeout after collecting the stats
We have a hardcoded timeout of one second to send the daemon stats to
the GUI. In some cases it's not unusual to hit the deadline.

So in order to just measure the network call, we now initiate the
timeout after collecting the daemon statistics.

See: #1453
2025-12-05 15:32:18 +01:00
Gustavo Iñiguez Goia
7224acc7bd print the notifications type on error 2025-11-27 21:32:39 +01:00
Gustavo Iñiguez Goia
2c3120b936
tasks: send results to the loggers
elastic, grafana, syslog, etc.
2025-11-20 01:16:33 +01:00
Gustavo Iñiguez Goia
1673856037
tasks: added IOC scanner
Preliminary version of a task for scanning IOCs in background
(Indicator Of Compromise).

It can execute scripts and commands in background periodically.

 - Added a new task scheduler to configure when the task is launched.
   Previously we only had an "Interval" field to run the task
periodically. Now it's possible to select the day of the week, specify
multiple times, hours or minutes.

        "schedule": [
            {
                "weekday": [5,6],
                "time": ["09:55:00", "20:15:20"],
            },
            {
                "weekday": [0,1,2,3,4],
                "time": ["15:00:00"],
            }
        ],
   Used only in this task for now.

 - This task supports 4 "tools": yara, scripts and debsums/dpkg.

   * The "yara" tool acts as a frontend to the yara cli tool, used to scan
     for IOCs with YARA rules.
   * The "debsums"/"dpkg" tools can be used to:
     - report changes in the MD5 checksums of installed Debian
       packages (debsums -c).
     - report changes in configuration files of installed Debian
       packages (debsums -ce).
     - verify the integrity of Debian packages (dpkg --verify, dpkg
       --verify <package1> <packag2>).
       The output of the format (rpm) is not parsed yet.

Configuration example of the task to report the output of the command
debsums:
{
  "name": "IOC-scanner",
  "data": {
     "interval": "15s",
     "schedule": [
         {
             "weekday": [0,1,2,3,4,5,6],
             "time": ["22:00:00"]
         }
     ],
     "tools": [
     {
         "name": "debsums",
         "msgStart": "IOC scanner debsums started",
         "msgEnd": "IOC scanner debsums finished",
         "enabled": false,
         "cmd": ["debsums", "-c"],
         "options": {
             "dirs": [],
             "files": [],
             "reports": {
                 "path": "/etc/opensnitchd/tasks/iocscanner/reports",
                 "format": ""
             }
         }
     }
   ]
}

The field "name" defines which tool to use. It's also used to parse and
format the output of the specified command.

The output of the command is sent to the GUI as an alert, and the
results can be reviewed under the Rules tab -> Alerts.
2025-09-22 00:11:28 +02:00
Gustavo Iñiguez Goia
66ee50bf7d
tasks: allow to load tasks from disk
Now it's possible to load tasks from disk.

There's a central file which contains the defined tasks:
/etc/opensnitchd/tasks/tasks.json

with this format:
{
    "tasks": [
        {
            "enabled": false,
            "name": "downloader",
            "configfile":
"/etc/opensnitchd/tasks/downloader/downloader.json",
        }
    ]
}

The field "name" defines the type of task to load, defined in each task.
It's static.

Whenever the tasks.json is modified, if the tasks are enabled, the task
configuration file defined in the "configfile" field will be loaded.

Configuration format of each task:

{
  "name": "downloader",
  "parent": "downloader",
  "data": {}
}

The field "name" is dynamic, unique, and identifies the instance of the
task. If there're two tasks configured with the same name, only one will
be loaded.
The "parent" field is used to know what's the base task. If it's not
specified, we'll try to use the "name" as the base task.

This allows you to run multiple instances of the same task with
different configurations: a downloader to update malware blocklists,
another to update IP blocklists, etc.

The "data" field depends of each task. It's generic, and each task is
responsible for defining it. For example for the Downloader task:

"data": {
    "interval": "1h",
    "timeout": "5s",
    "urls": [
        {
            "name": "adaway",
            "enabled": true,
            "remote": "https://adaway.o/hosts.txt",
            "localfile": "/tmp/blocklist/ads-adaway-hosts.txt"
        }
    ]
}

The tasks configuration file are also monitored for changes, and
whenever a change is detected the task is reloaded.

----

Notifications (WIP)

It's not yet fully defined and it'll change, but this is the state as
of today.

For real-time tasks, like pid-monitor, sockets monitor and node monitor,
the GUI initiates the task with a unique ID (a timestamp), to know who
is sending what, and display the data properly on the GUI.

For permanent tasks we use a unique ID defined for each task.

Right now, if we don't have the notifications channel opened and the
notification ID corresponds with one of the tasks, we'll post an alert
to the GUI, to display a desktop notification.

Each task can define if send notifications or not. For example:

{
  "name": "downloader",
  "data": {
      (...)
      "notify": {
          "enabled": true
      }
  }
}

then in each task, act accordingly to what is defined in the
configuration.
2025-09-11 12:19:38 +02:00
Gustavo Iñiguez Goia
4b38ca1260
config tests: added missing comma 2025-09-11 00:29:02 +02:00
Gustavo Iñiguez Goia
f9358a464f
Merge pull request #1418 from LordGrimmauld/audit-socket-path-config
daemon/audit: allow configuring audispd_events socket path
2025-09-11 00:24:21 +02:00
Gustavo Iñiguez Goia
970f8c356b
minor tasks notification change
Simplify tasks' notifications handling.
2025-09-03 17:45:33 +02:00
Grimmauld
ec75833dd5
daemon/audit: allow configuring audispd_events socket path 2025-08-26 20:30:02 +02:00
Gustavo Iñiguez Goia
53b948b11a
fixed exception loading outdated configuration
TODO: send the error to the GUI. It's important enough to warn the user
about it.

Issue: #1380
2025-07-19 18:49:15 +02:00
Gustavo Iñiguez Goia
e13b4b9f76
updated config tests 2025-06-19 23:49:45 +02:00
Gustavo Iñiguez Goia
a4d8d1bb27
improved daemon's connection logic with the server (GUI)
- New: if Server.Address is not set, stop the routine that attempts
   to connect with the remote server.
 - Spawn only one polling routine.
 - When changing the configuration:
   - if the new server.address changed and is not empty -> reconnect.
   - if the new server.address changed and is empty -> disconnect.
   - if previous server.address was empty and the new one is not -> connect.
   - if the authentication settings changed -> reconnect.

   If the server.address is not available, we'll try to connect
   indefinitely.
2025-06-19 01:59:30 +02:00
Gustavo Iñiguez Goia
f1318ef12d
fixed loading config when the procmon fails
- Fixed configuration loading, when the Process Monitor Method
   selected failed to work (e.g.: when ebpf modules are missing,
   auditd is not configured, etc).
   In this scenario, on the first run, the daemon was not loading the
   configuration from disk, and was instead using the default values, which
   led to some glitches (unexpected reconnections to the GUI, checksums
   option ignored, etc).
 - added more tests.
2025-06-18 00:35:17 +02:00
Gustavo Iñiguez Goia
c526907d75
reload procmon method after fw rules
There was a situation where if the ebpf modules path did not exist,
the fw rules were not added, causing the daemon to crash after
connecting to the GUI.
2024-12-17 12:33:38 +01:00
Gustavo Iñiguez Goia
5fa561607f
daemon/ui tests updated 2024-10-19 20:43:45 +02:00
Gustavo Iñiguez Goia
6622df9d38
allow to configure nfqueue bypass flag
Nfqueue bypass option skips the enqueue of packets to userspace
if no application is listening to the queue.
https://wiki.nftables.org/wiki-nftables/index.php/Queueing_to_userspace

If this flag is not specified, and for example the daemon dies
unexpectedly, all the outbound traffic will be blocked.

Up until now we've been using this flag by default not to block network
traffic if the daemon dies or is killed for some reason. But some users
want to use precisely this behaviour (#884, #1183, #1201).

Now you can configure it, to block connections if the daemon
unexpectedly dies.

The option is on by default in the configuration (QueueBypass: true).
If this item is not present in the daemon config file, then it'll be
false.
2024-10-19 10:51:40 +02:00
Gustavo Iñiguez Goia
c1c7138e37
refactored tasks notifications
moved to their own file.
2024-10-10 00:24:29 +02:00
Gustavo Iñiguez Goia
83fad69316
tasks: added sockets monitor task (netstat)
Added new task to monitor local sockets of nodes, similar to ss or
netstat.

More info: #1112
2024-10-07 23:40:40 +02:00
Gustavo Iñiguez Goia
58613543e2 added new task nodemonitor
Added new task to monitor the resources of remote nodes, like
ram, swap, number of processes or load average of the system.

The task is initiated when the user selects a node, and the data
received from the node is added to the right panel of the Nodes tab.

The task is stopped when changing to another tab, or when deselecting a
node.

Particularly useful for monitoring remote nodes.
2024-09-28 10:09:19 +02:00
Gustavo Iñiguez Goia
9e0f3a4797
introducing daemon tasks
daemon tasks are actions that are executed in background by the daemon.

They're started from the GUI (server) via a Notification (protobuf),
with the type TASK_START (protobuf).

Once received in the daemon, the TaskManager starts the task in
background.

Tasks may run at interval times (every 5s, 2days, etc), until they
finish an operation, until a timeout, etc.

Each task has each own configuration options, which will customize the
behaviour of its operations.

In this version, if the GUI is closed, the daemon will stop all the
running tasks.
Each Task has a flag to ignore this behaviour, for example if they need
to run until they finish and only send a notification to the GUI,
instead of streaming data continuously to the GUI (server).

 - Up until now we only had one task that could be initiated from the GUI:
   the process monitor dialog. It has been migrated to a Task{}.
 - go.mod bumped to v1.20, to use unsafe string functions.
 - go.sum updated accordingly.
2024-09-25 01:00:38 +02:00
Gustavo Iñiguez Goia
41690b2d2d
set default rules directory if empty
Use /etc/opensnitchd/rules as the default directory if it has not been
provided via configuration.
2024-05-29 23:43:46 +02:00
Gustavo Iñiguez Goia
03747ea0e3
improved config file reloading behaviour
Start monitoring the config file every time we read the file, to survive:
 - malformed json file
 - intermediate file removal (when writing we receive 2 write events,
   one of0 bytes)

"A watch will be automatically removed if the watched path is deleted or
renamed"
"A path can only be watched once; watching it more than once is a no-op and
will not return an error"
https://pkg.go.dev/github.com/fsnotify/fsnotify#Watcher.Add
2024-05-23 01:47:59 +02:00
Gustavo Iñiguez Goia
c0d1da20d2
improved rules reloading, cli parameters
- When reloading rules from a path:
   stop existing (domains,ips,regexp) lists monitors, stop rules
   watcher and start watching the new dir for changes, delete existing
   rules from memory, etc.
 - Previously, cli parameters (queue number, log file, etc) were taking
   into account before loading the configuration.
   Now the configuration file is loaded first (default-config.json), and
   if any of the cli parameter has been specified, it'll overwrite the
   loaded configuration from file.

   This means for example that if you use "-process-monitor-method proc",
   and "ebpf" is configured in default-config.json, firstly "ebpf" will
   be configured, and later "proc".

   (-queue-num option for now requires to match config option
   cfg.FwOptions.QueueNumber)
2024-05-22 00:47:54 +02:00
Gustavo Iñiguez Goia
661e3da48f
kill established connections only on fw/monitor changes 2024-05-19 23:43:12 +02:00
Gustavo Iñiguez Goia
eede54c525
allow to customize ebpf options
Allow to customize:

 - EventsWorkers: number of goroutines to handle kernel events.
   Default 8.

 - QueueEventsSize: max number of events in the queue.
   By default 0, meaning that it'll relay on the available goroutines to
   process the events. If it's > 0, and the daemon can't process the
   events fast enough, they'll be queued. Once the queue is full, it'll
   behave as it was of size 0.

If there're lost events, a message will be logged: "Lost ebpf events..."
2024-05-16 00:31:54 +02:00
Gustavo Iñiguez Goia
efc05663eb
fw: allow to configure interception queue number
- Added new configuration field to allow configure fw interception
   number queue (default to 0):
   "FwOptions": {
      "QueueNum": 0
   }
   (we still need to reconfigure nfqueue queues in order for this to
take effect).
 - If the fw configuration path is not supplied, default to
   /etc/opensnitchd/system-fw.json
2024-05-14 23:41:25 +02:00
Gustavo Iñiguez Goia
9afadcb009
fixed segfaults when loading fw/loggers
- The loggers were not being properly initialized.
 - The fw was only being load on reload, instead of on startup
   and reload.

Kudos to @1fishe2fishe for reporting this problem and proposing a
fix in #1130!
2024-05-14 19:47:35 +02:00
Gustavo Iñiguez Goia
8e9c1d2178
make connections flushing configurable
By default when adding the interception rules, we were killing all
existing connections, to force them go to the netfilter queue.

However in some environments this is not acceptable, so now it's configurable.

Besides, we were doing this only for nftables, so now it also works for
iptables.
2024-05-13 00:27:41 +02:00
Gustavo Iñiguez Goia
c3bbb9250e
stop proc monitor when disabling interception
When disabling the interception from the server (GUI), the network
interception was stopped, but the procs monitor kept running.

Now the procs monitor in use is also stopped, not to interfere with
the rest of the system (except 'proc').
2024-05-12 10:46:11 +02:00
Gustavo Iñiguez Goia
0b67c1a429
more work on reloading configuration
continuation of previous commit bde5d34deb

 - Allow to reconfigure stats limits (how many events we keep on the
   daemon, number of workers, ...)
 - Allow to reconfigure loggers.
2024-05-11 18:23:20 +02:00
Gustavo Iñiguez Goia
7d08b2b4a0
changed ui/client/configuration tests
In order to test ebpf<->proc changes we'll need to have access to a
valid ebpf module.
2024-05-06 00:10:00 +02:00
Gustavo Iñiguez Goia
bde5d34deb
reload more config options without restarting the daemon
Reload the configuration without restarting the daemon when changing:
 - server authentication options.
 - GC percentage.
 - Rules path.
 - Loggers.
 - FW options.
 - eBPF modules path.

Also, try to avoid unnecessary changes.
2024-05-02 21:14:59 +02:00
Gustavo Iñiguez Goia
dc43d5913c
allow to configure GC percentage
Added config option to set how often the garbage collector runs.

For example:

  "Internal": {
      "GCPercent": 75
  },

If this option is not specified in the config file, or the value
is 0, then the GC percentage is not configured.

More info:

https://pkg.go.dev/runtime/debug#SetGCPercent
2024-01-18 00:01:58 +01:00
Gustavo Iñiguez Goia
e5787aef65
structs fields reorganized
Structs' fields reorganized based on fieldalignment tool output
2024-01-14 20:44:49 +01:00
Gustavo Iñiguez Goia
050e3c51b0
fixed leak dispatching alerts
Fixed potential leak when disconnecting from the GUI, and at the same
time dispatching an alert/message to the GUI.
2023-12-31 20:12:44 +01:00
Gustavo Iñiguez Goia
ffb76683aa
allow to configure ebpf modules path
Now it's possible to configure eBPF modules path from the
default-config.json file:
 "Ebpf": {
   "ModulesPath": "..."
 }

If the option is not provided, or if it's empty, we'll keep loading from
the default directories:

 - /usr/local/lib/opensnitchd/ebpf
 - /usr/lib/opensnitchd/ebpf
 - /etc/opensnitchd/ebpf (deprecated, will be removed in the future).

Closes #928
2023-12-22 23:27:18 +01:00
Gustavo Iñiguez Goia
54ac5a3549
fw: allow to configure config file/ check interval
- Allow to configure system firewall configuration file path:
   * via cli (-fw-config-file).
   * via global configuration file.
 - Allow to configure fw rules check interval.

The system fw config file contains regular iptables/nftables rules.
Previously it was hardcoded to /etc/opensnitchd/system-fw.json

The interval to check if the interception rules were added was also
hardcoded to 10 seconds. Now it's possible to configure it.
A value of "0s" disables the interval, while "" defaults to 10 seconds.
2023-12-20 21:32:45 +01:00
Gustavo Iñiguez Goia
6873fd3a2d
updated ui/client tests 2023-12-17 00:39:41 +01:00
Gustavo Iñiguez Goia
211c864d76
allow to configure rules and config file paths
- Added cli option -config-file to specify an alternate path to the
   config file.
 - Allow to configure rules path from the configuration file (cli option
   takes precedence).
 - Default options are now /etc/opensnitchd/rules and
   /etc/opensnitchd/default-config.json. Previously the default rules
   directory was "rules" (relative path).

Closes #449
2023-12-17 00:22:07 +01:00
Gustavo Iñiguez Goia
431e2d3ed9
procmon/cache improvements
- Fixed several leaks.
 - Cache of events reorganized and improved.
   * items are added faster.
   * proc details are rebuilt if needed (checksums, proc tree, etc)
   * proc's tree is reused if we've got the parent in cache.

rel: #413
2023-12-12 14:48:17 +01:00
Gustavo Iñiguez Goia
19d376abf0
getparents code reorganization
Deoptimizing GetParents() until we figure out how to do it without
leaking mem.
2023-10-04 00:58:17 +02:00
Gustavo Iñiguez Goia
0556dc1c81
obtain process's parent hierarchy, checksums improvements
- Obtain the process's parent hierarchy.
 - Display the hierarchy on the pop-ups and the process dialog.
 - [pop-ups] Added a Detailed view with all the metadata of the
   process.
 - [cache-events] Improved the cache of processes.
 - [ruleseditor] Fixed enabling md5 checksum widget.

Related: #413, #406
2023-09-30 18:31:19 +02:00
Gustavo Iñiguez Goia
7a04a53eb1
display checksums on the process dialog
Display the checksums on the process details dialog.
Improved process' icon discovery.
2023-09-23 12:26:21 +02:00
Gustavo Iñiguez Goia
7a9bb17829
allow to filter connections by process checksum
Now you can create rules to filter processes by checksum. Only md5 is
available at the moment.

There's a global configuration option that you can use to enable or
disable this feature, from the config file or from the Preferences
dialog.

As part of this feature there have been more changes:

   - New proc monitor method (PROCESS CONNECTOR) that listens for
     exec/exit events from the kernel.
     This feature depends on CONFIG_PROC_EVENTS kernel option.

   - Only one cache of active processes for ebpf and proc monitor
     methods.

More info and details: #413.
2023-09-22 00:36:26 +02:00
Gustavo Iñiguez Goia
c97c322bcb
allow to secure (abstract) unix socket comms
- Allow to use SSL certificates to secure unix sockets communications.
- Allow to use abstract users sockets for server and nodes.

Go gRPC doesn't seem to understand unix sockets addresses that start
with "unix-abstract:", and python gRPC doesn't seem to understand
"unix:@" addresses.
Therefore, on the server (python gRPC) we use the format "unix:@" to
specify the address where the server will listen on, and rewrite it to
"unix-abstract:" before starting the server.

Note about certs and abstract unix sockets:
 When creating the SSL certificates, you'll have to specify the
 address of the unix socket as the Common Name of the certificates:

  Address: "unix:@my-abstract-socket"

  Common Name: @my-abstract-socket
2023-07-19 01:31:13 +02:00