设为首页收藏本站

 找回密码
 立即注册

只需一步,快速开始

搜索
查看: 112|回复: 0

[实用软件] 使用py批量检测云服务器访问ip是否为恶意ip

[复制链接]
累计签到:33 天
连续签到:2 天
灌水成绩
1
62
4336
主题
帖子
积分

等级头衔

ID : 983

助理工程师

积分成就 测量币 : 4336
在线时间 : 0 小时
注册时间 : 2025-10-15
最后登录 : 2026-6-28

勋章
UID勋章测量学徒测量员
发表于 2024-3-29 11:14:10 | 显示全部楼层 |阅读模式 IP:广东东莞
由于工作需要每天要封堵云上攻击ip,因此编写此脚本举行检测,需要在同一目录下创建url.txt放置待检测ip,apikey需要到https://x.threatbook.com/举行申请,一天调用上限50条。

  • import requests,re,os
  • city_info=[]
  • arr1=[]
  • def check_ip(ip):
  •     try:
  •         url = "https://api.threatbook.cn/v3/scene/ip_reputation"
  •         query = {
  •             "apikey": "",
  •             "resource": f"{ip}",
  •             "lang": "zh"
  •         }
  •         response = requests.request("GET", url, params=query)
  •         res=response.json()
  •         f = open('attack_ip.txt', mode='a+')
  •         if(response.json()["response_code"]==-4):
  •             url = "https://api.threatbook.cn/v3/scene/ip_reputation"
  •             query = {
  •                 "apikey": "",
  •                 "resource": f"{ip}",
  •                 "lang": "zh"
  •             }
  •             response1 = requests.request("GET", url, params=query)
  •             res1 = response1.json()
  •             if(res1["response_code"]!=-4):
  •                 ip_info1 = res1['data'][ip]
  •                 is_malicious = ip_info1['is_malicious']
  •                 # print(res1['data'])
  •                 if is_malicious:
  •                     city = ip_info1['basic']['location']['country'] + ' ' + ip_info1['basic']['location']['province'] + ' ' + \
  •                            ip_info1['basic']['location']['city']
  •                     city_info.append(ip_info1['basic']['location']['country'])
  •                     print(ip)
  •                     f.write(ip + ' ' + city + '\n')
  •             else:
  •                 print("API接口上限")
  •                 exit()
  •         else:
  •             ip_info = res['data'][ip]
  •             is_malicious = ip_info['is_malicious']
  •             # print(res)
  •             if is_malicious:
  •                 city = ip_info['basic']['location']['country'] + ' ' + ip_info['basic']['location']['province'] + ' ' + \
  •                        ip_info['basic']['location']['city']
  •                 city_info.append(ip_info['basic']['location']['country'])
  •                 # print(ip + ' ' + city)
  •                 # print("-------------------------")
  •                 print(ip)
  •                 f.write(ip + ' ' + city + '\n')
  •             # pass
  •         f.close()
  •     except Exception as e:
  •         print(e)





  • if __name__ == '__main__':
  •     arr = []
  •     city_count=[]
  •     with open("attack_ip.txt", 'r+') as file:
  •         file.truncate(0)
  •     for ip in open('url.txt', encoding='utf-8'):
  •         result = re.match(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", ip.strip())
  •         if result is None:  # 进一步确定错误范例
  •           continue
  •           # print("match()的返回值为空")
  •         arr.append(result.group())
  •     a=list(set(arr))
  •     chose = input('输入选项:1、去重ip 2、威胁识别\n')
  •     if chose == '1':
  •         for i in a:
  •             print(i)
  •     elif chose == '2':
  •         for x in a:
  •             check_ip(x)
  •         for x in open('attack_ip.txt'):
  •             res = re.findall(r"^\S*\s(\S*)", x)

  •             arr1.append(res[0])
  •             # print(res)
  •         f = open('attack_ip.txt', mode='a+')
  •         for i in set(arr1):
  •             # count函数某一个字符在列表中的出现次数

  •             f.write(f"{i} {arr1.count(i)}次" + '\n')
  •             print(f"{i} {arr1.count(i)}次")
  •         # print(len(set(arr1)))
  •         f.close()
  •     else:
  •         exit()
快速回复换一批
路过留名
2333333333
看帖回帖是美德。 悄悄地我来了,顺便留下一条充满正能量的回帖。 🌊🤝
催更小分队上线! 楼主分析得太透彻了,期待下一篇更新! ⏳🔥
马克留名。 顺便问一句,有没有同行遇到过类似的问题?求交流! 👋👥
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|精密测量技术论坛 ( 桂ICP备2026007449号-1 )

GMT+8, 2026-7-5 15:09 , Processed in 0.285881 second(s), 43 queries .

Powered by 精密测量技术论坛

© 2025-2026 联系站长

快速回复 返回顶部 返回列表