IT/Python

[Python] 파이썬을 이용한 바이러스 토탈 API 조회

김반장님 2021. 1. 22. 13:51
반응형

파이썬을 이용하여 바이러스 토탈 탐지명을 조회 해보자

#- * -coding: utf - 8 - * -
from urllib import request
from urllib import parse
from urllib.request import urlopen
from urllib.request import urlopen
import json
import time

# 모듈선언

VT_KEY = 'api key'
#바이러스토탈 api키, 가입 후 제공받을 수 있음 1 분에 4 개 제한
HOST = 'www.virustotal.com'
SCAN_URL = 'https://www.virustotal.com/vtapi/v2/file/scan'
REPORT_URL = 'https://www.virustotal.com/vtapi/v2/file/report'
md5str= ''
# md5 값 담을 변수 선언
fields = [('apikey' , VT_KEY)]

# 전달할 apikey 값 담기
txtf = open('test.ini', 'r' )
# test.txt에 md5값을 담아두고 ;r read함

while True:
# while문으로 반복돌림
line = txtf.readline()
md5str = line.strip('\n')
# 한 줄 씩 읽음.개행으로 구분함
if not md5str: break
parameters = {'resource' : md5str,'apikey' : VT_KEY}
data = parse.urlencode(parameters).encode('utf-8')
req = request.Request(REPORT_URL, data)
response = urlopen(req)
data = response.read()

data = json.loads(data.decode('utf-8'))
# 데이터를 json형태로 읽어서 data변수에 담음.
md5 = data.get('md5', {})
scan = data.get('scans' , {})
# 바이러스토탈에서 응답값 던져줄 때 내가 필요한 md5값과 scan결과 값 파싱
keys = scan.keys()
# keys는 바이러스토탈에서 지원하는 백신엔진 목록
print ("")

print("========================== Virus Total Loading ==========================")
print ("=========================================================================")
# 바이러스 토탈이 지원하는 백신 중 하나도 탐지되는게 없으면 ;{} 값이 md5에 들어감.그러므로 "no match" 출력
if md5 == {}:
print ("!!!!!!!!! Sorry, No Match !!!!!!!!!")
else :
print ("MD5 : ",md5)

print ("=========================================================================")
time.sleep(20)
#1분에 4개로 제한되어 있어, 20초씩 sleep시켜줌.
for key in keys:
if key == 'AhnLab-V3' :
print('%-20s: %s' % (key, scan[key]['result']))
elif key == 'ALYac' :
print('%-20s: %s' % (key, scan[key]['result']))
elif key == 'nProtect' :
print('%-20s: %s' % (key, scan[key]['result']))
elif key == 'ViRobot' :
print('%-20s: %s' % (key, scan[key]['result']))

txtf.close()
print("=================================clear===================================")

 

 

출처

 

파이썬 - 바이러스토탈(Virustotal) API 사용하여 질의 하기

바이러스토탈에서 일반 사용자에게 1분에 4개정도를 조회할 수 있는 API키를 제공해주는 데, 악성코드 ...

blog.naver.com

 

반응형

'IT > Python' 카테고리의 다른 글

[Python] 주석처리 / 숫자 계산  (0) 2021.01.23
[Python] Hello world / 들여쓰기  (0) 2021.01.22
[Python] 파이썬 Ping 테스트  (0) 2021.01.22
[Python] 파이썬 패킷 생성  (0) 2021.01.22
[Python] 파이썬 개요 및 설치  (0) 2021.01.21