我是靠谱客的博主 清秀吐司,这篇文章主要介绍基于树莓派的WIFI抓包、分析(wireshark)并上传至阿里云数据库,现在分享给大家,希望可以做个参考。

一、系统功能
1)实现对环境中所有wifi信号的抓取
2)对抓取后的报文进行解析,能够拿到每个报文的rss、源MAC地址、目标MAC地址、时间戳
3)建立云数据库存储解析后的数据
4)开发安卓app,用图形UI对数据库的数据进行访问并按照需求进行数据处理(比如实现定位算法,进行mac地址对比等)

二、所需设备
树莓派、支持monitor模式的无线网卡

三、话不多说上代码(抓包、分析并上传到阿里云数据库),需要app代码请私聊
# encoding: utf-8
from tracemalloc import start
from scapy.all import *
import threading
import sys
import os
import csv
import pymysql
import re
import datetime

def dealwith():

复制代码
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
z = r'/home/pi/Desktop/demo/demo' y = r'pcap' k = r'/home/pi/Desktop/demo/capture' t = r'.txt' fff = r'/home/pi/Desktop/demo/filter n = int(input("input:")) for i in range(1,n+1): if i < n+1: j = str(i) h = str(i-1) print ('开始抓包') now = datetime.datetime.now() dpkt = sniff(iface ='wlan1mon', count=5,filter = 'wlan subtype Beacon') # 抓包 print ('抓包成功') wrpcap(z+j+y, dpkt) print ( '所抓的包已经保存') pcks = PcapReader(z+j+y) print ('开始解析pcap包') # 输出重定向 讲在控制台的输出重定向到 txt文本文件中 output = sys.stdout outputfile = open(k+j+t, 'w') sys.stdout = outputfile zArp = 0 zIcmp = 0 zEther = 0 zOther = 0 ipNum = set() for p in pcks: status1 = p.payload.name # 可能是ARP的报文 status2 = p.payload.payload.name # 可能是TCP报文 也可能是ICMP的报文 status3 = p.name # p.show() 输出报文, 在符合的情况下 if status1 == 'IP': ipNum.add(p.payload.src) # 将ip报文的源地址,和目的地址存在set集合里面(set去重) ipNum.add(p.payload.dst) p.show() print( '') else: if status1 == 'ARP': p.show() print( '') zArp += 1 if status2 == 'ICMP': p.show() print( '') zIcmp += 1 if status3 == 'Ethernet': p.show() print( '') zEther += 1 else: p.show() print( '') zOther += 1 print ('IP:' + str(len(ipNum)) + ' ARP:' + str(zArp) + ' ICMP:' + str(zIcmp) + ' Ethernet:' + str(zEther))# 报文数量的输出 outputfile.close() sys.stdout = output ccssvv = open('/home/pi/Desktop/filtt.csv','a',newline='') writer = csv.writer(ccssvv) data = [] filef = open(k+j+t, 'r') file_contents = filef.readlines() pp = open(fff+j+t, 'w') for content in file_contents: if 'mac_timestamp=' in content: data.append(now.strftime("%Y-%m-%d %H:%M:%S")) pp.write(now.strftime("%Y-%m-%d %H:%M:%S")) #将符合要求的内容写入文件 pp.write('n') if 'dBm_AntSignal=' in content: #print(content) #打印,看效果 jj = content.rfind('dBm_AntSignal=') rr = content[jj+15:jj+19]+'dBm' data.append(rr) pp.write(rr) #将符合要求的内容写入文件 pp.write('n') '''if 'mac_timestamp=' in content: jj = content.rfind('mac_timestamp=') rr = content[jj+16:jj+25] dateArray = datetime.datetime.utcfromtimestamp(int(rr)) rrtt = dateArray.strftime("%Y-%m-%d %H:%M:%S") data.append(rrtt) pp.write(rrtt) #将符合要求的内容写入文件 pp.write('n')''' if 'DA' in content: #print(content) #打印,看效果 jj = content.rfind('DA') rr = content[jj-22:jj-5] data.append(rr) pp.write(rr) #将符合要求的内容写入文件 pp.write('n') if 'addr2' in content: #检查包含pH的那行数据 #print(content) #打印,看效果 jj = content.rfind('addr2') # 找到最后一条数据所在的位置 #i = content.index('addr2', start) rr = content[jj+12:jj+29] data.append(rr) pp.write(rr) pp.write('n') if 'BSSID' in content: #print(content) #打印,看效果 jj = content.rfind('BSSID') rr = content[jj-19:jj-2] data.append(rr) pp.write(rr) #将符合要求的内容写入文件 pp.write('n') #outputfile.close() #sys.stdout = output # 恢复到控制台输出 if len(data) == 5: writer.writerow(data) data = [] conn = pymysql.connect(host='...', user='...', password='...', db='test', port=3306, charset='utf8') print('连接数据库成功!') cursor = conn.cursor() #测试代码 sql="select mac from userlist where phone = 1" cursor.execute(sql) res=cursor.fetchone() conn.commit() print(res) with open('/home/pi/Desktop/filtt.csv','r',encoding='utf-8') as f: read=csv.reader(f) for each in list(read)[1:]: i=tuple(each[0:]) sql="INSERT INTO data VALUE" + str(i) cursor.execute(sql) conn.commit() cursor.close() conn.close() print ('输出结束') print (dpkt)

dealwith() # 运行报文捕获函数

四、实例图片
在这里插入图片描述
阿里云数据库可参考:https://blog.csdn.net/cljhwt/article/details/10838480

乌拉!!!

最后

以上就是清秀吐司最近收集整理的关于基于树莓派的WIFI抓包、分析(wireshark)并上传至阿里云数据库的全部内容,更多相关基于树莓派内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(82)

评论列表共有 0 条评论

立即
投稿
返回
顶部