2018-04-02 05:25:32 +02:00
|
|
|
package firewall
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-03-06 21:28:22 +01:00
|
|
|
"regexp"
|
2020-07-25 21:23:53 +02:00
|
|
|
"strings"
|
2018-04-02 05:25:32 +02:00
|
|
|
"sync"
|
2020-02-22 00:27:35 +01:00
|
|
|
"time"
|
2018-04-02 05:25:32 +02:00
|
|
|
|
2020-07-25 21:23:53 +02:00
|
|
|
"github.com/fsnotify/fsnotify"
|
2020-12-09 18:18:42 +01:00
|
|
|
"github.com/evilsocket/opensnitch/daemon/core"
|
|
|
|
"github.com/evilsocket/opensnitch/daemon/log"
|
2018-04-02 05:25:32 +02:00
|
|
|
)
|
|
|
|
|
2020-03-06 21:28:22 +01:00
|
|
|
// DropMark is the mark we place on a connection when we deny it.
|
|
|
|
// The connection is dropped later on OUTPUT chain.
|
2018-04-02 05:25:32 +02:00
|
|
|
const DropMark = 0x18BA5
|
|
|
|
|
2020-03-06 21:28:22 +01:00
|
|
|
// Action is the modifier we apply to a rule.
|
2020-02-25 01:30:24 +01:00
|
|
|
type Action string
|
|
|
|
|
2020-03-06 21:28:22 +01:00
|
|
|
// Actions we apply to the firewall.
|
2020-02-25 01:30:24 +01:00
|
|
|
const (
|
2020-07-25 21:23:53 +02:00
|
|
|
ADD = Action("-A")
|
|
|
|
INSERT = Action("-I")
|
|
|
|
DELETE = Action("-D")
|
|
|
|
FLUSH = Action("-F")
|
|
|
|
NEWCHAIN = Action("-N")
|
|
|
|
DELCHAIN = Action("-X")
|
|
|
|
|
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
|
|
|
systemRulePrefix = "opensnitch-filter"
|
2020-02-25 01:30:24 +01:00
|
|
|
)
|
|
|
|
|
2018-04-02 05:25:32 +02:00
|
|
|
// make sure we don't mess with multiple rules
|
|
|
|
// at the same time
|
2020-02-22 00:27:35 +01:00
|
|
|
var (
|
|
|
|
lock = sync.Mutex{}
|
|
|
|
|
2020-04-19 20:13:31 +02:00
|
|
|
queueNum = 0
|
|
|
|
running = false
|
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
|
|
|
// check that rules are loaded every 30s
|
2020-11-15 00:53:13 +01:00
|
|
|
rulesChecker = time.NewTicker(time.Second * 30)
|
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
|
|
|
rulesCheckerChan = make(chan bool)
|
|
|
|
regexRulesQuery, _ = regexp.Compile(`NFQUEUE.*ctstate NEW,RELATED.*NFQUEUE num.*bypass`)
|
|
|
|
regexDropQuery, _ = regexp.Compile(`DROP.*mark match 0x18ba5`)
|
|
|
|
regexSystemRulesQuery, _ = regexp.Compile(systemRulePrefix + ".*")
|
|
|
|
|
|
|
|
systemChains = make(map[string]*fwRule)
|
2020-02-22 00:27:35 +01:00
|
|
|
)
|
2018-04-02 05:25:32 +02:00
|
|
|
|
2020-03-06 21:28:22 +01:00
|
|
|
// RunRule inserts or deletes a firewall rule.
|
2020-07-30 01:10:53 +02:00
|
|
|
func RunRule(action Action, enable bool, logError bool, rule []string) error {
|
2018-04-02 05:25:32 +02:00
|
|
|
if enable == false {
|
|
|
|
action = "-D"
|
|
|
|
}
|
|
|
|
|
2020-02-25 01:30:24 +01:00
|
|
|
rule = append([]string{string(action)}, rule...)
|
2018-04-02 05:25:32 +02:00
|
|
|
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
2020-11-26 16:25:48 +01:00
|
|
|
if _, err := core.Exec("iptables", rule); err != nil {
|
2020-07-30 01:10:53 +02:00
|
|
|
if logError {
|
2020-11-26 16:25:48 +01:00
|
|
|
log.Error("Error while running firewall rule, ipv4 err: %s", err)
|
2020-07-30 01:10:53 +02:00
|
|
|
log.Error("rule: %s", rule)
|
|
|
|
}
|
2020-11-26 16:25:48 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if core.IPv6Enabled {
|
|
|
|
if _, err := core.Exec("ip6tables", rule); err != nil {
|
|
|
|
if logError {
|
|
|
|
log.Error("Error while running firewall rule, ipv6 err: %s", err)
|
|
|
|
log.Error("rule: %s", rule)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2019-01-26 20:56:12 -08:00
|
|
|
}
|
2018-11-21 00:25:47 +01:00
|
|
|
|
2020-07-30 01:10:53 +02:00
|
|
|
return nil
|
2018-04-02 05:25:32 +02:00
|
|
|
}
|
|
|
|
|
2020-03-06 21:28:22 +01:00
|
|
|
// QueueDNSResponses redirects DNS responses to us, in order to keep a cache
|
|
|
|
// of resolved domains.
|
2018-04-02 05:25:32 +02:00
|
|
|
// INPUT --protocol udp --sport 53 -j NFQUEUE --queue-num 0 --queue-bypass
|
2020-07-30 01:10:53 +02:00
|
|
|
func QueueDNSResponses(enable bool, logError bool, qNum int) (err error) {
|
|
|
|
return RunRule(INSERT, enable, logError, []string{
|
2018-04-02 05:25:32 +02:00
|
|
|
"INPUT",
|
|
|
|
"--protocol", "udp",
|
|
|
|
"--sport", "53",
|
|
|
|
"-j", "NFQUEUE",
|
2020-04-19 20:13:31 +02:00
|
|
|
"--queue-num", fmt.Sprintf("%d", qNum),
|
2018-04-02 05:25:32 +02:00
|
|
|
"--queue-bypass",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-06 21:28:22 +01:00
|
|
|
// QueueConnections inserts the firewall rule which redirects connections to us.
|
|
|
|
// They are queued until the user denies/accept them, or reaches a timeout.
|
2020-07-25 21:48:16 +02:00
|
|
|
// OUTPUT -t mangle -m conntrack --ctstate NEW,RELATED -j NFQUEUE --queue-num 0 --queue-bypass
|
2020-07-30 01:10:53 +02:00
|
|
|
func QueueConnections(enable bool, logError bool, qNum int) (err error) {
|
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
|
|
|
return RunRule(INSERT, enable, logError, []string{
|
2018-04-02 05:25:32 +02:00
|
|
|
"OUTPUT",
|
|
|
|
"-t", "mangle",
|
|
|
|
"-m", "conntrack",
|
2020-07-25 21:48:16 +02:00
|
|
|
"--ctstate", "NEW,RELATED",
|
2018-04-02 05:25:32 +02:00
|
|
|
"-j", "NFQUEUE",
|
2020-04-19 20:13:31 +02:00
|
|
|
"--queue-num", fmt.Sprintf("%d", qNum),
|
2018-04-02 05:25:32 +02:00
|
|
|
"--queue-bypass",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-06 21:28:22 +01:00
|
|
|
// DropMarked rejects packets marked by OpenSnitch.
|
2018-04-10 13:11:39 +02:00
|
|
|
// OUTPUT -m mark --mark 101285 -j DROP
|
2020-07-30 01:10:53 +02:00
|
|
|
func DropMarked(enable bool, logError bool) (err error) {
|
|
|
|
return RunRule(ADD, enable, logError, []string{
|
2018-04-02 05:25:32 +02:00
|
|
|
"OUTPUT",
|
|
|
|
"-m", "mark",
|
|
|
|
"--mark", fmt.Sprintf("%d", DropMark),
|
2018-04-10 13:11:39 +02:00
|
|
|
"-j", "DROP",
|
2018-04-02 05:25:32 +02:00
|
|
|
})
|
|
|
|
}
|
2020-02-22 00:27:35 +01:00
|
|
|
|
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
|
|
|
// CreateSystemRule create the custom firewall chains and adds them to system.
|
|
|
|
func CreateSystemRule(rule *fwRule, logErrors bool) {
|
|
|
|
chainName := systemRulePrefix + "-" + rule.Chain
|
|
|
|
if _, ok := systemChains[rule.Table+"-"+chainName]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
RunRule(NEWCHAIN, true, logErrors, []string{chainName, "-t", rule.Table})
|
2020-07-25 21:23:53 +02:00
|
|
|
|
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
|
|
|
// Insert the rule at the top of the chain
|
|
|
|
if err := RunRule(INSERT, true, logErrors, []string{rule.Chain, "-t", rule.Table, "-j", chainName}); err == nil {
|
|
|
|
systemChains[rule.Table+"-"+chainName] = rule
|
|
|
|
}
|
2020-07-25 21:23:53 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
// DeleteSystemRules deletes the system rules
|
|
|
|
func DeleteSystemRules(logErrors bool) {
|
|
|
|
for _, r := range fwConfig.SystemRules {
|
|
|
|
chain := systemRulePrefix + "-" + r.Rule.Chain
|
|
|
|
if _, ok := systemChains[r.Rule.Table+"-"+chain]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
RunRule(FLUSH, true, logErrors, []string{chain, "-t", r.Rule.Table})
|
|
|
|
RunRule(DELETE, false, logErrors, []string{r.Rule.Chain, "-t", r.Rule.Table, "-j", chain})
|
|
|
|
RunRule(DELCHAIN, true, logErrors, []string{chain, "-t", r.Rule.Table})
|
|
|
|
delete(systemChains, r.Rule.Table+"-"+chain)
|
|
|
|
}
|
2020-07-25 21:23:53 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
// AddSystemRule inserts a new rule.
|
|
|
|
func AddSystemRule(action Action, rule *fwRule, enable bool) (err error) {
|
|
|
|
chain := systemRulePrefix + "-" + rule.Chain
|
|
|
|
if rule.Table == "" {
|
|
|
|
rule.Table = "filter"
|
|
|
|
}
|
|
|
|
r := []string{chain, "-t", rule.Table}
|
|
|
|
if rule.Parameters != "" {
|
|
|
|
r = append(r, strings.Split(rule.Parameters, " ")...)
|
|
|
|
}
|
|
|
|
r = append(r, []string{"-j", rule.Target}...)
|
|
|
|
if rule.TargetParameters != "" {
|
|
|
|
r = append(r, strings.Split(rule.TargetParameters, " ")...)
|
|
|
|
}
|
2020-07-25 21:23:53 +02:00
|
|
|
|
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
|
|
|
return RunRule(action, enable, true, r)
|
2020-07-25 21:23:53 +02:00
|
|
|
}
|
|
|
|
|
2020-03-06 21:28:22 +01:00
|
|
|
// AreRulesLoaded checks if the firewall rules are loaded.
|
2020-02-22 00:27:35 +01:00
|
|
|
func AreRulesLoaded() bool {
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
2020-11-26 16:25:48 +01:00
|
|
|
var outDrop6 string
|
|
|
|
var outMangle6 string
|
|
|
|
|
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
|
|
|
outDrop, err := core.Exec("iptables", []string{"-n", "-L", "OUTPUT"})
|
2020-02-22 00:27:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
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
|
|
|
outMangle, err := core.Exec("iptables", []string{"-n", "-L", "OUTPUT", "-t", "mangle"})
|
2020-02-22 00:27:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2020-11-26 16:25:48 +01:00
|
|
|
|
|
|
|
if core.IPv6Enabled {
|
|
|
|
outDrop6, err = core.Exec("ip6tables", []string{"-n", "-L", "OUTPUT"})
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
outMangle6, err = core.Exec("ip6tables", []string{"-n", "-L", "OUTPUT", "-t", "mangle"})
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2020-02-22 00:27:35 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
systemRulesLoaded := true
|
|
|
|
if len(systemChains) > 0 {
|
|
|
|
for _, rule := range systemChains {
|
|
|
|
if chainOut4, err4 := core.Exec("iptables", []string{"-n", "-L", rule.Chain, "-t", rule.Table}); err4 == nil {
|
|
|
|
if regexSystemRulesQuery.FindString(chainOut4) == "" {
|
|
|
|
systemRulesLoaded = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-11-26 16:25:48 +01:00
|
|
|
if core.IPv6Enabled {
|
|
|
|
if chainOut6, err6 := core.Exec("ip6tables", []string{"-n", "-L", rule.Chain, "-t", rule.Table}); err6 == nil {
|
|
|
|
if regexSystemRulesQuery.FindString(chainOut6) == "" {
|
|
|
|
systemRulesLoaded = false
|
|
|
|
break
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 16:25:48 +01:00
|
|
|
result := regexDropQuery.FindString(outDrop) != "" &&
|
|
|
|
regexRulesQuery.FindString(outMangle) != "" &&
|
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
|
|
|
systemRulesLoaded
|
2020-11-26 16:25:48 +01:00
|
|
|
|
|
|
|
if core.IPv6Enabled {
|
|
|
|
result = result && regexDropQuery.FindString(outDrop6) != "" &&
|
|
|
|
regexRulesQuery.FindString(outMangle6) != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2020-02-22 00:27:35 +01:00
|
|
|
}
|
|
|
|
|
2020-03-06 21:28:22 +01:00
|
|
|
// StartCheckingRules checks periodically if the rules are loaded.
|
|
|
|
// If they're not, we insert them again.
|
2020-11-15 00:53:13 +01:00
|
|
|
func StartCheckingRules() {
|
2020-02-22 00:27:35 +01:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-rulesCheckerChan:
|
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
|
|
|
goto Exit
|
2020-02-22 00:27:35 +01:00
|
|
|
case <-rulesChecker.C:
|
2020-02-25 01:30:24 +01:00
|
|
|
if rules := AreRulesLoaded(); rules == false {
|
2020-07-25 21:23:53 +02:00
|
|
|
log.Important("firewall rules changed, reloading")
|
2020-11-15 00:53:13 +01:00
|
|
|
CleanRules(log.GetLogLevel() == log.DEBUG)
|
2020-07-25 21:23:53 +02:00
|
|
|
insertRules()
|
|
|
|
loadDiskConfiguration(true)
|
2020-02-22 00:27:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
|
|
|
Exit:
|
|
|
|
log.Info("exit checking fw rules")
|
2020-02-22 00:27:35 +01:00
|
|
|
}
|
|
|
|
|
2020-03-06 21:28:22 +01:00
|
|
|
// StopCheckingRules stops checking if the firewall rules are loaded.
|
2020-02-22 00:27:35 +01:00
|
|
|
func StopCheckingRules() {
|
2020-11-15 00:53:13 +01:00
|
|
|
rulesChecker.Stop()
|
2020-02-22 00:27:35 +01:00
|
|
|
rulesCheckerChan <- true
|
|
|
|
}
|
2020-04-19 20:13:31 +02:00
|
|
|
|
|
|
|
// IsRunning returns if the firewall rules are loaded or not.
|
|
|
|
func IsRunning() bool {
|
|
|
|
return running
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// CleanRules deletes the rules we added.
|
|
|
|
func CleanRules(logErrors bool) {
|
|
|
|
QueueDNSResponses(false, logErrors, queueNum)
|
|
|
|
QueueConnections(false, logErrors, queueNum)
|
|
|
|
DropMarked(false, logErrors)
|
|
|
|
DeleteSystemRules(logErrors)
|
2020-07-25 21:23:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func insertRules() {
|
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
|
|
|
if err := QueueDNSResponses(true, true, queueNum); err != nil {
|
2020-07-25 21:23:53 +02:00
|
|
|
log.Error("Error while running DNS firewall rule: %s", err)
|
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
|
|
|
} else if err = QueueConnections(true, true, queueNum); err != nil {
|
2020-07-25 21:23:53 +02:00
|
|
|
log.Fatal("Error while running conntrack firewall rule: %s", err)
|
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
|
|
|
} else if err = DropMarked(true, true); err != nil {
|
2020-07-25 21:23:53 +02:00
|
|
|
log.Fatal("Error while running drop firewall rule: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 20:13:31 +02:00
|
|
|
// Stop deletes the firewall rules, allowing network traffic.
|
|
|
|
func Stop(qNum *int) {
|
|
|
|
if running == false {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if qNum != nil {
|
|
|
|
queueNum = *qNum
|
|
|
|
}
|
|
|
|
|
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
|
|
|
configWatcher.Close()
|
2020-04-19 20:13:31 +02:00
|
|
|
StopCheckingRules()
|
2020-11-15 00:53:13 +01:00
|
|
|
CleanRules(log.GetLogLevel() == log.DEBUG)
|
2020-04-19 20:13:31 +02:00
|
|
|
|
|
|
|
running = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init inserts the firewall rules.
|
|
|
|
func Init(qNum *int) {
|
|
|
|
if running {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if qNum != nil {
|
|
|
|
queueNum = *qNum
|
|
|
|
}
|
2020-07-25 21:23:53 +02:00
|
|
|
insertRules()
|
2020-04-19 20:13:31 +02:00
|
|
|
|
2020-07-25 21:23:53 +02:00
|
|
|
if watcher, err := fsnotify.NewWatcher(); err == nil {
|
|
|
|
configWatcher = watcher
|
2020-04-19 20:13:31 +02:00
|
|
|
}
|
2020-07-25 21:23:53 +02:00
|
|
|
loadDiskConfiguration(false)
|
|
|
|
|
2020-11-15 00:53:13 +01:00
|
|
|
go StartCheckingRules()
|
2020-04-19 20:13:31 +02:00
|
|
|
|
|
|
|
running = true
|
|
|
|
}
|