-
Notifications
You must be signed in to change notification settings - Fork 1
/
features.py
266 lines (232 loc) · 6.59 KB
/
features.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"""
Compute features
"""
import math
import re
import sys
from collections import Counter
import inspect
import numpy as np
from sklearn import preprocessing
import mlpy
import whois
from IPython import embed
from functools import wraps
import errno
import os
import signal
class TimeoutError(Exception):
pass
def timeout(seconds=10, error_message=os.strerror(errno.ETIME)):
def decorator(func):
def _handle_timeout(signum, frame):
raise TimeoutError(error_message)
def wrapper(*args, **kwargs):
signal.signal(signal.SIGALRM, _handle_timeout)
signal.alarm(seconds)
try:
result = func(*args, **kwargs)
finally:
signal.alarm(0)
return result
return wraps(func)(wrapper)
return decorator
class Features(object):
def __init__(self, p):
self.p = p
self.X = None
self.X_scaled = None
self.features_list = []
def f01_dotsnumber(self, fqdn):
"""
Number of dots in FQDN. Ex: toto.tata.com -> 2
"""
return len(fqdn.split('.')) - 1
def f03_occurrences(self, fqdn):
"""
Number of occurences of the fqdn in the dataset
"""
return self.p.occurrences[fqdn]
def f04_length(self, fqdn):
"""
Length of FQDN without the TLD. Ex: toto.com -> 4
"""
return len(fqdn) - len(fqdn.split('.')[-1]) - 1
def f06_entropy(self, fqdn):
"""
Shannon entropy in FQDN without TLD and without dots: Ex: "1223334444" -> 1.8464393446710154
Code snippet from http://rosettacode.org/wiki/Entropy#Python
"""
# remove tld
fqdn = '.'.join(fqdn.split('.')[:-1])
# remove dots
fqdn = fqdn.replace('.', '')
p, lns = Counter(fqdn), float(len(fqdn))
return -sum( count/lns * math.log(count/lns, 2) for count in p.values())
def f07_includenumbers(self, fqdn):
"""
FQDN includes numbers?
"""
if re.search(r'[0-9]', fqdn):
return 1
return -1
def f08_includehyphens(self, fqdn):
"""
FQDN includes hyphen(s)?
"""
if '-' in fqdn:
return 1
return -1
def f09_vowelsper(self, fqdn):
"""
Percentage of vowels in FQDN without TLD and dots
"""
# remove tld
fqdn = '.'.join(fqdn.split('.')[:-1])
# remove dots
fqdn = fqdn.replace('.', '')
vowels = list("aeiouy")
number_of_vowels = float(sum(fqdn.count(c) for c in vowels))
return number_of_vowels / len(fqdn)
def f10_includemedicdoctor(self, fqdn):
"""
Include 'medic' or 'doctor' in FQDN?
"""
if 'medic' in fqdn or 'doctor' in fqdn:
return 1
return -1
def f11_includeship(self, fqdn):
"""
Include 'ship' in FQDN?
"""
if 'ship' in fqdn:
return 1
return -1
def f12_includemail(self, fqdn):
"""
Include 'mail' or 'smtp' in FQDN?
"""
if 'mail' in fqdn or 'smtp' in fqdn:
return 1
return -1
@timeout(5)
def f13_whois_networksolutions(self, fqdn):
"""
Registrar in whois == Network Solutions LLC if not a subdomain?
"""
if len(fqdn.split('.')) - 1 == 1:
try:
if whois.query(fqdn) == 'NETWORK SOLUTIONS, LLC.':
return 1
return -1
except:
return -1
else:
return -1
def f14_tld_is_biz(self, fqdn):
"""
TLD == .biz?
"""
if fqdn.split('.')[-1] == 'biz':
return 1
return -1
def f15_tld_is_cc(self, fqdn):
"""
TLD == .cc?
"""
if fqdn.split('.')[-1] == 'cc':
return 1
return -1
def f16_tld_is_com(self, fqdn):
"""
TLD == .com?
"""
if fqdn.split('.')[-1] == 'com':
return 1
return -1
def f17_tld_is_info(self, fqdn):
"""
TLD == .info?
"""
if fqdn.split('.')[-1] == 'info':
return 1
return -1
def f18_tld_is_net(self, fqdn):
"""
TLD == .net?
"""
if fqdn.split('.')[-1] == 'net':
return 1
return -1
def f19_tld_is_org(self, fqdn):
"""
TLD == .org?
"""
if fqdn.split('.')[-1] == 'org':
return 1
return -1
def f20_tld_is_pw(self, fqdn):
"""
TLD == .pw?
"""
if fqdn.split('.')[-1] == 'pw':
return 1
return -1
def f21_tld_is_ru(self, fqdn):
"""
TLD == .ru?
"""
if fqdn.split('.')[-1] == 'ru':
return 1
return -1
def f22_tld_is_su(self, fqdn):
"""
TLD == .su?
"""
if fqdn.split('.')[-1] == 'su':
return 1
return -1
def f23_includens(self, fqdn):
"""
include ns[0-9] in FQDN?
"""
if re.search(r'ns[0-9]', fqdn):
return 1
return -1
def compute(self):
for o in inspect.getmembers(self, predicate=inspect.ismethod):
self.features_list.append(o[0])
i = 0
for fqdn in self.p.fqdn:
# print fqdn, i, len(self.p.fqdn)
sys.stdout.flush()
features = [
self.f01_dotsnumber(fqdn),
self.f03_occurrences(fqdn),
self.f04_length(fqdn),
self.f06_entropy(fqdn),
self.f07_includenumbers(fqdn),
self.f08_includehyphens(fqdn),
self.f09_vowelsper(fqdn),
self.f10_includemedicdoctor(fqdn),
self.f11_includeship(fqdn),
self.f12_includemail(fqdn),
self.f13_whois_networksolutions(fqdn),
self.f14_tld_is_biz(fqdn),
self.f15_tld_is_cc(fqdn),
self.f16_tld_is_com(fqdn),
self.f17_tld_is_info(fqdn),
self.f18_tld_is_net(fqdn),
self.f19_tld_is_org(fqdn),
self.f20_tld_is_pw(fqdn),
self.f21_tld_is_ru(fqdn),
self.f22_tld_is_su(fqdn),
self.f23_includens(fqdn)
]
if self.X != None:
self.X = np.append(self.X, [features], axis=0)
else:
self.X = np.array([features])
i += 1
# scale
self.X_scaled = preprocessing.scale(self.X)