当前位置: 首页 > 工具软件 > BitDefender > 使用案例 >

Bitdefender 查询域名和url是否恶意

鲁单弓
2023-12-01

Bitdefender在国内感觉文档还是比较少,都没怎么看到,今天公司给了个api要求把文件中的域名或者url给筛选出恶意与非恶意了,文档中只写了两种语言的例子,python和php,我这里就使用python完成了。

# coding:utf-8
import os
import sys
import time

reload(sys)
sys.setdefaultencoding('utf8')

import io
import re
import urllib2
import json
import threadpool

'''
    筛选恶意网站 和 BitdefenderTest.py 不同的是 筛选恶意网址的条件不同。
'''

# 这两个参数根据提供方给你提供的替换好就行了
API_KEY = "insert-your-API-key-here"
API_URL = "https://nimbus.bitdefender.net/batch/url/status"
# 记录非恶意网址的数组
safe_data = []
# 记录恶意网址的数组
spite_data = []
# 记录失败网址的数组
error_data = []
# 记录所有网址的数组
line_data = []
# 记录文件名字
name = ""

'''
    获取所有的网址
'''
def getAllUrl(urlTexts):
    # 遍历传入的文件
    for urlText in urlTexts:
        f = io.open(urlText, 'r', encoding='UTF-8')

        # 初始化需要用到的数组
        global line_data
        line_data = []
        global safe_data
        safe_data = []
        global spite_data
        spite_data = []
        global error_data
        error_data = []

        # 遍历文件
        for line in f.readlines():
            line = line.strip()
            line_data.append(line)
        f.close()

        # 设置文件的名字
        global name
        name = os.path.splitext(urlText)[0]
        print (len(line_data))

        # 开启线程池调用接口
        pool = threadpool.ThreadPool(10)
        requests = threadpool.makeRequests(identify, line_data)
        [pool.putRequest(req) for req in requests]
        pool.wait()
        write_data()

'''
    调接口识别恶意与非恶意的网址
'''
def identify(identify_url):
    try:
        # 格式化一下网址
        url = identify_url
        if not re.match("http://", identify_url) and not re.match("https://", identify_url):
            url = "http://" + identify_url

        # 开始调用
        request = urllib2.Request(API_URL,
                                  json.dumps([dict(url=url)]),
                                  {'Content-Type': 'application/json',
                                   "X-Nimbus-ClientId": API_KEY})
        response = urllib2.urlopen(request)
        data = json.loads(response.read())

        # 识别非恶意和恶意的网址
        if data[0]["status_code"] == 0 \
                and data[0]["status_message"] == "not found" \
                and data[0]["domain_grey"] is False:
            safe_data.append(identify_url)
        else:
            spite_data.append(identify_url)
    except Exception, ex:
        ex = "Error: " + str(ex) + "  --" + identify_url
        error_data.append(identify_url)
        print ex;

'''
    将数据写入文件
'''
def write_data():
    # 总数对比
    text_name = name + "_total_count.txt"
    cf = open(text_name, "w")
    cf.write("总数" + str(len(line_data)) + "\n")
    cf.write("非恶意总数" + str(len(safe_data)) + "\n")
    cf.write("恶意总数" + str(len(spite_data)) + "\n")
    cf.write("失败总数" + str(len(error_data)) + "\n")
    cf.close()

    # 非恶意网站
    text_name = name + "_safe.txt"
    with open(text_name, "w") as f:
        for sd in safe_data:
            f.write(sd + "\n")

    # 恶意网站
    text_name = name + "_spite.txt"
    with open(text_name, "w") as f:
        for sd in spite_data:
            f.write(sd + "\n")

    # 查询失败的网站
    text_name = name + "_error_data.txt"
    with open(text_name, "w") as f:
        for ed in error_data:
            f.write(ed + "\n")


if __name__ == "__main__":
    print (time.strftime("%H:%M:%S", time.localtime(time.time())))
    getAllUrl(["domain.txt", "ip.txt", "url.txt"])
    print (time.strftime("%H:%M:%S", time.localtime(time.time())))


这里条件筛选我根据公司需求做了修改,这个代码修修改改也就能使用了。Bitdefender的接口这个调用也是挺简单,他还有一个可以同时传多个url识别过滤的,不过我url有点多一次传不了,就还是一个一个传了。这个我只是做任务的一个工具,没有去考虑性能和时间层面。

 类似资料: