-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
191 lines (151 loc) · 5.41 KB
/
main.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
package main
import "github.com/ThomsonReutersEikon/go-ntlm/ntlm"
import l4g "github.com/ThomsonReutersEikon/log4go"
import (
"flag"
"log"
"net/http"
"strings"
"unicode"
"encoding/binary"
"encoding/base64"
)
var (
listen = flag.String("listen", "localhost:8080", "listen on address")
user = flag.String("user", "user", "NTLM user we expect")
password = flag.String("password", "password", "NTLM password we expect")
domain = flag.String("domain", "domain", "NTLM domain we expect")
)
var challenges map[string]*ntlm.ChallengeMessage
func main() {
flag.Parse()
// Just ignore internal messages from ntlm lib for now
l4g.Close()
challenges = make(map[string]*ntlm.ChallengeMessage)
proxyHandler := http.HandlerFunc(proxyHandlerFunc)
log.Fatal(http.ListenAndServe(*listen, proxyHandler))
}
func proxyHandlerFunc(w http.ResponseWriter, r *http.Request) {
var err error
// Make sure there is some kind of authentication
if r.Header.Get("Proxy-Authorization") == "" {
log.Println(r.RemoteAddr, "ERROR: No authentication detected")
w.Header().Set("Proxy-Authenticate", "NTLM")
w.WriteHeader(407)
return
}
// Parse the proxy authorization header
proxy_auth := r.Header.Get("Proxy-Authorization")
parts := strings.SplitN(proxy_auth, " ", 2)
proxy_auth_type := parts[0]
proxy_auth_payload := parts[1]
// Filter out unsupported authentication methods
if proxy_auth_type != "NTLM" {
log.Println(r.RemoteAddr, "Unsupported " + proxy_auth_type + " authentication detected")
w.Header().Set("Proxy-Authenticate", "NTLM")
w.WriteHeader(407)
return
}
// Decode base64 auth data and get NTLM message type
raw_proxy_auth_payload, _ := base64.StdEncoding.DecodeString(proxy_auth_payload)
ntlm_message_type := binary.LittleEndian.Uint32(raw_proxy_auth_payload[8:12])
// Handle NTLM negotiate message
if ntlm_message_type == 1 {
log.Println(r.RemoteAddr, "NTLM Negotiate message received")
session, err := ntlm.CreateServerSession(ntlm.Version2, ntlm.ConnectionOrientedMode)
if err != nil {
log.Println(r.RemoteAddr, "Error with CreateServerSession:", err)
return
}
session.SetUserInfo(*user, *password, *domain)
challenge, err := session.GenerateChallengeMessage()
if err != nil {
log.Println(r.RemoteAddr, "Error with GenerateChallengeMessage:", err)
return
}
challenges[r.RemoteAddr] = challenge
proxy_auth_payload := base64.StdEncoding.EncodeToString(challenge.Bytes())
log.Println(r.RemoteAddr, "NTLM Challenge message sent")
w.Header().Set("Proxy-Authenticate", "NTLM " + proxy_auth_payload)
w.WriteHeader(407)
return
}
if ntlm_message_type == 3 {
log.Println(r.RemoteAddr, "NTLM Challenge response received")
challenge := challenges[r.RemoteAddr]
if challenge == nil {
log.Println(r.RemoteAddr, "ERROR: Challenge response when no challenge was sent")
w.Header().Set("Proxy-Authenticate", "NTLM")
w.WriteHeader(407)
return
}
msg, err := ntlm.ParseAuthenticateMessage(raw_proxy_auth_payload, 2)
if err != nil {
msg2, err := ntlm.ParseAuthenticateMessage(raw_proxy_auth_payload, 1)
if err != nil {
log.Println(r.RemoteAddr, "Error with ParseAuthenticateMessage:", err)
return
}
session, err := ntlm.CreateServerSession(ntlm.Version1, ntlm.ConnectionOrientedMode)
if err != nil {
log.Println(r.RemoteAddr, "Error with CreateServerSession:", err)
return
}
session.SetServerChallenge(challenge.ServerChallenge)
session.SetUserInfo(*user, *password, *domain)
err = session.ProcessAuthenticateMessage(msg2)
if err != nil {
log.Println(r.RemoteAddr, "NTLM Authentication Failed:", err)
w.Header().Set("Proxy-Authenticate", "NTLM")
w.WriteHeader(407)
return
}
} else {
session, err := ntlm.CreateServerSession(ntlm.Version2, ntlm.ConnectionOrientedMode)
if err != nil {
log.Println(r.RemoteAddr, "Error with CreateServerSession:", err)
return
}
session.SetServerChallenge(challenge.ServerChallenge)
session.SetUserInfo(*user, *password, *domain)
err = session.ProcessAuthenticateMessage(msg)
if err != nil {
log.Println(r.RemoteAddr, "ERROR:", err)
w.Header().Set("Proxy-Authenticate", "NTLM")
w.WriteHeader(407)
return
}
}
}
log.Println(r.RemoteAddr, "Downstream request ", r.Method, r.URL)
// Transform request
r.RequestURI = ""
r.URL.Scheme = strings.Map(unicode.ToLower, r.URL.Scheme)
r.Header.Del("Proxy-Connection")
r.Header.Del("Proxy-Authorization")
log.Println(r.RemoteAddr, "Upstream request ", r.Method, r.URL)
// Fetch upstream
client := &http.Client{}
resp, err := client.Do(r)
if err != nil {
log.Fatal(err)
}
log.Println(r.RemoteAddr, "Upstream response ", resp.Proto, resp.Status)
hj, ok := w.(http.Hijacker)
if !ok {
http.Error(w, "webserver doesn't support hijacking", http.StatusInternalServerError)
return
}
// Hijack the response writer
conn, bufrw, err := hj.Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Don't forget to close the connection:
defer conn.Close()
log.Println(r.RemoteAddr, "Downstream response", resp.Proto, resp.Status)
// Write downstream response
resp.Write(bufrw)
bufrw.Flush()
}