Commit graph

76 commits

Author SHA1 Message Date
Gustavo Iñiguez Goia
5453a49692 tests: updated rules tests
- Updated rules tests to use the new description field.
- Removed debugging traces from the loggers module.
2022-05-18 15:27:53 +02:00
Gustavo Iñiguez Goia
3c524c1942 ui, rules: added description field
- Added ability to add a description to the rules.
- Display the description field on the Rules view, and remove the internal
  fields (operator, operator_data, etc).
- Added DB migrations.
- Improved rules' executable path field tooltip (#661).

Closes #652 #466
2022-05-12 13:38:23 +02:00
Gustavo Iñiguez Goia
cbe8f5ab9a rules: added operator unit tests
New tests for operator type lists.domains_regexp, lists.ips and
lists.nets.

New tests to check leaks when reloading lists:
53419fe
2022-01-27 00:17:46 +01:00
Gustavo Iñiguez Goia
53419fe8bc rules: fixed leak/panic when parsing/loading lists
Rules of type lists [domains, IPs, network ranges] are reloaded
whenever a file containing the data changes.

One could expect to update these files once or twice a day, but
by updating the files continuously it was possible to cause a leak,
leading to a crash.
2022-01-26 21:12:16 +01:00
Gustavo Iñiguez Goia
743ef71d7e rules: don't load rules that fail to compile
Be sure that we don't load invalid regexp rules.

related: #536
2021-11-12 12:08:31 +01:00
Gustavo Iñiguez Goia
3b6c0412e0 rules: added more lists types to block/allow
- Added lists of IPs and network ranges. One per line. Blank lines or
  lines that start with # are ignored.
- Added lists of domains with regular expressions. One per line.
  Blank lines or lines that start with # are ignored.

Until now you could use regular expressions with the fields
"To this host" or "To this IP", to match multiple domains or IPs.
But if you wanted to use more than 2-3 domains or IPs it was not really
user friendly.

Hopefully we'll be a little more versatile to cover use cases like #461
and #485.
2021-09-18 00:17:58 +02:00
Gustavo Iñiguez Goia
8d3540f7f9 added action reject
When blocking a connection via libnetfilter-queue using NF_DROP the
connection is discarded. If the blocked connection is a DNS query, the app
that initiated it will wait until it times out, which is ~30s.

This behaviour can for example cause slowdowns loading web pages: #481

This change adds the option to reject connections by killing the socket
that initiated them.

Denying:
    $ time telnet 1.1.1.1 22
    Trying 1.1.1.1...
    telnet: Unable to connect to remote host: Connection timed out

    real	2m10,039s

Rejecting:
    $ time telnet 1.1.1.1 22
    Trying 1.1.1.1...
    telnet: Unable to connect to remote host: Software caused connection abort

    real	0m0,005s
2021-09-12 10:54:24 +02:00
Gustavo Iñiguez Goia
96722ed740 blocklists: sanitize hosts entries
Remove \r\n\t\s from the end of each line of a blocklist.

If the entries of a list had these characters caused to not match
connections and not apply the rule.

closes #429
2021-08-22 23:07:25 +02:00
Gustavo Iñiguez Goia
cec619aa3b removed debugging log message 2021-07-16 23:24:52 +02:00
Gustavo Iñiguez Goia
85802811d1 fixed race condition counting lists of domains
Removed the number of loaded lists of domains, that caused a race
condition and added no value. It was only used for debugging
purposes.
2021-07-07 18:50:30 +02:00
Gustavo Iñiguez Goia
3d11134363 fixed race condition when deleting a rule from disk 2021-06-05 16:33:50 +02:00
Gustavo Iñiguez Goia
ff40e99f77 tests, rules: added Duration change test
Verify that if the user changes the Duration of a temporary rule, the
old timer does not delete the rule.
2021-06-03 17:35:48 +02:00
Gustavo Iñiguez Goia
403c218209 prevent deleting temporary rules when the Duration changes
When the Duration of a rule changed (from 1h to 5m, from 5m to until
restart, etc), the timer of the old rule was fired, causing deleting the
rule from the list.

