Run The Bridge

(API+Python) VirusTotal API 이용해서 URL 검색하기 본문

Python/How to Use Python

(API+Python) VirusTotal API 이용해서 URL 검색하기

anfrhrl5555 2021. 4. 3. 14:09
728x90
반응형

무료로 파일 검사를 제공하는 웹 사이트

VirusTotal은 File, URL, IP, etc... 여러가지에 대해 악성유무를 검사할 수 있는 유용한 사이트이다.

 

VirusTotal에서 제공하는 API를 가지고 python code를 짜서 손쉽게 데이터를 받아올 수 있다.

 

 

 

VirusTotal API

해당 사이트에 좌측을 보면 FILES에 scan, rescan, feed, search 등이 있고 /file/report로 결과값을 볼 수 있다.

 

URL이나 domain, ip 모두 동일하다

import requests
import time

# 바이러스토탈 API key
my_apikey = '<your_apitkey>'  # 웹 서버와 연동 시 고쳐야 할 부분

# 바이러스 토탈 URL 스캔
url = 'https://www.virustotal.com/vtapi/v2/url/scan'
scan_url = 'http://amaz0n.zhongxiaoyang.top/'  # 웹 서버와 연동 시 바뀌어야 할 부분

# 바이러스토탈 URL 스캔 시작
params = {'apikey': my_apikey, 'url': scan_url}
response_scan = requests.post(url, data=params)
result_scan = response_scan.json()

scan_id = result_scan['scan_id']  # 결과를 출력을 위해 scan_id 값 저장

# URL 스캔 시작 안내, 60초 대기
print('Virustotal File Scan Start (60 Seconds Later) : ', scan_url, '\n')

# 스캔 후 1분 대기
# time.sleep(5)
#
# 바이러스토탈 URL 스캔 결과 주소
url_report = 'https://www.virustotal.com/vtapi/v2/url/report'

# 결과 파일 찾기 위해 scan_id 입력
url_report_params = {'apikey': my_apikey, 'resource': scan_id}

# 바이러스토탈 URL 스캔 결과 리포트 조회
response_report = requests.get(url_report, params=url_report_params)

# 점검 결과 데이터 추출
report = response_report.json()  # 결과 값을 report에 json형태로 저장
report_verbose_msg = report.get('verbose_msg')
report_scans = report.get('scans')  # scans 값 저장
report_scans_vendors = list(report['scans'].keys())  # Vendor 저장
report_scans_vendors_cnt = len(report_scans_vendors)  # 길이 저장
report_scan_data = report.get('scan_data')

print(report_verbose_msg, '\n')
#time.sleep(1)

# 파일 스캔 결과 리포트 데이터 보기
print('Scan Data (UTC) :', report_scan_data)
print('Scan URL Vendor CNT: ', report_scans_vendors_cnt, '\n')

# 바이러스 스캔 엔진사 별 데이터 정리

numbers = 1
for vendor in report_scans_vendors:
    outputs = report_scans[vendor]
    outputs_result = report_scans[vendor].get('result')
    outputs_detected = report_scans[vendor].get('detected')

# outputs_detected = True, False
# outputs_result = clean site, unrated site, malware site, malicious site, Phishing site
    if outputs_result != 'clean site':
        if outputs_result != 'unrated site':
            print(f'[No].{numbers}',
                  ",[Vendor Name]:", vendor,
                  ',[Vendor Result]:', outputs_result,
                  ',[Vendor Detected]:', outputs_detected)
            numbers += 1

위의 코드를 실행하면

phising site를 하나 입력하면 Vendor 들이 검사를 통해 사이트의 정상 유무를 판별해준다.

 

감사합니다 Thank you!

728x90
반응형
Comments