个人事务

统计Excel中的IP段出现个数,找出恶意IP

  • btt
  • [btt]
  • Topic Author
  • Offline
  • Administrator
  • Administrator
More
12 Apr 2025 16:11 #220 by btt
注意安装Python 支持模块:
pip install netaddr
pip install xlrd

Please Log in or Create an account to join the conversation.

  • btt
  • [btt]
  • Topic Author
  • Offline
  • Administrator
  • Administrator
More
12 Apr 2025 16:09 #219 by btt
New Topic
整理一个IP中前3位,次数超过3次,则默认是IP段过来攻击。
1、准备工作
  要人手做,准备一个EXCEL表格,把整理好的IP放进去,去重排序好:
2、脚本思路:
很容易发现这些IP,以“.” 为分隔符,每一段不一定为3位,所以一开始要补前导0,
因为我们需要拿IP前三段进行统计(变量:newip),超过3次,则认为是IP段过来攻击。
超过3次:用的是字符串的比较,两个for循环,cnt计数器是为了优化的,表示下一次循环移动的下标位置
用python的集合去输出结果,是为了去除最终结果的重复项,因为
if cnt >= 3:   这里是有bug的,会多次写入
Code:
# -*- coding: utf-8 -*-  # @Time : 2022/8/7  # @File : IP.py import netaddr import sys import xlrd book = xlrd.open_workbook("test.xls")  # 获取工作簿对象 table = book.sheet_by_index(0) rows = table.nrows arr = [] for i in range(rows):     ip = table.cell_value(i,0)     tip1 = str(ip.split('.')[0])     #不够3位,补前导0     ip1 = tip1.zfill(3)     tip2 = str(ip.split('.')[1])     ip2 = tip2.zfill(3)     tip3 = str(ip.split('.')[2])     ip3 = tip3.zfill(3)     # newip = print('%s.%s.%s' % (str(ip1), str(ip2), str(ip3)))     ## 拿到ip前三段,最后1段假设为0,方便后面去除前导0     newip = str(ip1) + '.' + str(ip2) + '.' + str(ip3)     # print(newip)     arr.append(newip) # for i in range(rows): #     print(arr[i]) # 计数器:cnt # 集合保存最终结果 result = set() for i in range(rows):     cnt = 1     for j in range(i+1, rows):         while 1:             if j >= rows:                 break;             if arr[i] == arr[j]:                 cnt = cnt + 1                 j = j + 1             else:                 break;         if cnt >= 3:             aa = arr[i] + '.' + '000'      #IP最后一段默认补0,方便后面去除前导0             #去除前导0             no_zero_ip = netaddr.IPAddress(aa, flags=netaddr.ZEROFILL).ipv4()             bb = str(no_zero_ip) + '/' + '24'             result.add(bb)           #  print(arr[i])             i = cnt + i - 1;             break; sortres = sorted(result) for item in sortres:     print(item, end='\n')

最后输出结果是这样的:
G:\IP\aliyun>python IP.py


