Skip to content

Instantly share code, notes, and snippets.

@manasmbellani
Last active March 3, 2022 01:20
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save manasmbellani/ff2cf7564d96540a19f072e2ba0eb988 to your computer and use it in GitHub Desktop.
Save manasmbellani/ff2cf7564d96540a19f072e2ba0eb988 to your computer and use it in GitHub Desktop.
Subdomaincheck.py - A script to detect subdomain takeover possibilities given a list of domains.
#!/usr/bin/python
from argparse import ArgumentParser, RawTextHelpFormatter
import subprocess
import shlex
import requests
USER_AGENT_STR = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0"
COMMON_HOSTING_PROVIDERS = {"heroku": "heroku",
"zendesk": "zendesk",
"bitbucket": "bitbucket",
"shopify": "shopify",
"teamwork": "teamwork",
"unbounce": "unbounce",
"github": "github",
"helpjuice": "helpjuice",
"helpscout": "helpscout",
"cargocollective": "cargocollective",
"statuspage": "statuspage",
"tumblr": "tumblr"}
UNMANAGED_DOMAIN_MSGS=["no application was found", "no such app",
"specified bucket does not exist",
"there isn't a github page"]
ANSI_GRN = '\033[0;32m'
ANSI_CLR = '\033[0;0m'
DEFAULT_REQUEST_TIMEOUT = 10
DESCRIPTION = """A python script which automates checking of each domain in a list of domains provided to confirm if it could be susceptible to sub-domain takeover.
The checks performed on each domain are as follows:-
* Check 1: Domain contains a name of public hosting provider with whom apps can be registered by any user. Recommend checking these ones manually as Check 2 could miss some unregistered apps sometimes.
* Check 2: HTTP/HTTPS request to the domain name contains a message which could indicate that no app is registered at given domain e.g. "no such app". Check 2 is necessary to confirm that there is actually an unregistered app at the public hosting provider location e.g. Heroku. Additionally, sometimes a Heroku registered website may be behind a CDN provide due to which Check 1 may not work (e.g. abc.xyz.com is hosted on Heroku, BUT the IP resolves to Akamai/Cloudflare). Direct HTTP(S) call could validate the presence of an unregistered app very quickly.
If Check 1 reveals valuable data (e.g. CNAME to a common hosting provider such as Heroku), then the following message is provided: -
"Common Hosting Provider found in an alias! Provider: heroku, Domain: abc.xyz.com"
Additionally, the output of the script will be clearly highlighted.
If Check 2 reveals valuable data (e.g. a message such as "no application was found" obtained when making a HTTP(S) request), then the following message is provided: -
"Unmanaged text 'no application was found' found in response for 'abc.xyz.com'! HTTPS response is: <http-response from GET https://abc.xyz.com>"
The script can be run on any Linux machine. Minimum requirements to run this script are:
* Python >v2.7
* Linux Host
* PyRequests
Sample Usage:
./subdomaincheck.py -i domainslist.txt -o /tmp/out.txt
"""
EPILOG ="""The MIT License (MIT)
---------------------
Copyright 2017 Manas Bellani
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
def exec_process(cmdline):
p = subprocess.Popen(shlex.split(cmdline), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out,err = p.communicate()
if err == "":
return out
else:
return out + err
def outmsg(msg, f, start_color=ANSI_CLR):
f.write(msg+"\n")
end_color = ANSI_CLR
print start_color + msg + end_color
parser = ArgumentParser(description=DESCRIPTION, epilog=EPILOG, formatter_class=RawTextHelpFormatter)
parser.add_argument("-i,--infile", action="store", dest="infile",
help=("list of domains for testing - one on each new line, e.g. can be generated via knockpy, sublist3r"), required=True)
parser.add_argument("-o,--outfile", action="store", dest="outfile",help="file to output the results", required=True)
parser.add_argument("-t,--timeout", action="store", dest="timeout", help="default HTTP/HTTPS request timeout", default=DEFAULT_REQUEST_TIMEOUT)
args = parser.parse_args()
infile = args.infile
outfile = args.outfile
timeout = args.timeout
# Read the list of domains to be checked
with open(infile, "rb+") as f:
domains_list = f.readlines()
# Open the outfile for all logging
with open(outfile, "wb+") as f:
# Check each domain name
for domain_line in domains_list:
domain = domain_line.strip()
# Check the hostname via DNS to discover any CNAME aliases
cmdline = "host " + domain
outmsg(domain, f)
outmsg("---------------------------------------------------------------", f)
outmsg("[*] Checking DNS.", f)
host_resolve = exec_process(cmdline)
outmsg(host_resolve, f)
# locate any CNAME aliases
aliases = filter(lambda s: "alias" in s,
host_resolve.split("\n"))
headers = {"User-Agent": USER_AGENT_STR}
# now, check each aliass
for alias in aliases:
result = ""
# check if provider's url keyword exists in each alias
for provider, provider_keyword in COMMON_HOSTING_PROVIDERS.items():
result = ""
if provider_keyword in alias:
# If a known provider found - inform user!
result = "[+] Common Hosting Provider found in an alias! Provider: %s, Domain: %s\n" % (provider,domain)
outmsg(result, f, ANSI_GRN)
outmsg("[*] Making HTTP/HTTPS request.\n", f)
# Get both the http request and response to locate any signs of an unregistered app
valid_http_resp = False
valid_https_resp = False
try:
http_resp = requests.get("http://" + domain, headers=headers, verify=False, timeout=timeout)
valid_http_resp = True
except requests.exceptions.ConnectionError:
outmsg("[-] No connection could be made to '%s'. Determine if DNS entry is defined for the domain.\n" % domain, f)
except requests.exceptions.ReadTimeout:
outmsg("[-] No connection could be made to '%s'. HTTP Request timed out. Try accessing website manually.\n" % domain, f)
except requests.exceptions.TooManyRedirects:
outmsg("[-] No connection could be made to '%s'. HTTP Request with excessive redirections. Try testing website manually.\n" % domain, f)
try:
https_resp = requests.get("https://" + domain, headers=headers, verify=False, timeout=timeout)
valid_https_resp = True
except requests.exceptions.ConnectionError:
outmsg("[-] No connection could be made to '%s'. Determine if DNS entry is defined for the domain.\n" % domain, f)
except requests.exceptions.ReadTimeout:
outmsg("[-] No connection could be made to '%s'. HTTPS Request timed out. Try testing website manually.\n" % domain, f)
except requests.exceptions.TooManyRedirects:
outmsg("[-] No connection could be made to '%s'. HTTPS Request with excessive redirections. Try testing website manually.\n" % domain, f)
# Do we have any messages that indicate unmanaged subdomain in the http/https output
result = ""
if valid_http_resp or valid_https_resp:
for msg in UNMANAGED_DOMAIN_MSGS:
msg_to_locate = msg.lower()
if valid_http_resp:
if msg_to_locate in http_resp.text.lower():
# Located msgs that indicate an unmanaged app in HTTP response
# that indicate susceptibility to takeover
result = "[+] Unmanaged text '%s' found in response for '%s'! HTTP response is:" % (msg_to_locate, domain)
result += http_resp.text+"\n"
outmsg(result, f, ANSI_GRN)
if valid_https_resp:
if msg_to_locate in https_resp.text.lower():
# Located msgs that indicate an unmanaged app in HTTPS response
# that indicate susceptibility to takeover
result = "[+] Unmanaged text '%s' found in response for '%s'! HTTPS response is:" % (msg_to_locate, domain)
result += https_resp.text+"\n"
outmsg(result, f, ANSI_GRN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment