-
Notifications
You must be signed in to change notification settings - Fork 57
/
shield.go
115 lines (90 loc) · 3.13 KB
/
shield.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
package main
import (
"os"
"strings"
"log"
"os/exec"
"github.com/BurntSushi/toml"
)
var (
dryrun int
proxy int
underattack int
basicddos int
autossl int
whitelist []string
proxydomains []string
whitelist_text string
)
// Info from config file
type Config struct {
DryRun int
BasicDdos int
UnderAttack int
Proxy int
Autossl int
Whitelist []string
ProxyDomains []string
}
// Reads info from config file
func ReadConfig() Config {
var configfile = "/etc/nshield/nshield.conf"
_, err := os.Stat(configfile)
if err != nil {
log.Fatal("Config file is missing: ", configfile)
}
var config Config
if _, err := toml.DecodeFile(configfile, &config); err != nil {
log.Fatal(err)
}
//log.Print(config.Index)
return config
}
func exec_shell(command string) string {
out, err := exec.Command("/bin/bash","-c",command).Output()
if err != nil {
log.Fatal(err)
}
return string(out)
}
func main() {
var config = ReadConfig()
basicddos = config.BasicDdos
underattack = config.UnderAttack
dryrun = config.DryRun
proxy = config.Proxy
autossl = config.Autossl
whitelist = config.Whitelist
proxydomains = config.ProxyDomains
exec_shell("wget -O /etc/nshield/ipsets/firehol_level1.netset https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/firehol_level1.netset >/dev/null 2>&1")
exec_shell("wget -O /etc/nshield/ipsets/botscout_1d.ipset https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/botscout_1d.ipset >/dev/null 2>&1")
exec_shell("wget -O /etc/nshield/ipsets/bi_any_2_30d.ipset https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/bi_any_2_30d.ipset >/dev/null 2>&1")
exec_shell("wget -O /etc/nshield/snort_ipfilter.ipset https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/snort_ipfilter.ipset >/dev/null 2>&1")
f, err := os.OpenFile("/var/log/nshield.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
log.SetOutput(f)
blocklist_ipset:=exec_shell("cat /etc/nshield/ipsets/*.ipset")
blocklist_netset:=exec_shell("cat /etc/nshield/ipsets/*.netset")
iplist:=exec_shell("cat /var/log/iptables.log | awk ' { print $11 } ' | sed s'/SRC=//'g | sort -n | uniq -c | sort -rn | head -n25 | grep -v 192.168. | awk '{ print $2 }'")
log.Println("Top incoming requests are: \n"+iplist)
// resolve with curl https://api.iptoasn.com/v1/as/ip/8.8.8.8 | jq '.as_description'
// Split on comma.
splitted_iplist := strings.Split(iplist, " ")
// Display all elements.
for i := range splitted_iplist {
ip:=splitted_iplist[i]
if strings.Contains(blocklist_ipset,ip) {
log.Println("Banning "+ip+" because found in IP blocklists")
exec_shell("ipset add block "+ip+" timeout 300")
}
network:=exec_shell(`echo "+ip+" | awk -F '.' '{ print $1"."$2"."$3 }'`)
if strings.Contains(blocklist_netset,network) {
log.Println("Banning "+ip+" because found in Net blocklists")
exec_shell("ipset add block "+ip+" timeout 300")
}
//
}
}