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-12-09 18:18:42 +01:00
|
|
|
"github.com/evilsocket/opensnitch/daemon/core"
|
|
|
|
"github.com/evilsocket/opensnitch/daemon/log"
|
2020-12-12 18:16:59 +01:00
|
|
|
"github.com/fsnotify/fsnotify"
|
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-12-12 18:16:59 +01:00
|
|
|
rulesChecker *time.Ticker
|
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`)
|
|
|
|
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-12-12 18:16:59 +01:00
|
|
|
func RunRule(action Action, enable bool, logError bool, rule []string) (err4, err6 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-12-12 18:16:59 +01:00
|
|
|
if _, err4 = core.Exec("iptables", rule); err4 != nil {
|
2020-07-30 01:10:53 +02:00
|
|
|
if logError {
|
2020-12-12 18:16:59 +01:00
|
|
|
log.Error("Error while running firewall rule, ipv4 err: %s", err4)
|
2020-07-30 01:10:53 +02:00
|
|
|
log.Error("rule: %s", rule)
|
|
|
|
}
|
2020-11-26 16:25:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if core.IPv6Enabled {
|
2020-12-12 18:16:59 +01:00
|
|
|
if _, err6 = core.Exec("ip6tables", rule); err6 != nil {
|
2020-11-26 16:25:48 +01:00
|
|
|
if logError {
|
2020-12-12 18:16:59 +01:00
|
|
|
log.Error("Error while running firewall rule, ipv6 err: %s", err6)
|
2020-11-26 16:25:48 +01:00
|
|
|
log.Error("rule: %s", rule)
|
|
|
|
}
|
|
|
|
}
|
2019-01-26 20:56:12 -08:00
|
|
|
}
|
2018-11-21 00:25:47 +01:00
|
|
|
|
2020-12-12 18:16:59 +01:00
|
|
|
return
|
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-12-12 18:16:59 +01:00
|
|
|
func QueueDNSResponses(enable bool, logError bool, qNum int) (err4, err6 error) {
|
2020-07-30 01:10:53 +02:00
|
|
|
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-12-12 18:16:59 +01:00
|
|
|
func QueueConnections(enable bool, logError bool, qNum int) (err4, err6 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",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
2020-12-12 18:16:59 +01:00
|
|
|
if err4, err6 := RunRule(INSERT, true, logErrors, []string{rule.Chain, "-t", rule.Table, "-j", chainName}); err4 == nil && err6 == nil {
|
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
|
|
|
systemChains[rule.Table+"-"+chainName] = rule
|
|
|
|
}
|
2020-07-25 21:23:53 +02:00
|
|
|
}
|
|
|
|
|
2020-12-22 22:06:37 +01:00
|
|
|
// DeleteSystemRules deletes the system rules.
|
|
|
|
// If force is false and the rule has not been previously added,
|
|
|
|
// it won't try to delete the rules. Otherwise it'll try to delete them.
|
|
|
|
func DeleteSystemRules(force, logErrors bool) {
|
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
|
|
|
for _, r := range fwConfig.SystemRules {
|
|
|
|
chain := systemRulePrefix + "-" + r.Rule.Chain
|
2020-12-22 22:06:37 +01:00
|
|
|
if _, ok := systemChains[r.Rule.Table+"-"+chain]; !ok && !force {
|
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
|
|
|
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.
|
2020-12-12 18:16:59 +01:00
|
|
|
func AddSystemRule(action Action, rule *fwRule, enable bool) (err4, err6 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
|
|
|
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 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
|
|
|
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 {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 22:50:44 +01:00
|
|
|
result := 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 {
|
2021-01-10 22:50:44 +01:00
|
|
|
result = result && regexRulesQuery.FindString(outMangle6) != ""
|
2020-11-26 16:25:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-12-12 18:16:59 +01:00
|
|
|
if rulesChecker != nil {
|
|
|
|
rulesChecker.Stop()
|
|
|
|
}
|
2020-02-22 00:27:35 +01:00
|
|
|
rulesCheckerChan <- true
|
2020-12-12 18:16:59 +01:00
|
|
|
if configWatcher != nil {
|
|
|
|
rulesCheckerChan <- true
|
|
|
|
}
|
2020-02-22 00:27:35 +01:00
|
|
|
}
|
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)
|
2020-12-22 22:06:37 +01:00
|
|
|
DeleteSystemRules(true, logErrors)
|
2020-07-25 21:23:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func insertRules() {
|
2020-12-12 18:16:59 +01:00
|
|
|
if err4, err6 := QueueDNSResponses(true, true, queueNum); err4 != nil || err6 != nil {
|
2021-02-13 19:16:25 +03:00
|
|
|
log.Error("Error while running DNS firewall rule: %s %s", err4, err6)
|
2020-12-12 18:16:59 +01:00
|
|
|
} else if err4, err6 = QueueConnections(true, true, queueNum); err4 != nil || err6 != nil {
|
2021-02-13 19:16:25 +03:00
|
|
|
log.Fatal("Error while running conntrack firewall rule: %s %s", err4, err6)
|
2020-07-25 21:23:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-12 18:16:59 +01:00
|
|
|
if configWatcher != nil {
|
|
|
|
configWatcher.Remove(configFile)
|
|
|
|
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-12-12 18:16:59 +01:00
|
|
|
var err error
|
|
|
|
if configWatcher, err = fsnotify.NewWatcher(); err != nil {
|
2021-02-13 19:16:25 +03:00
|
|
|
log.Warning("Error creating firewall config watcher: %s", err)
|
2020-04-19 20:13:31 +02:00
|
|
|
}
|
2020-07-25 21:23:53 +02:00
|
|
|
loadDiskConfiguration(false)
|
|
|
|
|
2020-12-12 18:16:59 +01:00
|
|
|
rulesChecker = time.NewTicker(time.Second * 30)
|
2020-11-15 00:53:13 +01:00
|
|
|
go StartCheckingRules()
|
2020-04-19 20:13:31 +02:00
|
|
|
|
|
|
|
running = true
|
|
|
|
}
|