-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetUrl.py
94 lines (91 loc) · 2.96 KB
/
getUrl.py
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
import db,sys
import time
from scapy.all import *
from scapy.layers.http import HTTPRequest, HTTPResponse, HTTP
from scapy.utils import PcapReader
#packets = sniff(offline="./testcapture.pcap", session=TCPSession)
APPDOMAINS = {
'douyin':[
'douyin','ixigua','huoshan','pstatp'
],
'kuaishou':[
'kwaicdn','yximgs','gifshow','etoote'
],
'tencent':[
'qq.com'
],
}
MOVREG = {
'_vod':[
'mime_type=video_mp4','.mp4','.m3u8'
],
'_live':[
'.flv'
]
}
def getTimestamp():
return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
def packet_callback(packets):
try:
print("get a packet",getTimestamp(),"\n")
sys.stdout.flush()
for pkt in packets:
if pkt.haslayer(HTTPRequest):
#ip dst address
ip_layer = pkt.getlayer('IP')
#ip_layer.show()
if not ip_layer:
ipv6_layer = pkt.getlayer('IPv6')
if ipv6_layer:
ip_dstaddress = ipv6_layer.dst
else:
ip_dstaddress = 'None'
pkt.show()
else:
ip_dstaddress = ip_layer.dst
#tcp dst port
tcp_layer = pkt.getlayer('TCP')
tcp_destport = tcp_layer.dport
#http host & url
http_header = pkt[HTTPRequest].fields
if http_header.get("Host") is None:
host = 'unknown_host'
else:
host = bytes.decode(http_header.get("Host"))
if http_header.get("Path") is None:
path = 'unknown_path'
else:
path = bytes.decode(http_header.get("Path"))
if tcp_destport == 443:
urltitle = 'https://'
else:
urltitle = 'http://'
#complete url
if tcp_destport == 443 or tcp_destport == 80:
finurl = urltitle+host+path
else:
finurl = urltitle+host+':'+str(tcp_destport)+path
timestamp = getTimestamp()
#According to the domain name to judge the APP name
APPNAME = 'others'
for key in APPDOMAINS.keys():
for item in APPDOMAINS.get(key):
if item in finurl:
APPNAME = key
break;
#According to the url to judge the url type
TYPE = '_others'
for key in MOVREG.keys():
for item in MOVREG.get(key):
if item in finurl:
TYPE = key
break;
#save to db
data = (finurl,timestamp,APPNAME,ip_dstaddress,tcp_destport,host,APPNAME+TYPE)
print(data)
db.insert(data)
elif pkt.haslayer(HTTPResponse):
pass
except Exception as e:
print(e)
sniff(iface = "wlan0", prn = packet_callback, store = False)