forked from x-falcon/Virtual-Hosts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hosts_to_wildcard.py
140 lines (133 loc) · 3.18 KB
/
hosts_to_wildcard.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
#!python2
#coding:utf-8
'''
Copyright (C) 2017 xfalcon
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import socket
import re
p=re.compile(r"\s+")
def find_sub(s1,s2):
if s1==s2:
return s1
if s1+"." in s2:
return s1
j=None
while 1:
i=s1.rfind(".",0,j)
if i==-1:
return ""
str3=s1[0:i]
if len(str3.split("."))<2:
return ""
if str3 in s2:
return str3
j=i
def query(s,dmap):
if s in dmap:
return dmap[s]
else:
j=0
s="."+s
while 1:
i=s.find(".",j)
if i==-1:
return None
temp_str=s[i:]
if temp_str in dmap:
return dmap[temp_str]
j=i+1
def turn_to_wild(host_file,result_file):
with open(host_file) as f:
lines=f.readlines()
dmap={}
history_domain=[]
for line in lines:
try:
ip,domain=p.split(line.strip())[0:2]
socket.inet_aton(ip)
if domain in history_domain:
continue
if ip in dmap:
dmap[ip].add(domain)
else:
dmap[ip]=set([domain])
history_domain.append(domain)
except Exception as e:
pass
count_map=[]
for ip,domains in dmap.iteritems():
count_map.append((ip,len(domains)))
count_map=sorted(count_map,key=lambda item:item[1],reverse=True)
domain_ip={}
for count in count_map:
ip=count[0]
domains=dmap[ip]
domains=sorted([x[::-1] for x in domains])
first=""
first=domains[0]
for index,domain in enumerate(domains):
temp_str=find_sub(first,domain)
d_ip=query("."+temp_str[::-1],domain_ip)
if d_ip and d_ip!=ip:
domain_ip[first[::-1]]=ip
domain_ip[domain[::-1]]=ip
first=domain
continue
if not temp_str:
if first==domains[index-1]:
domain_ip[first[::-1]]=ip
else:
domain_ip["."+first[::-1]]=ip
first=domain
continue
first=temp_str
if first==domains[-1]:
domain_ip[first[::-1]]=ip
else:
domain_ip["."+first[::-1]]=ip
with open(result_file,"w") as f:
for domain,ip in domain_ip.iteritems():
f.write(ip+"\t"+domain+"\n")
def vaild(source_file,dist_file):
dist_map={}
with open(dist_file) as f:
for l in f.readlines():
ip,domain=l.strip().split("\t")
dist_map[domain]=ip
count=0
lines=0
history_domain=[]
with open(source_file) as f:
for l in f.readlines():
try:
ip,domain=p.split(l.strip())[0:2]
socket.inet_aton(ip)
if domain in history_domain:
continue
history_domain.append(domain)
lines+=1
d_ip=query(domain,dist_map)
if d_ip:
if ip!=d_ip:
print domain,ip,d_ip
else:
count+=1
else:
print domain+" not found"
except Exception as e:
pass
print count,lines
if __name__ == '__main__':
hosts_file="hosts.txt"
turn_to_wild(hosts_file,hosts_file+".vhosts")
vaild(hosts_file,hosts_file+".vhosts")