在渗透测试中收集子域名是必不可少的环节,因为在测试目标主站时如果未发现相关漏洞,通常会从其子域名寻找突破点。收集子域名的方法有很多种,这里是通过bing搜索引擎来进行子域名收集。
代码如下:
#简单的子域名挖掘
#用法:python subdomain.py xxx.com 枚举子域名数量
from email import header
from os import link
import requests
from bs4 import BeautifulSoup
from urllib.parse import urlparse
import sys
def bing_search(site,pages):
Subdomain=[]
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0',
'Accept': '*/*',
'Accept-Language':'en-us,en;q=0.5',
'Accept-Encoding':'qzip,deflate',
'referer':'http://cn.bing.com/search?q=email+site%3abaidu.com&qs=n&sp=-1&pq=emailsie%3abaidu.com&first=2&FORM=PERE1'
}
for i in range(1,int(pages)+1):
url="https://cn.bing.com/search?q=site%3a"+site+"&go=Search&qs=ds&first="+ str((int(i)-1)*10)+"&FORM=PERE"
conn=requests.session()
conn.get('http://cn.bing.com',headers=headers)
html=conn.get(url,stream=True,headers=headers,timeout=8)
soup=BeautifulSoup(html.content,'html.parser')
job_bt=soup.findAll('h2')
for i in job_bt:
link=i.a.get('href')
domain=str(urlparse(link).scheme+"://"+urlparse(link).netloc)
if domain in Subdomain:
pass
else:
Subdomain.append(domain)
print(domain)
if __name__=='__main__':
#site=baidu.com
if len(sys.argv)==3:
site=sys.argv[1]
page=sys.argv[2]
else:
print("usage: %s baidu.com 10" % sys.argv[0])
sys.exit(-1)
Subdomain=bing_search(site,page)
运行示例: