-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvirustotalapi.go
228 lines (206 loc) · 6.37 KB
/
virustotalapi.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package virustotal
import (
"errors"
"io"
"os"
"vighnesh.org/virustotal/json"
"vighnesh.org/virustotal/net/http"
"vighnesh.org/virustotal/net/multipart"
"vighnesh.org/virustotal/util"
)
const (
URL_FILE_SCAN string = "https://www.virustotal.com/vtapi/v2/file/scan"
URL_FILE_RESCAN string = "https://www.virustotal.com/vtapi/v2/file/rescan"
URL_FILE_UPLOAD string = "https://www.virustotal.com/vtapi/v2/file/scan/upload_url"
URL_FILE_DOWNLOAD string = "https://www.virustotal.com/vtapi/v2/file/download"
URL_FILE_BEHAVIOUR string = "https://www.virustotal.com/vtapi/v2/file/behaviour"
URL_FILE_SCAN_REPORT string = "https://www.virustotal.com/vtapi/v2/file/report"
URL_URL_SCAN string = "https://www.virustotal.com/vtapi/v2/url/scan"
URL_URL_SCAN_REPORT string = "http://www.virustotal.com/vtapi/v2/url/report"
URL_IP_SCAN_REPORT string = "http://www.virustotal.com/vtapi/v2/ip-address/report"
URL_DOMAIN_REPORT string = "http://www.virustotal.com/vtapi/v2/domain/report"
URL_COMMENTS string = "https://www.virustotal.com/vtapi/v2/comments/put"
)
var (
API_KEY_ERROR error = errors.New("API Key is Not Valid")
FILE_ERROR error = errors.New("File Not Found or Can not Access")
FILE_STREAM_ERROR error = errors.New("File Stream is nil")
COMMENT_ERROR error = errors.New("Some Thing Went Wrong")
DOMAIN_ERROR error = errors.New("Domain is Not Valid")
FILE_NAME_ERROR error = errors.New("File Name Error")
IP_ADDRESS_ERROR error = errors.New("IP Address is Not Valid")
)
type VirusTotalApi interface {
ScanFile(file string) (*json.Response, error)
ScanFileStream(filename string, reader io.Reader) (*json.Response, error)
FileUploadURL() (string, error)
FileDownload(hash string) ([]byte, error)
FileBehaviour(hash string) (string, error)
ScanURL(url string) (*json.Response, error)
ReScanFile(resource string) (*json.ReScanResponse, error)
FileReport(resource string) (*json.Report, error)
URLReport(resource string) (*json.URLReport, error)
IPReport(ip string) (*json.IPReport, error)
DomainReport(domain string) (*json.DomainReport, error)
Comment(resource, comment string) (*json.CommentStatus, error)
}
type virustotal struct {
apiKey string
}
func Configure(apiKey string) (VirusTotalApi, error) {
if apiKey != "" && len(apiKey) > 0 {
return virustotal{apiKey}, nil
}
return nil, API_KEY_ERROR
}
func (virustotal virustotal) ScanFile(file string) (*json.Response, error) {
f, e := os.Open(file)
defer f.Close()
if e != nil {
return nil, e
}
return virustotal.ScanFileStream(f.Name(), f)
}
func (virustotal virustotal) ScanFileStream(filename string, reader io.Reader) (*json.Response, error) {
if filename == "" {
return nil, FILE_NAME_ERROR
}
if reader == nil {
return nil, FILE_STREAM_ERROR
}
me := &multipart.MultipartEntity{}
me.AddTextBody("apikey", virustotal.apiKey)
me.AddBinaryBody("file", filename, reader)
response, e := http.RequestPost(me, URL_FILE_SCAN)
if e != nil {
return nil, e
}
var report json.Response
e = util.To(response, &report)
if e != nil {
return nil, e
}
return &report, e
}
func (virustotal virustotal) FileUploadURL() (string, error) {
response, e := http.RequestGet(virustotal.apiKey, URL_FILE_UPLOAD, "", "")
if e != nil {
return "", e
}
return string(response), e
}
func (virustotal virustotal) FileDownload(hash string) ([]byte, error) {
response, e := http.RequestGet(virustotal.apiKey, URL_FILE_DOWNLOAD, "hash", hash)
if e != nil {
return nil, e
}
return response, e
}
func (virustotal virustotal) FileBehaviour(hash string) (string, error) {
response, e := http.RequestGet(virustotal.apiKey, URL_FILE_BEHAVIOUR, "hash", hash)
if e != nil {
return "", e
}
return string(response), e
}
func (virustotal virustotal) ScanURL(url string) (*json.Response, error) {
me := &multipart.MultipartEntity{}
me.AddTextBody("apikey", virustotal.apiKey)
me.AddTextBody("url", url)
response, e := http.RequestPost(me, URL_URL_SCAN)
if e != nil {
return nil, e
}
var report json.Response
e = util.To(response, &report)
if e != nil {
return nil, e
}
return &report, e
}
func (virustotal virustotal) ReScanFile(resource string) (*json.ReScanResponse, error) {
me := &multipart.MultipartEntity{}
me.AddTextBody("apikey", virustotal.apiKey)
me.AddTextBody("resource", resource)
response, e := http.RequestPost(me, URL_FILE_RESCAN)
if e != nil {
return nil, e
}
var report json.ReScanResponse
e = util.To(response, &report)
if e != nil {
return nil, e
}
return &report, e
}
func (virustotal virustotal) FileReport(resource string) (*json.Report, error) {
me := &multipart.MultipartEntity{}
me.AddTextBody("apikey", virustotal.apiKey)
me.AddTextBody("resource", resource)
response, e := http.RequestPost(me, URL_FILE_SCAN_REPORT)
if e != nil {
return nil, e
}
var report json.Report
e = util.To(response, &report)
if e != nil {
return nil, e
}
return &report, e
}
func (virustotal virustotal) URLReport(resource string) (*json.URLReport, error) {
me := &multipart.MultipartEntity{}
me.AddTextBody("apikey", virustotal.apiKey)
me.AddTextBody("resource", resource)
me.AddTextBody("scan", "1")
response, e := http.RequestPost(me, URL_URL_SCAN_REPORT)
if e != nil {
return nil, e
}
var report json.URLReport
e = util.To(response, &report)
if e != nil {
return nil, e
}
return &report, e
}
func (virustotal virustotal) IPReport(ip string) (*json.IPReport, error) {
response, e := http.RequestGet(virustotal.apiKey, URL_IP_SCAN_REPORT, "ip", ip)
if e != nil {
return nil, e
}
var report json.IPReport
e = util.To(response, &report)
if e != nil {
return nil, e
}
return &report, e
}
func (virustotal virustotal) DomainReport(domain string) (*json.DomainReport, error) {
response, e := http.RequestGet(virustotal.apiKey, URL_DOMAIN_REPORT, "domain", domain)
if e != nil {
return nil, e
}
var report json.DomainReport
e = util.To(response, &report)
if e != nil {
return nil, e
}
return &report, e
}
func (virustotal virustotal) Comment(resource, comment string) (*json.CommentStatus, error) {
me := &multipart.MultipartEntity{}
me.AddTextBody("resource", resource)
me.AddTextBody("comment", comment)
me.AddTextBody("apikey", virustotal.apiKey)
response, e := http.RequestPost(me, URL_COMMENTS)
if e != nil {
return nil, e
}
var report json.CommentStatus
e = util.To(response, &report)
if e != nil {
return nil, e
}
return &report, e
}