-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathdnp.py
271 lines (225 loc) · 8.56 KB
/
dnp.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
266
267
268
269
270
271
#!/usr/bin/env python3
# coding: utf-8
# A simple modernized enterprise domain name predictor and generator
#
# Build By LandGrey
#
import os
import sys
import time
import argparse
import itertools
from tld import get_tld
try:
import ConfigParser
except ImportError as e:
import configparser as ConfigParser
def guess_main_domain(name):
tld = get_tld(name, fix_protocol=True)
top_domain = name[:-len(tld) - 1].split(".")[-1] + "." + tld
return top_domain
def unique(seq, idfun=None):
if idfun is None:
def idfun(x): return x
seen = {}
results = []
for item in seq:
marker = idfun(item)
if marker in seen:
continue
seen[marker] = 1
results.append(item)
return results
def awesome_factory(join_string, elements, mix_join=None):
element_number = 3 if len(elements) >= 3 else 2
for length in range(2, element_number + 1):
for x in itertools.combinations(elements, length):
for y in itertools.product(*x):
for z in itertools.permutations(y):
yield join_string.join(z)
if mix_join and len(z) >= 3:
yield join_string.join(z[:-1]) + mix_join + z[-1]
yield z[0] + mix_join + join_string.join(z[1:])
def read_all_domain_names(content):
init_domain_names = []
if os.path.isfile(content):
with open(content, 'r') as f:
for name in f.readlines():
name = name.strip()
if name:
init_domain_names.append(name)
return init_domain_names
def get_configuration(mode):
config_dict = {
'environment': [],
'version': [],
'role': [],
'technology': [],
'service': [],
'monitor': [],
'time': [],
'space': [],
'area': [],
'proxy': [],
'number': [],
'application': []
}
regular_path = os.path.join(current_dir, "rules", "regular.cfg")
if mode:
cfg_path = os.path.join(current_dir, "rules", "predictor-{}.cfg".format(mode))
else:
cfg_path = os.path.join(current_dir, "rules", "predictor-default.cfg")
for cfg in [regular_path, cfg_path]:
try:
config = ConfigParser.ConfigParser(allow_no_value=True)
config.optionxform = str
config.read(cfg)
for s in config.sections():
for o in config.options(s):
config_dict[s].append(o)
except Exception as e:
exit("[-] parse config file: {} error".format(cfg))
return config_dict
def name_filter(name, original_domain, domain_prefix):
js_chunk = []
if "-" in name:
for v1 in name.split("."):
for v2 in v1.split("-"):
js_chunk.append(v2)
else:
js_chunk = name.split(".")
name_length = len(name)
js_chunk_length = len(js_chunk)
if js_chunk_length >= 3:
# drop aa.bb.cc/aa-bb-cc/aa-bb.cc/aa.bb-cc too short name
if name_length <= (js_chunk_length * 2 + js_chunk_length - 1):
return None
# drop aaaaaa-bbbbbb-cccccc too long name
if name.count("-") >= js_chunk_length - 1 and name_length >= (js_chunk_length * 6 + js_chunk_length - 1):
return None
# drop aaaaaaaa./-bbbbbbbb./-cccccccc too long name
if name_length >= (js_chunk_length * 8 + js_chunk_length - 1):
return None
if domain_prefix:
if domain_prefix in js_chunk:
if len(js_chunk) <= 3:
return name + "." + original_domain[len(domain_prefix) + 1:]
else:
return name + "." + original_domain
else:
return name + "." + original_domain
return None
def get_main_domain_similar_domain_names(main_domain_name, config):
results = []
environment = config['environment']
role = config['role']
technology = config['technology']
service = config['service']
proxy = config['proxy']
version = config['version']
monitor = config['monitor']
time = config['time']
space = config['space']
area = config['area']
application = config['application']
small_lists = [environment, role, technology, service, monitor, proxy]
all_lists = small_lists[:]
for ex in [version, time, space, area, application]:
all_lists.append(ex)
for one_lists in all_lists:
for item in one_lists:
results.append(item + "." + main_domain_name)
for js in [".", "-"]:
for name in awesome_factory(js, small_lists):
nf = name_filter(name, main_domain_name, None)
if nf:
results.append(nf)
for name in awesome_factory("-", small_lists, mix_join="."):
nf = name_filter(name, main_domain_name, None)
if nf:
results.append(nf)
return results
def get_normal_domain_similar_domain_names(domain_name, config):
results = []
environment = config['environment']
role = config['role']
technology = config['technology']
service = config['service']
monitor = config['monitor']
proxy = config['proxy']
prefix = domain_name.split(".")[0]
small_lists = [[prefix], environment, role, technology, service, monitor, proxy]
for js in [".", "-"]:
for name in awesome_factory(js, small_lists):
nf = name_filter(name, domain_name, prefix)
if nf:
results.append(nf)
for name in awesome_factory("-", small_lists, mix_join="."):
nf = name_filter(name, domain_name, prefix)
if nf:
results.append(nf)
return results
def get_single_similar_domain_names(original_domain, predictor_mode):
config = get_configuration(mode=predictor_mode)
main_domain = guess_main_domain(original_domain)
is_main_domain = True if original_domain == main_domain else False
if is_main_domain:
return get_main_domain_similar_domain_names(main_domain, config)
else:
return get_normal_domain_similar_domain_names(original_domain, config)
def printer(_predictor_mode, _output_path, _begin_time, _count):
print("[+] current mode: [{0}]\n"
"[+] A total of : {1:} lines\n"
"[+] Store in : {2} \n"
"[+] Cost : {3} seconds".format(_predictor_mode, _count, _output_path, str(time.time() - _begin_time)[:6]))
if __name__ == "__main__":
begin_time = time.time()
ascii_banner = r'''
_ _
(.)_(.)
_ ( _ ) _
/ \/`-----'\/ \
__\ ( ( ) ) /__
) /\ \._./ /\ (
)_/ /|\ /|\ \_(
dnp.py
'''
try:
current_dir = os.path.dirname(os.path.join(os.path.abspath(sys.argv[0]))).encode('utf-8').decode()
except UnicodeError:
try:
current_dir = os.path.dirname(os.path.abspath(sys.argv[0])).decode('utf-8')
except UnicodeError:
current_dir = "."
exit('[*] Please move dnp.py script to full ascii path, than apply it')
output_path = os.path.join(current_dir, 'results')
if not os.path.exists(output_path):
os.mkdir(output_path)
print(ascii_banner)
parser = argparse.ArgumentParser()
parser.add_argument('-d', dest='domain_name', default='', help='single domain name')
parser.add_argument('-f', dest='file_path', default='', help='domain names file path')
parser.add_argument('-m', dest='predictor_mode', default='default', choices=['default', 'simple'], help='choose predictor mode: [default, simple]')
parser.add_argument('-o', '--output', dest='output', default='', help='result output path')
if len(sys.argv) == 1:
sys.argv.append('-h')
args = parser.parse_args()
input_name = args.domain_name
input_file = args.file_path
predictor_mode = args.predictor_mode
output_path = args.output \
if args.output else os.path.join(output_path, (input_name if input_name else str(begin_time)[4:10]) + "-" + predictor_mode + ".txt")
count = 0
with open(output_path, 'w') as f:
if input_name:
for x in unique(get_single_similar_domain_names(input_name, predictor_mode)):
count += 1
f.write(x + "\n")
else:
join_results = []
for ns in read_all_domain_names(input_file):
join_results.extend(get_single_similar_domain_names(ns, predictor_mode))
for x in unique(join_results):
count += 1
f.write(x + "\n")
printer(predictor_mode, output_path, begin_time, count)