-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-redirect-tester.py
185 lines (159 loc) · 5.82 KB
/
http-redirect-tester.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
#!/usr/bin/python
import subprocess
import argparse
import os.path
from sys import platform
def run_check(domain, mode):
responsecode = ""
error = False
outputstring = ""
nexthop = ""
arrow = " -> "
redirects = 0
redirecterror = False
result = ""
previoushop = ""
domain = domain.rstrip()
#check if mode is http or https
if mode == "http":
print("Output for: http://" + domain)
outputstring += "http://"
result = subprocess.run(["http", "--follow", "-h", "--all", domain], capture_output=True, text=True)
elif mode == "https":
print("Output for: https://" + domain)
outputstring += "https://"
result = subprocess.run(["https", "--follow", "-h", "--all", domain], capture_output=True, text=True)
else:
return
#Parse the headers
if result.returncode == 0:
parser = result.stdout.split('\n')
outputstring += domain + arrow
for line in parser:
if "HTTP/" in line:
chunks = line.split(' ')
responsecode = int(chunks[1])
if responsecode >= 200 and responsecode < 300:
outputstring += str(responsecode)
break
if responsecode >= 300 and responsecode < 400:
redirects += 1
if responsecode >= 400:
error = True
if "location:" in line.lower():
nexthop = line.split(' ')
nexthop = nexthop[1].rstrip()
outputstring += str(responsecode) + " " + nexthop + arrow
if redirects == 1:
if "https://" in nexthop and mode == "http":
temp = nexthop.split('https://')
if domain != temp[1].replace("/", ""):
redirecterror = True
error = True
if "http://" in nexthop:
print_error("Unsecure redirect in chain.")
error = True
else:
print_error("Seems the site does not exist.")
outputstring = ""
error = True
print("Did " + str(redirects) + " redirects.")
if error == True:
print_error("Errors occured!")
if redirecterror == True:
print_error("First redirect is incorrect!")
if outputstring != "":
if error == True or redirecterror == True:
print_error(outputstring)
else:
print_success(outputstring)
print_formatter()
def print_formatter():
print("------------------------------------------------------")
return
def print_error(msg):
CRED = '\033[91m'
CEND = '\033[0m'
print(CRED + msg + CEND)
def print_success(msg):
CGREEN = '\33[32m'
CEND = '\033[0m'
print(CGREEN + msg + CEND)
def dns_check(domain):
domain = domain.rstrip()
result = subprocess.run(["nslookup", domain], capture_output=True, text=True)
if result.returncode != 0:
print_error("Domain " + domain + " not found!")
return result.returncode
def ping_check(domain):
domain = domain.rstrip()
result = subprocess.run(["ping", "-c", "2", domain], capture_output=True, text=True)
if result.returncode != 0:
print_error("Could not ping " + domain + "!")
return result.returncode
def is_tool(name):
#check if tool is installed
from shutil import which
return which(name) is not None
def main():
result = 0
domain = ""
#Platform check
if platform != "linux" and platform != "linux2":
print_error("This script currently only supports linux")
return
#Tool checks
if not is_tool("httpie"):
print("Please install httpie!")
return
if not is_tool("nslookup"):
print("Please install nslookup!")
return
#Build arguments
argparser = argparse.ArgumentParser(prog='http-redirect-tester.py')
group = argparser.add_mutually_exclusive_group(required=True)
group.add_argument("-i", "--input", help="the domain to check")
group.add_argument("-l", "--list", help="a text file with a list of domains, one domain per line")
argparser.add_argument("-m", "--mode", choices=["http","https"], required=True, help="Choose the operating mode")
argparser.add_argument("-p", "--ping", action="store_true", default=False, help="Do a ping check of the domain")
argparser.add_argument("-n", "--nslookup", action="store_true", default=False, help="Do a nslookup check of the domain")
args = argparser.parse_args()
domainlist = args.list
mode = args.mode
if mode != "http" and mode != "https":
print_error("Unknown mode!")
print_error("Exiting!")
return
#Run with single input domain
if args.input:
domain = args.input.rstrip()
print_formatter()
if args.nslookup:
result += dns_check(domain)
if args.ping:
result += ping_check(domain)
if result != 0:
print_error("Errors for site " + domain + " occured!")
return
run_check(domain, mode)
#run with input domain list
if args.list:
if not os.path.isfile(domainlist):
print_error('File does not exist.')
return
f = open(domainlist, "r")
lines = f.readlines()
f.close()
for line in lines:
domain = line.rstrip()
print_formatter()
if args.nslookup:
result += dns_check(domain)
if args.ping:
result += ping_check(domain)
if result != 0:
print_error("Errors for site " + domain + " occured!")
continue
run_check(domain, mode)
if __name__ == "__main__":
main()