-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfirewall.go
277 lines (257 loc) · 6.83 KB
/
firewall.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
Coded by Leeon123
Date: 2020/9/20 18:01
It is not only for http server,
also for other tcp server.
*/
package main
import (
"fmt"
"io"
"net"
"os"
"os/exec"
"runtime"
"strings"
"sync"
"time"
"unsafe"
)
var (
// You can edit this
waf_port = "0.0.0.0:80" //your waf address
real_port = "localhost:1337" //your real address
pps_per_ip_limit = 10 //Limit the packets per second of ip
connection_limit = 10 //Limit the connections of ip
banned_time float64 = 60 //Blocking time of the banned ip
//You better know what are this
connection_per_ip sync.Map //changed to sync.Map because map is unsafe
rps_per_ip sync.Map //changed to sync.Map because map is unsafe
banned_list sync.Map //changed to sync.Map for new method
connMap sync.Map //for counting connection
errMsg = "HTTP/1.1 503 service unavailable\r\n\r\n"
access_log_chan = make(chan string)
banned_log_chan = make(chan string)
)
func main() {
listener, err := net.Listen("tcp", waf_port)
if err != nil {
panic("connection error:" + err.Error())
}
go access_log()
go banned_log()
go unban()
go monitor()
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Accept Error:", err)
continue
}
remoteIP := strings.Split(conn.RemoteAddr().String(), ":")[0] //Get the Ip
if isBanned(remoteIP) {
conn.Close()
continue
}
connections, ok := connection_per_ip.Load(remoteIP)
if ok {
if connections.(int) >= connection_limit {
banned_list.Store(remoteIP, time.Now())
banned_log_chan <- remoteIP + " [" + time.Now().Format("2006-01-02 15:04:05") + "] Banned due to connection limit"
conn.Close()
continue
}
connection_per_ip.Store(remoteIP, connections.(int)+1)
} else {
connection_per_ip.Store(remoteIP, 1)
}
connMap.Store(conn.RemoteAddr().String(), conn) //might be used in soon...
access_log_chan <- remoteIP + " [" + time.Now().Format("2006-01-02 15:04:05") + "] Connected"
go handle(conn, remoteIP)
}
}
func access_log() {
file, err := os.OpenFile("access.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Printf("Error message: %s\n", err)
os.Exit(1)
}
for v := range access_log_chan { //It will stop after close the channel
file.Write(str2bytes(v + "\n"))
}
}
func banned_log() {
file, err := os.OpenFile("banned.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Printf("Error message: %s\n", err)
os.Exit(1)
}
for v := range banned_log_chan { //It will stop after close the channel
file.Write(str2bytes(v + "\n"))
}
}
func unban() {
for {
banned_list.Range(func(ip, time_banned interface{}) bool {
tmp := time_banned.(time.Time)
if used := time.Since(tmp); used.Seconds() >= banned_time {
banned_list.Delete(ip.(string))
}
return true
})
time.Sleep(time.Second * 1) //check every 1 second
}
}
func monitor() {
for {
rps := 0
currentConn := 0
bannedIP := 0
rps_per_ip.Range(func(ip, times interface{}) bool {
rps++
if times.(int) >= pps_per_ip_limit { //limit the pps
banned_list.Store(ip.(string), time.Now())
banned_log_chan <- ip.(string) + " [" + time.Now().Format("2006-01-02 15:04:05") + "] Banned due to pps limit"
}
rps_per_ip.Delete(ip.(string))
return true
})
connMap.Range(func(addr, conn interface{}) bool {
currentConn++
return true
})
banned_list.Range(func(ip, time_banned interface{}) bool {
bannedIP++
return true
})
fmt.Printf("Connections: %d \nBanned IP: %d \nRps: %d \n", currentConn, bannedIP, rps)
time.Sleep(time.Second)
clearScreen()
}
}
func isBanned(remoteIP string) bool {
banned := false
banned_list.Range(func(ip, _ interface{}) bool {
if ip == remoteIP {
banned = true
return false
}
return true
})
return banned
}
/*
func readhttp(src net.Conn) (string, bool) { //More function under developing
buf := make([]byte, 8192) //i think you won't send a packet over 65535 bits right....?
payload := ""
for {
n, err := src.Read(buf)
if err != nil {
if err != io.EOF {
return "", false
}
break
}
TODO:
Need to check post header
because the post data is after the \r\n\r\n
if n > 0 {
payload += bytes2str(buf[:n])
if len(payload) > 4 {
if payload[len(payload)-4:] == "\r\n\r\n" { //2 crlf, end of the http request
break
}
}
}
}
return payload, true
}*/
func handle(src net.Conn, remoteIP string) {
defer src.Close()
defer func() {
connections, ok := connection_per_ip.Load(remoteIP)
if ok && connections.(int) > 0 {
connection_per_ip.Store(remoteIP, connections.(int)-1)
} else {
connection_per_ip.Delete(remoteIP) //delete it if it equals to 0.
}
connMap.Delete(src.RemoteAddr().String())
}()
if src, ok := src.(*net.TCPConn); ok {
src.SetNoDelay(false)
}
var dst net.Conn
requestsPerConnection := 0
for {
src.SetDeadline(time.Now().Add(10 * time.Second)) //10 second timeout
if isBanned(remoteIP) {
return
}
if requestsPerConnection >= 50 {
return
}
buf := make([]byte, 8192) //i think you won't send a packet over 65535 bits right....?
n, err := src.Read(buf)
if err != nil {
if dst != nil {
dst.Close()
}
return
}
request := buf[:n]
/*
request, ok := readhttp(src)
if !ok {
if dst != nil {
dst.Close()
}
return
}*/
if dst == nil {
//fmt.Println("Started a connection to real server")
dst, err = net.DialTimeout("tcp", real_port, time.Second*10)
if err != nil {
src.Write(str2bytes(errMsg))
return
}
if dst, ok := dst.(*net.TCPConn); ok {
dst.SetNoDelay(false)
}
go func() {
defer dst.Close()
io.Copy(src, dst) //directly transfer the data from real server to client.
}()
} else {
//fmt.Println("Re use the connection")
}
dst.SetDeadline(time.Now().Add(10 * time.Second)) //10 second timeout
dst.Write(request)
//dst.Write(str2bytes(request))
//fmt.Println(request)// we can filter request later, such as some injection or exploit...
requestsPerConnection++
rps, ok := rps_per_ip.Load(remoteIP)
if ok {
rps_per_ip.Store(remoteIP, rps.(int)+1)
} else {
rps_per_ip.Store(remoteIP, 1)
}
}
}
func clearScreen() {
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd", "/c", "cls")
} else {
cmd = exec.Command("clear")
}
cmd.Stdout = os.Stdout
cmd.Run()
}
func str2bytes(s string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&s))
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h))
}
func bytes2str(s []byte) string {
return *(*string)(unsafe.Pointer(&s))
}