前言

昨天刚入职,实习的第一天没什么事干,又有点事干。

如果这也算渗透的话,我愿称之为:面向excel渗透。

这活有点蓝队溯源的性质,只有一点。

情景

客户的系统被邮件钓鱼了,需要溯源,同事给了两份 excel 表格,一份是 ip.xls,另一份是 qbu.xlsx

ip.xls 记录了ip、地址和攻击性质,需求是筛选出恶意或者海外的ip数据,再将恶意ip与qbu.xlsx里面的ip进行对比,获得是恶意或者海外ip的登录信息,缩小溯源的范围。

image-20220804085013315

qbu.xlsx 记录了时间,ip,用户名,登录详情,目标ip。需求是将ip.xls筛选出的恶意ip和qbu.xlsx的ip进行对比,筛选出那一行的数据。

image-20220804085427964

分析

对于这个需求,前一个表格可以通过简单的包含等于条件筛选出恶意ip数据,但后一份表格无法通过一个个去对比ip,因为ip量有点大,所以只能通过python去处理数据啦。

处理excel表格的python库有很多,我们又应该选择哪个呢?可以用以下命令安装:

1
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xlrd==1.2.0,xlwt

因为新版本只支持 .xls,不支持 .xlsx,而旧版本支持 .xlsx,所以我们安装旧版本xlrd。

写入 excel 表格则使用 xlwt。

解决方案

针对上面的分析,我们可以使用以下代码来解决:

筛选 ip.xls 的 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
import xlrd 

# 创建excel,取第一个工作簿
rbook = xlrd.open_workbook('ip.xls')
rbook.sheets()
rsheet = rbook.sheet_by_index(0)

# 将ip写入到txt中
def writeDatatxt(data):
with open('result.txt','a+')as f:
f.write(data)

# 过滤数据
def filterData():
global count
count = 0
for row in rsheet.get_rows():
f = row[0].value
# 排除第一行,即表头
if f != 'ip':
data = row[1].value
# 筛选条件
if '中国' not in data or '恶意' in data:
print(row[0].value)
count += 1
writeDatatxt(str(row[0].value)+'\n')

if __name__ == "__main__":
filterData()
print("总共有 "+ str(count) +" 条数据。")

筛选 qbu.xlsx 的代码:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
# coding: utf-8
import xlrd
import xlwt

#################### 创建读取excel #########################
# 打开excel文件,创建一个workbook对象,book对象也就是fruits.xlsx文件,表含有sheet名
rbook = xlrd.open_workbook('qbu.xlsx')
# sheets方法返回对象列表,[<xlrd.sheet.Sheet object at 0x103f147f0>]
rbook.sheets()
# xls默认有3个工作簿,Sheet1,Sheet2,Sheet3
rsheet = rbook.sheet_by_index(0) # 取第一个工作簿

#################### 创建写入excel #########################
# 第一步:创建工作簿
wb = xlwt.Workbook()
# 第二步:创建工作表
ws = wb.add_sheet("result")

# 从txt中读取所有ip,并返回一个列表
def readData():
with open('ip.txt')as f:
ips = f.read().replace('\n',',').split(',')
return ips

def writeDatatxt(data):
with open('result.txt','a+')as f:
f.write(data)


def writeDataxls(data,count):
# 使用 enumerate 获取元素和序号,方便插入excel中
# count 默认从1开始,预留了一行,如有需求,方便添加头部
# i的列号,从0开始,d是数据
for i,d in enumerate(data):
ws.write(count,i,d)

# 循环工作簿的所有行
def filterData():
global count
count = 0
data = readData()
# 遍历所有的行
for row in rsheet.get_rows():
# 获取第一行第一列的值
head = row[0].value
if head != 'startTime': # 排除第一行
ip = row[1]
ip_value = ip.value
# 筛选符合的ip数据,写入表格中
if ip_value in data:
count += 1
row_data = []
for i in range(0,10):
row_data.append(str(row[i].value))
# writeDatatxt(str(data)+'\n')
writeDataxls(data,count)
# print(data)
if __name__ == "__main__":
filterData()
wb.save("result.xls")
print("总共有 "+ str(count) +" 条数据。")

总结分析

之前很少使用python编写excel相关的代码,碰到这活有点吃亏。

手工是不可能手工的,这辈子都不可能手工。

能用脚本处理的工作,如果你不能解决,那只能说明你还欠缺这方面的能力,也是你需要加强的地方。

我觉得以后这种面向excel的渗透少不了,所以适当的可以学习一下如何使用python处理excel。

以后碰到这种活,心里才会有底。

知识不断的积累,才能建成高楼大厦。