forked from nishadhperera/IoT-device-identification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
features_scapy.py
219 lines (178 loc) · 5.47 KB
/
features_scapy.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# This program contains methods to extract features from a packet
# Condition: packets are extracted using scapy rdpcap method
# Author: Nishadh Aluthge
from _csv import Error
from scapy.all import IPOption
def get_length_feature(packet): # Extracts Packet length
try:
a = len(packet)
return a
except Error:
return 0
def get_LLC_feature(packet): # check for LLC layer header
try:
x = packet["LLC"]
return 1
except IndexError:
return 0
def get_padding_feature(packet): # check for padding layer header
try:
x = packet["Padding"]
return 1
except IndexError:
return 0
def get_arp_feature(packet):
try:
if packet[0].type == 2054: # ARP feature detected (0 - Ethernet or 802.3 layer)
return 1
else:
return 0
except AttributeError:
return 0
def get_ip_feature(packet):
try:
if packet[0].type == 2048: # IP feature detected
return 1, "IP"
else:
return 0, "None"
except AttributeError:
return 0, "None"
def get_eapol_feature(packet):
try:
if packet[0].type == 34958: # EAPoL feature detected
return 1
else:
return 0
except AttributeError:
return 0
def get_icmp_feature(packet):
try:
if packet["IP"].proto == 1: # ICMP feature detected
return 1, 0
elif packet["IP"].proto == 58: # ICMPv6 feature detected
return 0, 1
else:
return 0, 0
except IndexError:
return 0, 0
def get_tcpudp_feature(packet):
try:
if packet["IP"].proto == 6: # TCP feature detected
return 1, 0, "TCP"
elif packet["IP"].proto == 17: # UDP feature detected
return 0, 1, "UDP"
else:
return 0, 0, "None"
except IndexError:
return 0, 0, "None"
def get_r_alert_feature(packet):
try:
if int(packet["IP"].ihl) > 5: # detecting Router Alert IP Option
for op in packet["IP"].options:
if type(op) == IPOption and int(op.option) == 20:
return 1
else:
return 0
else:
return 0
except IndexError:
return 0
def get_dest_ip_counter_feature(packet, dest_ip_set, dst_ip_counter):
if packet["IP"].dst not in dest_ip_set: # Counting the Destination IP counter value
dest_ip_set[packet["IP"].dst] = 1
dst_ip_counter = dst_ip_counter + 1
else:
dest_ip_set[packet["IP"].dst] += 1
return dst_ip_counter, dest_ip_set, dst_ip_counter
def get_dns_feature(packet, tl_pro):
try:
if packet['' + tl_pro + ''].sport == 53 or packet['' + tl_pro + ''].dport == 53: # DNS feature detected
return 1
else:
return 0
except IndexError:
return 0
def get_bootp_dhcp_feature(packet, tl_pro):
try:
if packet['' + tl_pro + ''].sport == 67 or packet['' + tl_pro + ''].sport == 68: # BOOTP, DHCP feature detected
return 1, 1
else:
return 0, 0
except IndexError:
return 0, 0
def get_http_feature(packet, tl_pro):
try:
if packet['' + tl_pro + ''].sport == 80 or packet['' + tl_pro + ''].dport == 80: # HTTP feature detected
return 1
else:
return 0
except IndexError:
return 0
def get_ntp_feature(packet, tl_pro):
try:
if packet['' + tl_pro + ''].sport == 123 or packet['' + tl_pro + ''].dport == 123: # NTP feature detected
return 1
else:
return 0
except IndexError:
return 0
def get_https_feature(packet, tl_pro):
try:
if packet['' + tl_pro + ''].sport == 443: # HTTPS feature detected
return 1
else:
return 0
except IndexError:
return 0
def get_ssdp_feature(packet, tl_pro):
try:
if packet['' + tl_pro + ''].sport == 1900: # SSDP feature detected
return 1
else:
return 0
except IndexError:
return 0
def get_mdns_feature(packet, tl_pro):
try:
if packet['' + tl_pro + ''].sport == 5353: # MDNS feature detected
return 1
else:
return 0
except IndexError:
return 0
def get_srcpc_feature(packet, tl_pro):
try:
if int(packet['' + tl_pro + ''].sport) >= 49152: # Calculating source port value
return 3
elif int(packet['' + tl_pro + ''].sport) >= 1024:
return 2
elif int(packet['' + tl_pro + ''].sport) >= 0:
return 1
else:
return 0
except IndexError:
return 0
def get_dstpc_feature(packet, tl_pro):
try:
if int(packet['' + tl_pro + ''].dport) >= 49152: # Calculating destination port value
return 3
elif int(packet['' + tl_pro + ''].dport) >= 1024:
return 2
elif int(packet['' + tl_pro + ''].dport) >= 0:
return 1
else:
return 0
except IndexError:
return 0
def get_rawdata_feature(packet): # Analyse for the presence of raw data in the payload
try:
x = packet["Raw"]
return 1
except IndexError:
return 0
def get_payload_feature(packet): # calculates the amount of rawdata present in the payload
try:
x = len(packet["Raw"])
except (IndexError, AttributeError) as e:
x = 0
return x