报表数据分析
- 对一些统计数据进行描图,这里以其中一个图为例
pip install matplotlib
- matplotlib对中文支持不友好,需要配置一下
1
2
3
4
5# 指定默认字体
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['font.family']='sans-serif'
# 解决负号显示为方块的问题
matplotlib.rcParams['axes.unicode_minus']=False
1 | def draw_pic_2_1(data_list): |
IP归属地查询
- 要分析威胁事件,首先要查寻一下IP归属地,这里是单独写的一个模块
- 我采用爬虫的方式,获取相关信息,网站地址就不展示了,一个常用网站,懂得都懂
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41import requests
import re
class ip_to_addr_spider(object):
def __init__(self):
self.url = "https://www.xxxxx.com/iplookup.asp?ip={}&action=2"
self.headers = {"User-Agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"}
def get_data(self,ip):
self.url = self.url.format(ip)
try:
response = requests.get(self.url, headers=self.headers, timeout=5).content.decode("gbk")
except:
return None
return response
def parse_data(self, response):
re_com = re.compile("""var ip_result.*};""")
try:
ret = re_com.search(response)
data = eval(ret.group(0)[16:-1]) # 截取出所需的数据,转化成python对象
except:
data = None
return data
def run(self,ip):
response = self.get_data(ip)
data = self.parse_data(response)
if data:
dst_ct = data["ip_c_list"][0]["ct"]
ASN = data['ASN归属地']
else:
dst_ct = None
ASN = None
return dst_ct,ASN
if __name__ == "__main__":
ip = "xxx.12.21.xx"
proxy = ip_to_addr_spider()
dst_ct,ASN = proxy.run(ip)
print(ASN)
print(dst_ct)
- 因为表中的IP存在一个单元格有多个的情况,需要拆分,并去除内网IP
- 形成IP列表,方便爬虫发送请求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24def deal_with_ip_list(data_list):
"""
将源目IP中,有几个的情况拆分,并去除内网IP
param:
data_list: IP列表
ret:
ip_list: 处理好的IP列表
"""
data_list = data_list[1:]
new_list = []
for data in data_list:
if data.find(",") != -1:
t_list = data.split(",")
new_list.extend(t_list)
else:
new_list.append(data)
new_list = set(new_list)
ip_list = list(new_list)
new_list = []
for ip in ip_list:
if ip.find("192.168") == -1: # 去除内网IP
new_list.append(ip)
ip_list = new_list
return ip_list
筛选出国处的IP
1 | def analyze_ip(ip_list): |
筛选第一类高危事件
1 | def high_risk_ip(ip_list, data_list): |