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()