如何在Python中制作子域扫描仪?代码实现教程

2021年11月16日15:34:39 发表评论 901 次浏览

Python如何制作子域扫描仪?本文带你学习如何构建 Python 脚本以使用请求库扫描给定域的子域。

查找特定网站的子域可让你探索其完整的域基础结构。当涉及道德黑客渗透测试的信息收集阶段时,构建这样的工具非常方便。

手动搜索子域将花费很长时间。幸运的是,我们不必这样做,在本教程中,我们将使用请求库在 Python 中构建一个子域扫描程序。让我们开始吧!

相关:如何在 Python 中使用 Shodan API。

Python制作子域扫描仪示例介绍 - 让我们安装它:

pip3 install requests

我们在这里使用的方法是brute-forcing,换句话说,我们将测试该特定域的所有常见子域名,每当我们收到来自服务器的响应时,这对我们来说是一个指示子域是活动的。

如何在Python中制作子域扫描仪?打开一个新的 Python 文件并继续,让我们使用google.com进行演示,我使用它是因为 google 有很多子域:

import requests

# the domain to scan for subdomains
domain = "google.com"

现在我们需要一个大的子域列表来扫描,我使用了 100 个子域的列表只是为了演示,但在现实世界中,如果你真的想发现所有子域,你必须使用一个更大的列表,检查这个 github包含多达 10K 个子域的存储库

我在当前目录中有一个文件“subdomains.txt”,请确保你也这样做(在此存储库中获取你选择的列表):

# read all subdomains
file = open("subdomains.txt")
# read all content
content = file.read()
# split by new lines
subdomains = content.splitlines()

现在subdomainslist 包含我们要测试的子域,让我们暴力​​破解,Python制作子域扫描仪示例如下:

# a list of discovered subdomains
discovered_subdomains = []
for subdomain in subdomains:
    # construct the url
    url = f"http://{subdomain}.{domain}"
    try:
        # if this raises an ERROR, that means the subdomain does not exist
        requests.get(url)
    except requests.ConnectionError:
        # if the subdomain does not exist, just pass, print nothing
        pass
    else:
        print("[+] Discovered subdomain:", url)
        # append the discovered subdomain to our list
        discovered_subdomains.append(url)

如何在Python中制作子域扫描仪?首先,我们建立适合发送请求的 URL,然后我们使用requests.get()函数从服务器获取HTTP响应,这将在服务器没有响应时引发ConnectionError异常,这就是我们包装的原因它在try/except块中。

如果未引发异常,则子域存在。让我们将所有发现的子域写入一个文件:

# save the discovered subdomains into a file
with open("discovered_subdomains.txt", "w") as f:
    for subdomain in discovered_subdomains:
        print(subdomain, file=f)

这是我运行脚本时的部分结果:

如何在Python中制作子域扫描仪?代码实现教程

Python如何制作子域扫描仪?完成后,你会看到一个新文件discovered_subdomains.txt出现,其中包括所有发现的子域!你会注意到运行脚本时非常慢,尤其是当你使用较长的列表时,因为它使用单个线程进行扫描。但是,如果要加快扫描过程,可以使用多个线程进行扫描,我已经写了一个,请在此处查看。

好的,我们完成了,现在你知道如何发现你想要的任何网站的子域了!

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: