微步api之ip信誉批量查询脚本

最近在当监控小子,每天就是查查ip封封ip,虽然不累但是异常枯燥,而且每天的日报整理起来也非常麻烦,于是利用微步的api接口,编写了批量查询和统计的脚本,再也不怕日报整理麻烦了。

直接从waf生成当天的告警记录的表格,利用python的csv模块进行文件读取,将ip过滤去重之后进行api请求,最后直接统计告警类型和危害程度高的ip

具体代码如下,纯原创


import csv
import requests
import time
from collections import Counter

# 文件路径
path = '文件路径'
# api
url = "https://api.threatbook.cn/v3/scene/ip_reputation"
# key
key = "自己的apikey"
context_list=[]

# 字典列表去重
def remove_duplicate_dicts(input_list):
    unique_list = []
    seen_dicts = set()
    for dictionary in input_list:
        dictionary_tuple = tuple(sorted(dictionary.items()))

        if dictionary_tuple not in seen_dicts:
            seen_dicts.add(dictionary_tuple)
            unique_list.append(dictionary)

    return unique_list

# 读取文件中的ip和安全类型
def ip_list():
    ListForIp = []
    # 文件读取,path是文件路径
    with open(path, 'r') as file:
        reader = csv.reader(file)
        # 遍历每行数据

        #需要修改表格中ip的列数,row[?]
        for row in reader:
            # 去表头
            if row[2] == "安全类型" or row[4] == "源IP":
                continue
            # 生成一个字典,其格式为{"type","攻击类型","ip","x.x.x.x"}
            dirt = {"type": row[2].replace(" ", ""), "ip": row[4].replace(" ", "")}
            # 将字典存储到列表中
            ListForIp.append(dirt)
            # 去重
            #ListForIp = remove_duplicate_dicts(ListForIp)
        #     因为微步限制,所以限制列表内超过50个数据
        if len(ListForIp) > 50:
            print(ListForIp)
            print("ip数量超过50")

            # return False
            # ListForIp = ListForIp[:20]

            # print(ListForIp)

        else:

            return ListForIp



# 攻击统计
def Type_statistics(ip):
    type_list = []
    try:
        print("流量ip统计")

        for item in ip:
            type_list.append(item["type"])


        counter = Counter(type_list)

        for element, count in counter.items():

            print(f"{element}出现了 {count} 次", end=",")

    except:
        print("Type_statistics模块异常")



# api访问
def API(Ip):

    Ip = remove_duplicate_dicts(Ip)
    try:
        for item in Ip:
            response = requests.request("GET", url, params={
                "apikey": key,
                "resource":item["ip"],
                "lang": "zh"
            })
            # 每一次访问后休息0.5秒
            time.sleep(0.5)
            # 如果response_code=0则返回正确数据
            if response.json()["response_code"] == 0:
                # 提取响应中的安全等级
                severity_value = response.json()["data"][item["ip"]]["severity"]
                if severity_value in ["中", "高", "严重"]:
                    print(item["type"]+":"+item["ip"] + ":" + severity_value)
                    context=item["type"]+":"+item["ip"] + ":" + severity_value
                    context_list.append(context)

            else:
                # 否则
                print(item["ip"])
                print(response.json())
                break
        return context_list

    except:
        # 抛出异常
        pass
# 封禁ip类型统计
def context_sum(data):
    type_counter = Counter()
    result_list = []

    for entry in data:
        parts = entry.split(':')
        attack_type = parts[0]
        type_counter[attack_type] += 1

    for attack_type, count in type_counter.items():
        result_list.append(f"{attack_type} 类型出现了 {count} 次")
    print("封禁ip统计")
    print(result_list)


if __name__ == '__main__':

    ip_list=ip_list()
    context_list=API(ip_list)
    context_sum(context_list)

    Type_statistics(ip_list)







 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
微步社区查询接口是一个基于Python开发的接口,可以通过该接口来获取微步社区中的相关信息。 通过Python的requests库可以发送HTTP请求到微步社区查询接口的URL,并传入需要查询的参数,例如IP地址、域名、URL等。接口会返回与请求相关的信息,例如该IP地址或域名是否被微步识别为恶意或可疑的信息,以及相关的威胁情报、风险评级等。 使用该接口的步骤大致如下: 1. 首先,引入requests库并导入所需模块; 2. 指定微步社区查询接口的URL,并将所需查询参数作为请求的参数; 3. 发送HTTP请求,并获取返回的数据; 4. 对返回的数据进行解析,提取所需的信息; 5. 对数据进行必要的处理和展示。 例如,我们可以通过该接口查询一个IP地址的信息,首先构造请求URL,然后发送GET请求,获取返回信息,并解析出IP的威胁情报和风险评级等数据。 ```python import requests def query_threat_info(ip): url = f"https://api.weibeiapi.com/微步社区查询接口URL?ip={ip}" response = requests.get(url) data = response.json() if response.status_code == 200: threat_info = data["threat_info"] risk_level = data["risk_level"] print(f"IP地址 {ip} 的威胁情报为:{threat_info}") print(f"IP地址 {ip} 的风险评级为:{risk_level}") else: print("查询失败,请检查IP地址是否正确。") # 调用示例 query_threat_info("192.168.1.1") ``` 注意,上述代码中的微步社区查询接口URL是示意,并非真实可用的URL,实际使用时需要替换为正确的URL。 总之,微步社区查询接口提供了方便的方式来获取微步社区中的相关信息,通过Python可以快速构建查询工具来实现调用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值