This erroneous behaviour could be one of the reasons of #429
2021-06-01 23:52:02 +02:00
Gustavo Iñiguez Goia
60414931d6 when deleting a rule stop lists of domains monitor 2021-05-25 22:21:05 +02:00
Gustavo Iñiguez Goia
36cdb76113 prevent leaks when reloading domains lists in debug
If the log level was debug, when reloading the lists of domains could
lead to memory leaks.
2021-03-21 21:16:57 +01:00
Gustavo Iñiguez Goia
53904cb70f ignore hidden files when loading lists of domains 2021-03-14 20:23:48 +01:00
Gustavo Iñiguez Goia
0d2e8b56ea added operator tests
fixed lists operator tests.
added domain lists tests.
2021-03-05 01:10:01 +01:00
Gustavo Iñiguez Goia
408150073a fix deadlock
closes #365
2021-03-04 12:10:53 +01:00
Gustavo Iñiguez Goia
c7d93d83a5 improved rules (re)loading
- Delete lists of domains if the rule about to change is of type Lists.
- Monitor the lists of domains, and reload them if they're modified.
- Delete rules from disk when the Duration changes from
  Always (saved on disk) to !Always (temporary).
- After the above operation a fsnotify Remove event is fired. Don't
  delete the rule from memory if it's temporary.
- Rules are only compiled if they're enabled, avoiding unnecessary
  allocations.
2021-03-01 12:41:35 +01:00
Gustavo Iñiguez Goia
1d277efd01 updated operator and loader tests 2021-02-27 01:56:49 +01:00
Gustavo Iñiguez Goia
44e8561e0c load/reload rules from disk one by one
Any rule changed on disk caused reloading of all rules, usually
up to three times. This caused some problems.

- Don't compile Operators if the rule is disabled.
- Empty lists of domains if the user disables the rule.
- Delete rule from disk if the duration is not Always anymore.
2021-02-27 01:39:04 +01:00
Gustavo Iñiguez Goia
4532c2513e delete loaded lists when needed
There're some situations where we need to delete loaded lists:
 - When an enabled rule of type Lists is disabled (after changed on
   disk, or when configured from the GUI).
 - When an enabled rule of type List with an Operator of type Lists is
   disabled.
2021-02-25 20:09:23 +01:00
Gustavo Iñiguez Goia
c9ba858fc5 add missing operator lists file
needed to load lists.
2021-02-25 13:51:36 +01:00
Gustavo Iñiguez Goia
26671ded24 added support for list of domains
Initial support to filter connections using lists of domains.

The lists must be in hosts format:
- 0.0.0.0 www.domain.com
- 127.0.0.1 www.domain.com

From the rules editor, create a new rule, and select
[x] To this lists of domains

Select a directory with files in hosts format, select [x] Priority rule,
select [x] Deny and click on Apply.

An example of a list in hosts format:
https://www.github.developerdan.com/hosts/lists/ads-and-tracking-extended.txt

Note: you can also add a list of domains to allow, not only domains to
block.

TODOs:
- support for URLs besides directories (local lists).
- support for scheduled updates of the above URLs.

related #298
2021-02-25 13:38:48 +01:00
Gustavo Iñiguez Goia
b066b11c47 improved complex rules
Every rule has an entry point besides a constructor, which configures
all it needs to match connections, based on user defined criteria (ip,
regexp, etc).

This only needs to be done the first time we load a rule, because the
fields of a rule are static. However for rules of type "lists" we were
iniatializing each rule of this type once per connection that it
matched.
2021-02-19 01:03:45 +01:00
Gustavo Iñiguez Goia
b4672830cd fixed regexp rules exceptions
- ui, ruleseditor: added missing operator when using a regular
  expression on the DstIP/Net field.
- daemon, rules: ensure that regular expressions are of type string
  before evaluating them.

