-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalware-sums.go
More file actions
99 lines (85 loc) · 2.05 KB
/
malware-sums.go
File metadata and controls
99 lines (85 loc) · 2.05 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
package main
import (
"crypto/md5"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"github.com/gin-gonic/gin"
"gopkg.in/yaml.v2"
)
const filename = "/app/malwares.yml"
type Malware struct {
Data []Data `yaml:"data"`
}
type Data struct {
Title string `yaml:"title"`
Sha256Sum string `yaml:"sha256sum"`
Md5Sum string `yaml:"md5sum"`
Info string `yaml:"info"`
Counter int64 `yaml:"counter"`
}
func (m *Malware) LoadData(filename string) {
if fileExists(filename) {
file, _ := ioutil.ReadFile(filename)
_ = yaml.Unmarshal(file, m)
} else {
return
}
}
func (m *Malware) SearchSum(checksum string) *Data {
for i, row := range m.Data {
if checksum == row.Md5Sum || checksum == row.Sha256Sum {
m.Data[i].Counter += 1
return &row
}
}
return nil
}
func (m *Malware) SaveData(filename string) {
file, _ := yaml.Marshal(m)
_ = ioutil.WriteFile(filename, file, 0644)
}
func CheckSum(c *gin.Context) {
sum := c.PostForm("sum")
fmt.Println(sum)
var m Malware
m.LoadData(filename)
data := m.SearchSum(sum)
m.SaveData(filename)
if data != nil {
c.HTML(http.StatusOK, "file_sum.html", gin.H{"data": data})
return
}
c.HTML(http.StatusOK, "file_sum.html", gin.H{"NotAMalware": "Undetected"})
}
func CheckFile(c *gin.Context) {
file, _ := c.FormFile("sus_file")
fileOpened, _ := file.Open()
fileData, _ := ioutil.ReadAll(fileOpened)
hasher := md5.New()
io.WriteString(hasher, string(fileData))
stringHash := fmt.Sprintf("%x", hasher.Sum(nil))
fmt.Println(stringHash)
var m Malware
m.LoadData(filename)
data := m.SearchSum(stringHash)
m.SaveData(filename)
if data != nil {
c.HTML(http.StatusOK, "file_upload.html", gin.H{"data": data})
return
}
c.HTML(http.StatusOK, "file_upload.html", gin.H{"NotAMalware": "Undetected"})
}
func IndexHandler(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
//curl http://localhost:8000/malware -XPOST -F 'sum=d41d8cd98f00b204e9800998ecf8427e'