2019-11-22 11:35:16 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-11-28 11:16:14 +01:00
|
|
|
func passAndLearn(resp *http.Response) error {
|
2019-11-22 11:35:16 +01:00
|
|
|
|
|
|
|
ProxyFlow.response = resp
|
|
|
|
ProxyFlow.seniority++
|
|
|
|
req := ProxyFlow.request
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case resp.StatusCode == 401:
|
|
|
|
log.Println("401: We don't want to store credentials")
|
|
|
|
case resp.StatusCode > 399:
|
|
|
|
buf := bytes.NewBufferString(BlockMessage)
|
|
|
|
resp.Body = ioutil.NopCloser(buf)
|
|
|
|
resp.Status = "403 Forbidden"
|
|
|
|
resp.StatusCode = 403
|
|
|
|
resp.Header["Content-Length"] = []string{fmt.Sprint(buf.Len())}
|
|
|
|
resp.Header.Set("Content-Encoding", "none")
|
|
|
|
log.Println("Filing inside bad class")
|
|
|
|
feedRequest(req, "BAD")
|
2019-11-24 19:59:18 +01:00
|
|
|
ControPlane.StatsTokens <- "LEARN-BAD"
|
2019-11-22 11:35:16 +01:00
|
|
|
default:
|
|
|
|
log.Println("Filing inside Good Class: ", resp.StatusCode)
|
|
|
|
feedRequest(req, "GOOD")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:16:14 +01:00
|
|
|
func blockAndlearn(resp *http.Response) error {
|
2019-11-24 12:56:29 +01:00
|
|
|
|
|
|
|
ProxyFlow.response = resp
|
|
|
|
ProxyFlow.seniority++
|
|
|
|
req := ProxyFlow.request
|
|
|
|
|
|
|
|
buf := bytes.NewBufferString(BlockMessage)
|
|
|
|
resp.Body = ioutil.NopCloser(buf)
|
|
|
|
resp.Status = "403 Forbidden"
|
|
|
|
resp.StatusCode = 403
|
|
|
|
resp.Header["Content-Length"] = []string{fmt.Sprint(buf.Len())}
|
|
|
|
resp.Header.Set("Content-Encoding", "none")
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case resp.StatusCode == 401:
|
|
|
|
log.Println("401: We don't want to store credentials")
|
|
|
|
case resp.StatusCode > 399:
|
|
|
|
log.Println("Filing inside bad class")
|
|
|
|
feedRequest(req, "BAD")
|
|
|
|
default:
|
|
|
|
log.Println("Filing inside Good Class: ", resp.StatusCode)
|
2019-11-24 19:59:18 +01:00
|
|
|
ControPlane.StatsTokens <- "LEARN-GOOD"
|
2019-11-24 12:56:29 +01:00
|
|
|
feedRequest(req, "GOOD")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-22 11:35:16 +01:00
|
|
|
func sanitizeHeaders(s string) string {
|
|
|
|
|
2019-12-04 14:38:28 +01:00
|
|
|
re := regexp.MustCompile(`[a-zA-Z]{4,32}|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})|([{][/].*[}])|([0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12})`)
|
2019-11-22 11:35:16 +01:00
|
|
|
matched := re.FindAllString(s, -1)
|
2019-12-04 14:35:40 +01:00
|
|
|
|
|
|
|
tmpSt := strings.ToLower(strings.Join(matched, " "))
|
|
|
|
tmpSt = strings.ReplaceAll(tmpSt, "{", "")
|
|
|
|
tmpSt = strings.ReplaceAll(tmpSt, "}", "")
|
|
|
|
|
|
|
|
log.Println("Matched: " + tmpSt)
|
|
|
|
|
|
|
|
return tmpSt
|
2019-11-22 11:35:16 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func feedRequest(req *http.Request, class string) {
|
|
|
|
|
|
|
|
feed := formatRequest(req)
|
|
|
|
|
|
|
|
feed = sanitizeHeaders(feed)
|
|
|
|
|
|
|
|
if class == "BAD" {
|
|
|
|
|
2019-12-09 15:51:56 +01:00
|
|
|
log.Println("Feeding BAD token: ", feed)
|
2019-12-04 14:35:40 +01:00
|
|
|
|
2019-12-09 15:51:56 +01:00
|
|
|
ControPlane.BadTokens <- feed
|
2019-11-22 11:35:16 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if class == "GOOD" {
|
|
|
|
|
2019-12-09 15:51:56 +01:00
|
|
|
log.Println("Feeding GOOD Token:", feed)
|
2019-12-04 14:35:40 +01:00
|
|
|
|
2019-12-09 15:51:56 +01:00
|
|
|
ControPlane.GoodTokens <- feed
|
2019-11-22 11:35:16 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatRequest(req *http.Request) string {
|
|
|
|
|
2019-11-24 16:33:38 +01:00
|
|
|
ingestBody := req.ContentLength < 2048 && req.ContentLength > 1
|
|
|
|
|
|
|
|
log.Println("Ingesting the body: ", ingestBody)
|
|
|
|
|
|
|
|
requestDump, err := httputil.DumpRequest(req, ingestBody)
|
2019-11-22 11:35:16 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
2019-12-04 14:35:40 +01:00
|
|
|
return fmt.Sprintf("{%s} %s\n", req.URL.Path, requestDump)
|
|
|
|
|
2019-11-22 11:35:16 +01:00
|
|
|
}
|