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

yipeiwu_com5年前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程序设计有所帮助。

相关文章

使用pandas读取csv文件的指定列方法

根据教程实现了读取csv文件前面的几行数据,一下就想到了是不是可以实现前面几列的数据。经过多番尝试总算试出来了一种方法。 之所以想实现读取前面的几列是因为我手头的一个csv文件恰好有后面...

基于python requests库中的代理实例讲解

直接上代码: #request代理(proxy) """ 1.启动代理服务器Heroku,相当于aliyun 2.在主机1080端口启动Socks 服务 3.将请求转发到1080端口...

Python实现的多线程端口扫描工具分享

Python实现的多线程端口扫描工具分享

昨晚今晚写了两晚,总算把Py Port Scanner 写完了,姑且称之为0.1版本,算是一个Python多线程端口扫描工具。 水平有限,实话中间有一些困惑和不解的地方,代码可能也写的比...

python实现搜索本地文件信息写入文件的方法

python实现搜索本地文件信息写入文件的方法

本文实例讲述了python实现搜索本地文件信息写入文件的方法。分享给大家供大家参考,具体如下: 主要功能: 在指定的盘符,如D盘,搜索出与用户给定后缀名(如:jpg,png)相关的文件,...

python递归打印某个目录的内容(实例讲解)

以下函数列出某个目录下(包括子目录)所有文件,本随笔重点不在于递归函数的实现,这是一个很简单的递归,重点在于熟悉Python 库os以及os.path一些函数的功能和用法。 1. os....