-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
115 lines (108 loc) · 4.02 KB
/
auth.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 server
import (
"fmt"
"time"
fiber "github.com/gofiber/fiber/v2"
bcrypt "golang.org/x/crypto/bcrypt"
encryption "github.com/0187773933/FireC2Server/v1/encryption"
)
func validate_login_credentials( context *fiber.Ctx ) ( result bool ) {
result = false
uploaded_username := context.FormValue( "username" )
if uploaded_username == "" { fmt.Println( "username empty" ); return }
if uploaded_username != GlobalServer.Config.AdminUsername { fmt.Println( "username not correct" ); return }
uploaded_password := context.FormValue( "password" )
if uploaded_password == "" { fmt.Println( "password empty" ); return }
fmt.Println( "uploaded_username ===" , uploaded_username )
fmt.Println( "uploaded_password ===" , uploaded_password )
password_matches := bcrypt.CompareHashAndPassword( []byte( uploaded_password ) , []byte( GlobalServer.Config.AdminPassword ) )
if password_matches != nil { fmt.Println( "bcrypted password doesn't match" ); return }
result = true
return
}
func HandleLogout( context *fiber.Ctx ) ( error ) {
context.Cookie( &fiber.Cookie{
Name: GlobalServer.Config.ServerCookieName ,
Value: "" ,
Expires: time.Now().Add( -time.Hour ) , // set the expiration to the past
HTTPOnly: true ,
Secure: true ,
})
context.Set( "Content-Type" , "text/html" )
return context.SendString( "<h1>Logged Out</h1>" )
}
// POST http://localhost:5950/admin/login
func HandleLogin( context *fiber.Ctx ) ( error ) {
valid_login := validate_login_credentials( context )
if valid_login == false { return serve_failed_attempt( context ) }
context.Cookie(
&fiber.Cookie{
Name: GlobalServer.Config.ServerCookieName ,
Value: encryption.SecretBoxEncrypt( GlobalServer.Config.BoltDBEncryptionKey , GlobalServer.Config.ServerCookieAdminSecretMessage ) ,
Secure: true ,
Path: "/" ,
// Domain: "blah.ngrok.io" , // probably should set this for webkit
HTTPOnly: true ,
SameSite: "Lax" ,
Expires: time.Now().AddDate( 10 , 0 , 0 ) , // aka 10 years from now
} ,
)
return context.Redirect( "/" )
}
func validate_admin_cookie( context *fiber.Ctx ) ( result bool ) {
result = false
admin_cookie := context.Cookies( GlobalServer.Config.ServerCookieName )
if admin_cookie == "" { fmt.Println( "admin cookie was blank" ); return }
admin_cookie_value := encryption.SecretBoxDecrypt( GlobalServer.Config.BoltDBEncryptionKey , admin_cookie )
if admin_cookie_value != GlobalServer.Config.ServerCookieAdminSecretMessage { fmt.Println( "admin cookie secret message was not equal" ); return }
result = true
return
}
func validate_admin( context *fiber.Ctx ) ( result bool ) {
result = false
admin_cookie := context.Cookies( GlobalServer.Config.ServerCookieName )
if admin_cookie != "" {
admin_cookie_value := encryption.SecretBoxDecrypt( GlobalServer.Config.BoltDBEncryptionKey , admin_cookie )
if admin_cookie_value == GlobalServer.Config.ServerCookieAdminSecretMessage {
result = true
return
}
}
admin_api_key_header := context.Get( "key" )
if admin_api_key_header != "" {
if admin_api_key_header == GlobalServer.Config.ServerAPIKey {
result = true
return
}
}
admin_api_key_query := context.Query( "k" )
if admin_api_key_query != "" {
if admin_api_key_query == GlobalServer.Config.ServerAPIKey {
result = true
return
}
}
return
}
func validate_admin_mw( context *fiber.Ctx ) ( error ) {
admin_cookie := context.Cookies( GlobalServer.Config.ServerCookieName )
if admin_cookie != "" {
admin_cookie_value := encryption.SecretBoxDecrypt( GlobalServer.Config.BoltDBEncryptionKey , admin_cookie )
if admin_cookie_value == GlobalServer.Config.ServerCookieAdminSecretMessage {
return context.Next()
}
}
admin_api_key_header := context.Get( "key" )
if admin_api_key_header != "" {
if admin_api_key_header == GlobalServer.Config.ServerAPIKey {
return context.Next()
}
}
admin_api_key_query := context.Query( "k" )
if admin_api_key_query != "" {
if admin_api_key_query == GlobalServer.Config.ServerAPIKey {
return context.Next()
}
}
return context.Status( fiber.StatusUnauthorized ).SendString( "why" )
}