Skip to content

Commit fcf33b4

Browse files
committed
Add basic auth timing attack prevention
1 parent 74a21fa commit fcf33b4

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

https/rest/basicauth.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ func BasicAuth(admins map[string]string) gin.HandlerFunc {
1515
if ok {
1616
expectedHash := admins[username]
1717
if len(expectedHash) > 0 {
18+
// add username salt to slow down brute force password hash cracking
19+
// ToDo: consider using a better password hash algorithm like Argon2i
1820
hash := sha256.Sum256([]byte(username + password))
1921
exHash, err := base64.StdEncoding.DecodeString(expectedHash)
2022
if err == nil {
23+
// To prevent timing attacks based on error length
2124
hashMatch := (subtle.ConstantTimeCompare(hash[:], exHash[:]) == 1)
2225
if hashMatch {
2326
return
2427
}
2528
}
29+
} else {
30+
// Hash, decode and compare to prevent timing attacks based on username not found
31+
hash := sha256.Sum256([]byte(username + password))
32+
exHash, _ := base64.StdEncoding.DecodeString("u9cYLNDulUiPGh5vP+DY+U7Q0U5NsdznE/6CoyMcUj0=")
33+
subtle.ConstantTimeCompare(hash[:], exHash[:])
2634
}
2735
}
2836
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})

https/rest/router.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var runner WebProxy
4141

4242
// TODO: Add rate limitor
4343
// TODO: Add custom logging
44+
// TODO: Add admin username and password commands
4445

4546
// StartWebServiceRouter is used to setup the Rest server routes
4647
func StartWebServiceRouter(c *settings.Configuration, d *dynamic.Dynamicd, a *AppShutdown, m string) {

util/randomrange.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package util
2+
3+
import (
4+
"math/rand"
5+
"time"
6+
)
7+
8+
func RandomUIntRange(umin, umax uint) uint {
9+
rand.Seed(time.Now().UnixNano())
10+
min := int(umin)
11+
max := int(umax)
12+
return uint(rand.Intn(max-min+1) + min)
13+
}

0 commit comments

Comments
 (0)