reported here: #333
2021-01-17 16:45:28 +01:00
freddii
9e4248e923
fixed german translation (#327)
* fixed german translation

* fixed typing mistake

Co-authored-by: freddii <https://freddii@github.com>
2021-01-05 11:53:12 +01:00
Gustavo Iñiguez Goia
3a3363cb9a rules: delete file from disk if Duration changes
If the user changed the Duration of a rule, from Always to !Always
(temporary), we were not deleting from disk the file of the old
rule.
2021-01-04 12:48:05 +01:00
themighty1
078faa9fda
unittest: test live reloading of rules (#317)
* unittest: test live reloading of rules

* data files for the previous commit

* tests: move files under /testdata; give better names to rules

Co-authored-by: themighty1 <themighty1>
2020-12-28 12:59:29 +01:00
Gustavo Iñiguez Goia
9b035ca66e added unit tests for process parsing and rules 2020-12-19 19:31:09 +01:00
Gustavo Iñiguez Goia
e59133beb2 rules: skip checking not enabled rules 2020-12-18 16:42:34 +01:00
Gustavo Iñiguez Goia
4443673ed1 fixed how we check rules
- Rules are checked in alphabetical order.
- Deny and Priority rules must take precedence.
- If a rule matches and it's Allow-NoPriority, then return the rule.
- Otherwise we'll return nil.
2020-12-15 16:09:28 +01:00
Gustavo Iñiguez Goia
b6cbc6769e fixed rules Enabled check 2020-12-13 11:50:58 +01:00
Gustavo Iñiguez Goia
719c906267 fixed regexp rules when using case-sensitive strings 2020-12-11 22:03:00 +01:00
Gustavo Iñiguez Goia
d47e75d76f fixed filtering by dst network, fixed regression
- Fixed complex rules.
- Fixed filtering by destination network.
2020-12-10 23:09:24 +01:00
Gustavo Iñiguez Goia
e13015ce67 updated import paths 2020-12-09 18:18:42 +01:00
Gustavo Iñiguez Goia
1f415a5799 added option to filter by destination network
Now you can filter by destination network, for example:
- 127.0.0.1/8
- 192.168.1.0/24

This will ease to solve the request #89 .

Some common network ranges have also been added so you can select them
from a combo box. More info #73.

Added process.id operator operand for future use, in order to filter by
PID.
2020-12-04 01:11:47 +01:00
Gustavo Iñiguez Goia
820a6f2930 added option to set priority on the rules
If a rule has the priority flag set, no others rules will be checked.
So if you name the rule as 000-allow-xx and set the priority flag, the
rule wil lbe the only one that will be checked if it matches a
connection.

See #36 to know more on this feature.
2020-10-23 00:02:16 +02:00
Gustavo Iñiguez Goia
c2ee61076d sort rules by name
Sort rules by name, so they're checked in alphabetical order.

This way, you can place deny rules at the top of the list to get better
performance, since it won't check the rest of the rules.

Discussion: #36
2020-10-21 08:13:41 +02:00
Gustavo Iñiguez Goia
4949ab1e4e allow to filter connections by protocol
Added option to filter connections by protocol.

Just add an operand "protocol" as follow:
```
  "operator": {
    "type": "simple",
    "operand": "protocol",
    "sensitive": false,
    "data": "tcp",
    "list": []
  }
```

closes #64
2020-10-20 19:00:00 +02:00
Gustavo Iñiguez Goia
bc70423997 make rules case-insensitive by default
Destination host, process path and process arguments are now case
insensitive by default.

There's a new rule operator field (sensitive), to change this behaviour:
```
      {
        "type": "simple",
        "operand": "dest.host",
        "sensitive": false,
        "data": "opensnitch.io",
        "list": null
      }
```

Rules without this field will evaluate as false by default.

closes #45
2020-10-19 00:25:04 +02:00
Gustavo Iñiguez Goia
6d24c5464c fixed race conditions when manipulating rules 2020-06-20 18:58:59 +02:00
Gustavo Iñiguez Goia
fc2212f073 return better errors if a regexp rule fails to compile
If a regexp rule fails to compile, return the reason instead of a
generic error. It'll help to debug problems.
2020-06-19 18:02:09 +02:00
Gustavo Iñiguez Goia
cfc32cbd1c fixed race condition when deleting a rule 2020-06-14 20:30:19 +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
6bbf3b33be misc: sources formatting 2020-05-30 01:36:43 +02:00
Gustavo Iñiguez Goia
061d7a2ecd ignore malformed rules
Don't exist if a rule is malformed.

Reported by @jonau01 here #17
2020-05-11 10:39:56 +02:00
Gustavo Iñiguez Goia
c44fdf4342 added delete, getall, replace to the rules loader 2020-05-10 17:33:39 +02:00
Gustavo Iñiguez Goia
43898bc4c9 Allow to configure if a rule is enabled or not. 2020-05-10 17:17:05 +02: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