Skip to content

Commit 84787b0

Browse files
Merge pull request #1 from needmorecowbell/dev-golang-processor
migrate to golang
2 parents ae98481 + 5fe651c commit 84787b0

10 files changed

+229
-67
lines changed

server/README.md

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
# Yara Processor
22

3-
Proof of concept yara processor. Just takes in the raw eml and checks it against the yara ruleset, returning a json response.
4-
5-
## Installation
6-
7-
- `apt install python3 python3-pip yara python3-flask python3-yara gunicorn` (or using a virtualenv)
3+
YARA Processor Server. Just takes in the raw eml and checks it against the yara ruleset, returning a json response.
84

95
## Usage
10-
- `gunicorn app:app --bind=127.0.0.1:6000`
6+
- `go run main.go`
117

12-
spins up the flask server, opens an index route with welcome banner and a /scan route which accepts post requests, with the raw eml as byte data.
8+
spins up the server, opens an index route with welcome banner and a /scan route which accepts post requests, with the raw eml as byte data.
139

1410
curl:
1511

1612
```
17-
curl -X POST --data-binary "@/home/user/Downloads/test.eml" http://127.0.0.1:6000/scan
13+
curl -X POST --data-binary "@/home/user/Downloads/test.eml" http://127.0.0.1:8080/scan
1814
{"matches":["DetectMalicious"],"status":"malicious"}
1915
```

server/app.py

-56
This file was deleted.

server/go.mod

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module github.com/needmorecowbell/email_threatkb/server
2+
3+
go 1.21.4
4+
5+
require (
6+
github.com/bytedance/sonic v1.10.2 // indirect
7+
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
8+
github.com/chenzhuoyu/iasm v0.9.1 // indirect
9+
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
10+
github.com/gin-contrib/sse v0.1.0 // indirect
11+
github.com/gin-gonic/gin v1.9.1 // indirect
12+
github.com/go-playground/locales v0.14.1 // indirect
13+
github.com/go-playground/universal-translator v0.18.1 // indirect
14+
github.com/go-playground/validator/v10 v10.16.0 // indirect
15+
github.com/goccy/go-json v0.10.2 // indirect
16+
github.com/hillu/go-yara/v4 v4.3.2 // indirect
17+
github.com/json-iterator/go v1.1.12 // indirect
18+
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
19+
github.com/leodido/go-urn v1.2.4 // indirect
20+
github.com/mattn/go-isatty v0.0.20 // indirect
21+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
22+
github.com/modern-go/reflect2 v1.0.2 // indirect
23+
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
24+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
25+
github.com/ugorji/go/codec v1.2.11 // indirect
26+
golang.org/x/arch v0.6.0 // indirect
27+
golang.org/x/crypto v0.15.0 // indirect
28+
golang.org/x/net v0.18.0 // indirect
29+
golang.org/x/sys v0.14.0 // indirect
30+
golang.org/x/text v0.14.0 // indirect
31+
google.golang.org/protobuf v1.31.0 // indirect
32+
gopkg.in/yaml.v3 v3.0.1 // indirect
33+
)

server/go.sum

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
2+
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
3+
github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE=
4+
github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
5+
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
6+
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
7+
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
8+
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA=
9+
github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
10+
github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0=
11+
github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
12+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
13+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14+
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
15+
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
16+
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
17+
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
18+
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
19+
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
20+
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
21+
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
22+
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
23+
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
24+
github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE=
25+
github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
26+
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
27+
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
28+
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
29+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
30+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
31+
github.com/hillu/go-yara/v4 v4.3.2 h1:HGqUN3ORUduWZbb95RQjut4UzavGDbtt/C6SnGB3Amk=
32+
github.com/hillu/go-yara/v4 v4.3.2/go.mod h1:AHEs/FXVMQKVVlT6iG9d+q1BRr0gq0WoAWZQaZ0gS7s=
33+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
34+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
35+
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
36+
github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc=
37+
github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
38+
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
39+
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
40+
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
41+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
42+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
43+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
44+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
45+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
46+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
47+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
48+
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
49+
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
50+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
51+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
52+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
53+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
54+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
55+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
56+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
57+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
58+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
59+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
60+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
61+
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
62+
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
63+
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
64+
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
65+
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
66+
golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc=
67+
golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
68+
golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
69+
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
70+
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
71+
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
72+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
73+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
74+
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
75+
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
76+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
77+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
78+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
79+
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
80+
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
81+
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
82+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
83+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
84+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
85+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
86+
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
87+
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

