查看: 1455|回复: 0
收起左侧

[原创工具] 使用py批量检测云服务器访问ip是否为恶意ip

[复制链接]
xyk001
发表于 2024-3-29 11:14:10 | 显示全部楼层 |阅读模式
本帖最后由 xyk001 于 2024-3-29 11:15 编辑

由于工作需要每天要封堵云上攻击ip,因此编写此脚本进行检测,需要在同一目录下创建url.txt放置待检测ip,apikey需要到https://x.threatbook.com/进行申请,一天调用上限50条。
  1. import requests,re,os
  2. city_info=[]
  3. arr1=[]
  4. def check_ip(ip):
  5.     try:
  6.         url = "https://api.threatbook.cn/v3/scene/ip_reputation"
  7.         query = {
  8.             "apikey": "",
  9.             "resource": f"{ip}",
  10.             "lang": "zh"
  11.         }
  12.         response = requests.request("GET", url, params=query)
  13.         res=response.json()
  14.         f = open('attack_ip.txt', mode='a+')
  15.         if(response.json()["response_code"]==-4):
  16.             url = "https://api.threatbook.cn/v3/scene/ip_reputation"
  17.             query = {
  18.                 "apikey": "",
  19.                 "resource": f"{ip}",
  20.                 "lang": "zh"
  21.             }
  22.             response1 = requests.request("GET", url, params=query)
  23.             res1 = response1.json()
  24.             if(res1["response_code"]!=-4):
  25.                 ip_info1 = res1['data'][ip]
  26.                 is_malicious = ip_info1['is_malicious']
  27.                 # print(res1['data'])
  28.                 if is_malicious:
  29.                     city = ip_info1['basic']['location']['country'] + ' ' + ip_info1['basic']['location']['province'] + ' ' + \
  30.                            ip_info1['basic']['location']['city']
  31.                     city_info.append(ip_info1['basic']['location']['country'])
  32.                     print(ip)
  33.                     f.write(ip + ' ' + city + '\n')
  34.             else:
  35.                 print("API接口上限")
  36.                 exit()
  37.         else:
  38.             ip_info = res['data'][ip]
  39.             is_malicious = ip_info['is_malicious']
  40.             # print(res)
  41.             if is_malicious:
  42.                 city = ip_info['basic']['location']['country'] + ' ' + ip_info['basic']['location']['province'] + ' ' + \
  43.                        ip_info['basic']['location']['city']
  44.                 city_info.append(ip_info['basic']['location']['country'])
  45.                 # print(ip + ' ' + city)
  46.                 # print("-------------------------")
  47.                 print(ip)
  48.                 f.write(ip + ' ' + city + '\n')
  49.             # pass
  50.         f.close()
  51.     except Exception as e:
  52.         print(e)





  53. if __name__ == '__main__':
  54.     arr = []
  55.     city_count=[]
  56.     with open("attack_ip.txt", 'r+') as file:
  57.         file.truncate(0)
  58.     for ip in open('url.txt', encoding='utf-8'):
  59.         result = re.match(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", ip.strip())
  60.         if result is None:  # 进一步确定错误类型
  61.           continue
  62.           # print("match()的返回值为空")
  63.         arr.append(result.group())
  64.     a=list(set(arr))
  65.     chose = input('输入选项:1、去重ip 2、威胁识别\n')
  66.     if chose == '1':
  67.         for i in a:
  68.             print(i)
  69.     elif chose == '2':
  70.         for x in a:
  71.             check_ip(x)
  72.         for x in open('attack_ip.txt'):
  73.             res = re.findall(r"^\S*\s(\S*)", x)

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

  79.             f.write(f"{i} {arr1.count(i)}次" + '\n')
  80.             print(f"{i} {arr1.count(i)}次")
  81.         # print(len(set(arr1)))
  82.         f.close()
  83.     else:
  84.         exit()
复制代码


评分

参与人数 2人气 +3 收起 理由
smz2011 + 1 原创内容
mifanu + 2 原创内容

查看全部评分

您需要登录后才可以回帖 登录 | 快速注册

本版积分规则

手机版|杀毒软件|软件论坛| 卡饭论坛

Copyright © KaFan  KaFan.cn All Rights Reserved.

Powered by Discuz! X3.4( 沪ICP备2020031077号-2 ) GMT+8, 2024-5-9 19:56 , Processed in 0.116209 second(s), 17 queries .

卡饭网所发布的一切软件、样本、工具、文章等仅限用于学习和研究,不得将上述内容用于商业或者其他非法用途,否则产生的一切后果自负,本站信息来自网络,版权争议问题与本站无关,您必须在下载后的24小时之内从您的电脑中彻底删除上述信息,如有问题请通过邮件与我们联系。

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