forked from haxrob/amthoneypot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
117 lines (101 loc) · 3.2 KB
/
server.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
package main
import (
"crypto/rand"
"fmt"
"log"
"net/http"
"net/http/httputil"
"os"
"strings"
"time"
color "github.com/TwiN/go-color"
)
func whiteList(url string) bool {
switch url[1:] {
case "logon.htm", "invalid.htm", "logo.gif", "styles.css", "run.gif":
return true
}
return false
}
func parseAuthHeader(r *http.Request) map[string]string {
auth := make(map[string]string)
h := r.Header.Get("Authorization")
if len(h) > 0 {
s := strings.Split(h, ",")
for _, v := range s {
z := strings.Split(v, "=")
z0 := strings.Trim(z[0], "\" ")
z1 := strings.Trim(z[1], "\" ")
auth[z0] = z1
}
}
return auth
}
func doAuth(w http.ResponseWriter, r *http.Request) (bool, bool) {
auth := parseAuthHeader(r)
fmt.Println(color.Ize(color.Blue, "Login attempt: "), r.RemoteAddr)
fmt.Println(color.Ize(color.Blue, "Credentials: "), auth)
if auth["Digest username"] == "admin" && auth["response"] == "" {
return true, false
}
digest := "C10000000000000000000000000000"
b := make([]byte, 32)
rand.Read(b)
nonce := fmt.Sprintf("%x", b)[:32]
w.Header().Set("WWW-Authenticate", `Digest realm="Digest:`+digest+`", nonce="`+nonce+`", stale="false", qop="auth"`)
w.WriteHeader(http.StatusUnauthorized)
return false, true
}
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Server", "Intel(R) Active Management Technology 9.1.34")
p := r.URL.Path
if p == "/" {
http.Redirect(w, r, "logon.htm", 301)
return
}
content := p[1:]
if !whiteList(p) {
authSuccess, responseWritten := doAuth(w, r)
if !authSuccess {
if !responseWritten {
http.ServeFile(w, r, "static/invalid.htm")
}
return
}
}
http.ServeFile(w, r, "static/"+content)
}
func Log(handler http.Handler, logFile string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rAddress := color.Ize(color.Yellow, "Remote:"+r.RemoteAddr)
t := time.Now().Format(time.RFC850)
dump, err := httputil.DumpRequest(r, false)
if err == nil {
entry := fmt.Sprintf("%s\n%s\n%s", t, rAddress, dump)
f, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(color.Ize(color.Red, "error opening log file:"), err)
}
defer f.Close()
_, err = f.WriteString(entry)
if err != nil {
log.Println(color.Ize(color.Red, "error writing to log file:"), err)
}
}
handler.ServeHTTP(w, r)
})
}
func main() {
logFile := "amthoneypot.log"
if len(os.Args) > 1 {
logFile = os.Args[1]
}
port := ":16992"
fmt.Println(color.Ize(color.Green, "Starting server on port"+port))
http.HandleFunc("/", handler)
err := http.ListenAndServe(port, Log(http.DefaultServeMux, logFile))
if err != nil {
log.Fatal(color.Ize(color.Red, "Server failed to start:"), err)
}
}