Browse Source

Better sanitization of tokens

Loweel 4 years ago
parent
commit
a48a60145f
2 changed files with 6 additions and 8 deletions
  1. 4 8
      classifier.go
  2. 2 0
      file.go

+ 4 - 8
classifier.go

@@ -69,12 +69,10 @@ func blockAndlearn(resp *http.Response) error {
 
 func sanitizeHeaders(s string) string {
 
-	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})`)
+	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})`)
 	matched := re.FindAllString(s, -1)
 
 	tmpSt := strings.ToLower(strings.Join(matched, " "))
-	tmpSt = strings.ReplaceAll(tmpSt, "{", "")
-	tmpSt = strings.ReplaceAll(tmpSt, "}", "")
 
 	log.Println("Matched: " + tmpSt)
 
@@ -86,13 +84,11 @@ func feedRequest(req *http.Request, class string) {
 
 	feed := formatRequest(req)
 
-	feed = sanitizeHeaders(feed)
-
 	if class == "BAD" {
 
 		log.Println("Feeding BAD token: ", feed)
 
-		ControPlane.BadTokens <- feed
+		ControPlane.BadTokens <- sanitizeHeaders(feed)
 
 	}
 
@@ -100,7 +96,7 @@ func feedRequest(req *http.Request, class string) {
 
 		log.Println("Feeding GOOD Token:", feed)
 
-		ControPlane.GoodTokens <- feed
+		ControPlane.GoodTokens <- sanitizeHeaders(feed)
 
 	}
 
@@ -117,6 +113,6 @@ func formatRequest(req *http.Request) string {
 		fmt.Println(err)
 	}
 
-	return fmt.Sprintf("{%s} %s\n", req.URL.Path, requestDump)
+	return fmt.Sprintf("%s\n", requestDump)
 
 }

+ 2 - 0
file.go

@@ -36,6 +36,8 @@ func handlepanic() {
 func saveBayesToFile() {
 
 	log.Println("Trying to write json file")
+	defer handlepanic()
+	
 
 	var jsnBuf = new(bytes.Buffer)
 	var tmpJSON string