-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
188 lines (164 loc) · 4.87 KB
/
main.go
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
package main
import (
"bytes"
"encoding/csv"
"encoding/json"
"fmt"
"log"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"github.com/akamensky/argparse"
"github.com/schollz/progressbar"
)
func main() {
var results []VTResult
var buffer []string
var hashs []string
r := regexp.MustCompile("(?i)^https?://")
// argparse
parser := argparse.NewParser("hnuke", "Little handy tool to help hash analysis on VirusTotal (works with both VirusTotal free and entreprise API licence)")
apikey := parser.String("a", "apikey", &argparse.Options{Required: true, Help: "VirusTotal API Key"})
inFile := parser.String("i", "in", &argparse.Options{Required: true, Help: "Input file or directory"})
outFile := parser.String("o", "out", &argparse.Options{Required: false, Help: "Save results to specified file - if not mentioned results will be printed to standard output"})
format := parser.String("f", "format", &argparse.Options{Required: false, Default: "csv", Help: "Output format - csv | json supported"})
mode := parser.String("m", "mode", &argparse.Options{Required: false, Default: "parser", Help: "hnuke mode \n\n\tparser : scan for md5/sha1/sha256 hash inside provided input file or directory\n\tanalysis : get hash of provided input file or directory and search them in VirusTotal\n"})
recursive := parser.Flag("r", "recursive", &argparse.Options{Required: false, Help: "\nIf input path is a directory, scan for files recursively (works with both parser and analysis mode)"})
extensions := parser.StringList("e", "extension", &argparse.Options{Required: false, Help: "If input path is a directory, include specified file extension (can be used multiple time)"})
err := parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
}
// save file routine if program is interrupted
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
outputResults(results, *outFile, *format)
os.Exit(1)
}()
switch *mode {
case "parser":
if r.MatchString(*inFile) {
// parser mode - url
hashs, err = ExtractHashFromURL(*inFile)
if err != nil {
log.Fatal(err)
}
// parser mode - files
} else {
var files []string
files, err = RetrivesFilesFromUserPath(*inFile, *extensions, *recursive)
if err != nil {
log.Fatal(err)
}
for _, f := range files {
var h []string
h, err = ExtractHashFromFile(f)
if err != nil {
log.Fatal(err)
}
hashs = append(hashs, h...)
}
}
case "analysis":
if r.MatchString(*inFile) {
// analysis mode - url
var h string
h, err = HashContentFromURL(*inFile)
if err != nil {
log.Fatal(err)
}
hashs = append(hashs, h)
} else {
// analysis mode - files
files, err := RetrivesFilesFromUserPath(*inFile, *extensions, *recursive)
if err != nil {
log.Fatal(err)
}
for _, f := range files {
var h string
h, err = HashContentFromFile(f)
if err != nil {
log.Fatal(err)
}
hashs = append(hashs, h)
}
}
default:
log.Fatal("Unknown hnuke mode - please specify parser or analysis mode")
}
// hash search on VirusTotal
hashs = UniqueSliceMembers(hashs)
bar := progressbar.Default(int64(len(hashs)))
for _, h := range hashs {
res, err := GetHashInformations(h, *apikey)
if err != nil {
log.Fatal(err)
}
// check if the current result was already searched on another hashs type (avoid duplicate entries)
if len(res.id) > 0 && !StringInSlice(res.id, buffer) {
buffer = append(buffer, res.Sha256)
results = append(results, res)
}
bar.Add(1)
}
outputResults(results, *outFile, *format)
}
func outputResults(results []VTResult, out string, format string) {
var content []byte
var buffer bytes.Buffer
if len(results) > 0 {
var err error
switch strings.ToLower(format) {
case "json":
// json output
content, err = json.Marshal(results)
if err != nil {
log.Fatal(err)
}
default:
// csv output (default)
writer := csv.NewWriter(&buffer)
for i, value := range results {
if i == 0 {
if err := writer.Write(value.GetHeaders()); err != nil {
log.Fatal(err)
}
}
if err := writer.Write(value.GetValues()); err != nil {
log.Fatal(err)
}
}
writer.Flush()
if err := writer.Error(); err != nil {
log.Fatal(err)
}
content = buffer.Bytes()
}
}
// save data to standard output or file
if len(content) > 0 {
if len(out) == 0 {
fmt.Println(string(content))
} else {
var outFile string
outFile = out
if (strings.ToLower(format) == "json" || strings.ToLower(format) == "csv") && !strings.HasSuffix(strings.ToLower(out), strings.ToLower(format)) {
outFile += "." + strings.ToLower(format)
}
f, err := os.OpenFile(outFile, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
log.Fatal(err)
}
if _, err = f.Write(content); err != nil {
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
}
}
}