python实现上传样本到virustotal并查询扫描信息的方法

yipeiwu_com6年前Python基础

本文实例讲述了python实现上传样本到virustotal并查询扫描信息的方法。分享给大家供大家参考。具体方法如下:

import simplejson 
import urllib 
import urllib2 
import os  
 
MD5 = "5248f774d2ee0a10936d0b1dc89107f1" 
MD5 = "12fa5fb74201d9b6a14f63fbf9a81ff6" #do not have report on virustotal.com 
       
######################################################################## 
APIKEY = "e0a50a50e77fxxxxxxxxxxxxxx4f17e31 这里用你自己在virustotal上申请的账号的KEY" 
 
 
class VirusTotal: 
  """""" 
 
  def __init__(self, md5): 
    """Constructor""" 
    self._virus_dict = {} 
    self._md5 = md5 
     
     
  def repr(self): 
    return str(self._virus_dict) 
   
  def submit_md5(self, file_path): 
    import postfile                                      
    #submit the file 
    FILE_NAME = os.path.basename(file_path)  
               
                                                  
    host = "www.virustotal.com"                                
    selector = "https://www.virustotal.com/vtapi/v2/file/scan"                 
    fields = [("apikey", APIKEY)] 
    file_to_send = open(file_path, "rb").read()                        
    files = [("file", FILE_NAME, file_to_send)]                        
    json = postfile.post_multipart(host, selector, fields, files)               
    print json 
    pass 
   
  def get_report_dict(self): 
    result_dict = {} 
     
    url = "https://www.virustotal.com/vtapi/v2/file/report" 
    parameters = {"resource": self._md5, 
            "apikey": APIKEY} 
    data = urllib.urlencode(parameters) 
    req = urllib2.Request(url, data) 
    response = urllib2.urlopen(req) 
    json = response.read() 
     
    response_dict = simplejson.loads(json) 
    if response_dict["response_code"]: #has result  
      scans_dict = response_dict.get("scans", {}) 
      for anti_virus_comany, virus_name in scans_dict.iteritems(): 
        if virus_name["detected"]: 
          self._virus_dict.setdefault(anti_virus_comany, virus_name["result"]) 
    return self._virus_dict 

返回的结果为:{u'Sophos': u'Sus/Behav-1010'},如果有扫描出的结果的话..

调用的方法如下:

MD5 = "12fa5fb74201d9b6a14f63fbf9a81ff6" #do not have report on virustotal.com 
MD5 = "5248f774d2ee0a10936d0b1dc89107f1" 
FILE_PATH = r"D:\backSample\10\9af41bc012d66c98ca2f9c68ba38e98f_ICQLiteShell.dll" 
 
from getVirusTotalInfo import VirusTotal 
#得到扫描结果并打印出来 
virus_total = VirusTotal(MD5) 
print virus_total.get_report_dict() 
 
#提交文件到扫描,以后就可以根据这个MD5取扫描结果了 
virus_total.submit_md5(FILE_PATH) 

希望本文所述对大家的Python程序设计有所帮助。

相关文章

python numpy 部分排序 寻找最大的前几个数的方法

如下所示: import numpy as np K=4 a = np.array([0, 8, 0, 4, 5, 8, 8, 0, 4, 2]) a[np.argpartition...

浅谈python下含中文字符串正则表达式的编码问题

浅谈python下含中文字符串正则表达式的编码问题

前言 Python文件默认的编码格式是ascii ,无法识别汉字,因为ascii码中没有中文。 所以py文件中要写中文字符时,一般在开头加 # -*- coding: utf-8 -*-...

Python3+Appium实现多台移动设备操作的方法

需求: 连接本机的(两台安卓手机)或者本机安装的(安卓模拟器两个),实现同时安装本地apk包 。 demon.py 特别说明:必须写udid才能实现同时对两台以上的移动设备操作。对dev...

浅谈Python中的可变对象和不可变对象

什么是可变/不可变对象 不可变对象,该对象所指向的内存中的值不能被改变。当改变某个变量时候,由于其所指的值不能被改变,相当于把原来的值复制一份后再改变,这会开辟一个新的地址,变量再指向这...

pandas数据筛选和csv操作的实现方法

1. 数据筛选 a b c 0 0 2 4 1 6 8 10 2 12 14 16 3 18 20 22 4 24 26 28 5 30 32 34 6 36 38 40 7 42...