当前位置:编程学习 > 网站相关 >>

利用virustotal公共API获取病毒样本扫描结果

virustotal.com网站允许用户上传恶意样本,然后返回46个病毒扫描器的扫描报告。

利用virustotal.com网站提供的公共API,可以编写python脚本自动获取这个扫描报告。

官方网站就有详细的教程 https://www.virustotal.com/en/documentation/public-api/

首先要注册一个帐号,登录后在帐号Profile下可以发现自己的apikey。

然后教程依次介绍了:

上传和扫描文件

重新扫描已提交的文件 (直接利用文件hash值)

检索文件扫描报告(重点)

此外还介绍了:

发送和扫描可疑URL链接

检索URL扫描报告

检索IP地址扫描报告

检索IP域名扫描报告

对文件和URLs发表评论

我们感兴趣的是前3点,一个集成了这3点的python脚本如下:

https://code.google.com/p/malware-lu/source/browse/tools/virustotal.py

该脚本封装了官网教程的示例代码。

如果要将结果保存到文件,可以改写函数format_report如下:

[python] view plaincopy
def format_report(result,filename): 
    scans = result.get("scans") 
    f = file(filename+".report", "w") #filename is the sample file name 
    scan_list = [] 
    for k,v in scans.items(): 
        if v['detected'] == True: 
            scan_list.append("%s: %s" % (k, v['result'])) 
        else: 
            scan_list.append("%s: None" % k) 
    f.write('\n'.join(scan_list)) 
    f.write("\nSHA256: %s\n" % result['sha256']) 
    f.write("MD5: %s\n" % result['md5']) 
    f.write("Detection ratio: %s/%s\n" % \ 
                       (result['positives'], result['total'])) 
    f.write("Analysis date: %s\n" % (result['scan_date'])) 
    f.write("URL: %s\n" % result['permalink']) 
    f.close() 
------------------------分割线--------------------------

如果要获得大量病毒样本的扫描报告,可以写bat脚本执行上面的python脚本

但可能会经常返回

simplejson.scanner.JSONDecodeError

刚开始一直令我迷惑不解,以为是代码问题,后来查看官方FAQ才知道公共API一分钟只能发起4次查询请求。

于是每隔15秒发起一次请求

在dos下的命令为:

ping 127.0.0.1 -n 15 -w 1000 > nul

或者

timeout 15 > nul (Windows 7才有)

生成bat脚本的python脚本如下:

[python] view plaincopy
#Generate *.bat script for running virustotal.py 
#like: python virustotal.py -n filename 
 
import os 
 
dirpath = r'E:\Samples\' 
name_list = os.listdir(dirpath) 
count = 0 
f = open("get_vt_reports.bat","w") 
for name in name_list: 
    if not name.endswith("vir"): 
        continue 
    filepath = os.path.join(dirpath,name) 
    count += 1 
    f.write("echo %d\n" % count) 
    f.write("python virustotal.py -n %s\n" %filepath) 
##    f.write("ping 127.0.0.1 -n 15 -w 1000 > nul\n") 
    f.write("timeout 15 > nul \n") 
f.close() 

补充:综合编程 , 安全编程 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,