1.92.195.0/24
1.92.196.0/24
106.55.200.0/24
106.8.130.0/24
106.8.131.0/24
106.8.136.0/24
106.8.137.0/24
106.8.138.0/24
106.8.139.0/24
110.249.201.0/24
110.249.202.0/24
111.206.206.0/24
111.221.214.0/24
111.225.148.0/24
111.225.149.0/24
113.215.188.0/24
113.215.189.0/24
113.45.167.0/24
114.119.128.0/24
114.119.129.0/24
114.119.130.0/24
114.119.131.0/24
114.119.133.0/24
114.119.134.0/24
114.119.135.0/24
114.119.136.0/24
114.119.137.0/24
114.119.140.0/24
114.119.141.0/24
114.119.145.0/24
114.119.147.0/24
114.119.148.0/24
114.119.149.0/24
114.119.150.0/24
114.119.153.0/24
114.119.154.0/24
114.119.156.0/24
114.119.157.0/24
114.119.158.0/24
114.119.159.0/24
114.250.41.0/24
114.250.43.0/24
114.250.48.0/24
114.251.196.0/24
115.45.42.0/24
116.132.136.0/24
116.132.138.0/24
116.132.216.0/24
116.132.217.0/24
116.132.218.0/24
116.132.219.0/24
116.132.223.0/24
116.132.236.0/24
116.132.252.0/24
116.132.254.0/24
116.132.255.0/24
116.179.32.0/24
116.179.33.0/24
116.179.37.0/24
116.204.105.0/24
116.204.108.0/24
116.204.69.0/24
116.204.97.0/24
116.76.208.0/24
116.76.38.0/24
121.237.36.0/24
121.37.105.0/24
121.37.106.0/24
121.37.107.0/24
121.37.96.0/24
122.192.32.0/24
123.160.223.0/24
123.182.48.0/24
123.182.49.0/24
123.182.50.0/24
123.182.51.0/24
123.183.235.0/24
123.189.113.0/24
123.6.49.0/24
124.239.12.0/24
14.155.182.0/24
14.155.224.0/24
14.155.225.0/24
14.155.226.0/24
14.155.227.0/24
14.155.232.0/24
153.3.241.0/24
154.81.156.0/24
154.83.103.0/24
157.55.39.0/24
180.101.52.0/24
185.191.171.0/24
185.213.245.0/24
185.90.60.0/24
192.178.6.0/24
195.191.219.0/24
207.46.13.0/24
213.180.203.0/24
216.244.66.0/24
217.113.194.0/24
220.181.108.0/24
220.181.51.0/24
27.115.124.0/24
3.141.46.0/24
36.155.132.0/24
40.77.167.0/24
47.128.114.0/24
47.128.115.0/24
47.128.118.0/24
47.128.119.0/24
47.128.120.0/24
47.128.121.0/24
47.128.122.0/24
47.128.123.0/24
47.128.124.0/24
47.128.125.0/24
47.128.126.0/24
47.128.127.0/24
47.128.18.0/24
47.128.19.0/24
47.128.20.0/24
47.128.21.0/24
47.128.22.0/24
47.128.23.0/24
47.128.27.0/24
47.128.28.0/24
47.128.29.0/24
47.128.30.0/24
47.128.32.0/24
47.128.34.0/24
47.128.36.0/24
47.128.37.0/24
47.128.38.0/24
47.128.40.0/24
47.128.41.0/24
47.128.42.0/24
47.128.45.0/24
47.128.46.0/24
47.128.47.0/24
47.128.48.0/24
47.128.50.0/24
47.128.51.0/24
47.128.52.0/24
47.128.53.0/24
47.128.54.0/24
47.128.55.0/24
47.128.56.0/24
47.128.57.0/24
47.128.58.0/24
47.128.59.0/24
47.128.60.0/24
47.128.62.0/24
47.128.63.0/24
47.128.96.0/24
47.128.97.0/24
47.128.98.0/24
47.128.99.0/24
47.79.121.0/24
47.79.122.0/24
47.79.123.0/24
47.79.2.0/24
47.79.3.0/24
47.79.5.0/24
47.79.6.0/24
47.79.7.0/24
47.79.98.0/24
47.82.10.0/24
47.82.11.0/24
51.222.253.0/24
51.8.102.0/24
52.167.144.0/24
52.80.179.0/24
54.223.31.0/24
54.36.148.0/24
54.36.149.0/24
57.141.2.0/24
59.82.21.0/24
66.249.66.0/24
66.249.68.0/24
66.249.69.0/24
66.249.70.0/24
66.249.72.0/24
66.249.73.0/24
66.249.75.0/24
66.249.77.0/24
66.249.79.0/24
66.249.92.0/24
69.171.249.0/24
74.125.150.0/24
85.208.96.0/24
85.208.98.0/24
87.250.224.0/24
95.108.213.0/24

-注意:

xlrd1.2.0之后的版本不支持xlsx格式,支持xls格式。解决办法:
方案一::
卸载新版本
Code:
pip uninstall xlrd
(或者更早版本)

方案二:
将xlrd用到的excel版本格式修改为xls(保险起见,另存为xls格式)

Please Log in or Create an account to join the conversation.