You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

362 lines
8.5 KiB

  1. /*
  2. Open Source Initiative OSI - The MIT License (MIT):Licensing
  3. The MIT License (MIT)
  4. Copyright (c) 2013 DutchCoders <http://github.com/dutchcoders/>
  5. Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. this software and associated documentation files (the "Software"), to deal in
  7. the Software without restriction, including without limitation the rights to
  8. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  9. of the Software, and to permit persons to whom the Software is furnished to do
  10. so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. */
  21. package virustotal
  22. import (
  23. "bytes"
  24. "encoding/json"
  25. "fmt"
  26. "io"
  27. "io/ioutil"
  28. "mime/multipart"
  29. "net/http"
  30. "net/url"
  31. "path/filepath"
  32. "strings"
  33. )
  34. type VirusTotal struct {
  35. apikey string
  36. }
  37. type VirusTotalResponse struct {
  38. ResponseCode int `json:"response_code"`
  39. Message string `json:"verbose_msg"`
  40. }
  41. type ScanResponse struct {
  42. VirusTotalResponse
  43. ScanId string `json:"scan_id"`
  44. Sha1 string `json:"sha1"`
  45. Resource string `json:"resource"`
  46. Sha256 string `json:"sha256"`
  47. Permalink string `json:"permalink"`
  48. Md5 string `json:"md5"`
  49. }
  50. type FileScan struct {
  51. Detected bool `json:"detected"`
  52. Version string `json:"version"`
  53. Result string `json:"result"`
  54. Update string `json:"update"`
  55. }
  56. type ReportResponse struct {
  57. VirusTotalResponse
  58. Resource string `json:"resource"`
  59. ScanId string `json:"scan_id"`
  60. Sha1 string `json:"sha1"`
  61. Sha256 string `json:"sha256"`
  62. Md5 string `json:"md5"`
  63. Scandate string `json:"scan_date"`
  64. Positives int `json:"positives"`
  65. Total int `json:"total"`
  66. Permalink string `json:"permalink"`
  67. Scans map[string]FileScan `json:"scans"`
  68. }
  69. func (sr *ScanResponse) String() string {
  70. return fmt.Sprintf("scanid: %s, resource: %s, permalink: %s, md5: %s", sr.ScanId, sr.Resource, sr.Permalink, sr.Md5)
  71. }
  72. type ScanUrlResponse struct {
  73. ScanResponse
  74. }
  75. type RescanResponse struct {
  76. ScanResponse
  77. }
  78. func (sr *RescanResponse) String() string {
  79. return fmt.Sprintf("scanid: %s, resource: %s, permalink: %s, md5: %s", sr.ScanId, sr.Resource, sr.Permalink, sr.Md5)
  80. }
  81. type DetectedUrl struct {
  82. ScanDate string `json:"scan_date"`
  83. Url string `json:"url"`
  84. Positives int `json:"positives"`
  85. Total int `json:"total"`
  86. }
  87. type Resolution struct {
  88. LastResolved string `json:"last_resolved"`
  89. Hostname string `json:"hostname"`
  90. }
  91. type IpAddressReportResponse struct {
  92. VirusTotalResponse
  93. Resolutions []Resolution `json:"resolutions"`
  94. DetectedUrls []DetectedUrl `json:"detected_urls"`
  95. }
  96. type DomainReportResponse struct {
  97. VirusTotalResponse
  98. Resolutions []Resolution `json:"resolutions"`
  99. DetectedUrls []DetectedUrl `json:"detected_urls"`
  100. }
  101. type CommentResponse struct {
  102. VirusTotalResponse
  103. }
  104. func NewVirusTotal(apikey string) (*VirusTotal, error) {
  105. vt := &VirusTotal{apikey: apikey}
  106. return vt, nil
  107. }
  108. func (vt *VirusTotal) DomainReport(domain string) (*DomainReportResponse, error) {
  109. u, err := url.Parse("https://www.virustotal.com/vtapi/v2/domain/report")
  110. u.RawQuery = url.Values{"apikey": {vt.apikey}, "domain": {domain}}.Encode()
  111. resp, err := http.Get(u.String())
  112. if err != nil {
  113. return nil, err
  114. }
  115. defer resp.Body.Close()
  116. contents, err := ioutil.ReadAll(resp.Body)
  117. if err != nil {
  118. return nil, err
  119. }
  120. var domainReportResponse = &DomainReportResponse{}
  121. err = json.Unmarshal(contents, &domainReportResponse)
  122. return domainReportResponse, err
  123. }
  124. func (vt *VirusTotal) ScanUrl(url2 *url.URL) (*ScanResponse, error) {
  125. u, err := url.Parse("https://www.virustotal.com/vtapi/v2/url/scan")
  126. params := url.Values{"apikey": {vt.apikey}, "url": {url2.String()}}
  127. resp, err := http.PostForm(u.String(), params)
  128. if err != nil {
  129. return nil, err
  130. }
  131. defer resp.Body.Close()
  132. contents, err := ioutil.ReadAll(resp.Body)
  133. if err != nil {
  134. return nil, err
  135. }
  136. var scanResponse = &ScanResponse{}
  137. err = json.Unmarshal(contents, &scanResponse)
  138. return scanResponse, err
  139. }
  140. func (vt *VirusTotal) Report(resource string) (*ReportResponse, error) {
  141. u, err := url.Parse("https://www.virustotal.com/vtapi/v2/file/report")
  142. params := url.Values{"apikey": {vt.apikey}, "resource": {resource}}
  143. resp, err := http.PostForm(u.String(), params)
  144. if err != nil {
  145. return nil, err
  146. }
  147. defer resp.Body.Close()
  148. contents, err := ioutil.ReadAll(resp.Body)
  149. if err != nil {
  150. return nil, err
  151. }
  152. var reportResponse = &ReportResponse{}
  153. err = json.Unmarshal(contents, &reportResponse)
  154. return reportResponse, err
  155. }
  156. func (vt *VirusTotal) ReportUrl(url2 *url.URL) (*ReportResponse, error) {
  157. params := url.Values{"apikey": {vt.apikey}, "resource": {url2.String()}}
  158. u, err := url.Parse("https://www.virustotal.com/vtapi/v2/url/report")
  159. resp, err := http.PostForm(u.String(), params)
  160. if err != nil {
  161. return nil, err
  162. }
  163. defer resp.Body.Close()
  164. contents, err := ioutil.ReadAll(resp.Body)
  165. if err != nil {
  166. return nil, err
  167. }
  168. var reportResponse = &ReportResponse{}
  169. err = json.Unmarshal(contents, &reportResponse)
  170. return reportResponse, err
  171. }
  172. func (vt *VirusTotal) Comment(resource string, comment string) (*CommentResponse, error) {
  173. u, err := url.Parse("https://www.virustotal.com/vtapi/v2/comments/put")
  174. params := url.Values{"apikey": {vt.apikey}, "resource": {resource}, "comment": {comment}}
  175. resp, err := http.PostForm(u.String(), params)
  176. if err != nil {
  177. return nil, err
  178. }
  179. defer resp.Body.Close()
  180. contents, err := ioutil.ReadAll(resp.Body)
  181. if err != nil {
  182. return nil, err
  183. }
  184. var commentResponse = &CommentResponse{}
  185. err = json.Unmarshal(contents, &commentResponse)
  186. return commentResponse, err
  187. }
  188. func (vt *VirusTotal) IpAddressReport(ip string) (*IpAddressReportResponse, error) {
  189. u, err := url.Parse("http://www.virustotal.com/vtapi/v2/ip-address/report")
  190. u.RawQuery = url.Values{"apikey": {vt.apikey}, "ip": {ip}}.Encode()
  191. resp, err := http.Get(u.String())
  192. if err != nil {
  193. return nil, err
  194. }
  195. defer resp.Body.Close()
  196. contents, err := ioutil.ReadAll(resp.Body)
  197. if err != nil {
  198. return nil, err
  199. }
  200. var ipAddressReportResponse = &IpAddressReportResponse{}
  201. err = json.Unmarshal(contents, &ipAddressReportResponse)
  202. return ipAddressReportResponse, err
  203. }
  204. func (vt *VirusTotal) Rescan(hash []string) (*RescanResponse, error) {
  205. resource := strings.Join(hash, ",")
  206. resp, err := http.PostForm("https://www.virustotal.com/vtapi/v2/file/rescan", url.Values{"apikey": {vt.apikey}, "resource": {resource}})
  207. if err != nil {
  208. return nil, err
  209. }
  210. defer resp.Body.Close()
  211. contents, err := ioutil.ReadAll(resp.Body)
  212. if err != nil {
  213. return nil, err
  214. }
  215. var rescanResponse = &RescanResponse{}
  216. err = json.Unmarshal(contents, &rescanResponse)
  217. return rescanResponse, err
  218. }
  219. func (vt *VirusTotal) Scan(path string, file io.Reader) (*ScanResponse, error) {
  220. params := map[string]string{
  221. "apikey": vt.apikey,
  222. }
  223. request, err := newfileUploadRequest("http://www.virustotal.com/vtapi/v2/file/scan", params, path, file)
  224. if err != nil {
  225. return nil, err
  226. }
  227. client := &http.Client{}
  228. resp, err := client.Do(request)
  229. if err != nil {
  230. return nil, err
  231. }
  232. defer resp.Body.Close()
  233. contents, err := ioutil.ReadAll(resp.Body)
  234. if err != nil {
  235. return nil, err
  236. }
  237. var scanResponse = &ScanResponse{}
  238. err = json.Unmarshal(contents, &scanResponse)
  239. return scanResponse, err
  240. }
  241. // Creates a new file upload http request with optional extra params
  242. func newfileUploadRequest(uri string, params map[string]string, path string, file io.Reader) (*http.Request, error) {
  243. body := &bytes.Buffer{}
  244. writer := multipart.NewWriter(body)
  245. for key, val := range params {
  246. _ = writer.WriteField(key, val)
  247. }
  248. part, err := writer.CreateFormFile("file", filepath.Base(path))
  249. if err != nil {
  250. return nil, err
  251. }
  252. _, err = io.Copy(part, file)
  253. err = writer.Close()
  254. if err != nil {
  255. return nil, err
  256. }
  257. req, err := http.NewRequest("POST", uri, body)
  258. req.Header.Set("Content-Type", writer.FormDataContentType())
  259. return req, err
  260. }