This repository has been archived by the owner on Nov 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
linux.go
139 lines (131 loc) · 3.91 KB
/
linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// +build linux darwin
package discovery
import (
"fmt"
"os/exec"
"regexp"
"strconv"
"strings"
)
func CurrentFirewallRules() ([]*FirewallChain, error) {
output, err := exec.Command("/sbin/iptables", "-L", "-n", "-v").Output()
if err != nil {
return nil, fmt.Errorf("running iptables list command to obtain current firewall rules: %v", err)
}
chains, err := parseIptablesOutput(string(output))
if err != nil {
return nil, fmt.Errorf("parsing iptables list command output to obtain current firewall rules: %v", err)
}
return chains, nil
}
func parseIptablesOutput(output string) ([]*FirewallChain, error) {
var chains []*FirewallChain
cs := strings.Split(output, "\n\n")
fmt.Printf("Found %d chains\n", len(cs))
for _, c := range cs {
chain, err := parseIptablesChain(c)
if err == nil {
chains = append(chains, chain)
} else {
fmt.Printf("Warning: error occurred while parsing iptables chain: %v\n", err)
}
}
return chains, nil
}
var iptablesChainHeaderRegexp = regexp.MustCompile(`\AChain (?P<name>[a-zA-Z0-9-]+) \((policy (?P<policy>[a-zA-Z0-9-]+) )?`)
func parseIptablesChain(c string) (*FirewallChain, error) {
lines := strings.Split(c, "\n")
if len(lines) == 0 {
return nil, fmt.Errorf("chain output has no header")
}
if len(lines) < 2 {
return nil, fmt.Errorf("chain output has no rules header")
}
header := lines[0]
chain := &FirewallChain{}
matched, err := regexp.Match(iptablesChainHeaderRegexp.String(), []byte(header))
if err != nil {
return nil, fmt.Errorf("cannot parse chain header '%s': %v", header, err)
}
if !matched {
return nil, fmt.Errorf("cannot parse chain header '%s'", header)
}
match := iptablesChainHeaderRegexp.FindStringSubmatch(header)
for i, name := range iptablesChainHeaderRegexp.SubexpNames() {
switch name {
case "name":
chain.Name = match[i]
case "policy":
chain.Policy = match[i]
}
}
if chain.Name == "" {
return nil, fmt.Errorf("found no name for chain in '%s'", header)
}
rules := lines[2:]
for _, r := range rules {
if r != "" {
rule, err := parseIptablesRule(r)
if err != nil {
fmt.Printf("Warning: cannot parse rule for chain %s : %v\n", chain.Name, err)
} else {
if rule != nil {
chain.Rules = append(chain.Rules, rule)
}
}
}
}
return chain, nil
}
var iptablesRuleFieldSeparator = regexp.MustCompile("[[:blank:]]+")
var iptablesRuleDPortRegexp = regexp.MustCompile(`(tcp|udp) dpts?:(?P<minPort>\d+)(:(?P<maxPort>\d+))?`)
var iptablesRuleStateRegexp = regexp.MustCompile(`state [[:alpha:]]+(,[[:alpha:]]+)*`)
var iptablesRuleStatsInfoRegexp = regexp.MustCompile(`^ ?\d+[A-Z]? \d+[A-Z]? `)
func parseIptablesRule(r string) (*FirewallRule, error) {
r = iptablesRuleFieldSeparator.ReplaceAllLiteralString(r, " ")
r = iptablesRuleStatsInfoRegexp.ReplaceAllLiteralString(r, "")
fields := iptablesRuleFieldSeparator.Split(r, 8)
if len(fields) < 8 {
return nil, fmt.Errorf("rule '%s' has too few fields", r)
}
matchString := iptablesRuleStateRegexp.FindString(r)
if len(matchString) > 0 { //state condition, we ignore it
return nil, nil
}
if fields[3] == "lo" { // incoming interface is localhost
return nil, nil
}
var dports [2]int
matchString = iptablesRuleDPortRegexp.FindString(r)
if len(matchString) == 0 {
dports = [2]int{1, 65535}
} else {
match := iptablesRuleDPortRegexp.FindStringSubmatch(r)
dports = [2]int{0, 0}
for i, name := range iptablesRuleDPortRegexp.SubexpNames() {
var err error
switch name {
case "minPort":
dports[0], err = strconv.Atoi(match[i])
if err != nil {
return nil, fmt.Errorf("rule '%s' has invalid destination %s specification '%s'", r, name, match[i])
}
case "maxPort":
dports[1], _ = strconv.Atoi(match[i])
if err != nil {
dports[1] = 0
}
}
}
if dports[1] == 0 {
dports[1] = dports[0]
}
}
rule := &FirewallRule{
Target: fields[0],
Protocol: fields[1],
Source: fields[5],
Dports: dports,
}
return rule, nil
}