server/main.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
7+
"github.com/gin-gonic/gin"
8+
)
9+
10+
func setupRouter() *gin.Engine {
11+
// Disable Console Color
12+
r := gin.Default()
13+
14+
// Ping test
15+
r.GET("/", func(c *gin.Context) {
16+
c.JSON(http.StatusOK, gin.H{"status": "success", "message": "Welcome to the eml processor"})
17+
})
18+
r.POST("/scan", server.endpointScan)
19+
return r
20+
}
21+
22+
func main() {
23+
log.Println("Starting eml processor")
24+
r := setupRouter()
25+
// Listen and Server in 0.0.0.0:8080
26+
r.Run(":8080")
27+
}

server/rest/endpoint_index.http

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
http://127.0.0.1:6000/
1+
http://127.0.0.1:8080/

server/rest/endpoint_scan malicious.http

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
POST http://127.0.0.1:6000/scan
1+
POST http://127.0.0.1:8080/scan
22

33
Delivered-To: lao@gmail.com
44
From: crera <3@gmail.com>

server/rest/endpoint_scan.http

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
POST http://127.0.0.1:6000/scan
1+
POST http://127.0.0.1:8080/scan
22

33
Delivered-To: lao@gmail.com
44
From: crera <3@gmail.com>

server/server/endpoint_scan.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package server
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
"github.com/hillu/go-yara/v4"
8+
)
9+
10+
func endpointScan(c *gin.Context) {
11+
// get the eml from the body
12+
eml_bytes, err := c.GetRawData()
13+
14+
if err != nil {
15+
c.String(http.StatusBadRequest, "Error gathering eml from body")
16+
return
17+
}
18+
yc, err := initYARACompiler()
19+
if err != nil {
20+
c.String(http.StatusInternalServerError, "Error compiling rules")
21+
return
22+
}
23+
yaraRules, err := yc.GetRules()
24+
if err != nil {
25+
c.String(http.StatusInternalServerError, "Error retrieving compiled rules")
26+
return
27+
}
28+
29+
scanner, err := yara.NewScanner(yaraRules)
30+
if err != nil {
31+
c.String(http.StatusInternalServerError, "Error creating scanner from rules")
32+
return
33+
}
34+
35+
var matches yara.MatchRules
36+
err = scanner.SetCallback(&matches).ScanMem(eml_bytes)
37+
if err != nil {
38+
c.String(http.StatusInternalServerError, "Error scanning eml with rules")
39+
return
40+
}
41+
42+
if len(matches) == 0 {
43+
c.JSON(http.StatusOK, gin.H{"status": "clean", "matches": []string{}})
44+
return
45+
}
46+
47+
var matchStrings []string
48+
for _, match := range matches {
49+
matchStrings = append(matchStrings, match.Rule)
50+
}
51+
c.JSON(http.StatusOK, gin.H{"status": "malicious", "matches": matchStrings})
52+
53+
}

server/server/yara.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package server
2+
3+
import "github.com/hillu/go-yara/v4"
4+
5+
func initYARACompiler() (*yara.Compiler, error) {
6+
yaraCompiler, err := yara.NewCompiler()
7+
if err != nil {
8+
return nil, err
9+
}
10+
err = yaraCompiler.AddString(`rule DetectMalicious {
11+
strings:
12+
$malicious_string = "malicious_pattern"
13+
condition:
14+
$malicious_string
15+
}`, "rules")
16+
17+
if err != nil {
18+
//c.String(http.StatusInternalServerError, "Error retrieving compiled rules")
19+
return nil, err
20+
}
21+
return yaraCompiler, nil
22+
}

0 commit comments

Comments
 (0)