子域名查找器因‘label empty or too long’错误停止的原因排查
问题排查与修复方案
你遇到的label empty or too long错误,是因为字典文件里存在空行或者过长的子域名条目,导致生成的域名不符合DNS规则,而且代码没捕获这类异常,直接崩了。
具体原因:
- 当字典里有空行时,
line.strip()后得到空字符串,生成的test_url就是.youtube.com,这种域名在DNS解析时会触发"label empty"错误。 - 如果字典里有超过63字符的子域名(DNS规定每个域名标签最长63字符),就会触发"label too long"错误。
- 你的代码只捕获了
ConnectionError和InvalidURL,但这类DNS解析错误属于底层的socket.gaierror,没被捕获,所以程序直接停止运行。
修复后的代码:
import requests import socket url = "youtube.com" def request(test_url): try: return requests.get(f"http://{test_url}", timeout=2) except requests.exceptions.ConnectionError: pass except requests.exceptions.InvalidURL: print('INVALID URL:', test_url) except socket.gaierror as e: # 捕获DNS解析错误,比如label empty/too long print(f"DNS ERROR for {test_url}: {str(e)}") except requests.exceptions.RequestException as e: # 兜底捕获其他所有requests相关异常 print(f"ERROR for {test_url}: {str(e)}") with open("/home/kali/PycharmProjects/websitesub/subdomains-wodlist.txt", "r") as wordlist_file: for line in wordlist_file: word = line.strip() # 跳过空的子域名条目 if not word: continue # 可选:验证子域名长度不超过63字符 if len(word) > 63: print(f"SKIPPED too long subdomain: {word}") continue test_url = f"{word}.{url}" response = request(test_url) if response: print(f"Discovered SUBDOMAIN: {test_url}")
关键修复点:
- 增加空条目过滤:
if not word: continue,避免生成.youtube.com这种无效域名。 - 捕获
socket.gaierror处理DNS解析相关错误,程序不会直接崩溃。 - 可选子域名长度检查,提前过滤过长的条目。
- 用
f-string替代字符串拼接,代码更清晰。 - 兜底捕获
RequestException,覆盖其他未预料到的requests异常。
内容的提问来源于stack exchange,提问作者Cezar
相关产品推荐
相关产品推荐

