网络安全实验:提取并分析恶意IP地址的地理位置
背景介绍
作为一名网络安全工程师,我在日常工作中经常需要对网络流量进行监测和分析,以确保系统的安全性。最近,我正在进行一个网络安全实验,旨在通过某监测平台对公网IP资产进行监测,并根据IOC(Indicators of Compromise)信息提取被标记为“恶意”的IP地址。为了进一步了解这些恶意IP地址的来源,我决定使用GeoIP数据库来查询这些IP地址的地理位置,并将结果保存到一个新的Excel文件中。
本文将详细介绍如何使用Python实现这一过程,并分享代码的具体实现和功能。
实验目标
- 读取Excel文件中的IP地址:从Excel文件中读取所有IP地址,并筛选出“微步判定”列中内容为“恶意”的IP地址。
- 查询IP地址的地理位置:使用MaxMind GeoLite2数据库查询每个恶意IP地址的地理位置信息,包括国家、地区、城市、邮政编码、经纬度等。
- 保存结果到新的Excel文件:将查询到的地理位置信息与原始IP地址一起保存到一个新的Excel文件中,便于后续分析。
代码实现
依赖库安装
在开始之前,我们需要安装一些必要的Python库:
pip install pandas geoip2 openpyxl
代码详解
1. 读取Excel文件中的IP地址
我们首先定义一个函数read_ip_addresses
,用于读取Excel文件并筛选出“微步判定”列为“恶意”的IP地址。
import pandas as pd
def read_ip_addresses(excel_path):
try:
df = pd.read_excel(excel_path)
# 筛选出“微步判定”列中内容为“恶意”的行
malicious_ips = df[df['微步判定'] == '恶意']
return malicious_ips
except Exception as e:
print(f"读取Excel文件时出错: {e}")
return pd.DataFrame() # 返回一个空的DataFrame
2. 初始化GeoIP数据库
接下来,我们定义一个函数init_geoip_db
,用于初始化MaxMind GeoLite2数据库。
import geoip2.database
def init_geoip_db(db_path):
try:
return geoip2.database.Reader(db_path)
except FileNotFoundError:
print(f"GeoLite2数据库文件未找到,请确保文件路径正确: {db_path}")
return None
except Exception as e:
print(f"初始化GeoIP数据库时出错: {e}")
return None
3. 查询IP地址的地理位置
定义一个函数get_location
,用于查询每个IP地址的地理位置信息。
def get_location(ip_address, reader):
try:
response = reader.city(ip_address)
return {
'IP_Address': ip_address,
'Country': response.country.name,
'Region': response.subdivisions.most_specific.name,
'City': response.city.name,
'Postal_Code': response.postal.code,
'Latitude': response.location.latitude,
'Longitude': response.location.longitude
}
except geoip2.errors.AddressNotFoundError:
return {
'IP_Address': ip_address,
'Country': 'N/A',
'Region': 'N/A',
'City': 'N/A',
'Postal_Code': 'N/A',
'Latitude': 'N/A',
'Longitude': 'N/A'
}
except Exception as e:
print(f"查询IP地址地理位置时出错: {e}")
return None
4. 主函数
最后,定义主函数main
,用于整合上述功能并将结果保存到新的Excel文件中。
def main(excel_path, db_path, output_excel_path):
# 读取IP地址
ip_addresses = read_ip_addresses(excel_path)
# 初始化GeoIP数据库
geoip_reader = init_geoip_db(db_path)
if geoip_reader is None:
return
# 查询每个IP地址的地理位置并存储结果
results = []
for index, row in ip_addresses.iterrows():
ip = row['IP']
location = get_location(ip, geoip_reader)
if location: # 确保location不是None
location['微步判断'] = '恶意攻击' # 添加“某监测平台判断”列
results.append(location)
# 将结果转换为DataFrame
df_results = pd.DataFrame(results)
# 保存到新的Excel文件
df_results.to_excel(output_excel_path, index=False)
print(f"结果已保存到 {output_excel_path}")
if __name__ == "__main__":
excel_path = '/Users/liuxiaowei/Downloads/ioc_ip.xls' # 替换为你的Excel文件路径
db_path = 'GeoLite2-City.mmdb' # 替换为你的GeoLite2数据库文件路径
output_excel_path = 'ip_geolocations.xlsx' # 输出的Excel文件路径
main(excel_path, db_path, output_excel_path)
结果展示
运行上述代码后,程序会读取指定的Excel文件,筛选出所有被标记为“恶意”的IP地址,并使用GeoLite2数据库查询这些IP地址的地理位置信息。最终,查询到的结果将被保存到一个新的Excel文件中,文件名为ip_geolocations.xlsx
。
打开生成的Excel文件,我们可以看到每一行包含了一个恶意IP地址及其对应的地理位置信息,包括国家、地区、城市、邮政编码、经纬度等。这为我们进一步分析恶意IP的来源提供了有力支持。
总结
通过这个实验,我们不仅能够有效地提取并分析被标记为“恶意”的IP地址,还能借助GeoIP数据库获取这些IP地址的详细地理位置信息。这对于网络安全工程师来说是非常有价值的工具,可以帮助我们更好地理解潜在的安全威胁,并采取相应的防护措施。
备注
以上的的MaxMind GeoLite2数据库需要科学上网获取