Python3 通过VirusTotal API筛选0查杀的白样本

4 篇文章 0 订阅
4 篇文章 0 订阅

Python3 通过VirusTotal API筛选0查杀的白样本

import vt
import hashlib

class VTUpload(object):

    def __init__(self) -> None:
        self.file_list=[] //files to be upload
        num=len(self.file_list)
        print("total count is %d"%num)

        self.white_list=[]
        self.black_list=[]
        self.API="" // your api
        
    def uploadFile(self,filepath):
        with open(filepath, 'rb') as fp:
            data = fp.read()
            fp.close()
            file_md5= hashlib.md5(data).hexdigest()
            print(file_md5)
            client = vt.Client(self.API)
            up_flag=False
            harmless_count,suspicious_count,malicious_count=0,0,0
            try:
                file = client.get_object("/files/"+file_md5)
                if file:
                    print(file.last_analysis_stats) 
                    failure_count=file.last_analysis_stats['failure']
                    harmless_count=file.last_analysis_stats['harmless']
                    malicious_count=file.last_analysis_stats['malicious']
                    suspicious_count=file.last_analysis_stats['suspicious']
                    timeout_count=file.last_analysis_stats['timeout']
                    typeunsupported_count=file.last_analysis_stats['type-unsupported']
                    undetected_count=file.last_analysis_stats['undetected']
                
                up_flag=True
            except Exception as e: # md5 not found; TypeError: cannot unpack non-iterable NoneType object
                print("Exception happened")
                with open(filepath, "rb") as f:
                    analysis = client.scan_file(f,wait_for_completion=True)
                    # analysis = client.get_object("/analyses/{}", analysis.id)
                    if analysis.status == "completed":
                        print("Analysis completed!")
                    f.close()
                # client.scan_file_async(file=filepath)
                up_flag=False

            if up_flag==False:
                return self.uploadFile(filepath)
            else:
                return (suspicious_count,malicious_count,file_md5)

    def uploadAll(self):
        count=0
        for file in self.file_list:
            print("%d round begin!"%count)
            suspicious_count,malicious_count,file_md5=self.uploadFile(file) 
            if suspicious_count==0 and malicious_count==0:
                self.white_list.append(file)
            else:
                self.black_list.append(file)
            count+=1
            
            
        # print(self.black_list,self.white_list)

    def main(self):
        self.uploadAll()

if __name__=="__main__":
    vtup=VTUpload()
    vtup.main()
                
           

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值