-
Notifications
You must be signed in to change notification settings - Fork 38
/
app_protection.go
361 lines (326 loc) · 12.4 KB
/
app_protection.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package gramework
import (
"net"
"sync"
"sync/atomic"
)
// Protect enables Gramework Protection for routes registered after Protect() call.
//
// Protects all routes, that prefixed with given enpointPrefix.
// For example:
//
// app := gramework.New()
// app.GET("/internal/status", serveStatus) // will **not be** protected, .Protected() isn't called yet
// app.Protect("/internal")
// registerYourInternalRoutes(app.Sub("/internal")) // all routes here will be protected
//
// Any blacklisted ip can't access protected enpoints via any method.
// Blacklist can work automatically, manually or both. To disable automatic blacklist do App.MaxHackAttemts(-1).
// Automatic blacklist bans suspected IP after App.MaxHackAttempts(). This behaviour is disabled for whitelisted
// ip.
//
// See also App.Whitelist(), App.Untrust(), App.Blacklist(), App.Suspect(), App.MaxHackAttempts(),
// Context.IsWhitelisted(), Context.IsBlacklisted(), Context.IsSuspect(),
// Context.Whitelist(), Context.Blacklist(), Context.Suspect(), Context.HackAttemptDetected(),
// Context.SuspectsHackAttempts()
func (app *App) Protect(endpointPrefix string) {
if app.trustedIP == nil {
app.trustedIP = &ipList{
list: make(map[string]struct{}),
mu: &sync.RWMutex{},
}
}
if app.untrustedIP == nil {
app.untrustedIP = &ipList{
list: make(map[string]struct{}),
mu: &sync.RWMutex{},
}
}
if app.suspectedIP == nil {
app.suspectedIP = &suspectsList{
list: make(map[string]*suspect),
mu: &sync.RWMutex{},
}
}
if app.protectedPrefixes == nil {
app.protectedPrefixes = make(map[string]struct{})
app.protectedEndpoints = make(map[string]struct{})
}
app.protectedPrefixes[endpointPrefix] = struct{}{}
}
func nilHijackHandler(c net.Conn) {
}
func (app *App) protectionMiddleware(handler func(*Context)) func(ctx *Context) {
return func(ctx *Context) {
if ctx.IsBlacklisted() {
// force closing of the connection ASAP
ctx.Hijack(nilHijackHandler)
return
}
handler(ctx)
}
}
// Whitelist adds given ip to Gramework Protection trustedIP list.
// To remove IP from whitelist, call App.Untrust()
//
// See also App.Protect(), App.Untrust(), App.Blacklist(), App.Suspect(), App.MaxHackAttempts(),
// Context.IsWhitelisted(), Context.IsBlacklisted(), Context.IsSuspect(),
// Context.Whitelist(), Context.Blacklist(), Context.Suspect(), Context.HackAttemptDetected(),
// Context.SuspectsHackAttempts()
func (app *App) Whitelist(ip net.IP) (ok bool) {
if ip == nil {
return false
}
if ip.IsLoopback() {
return true
}
ipHash := app.prepareIPListKey(ip)
// now we trust this ip
app.trustedIP.mu.Lock()
app.trustedIP.list[ipHash] = struct{}{}
app.trustedIP.mu.Unlock()
// unban this ip
app.untrustedIP.mu.Lock()
delete(app.untrustedIP.list, ipHash)
app.untrustedIP.mu.Unlock()
// whitelisted ip can't be suspected
app.suspectedIP.mu.Lock()
delete(app.suspectedIP.list, ipHash)
app.suspectedIP.mu.Unlock()
return true
}
// Untrust removes given ip from trustedIP list, that
// enables protection of Gramework Protection enabled endpoints for given ip too.
// Opposite of App.Whitelist().
//
// See also App.Protect(), App.Whitelist(), App.Blacklist(), App.Suspect(), App.MaxHackAttempts(),
// Context.IsWhitelisted(), Context.IsBlacklisted(), Context.IsSuspect(),
// Context.Whitelist(), Context.Blacklist(), Context.Suspect(), Context.HackAttemptDetected(),
// Context.SuspectsHackAttempts()
func (app *App) Untrust(ip net.IP) (ok bool) {
if ip == nil {
return false
}
ipHash := app.prepareIPListKey(ip)
// now we don't trust this ip
app.trustedIP.mu.Lock()
delete(app.trustedIP.list, ipHash)
app.trustedIP.mu.Unlock()
return true
}
// Blacklist adds given ip to untrustedIP list, if it is not whitelisted. Any ip blacklisted with
// Gramework Protection can't access protected enpoints via any method.
//
// See also App.Protect(), App.Whitelist(), App.Untrust(), App.Suspect(), App.MaxHackAttempts(),
// Context.IsWhitelisted(), Context.IsBlacklisted(), Context.IsSuspect(),
// Context.Whitelist(), Context.Blacklist(), Context.Suspect(), Context.HackAttemptDetected(),
// Context.SuspectsHackAttempts()
func (app *App) Blacklist(ip net.IP) (ok bool) {
if ip == nil {
return false
}
ipHash := app.prepareIPListKey(ip)
app.trustedIP.mu.RLock()
if _, ok := app.trustedIP.list[ipHash]; ok {
app.trustedIP.mu.RUnlock()
return false
}
app.trustedIP.mu.RUnlock()
// ban this ip
app.untrustedIP.mu.Lock()
app.untrustedIP.list[ipHash] = struct{}{}
app.untrustedIP.mu.Unlock()
// we don't need to suspect already banned ip
app.suspectedIP.mu.Lock()
delete(app.suspectedIP.list, ipHash)
app.suspectedIP.mu.Unlock()
return true
}
// Suspect adds given ip to Gramework Protection suspectedIP list.
//
// See also App.Protect(), App.Untrust(), App.Blacklist(), App.Suspect(), App.MaxHackAttempts(),
// Context.IsWhitelisted(), Context.IsBlacklisted(), Context.IsSuspect(),
// Context.Whitelist(), Context.Blacklist(), Context.Suspect(), Context.HackAttemptDetected(),
// Context.SuspectsHackAttempts()
func (app *App) Suspect(ip net.IP) (ok bool) {
ipHash := app.prepareIPListKey(ip)
app.trustedIP.mu.RLock()
if _, ok := app.trustedIP.list[ipHash]; ok {
app.trustedIP.mu.RUnlock()
return false
}
app.trustedIP.mu.RUnlock()
// suspect this ip
app.suspectedIP.mu.Lock()
app.suspectedIP.list[ipHash] = &suspect{
hackAttempts: 0,
}
app.suspectedIP.mu.Unlock()
return true
}
// MaxHackAttempts sets new max hack attempts for blacklist triggering in the Gramework Protection.
// If 0 passed, MaxHackAttempts just returns current value
// without setting a new one.
// If -1 passed, automatic blacklist disabled.
// This function is threadsafe and atomic.
//
// See `ctx.Whitelist()`, `ctx.Blacklist()` and `ctx.Suspect()` for manual Gramework Protection control.
//
// See also App.Protect(), App.Whitelist(), App.Blacklist(), App.Suspect(),
// Context.IsWhitelisted(), Context.IsBlacklisted(), Context.IsSuspect(),
// Context.Whitelist(), Context.Blacklist(), Context.Suspect(), Context.HackAttemptDetected(),
// Context.SuspectsHackAttempts()
func (app *App) MaxHackAttempts(attempts int32) (oldValue int32) {
oldValue = atomic.LoadInt32(app.maxHackAttempts)
if attempts != 0 && atomic.CompareAndSwapInt32(app.maxHackAttempts, oldValue, attempts) {
app.internalLog.
WithField("old", oldValue).
WithField("new", attempts).
Infof("[Gramework Protection] Updated max hack attemts")
}
return
}
// IsWhitelisted checks if we have current client in Gramework Protection trustedIP list.
// Use ctx.Whitelist() to add current client to trusted list.
//
// See also App.Protect(), App.Whitelist(), App.Blacklist(), App.Suspect(),
// App.MaxHackAttempts(), Context.IsBlacklisted(), Context.IsSuspect(),
// Context.Whitelist(), Context.Blacklist(), Context.Suspect(), Context.HackAttemptDetected(),
// Context.SuspectsHackAttempts()
func (ctx *Context) IsWhitelisted() (isWhitelisted bool) {
if ctx.RemoteIP().IsLoopback() {
return true
}
ctx.App.trustedIP.mu.RLock()
_, isWhitelisted = ctx.App.trustedIP.list[ctx.remoteIPHash()]
ctx.App.trustedIP.mu.RUnlock()
return
}
// IsBlacklisted checks if we have current client in Gramework Protection untrustedIP list.
// Use ctx.Blacklist() to add current client to untrustedIP list.
//
// See also App.Protect(), App.Whitelist(), App.Blacklist(), App.Suspect(),
// App.MaxHackAttempts(), Context.IsWhitelisted(), Context.IsSuspect(),
// Context.Whitelist(), Context.Blacklist(), Context.Suspect(), Context.HackAttemptDetected(),
// Context.SuspectsHackAttempts()
func (ctx *Context) IsBlacklisted() (isBlacklisted bool) {
if ctx.IsWhitelisted() {
return false
}
ctx.App.untrustedIP.mu.RLock()
_, isBlacklisted = ctx.App.untrustedIP.list[ctx.remoteIPHash()]
ctx.App.untrustedIP.mu.RUnlock()
return
}
// IsSuspect checks if we have current client in Gramework Protection suspectedIP list.
// Use ctx.Suspect() to add current client to suspectedIP list.
//
// See also App.Protect(), App.Whitelist(), App.Blacklist(), App.Suspect(),
// App.MaxHackAttempts(), Context.IsWhitelisted(), Context.IsBlacklisted(),
// Context.Whitelist(), Context.Blacklist(), Context.Suspect(), Context.HackAttemptDetected(),
// Context.SuspectsHackAttempts()
func (ctx *Context) IsSuspect() (isSuspect bool) {
if ctx.IsWhitelisted() {
return false
}
ctx.App.suspectedIP.mu.RLock()
_, isSuspect = ctx.App.suspectedIP.list[ctx.remoteIPHash()]
ctx.App.suspectedIP.mu.RUnlock()
return
}
// Whitelist adds given ip to trustedIP list of the Gramework Protection.
// To remove IP from whitelist, call App.Untrust()
//
// See also App.Protect(), App.Untrust(), App.Blacklist(), App.Suspect(), App.MaxHackAttempts(),
// App.Whitelist(), Context.Untrust(), Context.IsWhitelisted(), Context.IsBlacklisted(), Context.IsSuspect(),
// Context.Blacklist(), Context.Suspect(), Context.HackAttemptDetected(),
// Context.SuspectsHackAttempts()
func (ctx *Context) Whitelist() (ok bool) {
return ctx.App.Whitelist(ctx.RemoteIP())
}
// Untrust deletes given ip from trustedIP list, that
// enables protection of Protect()'ed endpoints for given ip too.
// Opposite of Context.Whitelist().
//
// See also App.Protect(), App.Whitelist(), App.Blacklist(), App.Suspect(), App.MaxHackAttempts(),
// App.Untrust(), Context.IsWhitelisted(), Context.IsBlacklisted(), Context.IsSuspect(),
// Context.Whitelist(), Context.Blacklist(), Context.Suspect(), Context.HackAttemptDetected(),
// Context.SuspectsHackAttempts()
func (ctx *Context) Untrust() (ok bool) {
return ctx.App.Untrust(ctx.RemoteIP())
}
// Blacklist adds given ip to untrustedIP list, if it is not whitelisted. Any blacklisted ip can't
// access protected enpoints via any method.
//
// See also App.Protect(), App.Whitelist(), App.Untrust(), App.Suspect(), App.MaxHackAttempts(),
// App.Blacklist(), Context.IsWhitelisted(), Context.IsBlacklisted(), Context.IsSuspect(),
// Context.Whitelist(), Context.Suspect(), Context.HackAttemptDetected(),
// Context.SuspectsHackAttempts()
func (ctx *Context) Blacklist() (ok bool) {
return ctx.App.Blacklist(ctx.RemoteIP())
}
// Suspect adds current client ip to Gramework Protection suspectedIP list.
//
// See also App.Protect(), App.Untrust(), App.Blacklist(), App.Suspect(), App.MaxHackAttempts(),
// Context.IsWhitelisted(), Context.IsBlacklisted(), Context.IsSuspect(),
// Context.Whitelist(), Context.Blacklist(), Context.Suspect(), Context.HackAttemptDetected(),
// Context.SuspectsHackAttempts()
func (ctx *Context) Suspect() (ok bool) {
return ctx.App.Suspect(ctx.RemoteIP())
}
// remoteIPHash is a shortcut for app.prepareIPListKey() function that
// calculates hash for the []byte, which is what the ip is
func (ctx *Context) remoteIPHash() string {
return ctx.App.prepareIPListKey(ctx.RemoteIP())
}
// HackAttemptDetected adds given ip to Gramework Protection suspectedIP list.
// Use it when you detected app-level hack attempt from current client.
//
// See also App.Protect(), App.Whitelist(), App.Untrust(), App.Suspect(), App.MaxHackAttempts(),
// App.Blacklist(), Context.IsWhitelisted(), Context.IsBlacklisted(), Context.IsSuspect(),
// Context.Whitelist(), Context.Suspect(), Context.Blacklist(),
// Context.SuspectsHackAttempts()
func (ctx *Context) HackAttemptDetected() {
if ctx.IsWhitelisted() {
return
}
ipHash := ctx.remoteIPHash()
ctx.App.suspectedIP.mu.Lock()
if s, ok := ctx.App.suspectedIP.list[ipHash]; !ok || s == nil {
ctx.App.suspectedIP.list[ipHash] = &suspect{
hackAttempts: 1,
}
ctx.App.suspectedIP.mu.Unlock()
return
}
// release lock ASAP
ctx.App.suspectedIP.mu.Unlock()
atomic.AddInt32(&ctx.App.suspectedIP.list[ipHash].hackAttempts, 1)
}
// SuspectsHackAttempts returns hack attempts detected with Gramework Protection
// both automatically and manually by calling Context.HackAttemptDetected().
// For any whitelisted ip this function will return 0.
//
// See also App.Protect(), App.Whitelist(), App.Untrust(), App.Suspect(), App.MaxHackAttempts(),
// App.Blacklist(), Context.IsWhitelisted(), Context.IsBlacklisted(), Context.IsSuspect(),
// Context.Whitelist(), Context.Suspect(), Context.Blacklist(),
// Context.HackAttemptDetected()
func (ctx *Context) SuspectsHackAttempts() (attempts int32) {
if ctx.IsWhitelisted() {
return zero
}
ctx.App.suspectedIP.mu.RLock()
if s, ok := ctx.App.suspectedIP.list[ctx.remoteIPHash()]; ok && s != nil {
attempts = s.hackAttempts
}
ctx.App.suspectedIP.mu.RUnlock()
return
}
func (app *App) prepareIPListKey(ip net.IP) string {
if ip == nil {
// we should ban any invalid remoteIP headers
// to ban this type of attacks
return ""
}
return ip.String()
}