forked from projectdiscovery/interactsh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_server.go
More file actions
513 lines (456 loc) · 18 KB
/
Copy pathhttp_server.go
File metadata and controls
513 lines (456 loc) · 18 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package server
import (
"bytes"
"crypto/tls"
"encoding/base64"
"fmt"
"log"
"net"
"net/http"
"net/http/httptest"
"net/http/httputil"
"os"
"path/filepath"
"strconv"
"strings"
"sync/atomic"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/projectdiscovery/gologger"
stringsutil "github.com/projectdiscovery/utils/strings"
)
// HTTPServer is a http server instance that listens both
// TLS and Non-TLS based servers.
type HTTPServer struct {
options *Options
tlsserver http.Server
nontlsserver http.Server
customBanner string
staticHandler http.Handler
}
type noopLogger struct {
}
func (l *noopLogger) Write(p []byte) (n int, err error) {
return 0, nil
}
// disableDirectoryListing disables directory listing on http.FileServer
func disableDirectoryListing(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/") || r.URL.Path == "" {
http.NotFound(w, r)
return
}
next.ServeHTTP(w, r)
})
}
// NewHTTPServer returns a new TLS & Non-TLS HTTP server.
func NewHTTPServer(options *Options) (*HTTPServer, error) {
server := &HTTPServer{options: options}
// If a static directory is specified, also serve it.
if options.HTTPDirectory != "" {
abs, _ := filepath.Abs(options.HTTPDirectory)
gologger.Info().Msgf("Loading directory (%s) to serve from : %s/s/", abs, strings.Join(options.Domains, ","))
server.staticHandler = http.StripPrefix("/s/", disableDirectoryListing(http.FileServer(http.Dir(options.HTTPDirectory))))
}
// If custom index, read the custom index file and serve it.
// Supports {DOMAIN} placeholders.
if options.HTTPIndex != "" {
abs, _ := filepath.Abs(options.HTTPDirectory)
gologger.Info().Msgf("Using custom server index: %s", abs)
if data, err := os.ReadFile(options.HTTPIndex); err == nil {
server.customBanner = string(data)
}
}
router := &http.ServeMux{}
router.Handle("/", server.logger(server.corsMiddleware(http.HandlerFunc(server.defaultHandler))))
router.Handle("/register", server.corsMiddleware(server.authMiddleware(http.HandlerFunc(server.registerHandler))))
router.Handle("/deregister", server.corsMiddleware(server.authMiddleware(http.HandlerFunc(server.deregisterHandler))))
router.Handle("/poll", server.corsMiddleware(server.authMiddleware(http.HandlerFunc(server.pollHandler))))
if server.options.EnableMetrics {
router.Handle("/metrics", server.corsMiddleware(server.authMiddleware(http.HandlerFunc(server.metricsHandler))))
}
server.tlsserver = http.Server{Addr: options.ListenIP + fmt.Sprintf(":%d", options.HttpsPort), Handler: router, ErrorLog: log.New(&noopLogger{}, "", 0)}
server.nontlsserver = http.Server{Addr: options.ListenIP + fmt.Sprintf(":%d", options.HttpPort), Handler: router, ErrorLog: log.New(&noopLogger{}, "", 0)}
return server, nil
}
// ListenAndServe listens on http and/or https ports for the server.
func (h *HTTPServer) ListenAndServe(tlsConfig *tls.Config, httpAlive, httpsAlive chan bool) {
go func() {
if tlsConfig == nil {
return
}
h.tlsserver.TLSConfig = tlsConfig
httpsAlive <- true
if err := h.tlsserver.ListenAndServeTLS("", ""); err != nil {
gologger.Error().Msgf("Could not serve http on tls: %s\n", err)
httpsAlive <- false
}
}()
httpAlive <- true
if err := h.nontlsserver.ListenAndServe(); err != nil {
httpAlive <- false
gologger.Error().Msgf("Could not serve http: %s\n", err)
}
}
func (h *HTTPServer) logger(handler http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
req, _ := httputil.DumpRequest(r, true)
reqString := string(req)
gologger.Debug().Msgf("New HTTP request: \n\n%s\n", reqString)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, r)
resp, _ := httputil.DumpResponse(rec.Result(), true)
respString := string(resp)
for k, v := range rec.Header() {
w.Header()[k] = v
}
data := rec.Body.Bytes()
w.WriteHeader(rec.Result().StatusCode)
_, _ = w.Write(data)
var host string
// Check if the client's ip should be taken from a custom header (eg reverse proxy)
if originIP := r.Header.Get(h.options.OriginIPHeader); originIP != "" {
host = originIP
} else {
host, _, _ = net.SplitHostPort(r.RemoteAddr)
}
// if root-tld is enabled stores any interaction towards the main domain
if h.options.RootTLD {
for _, domain := range h.options.Domains {
if h.options.RootTLD && stringsutil.HasSuffixI(r.Host, domain) {
ID := domain
host, _, _ := net.SplitHostPort(r.RemoteAddr)
interaction := &Interaction{
Protocol: "http",
UniqueID: r.Host,
FullId: r.Host,
RawRequest: reqString,
RawResponse: respString,
RemoteAddress: host,
Timestamp: time.Now(),
}
buffer := &bytes.Buffer{}
if err := jsoniter.NewEncoder(buffer).Encode(interaction); err != nil {
gologger.Warning().Msgf("Could not encode root tld http interaction: %s\n", err)
} else {
gologger.Debug().Msgf("Root TLD HTTP Interaction: \n%s\n", buffer.String())
if err := h.options.Storage.AddInteractionWithId(ID, buffer.Bytes()); err != nil {
gologger.Warning().Msgf("Could not store root tld http interaction: %s\n", err)
}
}
}
}
}
if h.options.ScanEverywhere {
chunks := stringsutil.SplitAny(reqString, ".\n\t\"'")
for _, chunk := range chunks {
for part := range stringsutil.SlideWithLength(chunk, h.options.GetIdLength()) {
normalizedPart := strings.ToLower(part)
if h.options.isCorrelationID(normalizedPart) {
h.handleInteraction(normalizedPart, part, reqString, respString, host)
}
}
}
} else {
parts := strings.Split(r.Host, ".")
for i, part := range parts {
for partChunk := range stringsutil.SlideWithLength(part, h.options.GetIdLength()) {
normalizedPartChunk := strings.ToLower(partChunk)
if h.options.isCorrelationID(normalizedPartChunk) {
fullID := part
if i+1 <= len(parts) {
fullID = strings.Join(parts[:i+1], ".")
}
h.handleInteraction(normalizedPartChunk, fullID, reqString, respString, host)
}
}
}
}
}
}
func (h *HTTPServer) handleInteraction(uniqueID, fullID, reqString, respString, hostPort string) {
correlationID := uniqueID[:h.options.CorrelationIdLength]
interaction := &Interaction{
Protocol: "http",
UniqueID: uniqueID,
FullId: fullID,
RawRequest: reqString,
RawResponse: respString,
RemoteAddress: hostPort,
Timestamp: time.Now(),
}
buffer := &bytes.Buffer{}
if err := jsoniter.NewEncoder(buffer).Encode(interaction); err != nil {
gologger.Warning().Msgf("Could not encode http interaction: %s\n", err)
} else {
gologger.Debug().Msgf("HTTP Interaction: \n%s\n", buffer.String())
if err := h.options.Storage.AddInteraction(correlationID, buffer.Bytes()); err != nil {
gologger.Warning().Msgf("Could not store http interaction: %s\n", err)
}
}
}
const banner = `<h1> Interactsh Server </h1>
<a href='https://github.com/projectdiscovery/interactsh'><b>Interactsh</b></a> is an open-source tool for detecting out-of-band interactions. It is a tool designed to detect vulnerabilities that cause external interactions.<br><br>
If you notice any interactions from <b>*.%s</b> in your logs, it's possible that someone (internal security engineers, pen-testers, bug-bounty hunters) has been testing your application.<br><br>
You should investigate the sites where these interactions were generated from, and if a vulnerability exists, examine the root cause and take the necessary steps to mitigate the issue.
`
func extractServerDomain(h *HTTPServer, req *http.Request) string {
if h.options.HeaderServer != "" {
return h.options.HeaderServer
}
var domain string
// use first domain as default (todo: should be extracted from certificate)
if len(h.options.Domains) > 0 {
// attempts to extract the domain name from host header
for _, configuredDomain := range h.options.Domains {
if stringsutil.HasSuffixI(req.Host, configuredDomain) {
domain = configuredDomain
break
}
}
// fallback to first domain in case of unknown host header
if domain == "" {
domain = h.options.Domains[0]
}
}
return domain
}
// defaultHandler is a handler for default collaborator requests
func (h *HTTPServer) defaultHandler(w http.ResponseWriter, req *http.Request) {
atomic.AddUint64(&h.options.Stats.Http, 1)
domain := extractServerDomain(h, req)
w.Header().Set("Server", domain)
if !h.options.NoVersionHeader {
w.Header().Set("X-Interactsh-Version", h.options.Version)
}
reflection := h.options.URLReflection(req.Host)
if stringsutil.HasPrefixI(req.URL.Path, "/s/") && h.staticHandler != nil {
if h.options.DynamicResp && len(req.URL.Query()) > 0 {
values := req.URL.Query()
if headers := values["header"]; len(headers) > 0 {
for _, header := range headers {
if headerParts := strings.SplitN(header, ":", 2); len(headerParts) == 2 {
w.Header().Add(headerParts[0], headerParts[1])
}
}
}
if delay := values.Get("delay"); delay != "" {
if parsed, err := strconv.Atoi(delay); err == nil {
time.Sleep(time.Duration(parsed) * time.Second)
}
}
if status := values.Get("status"); status != "" {
if parsed, err := strconv.Atoi(status); err == nil {
w.WriteHeader(parsed)
}
}
}
h.staticHandler.ServeHTTP(w, req)
} else if req.URL.Path == "/" && h.customBanner != "" {
b := strings.ReplaceAll(h.customBanner, "{REFLECTION}", reflection)
fmt.Fprint(w, strings.ReplaceAll(b, "{DOMAIN}", domain))
} else if req.URL.Path == "/" && reflection == "" {
fmt.Fprintf(w, banner, domain)
} else if strings.EqualFold(req.URL.Path, "/robots.txt") {
fmt.Fprintf(w, "User-agent: *\nDisallow: / # %s", reflection)
} else if stringsutil.HasSuffixI(req.URL.Path, ".json") {
fmt.Fprintf(w, "{\"data\":\"%s\"}", reflection)
w.Header().Set("Content-Type", "application/json")
} else if stringsutil.HasSuffixI(req.URL.Path, ".xml") {
fmt.Fprintf(w, "<data>%s</data>", reflection)
w.Header().Set("Content-Type", "application/xml")
} else {
if h.options.DynamicResp && (len(req.URL.Query()) > 0 || stringsutil.HasPrefixI(req.URL.Path, "/b64_body:")) {
writeResponseFromDynamicRequest(w, req)
return
}
fmt.Fprintf(w, "<html><head></head><body>%s</body></html>", reflection)
}
}
// writeResponseFromDynamicRequest writes a response to http.ResponseWriter
// based on dynamic data from HTTP URL Query parameters.
//
// The following parameters are supported -
//
// body (response body)
// header (response header)
// status (response status code)
// delay (response time)
func writeResponseFromDynamicRequest(w http.ResponseWriter, req *http.Request) {
values := req.URL.Query()
if stringsutil.HasPrefixI(req.URL.Path, "/b64_body:") {
firstindex := strings.Index(req.URL.Path, "/b64_body:")
lastIndex := strings.LastIndex(req.URL.Path, "/")
decodedBytes, _ := base64.StdEncoding.DecodeString(req.URL.Path[firstindex+10 : lastIndex])
_, _ = w.Write(decodedBytes)
}
if headers := values["header"]; len(headers) > 0 {
for _, header := range headers {
if headerParts := strings.SplitN(header, ":", 2); len(headerParts) == 2 {
w.Header().Add(headerParts[0], headerParts[1])
}
}
}
if delay := values.Get("delay"); delay != "" {
parsed, _ := strconv.Atoi(delay)
time.Sleep(time.Duration(parsed) * time.Second)
}
if status := values.Get("status"); status != "" {
parsed, _ := strconv.Atoi(status)
w.WriteHeader(parsed)
}
if body := values.Get("body"); body != "" {
_, _ = w.Write([]byte(body))
}
if b64_body := values.Get("b64_body"); b64_body != "" {
decodedBytes, _ := base64.StdEncoding.DecodeString(string([]byte(b64_body)))
_, _ = w.Write(decodedBytes)
}
}
// RegisterRequest is a request for client registration to interactsh server.
type RegisterRequest struct {
// PublicKey is the public RSA Key of the client.
PublicKey string `json:"public-key"`
// SecretKey is the secret-key for correlation ID registered for the client.
SecretKey string `json:"secret-key"`
// CorrelationID is an ID for correlation with requests.
CorrelationID string `json:"correlation-id"`
}
// registerHandler is a handler for client register requests
func (h *HTTPServer) registerHandler(w http.ResponseWriter, req *http.Request) {
r := &RegisterRequest{}
if err := jsoniter.NewDecoder(req.Body).Decode(r); err != nil {
gologger.Warning().Msgf("Could not decode json body: %s\n", err)
jsonError(w, fmt.Sprintf("could not decode json body: %s", err), http.StatusBadRequest)
return
}
atomic.AddInt64(&h.options.Stats.Sessions, 1)
if err := h.options.Storage.SetIDPublicKey(r.CorrelationID, r.SecretKey, r.PublicKey); err != nil {
gologger.Warning().Msgf("Could not set id and public key for %s: %s\n", r.CorrelationID, err)
jsonError(w, fmt.Sprintf("could not set id and public key: %s", err), http.StatusBadRequest)
return
}
jsonMsg(w, "registration successful", http.StatusOK)
gologger.Debug().Msgf("Registered correlationID %s for key\n", r.CorrelationID)
}
// DeregisterRequest is a request for client deregistration to interactsh server.
type DeregisterRequest struct {
// CorrelationID is an ID for correlation with requests.
CorrelationID string `json:"correlation-id"`
// SecretKey is the secretKey for the interactsh client.
SecretKey string `json:"secret-key"`
}
// deregisterHandler is a handler for client deregister requests
func (h *HTTPServer) deregisterHandler(w http.ResponseWriter, req *http.Request) {
atomic.AddInt64(&h.options.Stats.Sessions, -1)
r := &DeregisterRequest{}
if err := jsoniter.NewDecoder(req.Body).Decode(r); err != nil {
gologger.Warning().Msgf("Could not decode json body: %s\n", err)
jsonError(w, fmt.Sprintf("could not decode json body: %s", err), http.StatusBadRequest)
return
}
if err := h.options.Storage.RemoveID(r.CorrelationID, r.SecretKey); err != nil {
gologger.Warning().Msgf("Could not remove id for %s: %s\n", r.CorrelationID, err)
jsonError(w, fmt.Sprintf("could not remove id: %s", err), http.StatusBadRequest)
return
}
jsonMsg(w, "deregistration successful", http.StatusOK)
gologger.Debug().Msgf("Deregistered correlationID %s for key\n", r.CorrelationID)
}
// PollResponse is the response for a polling request
type PollResponse struct {
Data []string `json:"data"`
Extra []string `json:"extra"`
AESKey string `json:"aes_key"`
TLDData []string `json:"tlddata,omitempty"`
}
// pollHandler is a handler for client poll requests
func (h *HTTPServer) pollHandler(w http.ResponseWriter, req *http.Request) {
ID := req.URL.Query().Get("id")
if ID == "" {
jsonError(w, "no id specified for poll", http.StatusBadRequest)
return
}
secret := req.URL.Query().Get("secret")
if secret == "" {
jsonError(w, "no secret specified for poll", http.StatusBadRequest)
return
}
data, aesKey, err := h.options.Storage.GetInteractions(ID, secret)
if err != nil {
gologger.Warning().Msgf("Could not get interactions for %s: %s\n", ID, err)
jsonError(w, fmt.Sprintf("could not get interactions: %s", err), http.StatusBadRequest)
return
}
// At this point the client is authenticated, so we return also the data related to the auth token
var tlddata, extradata []string
if h.options.RootTLD {
for _, domain := range h.options.Domains {
interactions, _ := h.options.Storage.GetInteractionsWithId(domain)
// root domains interaction are not encrypted
tlddata = append(tlddata, interactions...)
}
}
if h.options.Token != "" {
// auth token interactions are not encrypted
extradata, _ = h.options.Storage.GetInteractionsWithId(h.options.Token)
}
response := &PollResponse{Data: data, AESKey: aesKey, TLDData: tlddata, Extra: extradata}
if err := jsoniter.NewEncoder(w).Encode(response); err != nil {
gologger.Warning().Msgf("Could not encode interactions for %s: %s\n", ID, err)
jsonError(w, fmt.Sprintf("could not encode interactions: %s", err), http.StatusBadRequest)
return
}
gologger.Debug().Msgf("Polled %d interactions for %s correlationID\n", len(data), ID)
}
func (h *HTTPServer) corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// Set CORS headers for the preflight request
if req.Method == http.MethodOptions {
w.Header().Set("Access-Control-Allow-Origin", h.options.OriginURL)
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
w.WriteHeader(http.StatusNoContent)
return
}
w.Header().Set("Access-Control-Allow-Origin", h.options.OriginURL)
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
next.ServeHTTP(w, req)
})
}
func jsonBody(w http.ResponseWriter, key, value string, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(code)
_ = jsoniter.NewEncoder(w).Encode(map[string]interface{}{key: value})
}
func jsonError(w http.ResponseWriter, err string, code int) {
jsonBody(w, "error", err, code)
}
func jsonMsg(w http.ResponseWriter, err string, code int) {
jsonBody(w, "message", err, code)
}
func (h *HTTPServer) authMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if !h.checkToken(req) {
w.WriteHeader(http.StatusUnauthorized)
return
}
next.ServeHTTP(w, req)
})
}
func (h *HTTPServer) checkToken(req *http.Request) bool {
return !h.options.Auth || h.options.Auth && h.options.Token == req.Header.Get("Authorization")
}
// metricsHandler is a handler for /metrics endpoint
func (h *HTTPServer) metricsHandler(w http.ResponseWriter, req *http.Request) {
interactMetrics := h.options.Stats
interactMetrics.Cache = GetCacheMetrics(h.options)
interactMetrics.Cpu = GetCpuMetrics()
interactMetrics.Memory = GetMemoryMetrics()
interactMetrics.Network = GetNetworkMetrics()
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
_ = jsoniter.NewEncoder(w).Encode(interactMetrics)
}