python實現(xiàn)獲取aws route53域名信息的方法
最近由于工作原因接觸到aws的服務(wù),我需要實時獲取所有的域名信息,用于對其進行掃描,因此寫了一個自動化爬取腳本 給需要的人分享。
1.基礎(chǔ)準(zhǔn)備
代碼環(huán)境:python3 第三方庫:boto3 (安裝方法pip install boto3) 官方文檔:https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/route53.html#route53
2.獲取client
首先你需要獲取一個有效的key,指路
控制臺 -> IAM ->安全憑證 ->訪問密鑰,得到key后就可以正式開始編程了
#授權(quán)的key
access_key_id = ""
secret_access_key = ""
client = boto3.client('route53',
aws_access_key_id=access_key_id,
aws_secret_access_key=secret_access_key
)如果key的權(quán)限正常的話,這個client就能用來獲取我們需要的dns數(shù)據(jù)了。
3.獲取區(qū)域
route53的區(qū)域指的是根域名,每一個根域名都會有一個獨立的區(qū)域,如果我們想要獲取具體的解析記錄,就需要先獲取所有的域名id。
#獲取賬號下的區(qū)域id
def get_hostedzone_id(client):
return client.list_hosted_zones(
MaxItems='100',
#Marker='',
#DelegationSetId='string',
#HostedZoneType='PrivateHostedZone'
)- 單次查詢的最大記錄是100條
- marker,delegationsetid 是區(qū)域數(shù)目超過100時遍歷查詢是需要使用的參數(shù),不超過100的情況下不需要
- HostedZoneType可以用來指定查public或private區(qū)域,不寫就默認(rèn)全部
- private區(qū)域的域名只能在aws內(nèi)網(wǎng)中使用
正常的響應(yīng)語法如下
{
'HostedZones': [
{
'Id': 'string',
'Name': 'string',
'CallerReference': 'string',
'Config': {
'Comment': 'string',
'PrivateZone': True|False
},
'ResourceRecordSetCount': 123,
'LinkedService': {
'ServicePrincipal': 'string',
'Description': 'string'
}
},
],
'Marker': 'string',
'IsTruncated': True|False,
'NextMarker': 'string',
'MaxItems': 'string'
}IsTruncated = False 就表示查詢數(shù)據(jù)已經(jīng)到頭了
4.獲取指定區(qū)域的全部域名解析記錄
#獲取指定區(qū)域下的所有dns解析記錄
def get_dns_records(client, hostedzone_id):
#數(shù)據(jù)量低于300
response = client.list_resource_record_sets(
HostedZoneId=hostedzone_id,
MaxItems='300'
)
dns_records = response['ResourceRecordSets']
#數(shù)據(jù)量超出300部分循環(huán)
while(response['IsTruncated'] != False):
response = client.list_resource_record_sets(
HostedZoneId=hostedzone_id,
StartRecordName=response["NextRecordName"],
StartRecordType=response["NextRecordType"],
MaxItems='300'
)
dns_records.extend(response['ResourceRecordSets'])
return dns_records解析記錄的單次查詢上限是300條,超過的話就需要根據(jù)NextRecordName和NextRecordType 循環(huán)查詢。
5.獲取賬號下的全部解析記錄
通過上面2個函數(shù)組合一下,我們就能獲取賬號下的全部dns解析記錄
def get_all_dns_records(client):
dns_records = []
hostedzones = get_hostedzone_id(client)
for zone in hostedzones["HostedZones"]:
dns_records.extend(get_dns_records(client, zone['Id']))
return dns_records正常的響應(yīng)結(jié)果如下
{
'ResourceRecordSets': [
{
'Name': 'string',
'Type': 'SOA'|'A'|'TXT'|'NS'|'CNAME'|'MX'|'NAPTR'|'PTR'|'SRV'|'SPF'|'AAAA'|'CAA'|'DS',
'SetIdentifier': 'string',
'Weight': 123,
'Region': 'us-east-1'|'us-east-2'|'us-west-1'|'us-west-2'|'ca-central-1'|'eu-west-1'|'eu-west-2'|'eu-west-3'|'eu-central-1'|'eu-central-2'|'ap-southeast-1'|'ap-southeast-2'|'ap-southeast-3'|'ap-northeast-1'|'ap-northeast-2'|'ap-northeast-3'|'eu-north-1'|'sa-east-1'|'cn-north-1'|'cn-northwest-1'|'ap-east-1'|'me-south-1'|'me-central-1'|'ap-south-1'|'ap-south-2'|'af-south-1'|'eu-south-1'|'eu-south-2'|'ap-southeast-4'|'il-central-1',
'GeoLocation': {
'ContinentCode': 'string',
'CountryCode': 'string',
'SubdivisionCode': 'string'
},
'Failover': 'PRIMARY'|'SECONDARY',
'MultiValueAnswer': True|False,
'TTL': 123,
'ResourceRecords': [
{
'Value': 'string'
},
],
'AliasTarget': {
'HostedZoneId': 'string',
'DNSName': 'string',
'EvaluateTargetHealth': True|False
},
'HealthCheckId': 'string',
'TrafficPolicyInstanceId': 'string',
'CidrRoutingConfig': {
'CollectionId': 'string',
'LocationName': 'string'
}
},
],
'IsTruncated': True|False,
'NextRecordName': 'string',
'NextRecordType': 'SOA'|'A'|'TXT'|'NS'|'CNAME'|'MX'|'NAPTR'|'PTR'|'SRV'|'SPF'|'AAAA'|'CAA'|'DS',
'NextRecordIdentifier': 'string',
'MaxItems': 'string'
}
實測 NextRecordIdentifier 并沒有返回 ,也不影響查詢結(jié)果
6.獲取指定的DNS解析記錄
record_type = ['A','AAAA',"CNAME"]
#根據(jù)想要的dns記錄篩選最終數(shù)據(jù)
def get_dns_records_by_type(dns_records, record_type):
final_dns_records =[]
for record in dns_records:
if record['Type'] in record_type:
final_dns_records.append(record)
return final_dns_records- record_type 為需要提取的DNS解析類型,總共有 A | AAAA | CAA | CNAME | MX | NAPTR | NS | PTR | SOA | SPF | SRV | TXT 這些類型
- 這里的添加集合用的是append,之前用的都有extend 具體有啥區(qū)別 感興趣的可以自行研究
7.去重 + 公網(wǎng)開放測試
由于部分cname本身并不是有效域名 只是一個單純轉(zhuǎn)發(fā),且dns解析上無法判斷解析記錄是否公網(wǎng)開放,所以需要進行測試。這里提供一個簡單的方案 就是直接發(fā)起request請求,如果有響應(yīng)則證明解析有效。
#測試網(wǎng)站是否能訪問,這里使用set進行去重
def test_web_alive(dns_records):
web_list = []
dns_list = set()
for dns in dns_records:
dns_list.add(dns['Name'][:-1])
print(len(dns_list))
for dns in dns_list:
try:
response = requests.get("https://"+dns)
web_list.append([dns,response.status_code,'aws'])
except:
web_list.append([dns,'cant reach','aws'])
return web_list
#將測試結(jié)果存儲到excel,默認(rèn)第一行為表頭
def save_dns_to_xlsx(web_list, path):
workbook = openpyxl.load_workbook(path)
sheet =workbook["Sheet1"] # 默認(rèn)存到第一頁
for index,dns in enumerate(web_list):
sheet.cell(row=index+2,column=1).value = dns[0]
sheet.cell(row=index+2,column=2).value = dns[1]
sheet.cell(row=index+2,column=3).value = dns[2]
workbook.save(path)到此這篇關(guān)于python實現(xiàn)獲取aws route53域名信息的文章就介紹到這了,更多相關(guān)python獲取aws route53域名信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
互斥鎖解決 Python 中多線程共享全局變量的問題(推薦)
這篇文章主要介紹了互斥鎖解決 Python 中多線程共享全局變量的問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Python中五個你不知道的print函數(shù)用法詳解
今天我們來聊聊Python里最 平平無奇卻又無處不在的print()函數(shù),作為Python開發(fā)者,我們幾乎每天都在用它,但你真的了解它的所有玩法嗎,下面就跟隨小編來看看五個你不知道的print函數(shù)用法吧2026-01-01
Python調(diào)用ChatGPT的API實現(xiàn)文章生成
最近ChatGPT大火,在3.5版本后開放了接口API,所以很多人開始進行實操,這里我就用python來為大家實現(xiàn)一下,如何調(diào)用API并提問返回文章的說明2023-03-03
聽歌識曲--用python實現(xiàn)一個音樂檢索器的功能
本篇文章中主要介紹了用python實現(xiàn)一個音樂檢索器,類似于QQ音樂的搖一搖識曲,有興趣的同學(xué)可以了解一下。2016-11-11
Anaconda虛擬環(huán)境中安裝cudatoolkit和cudnn包并配置pytorch-gpu的配置教程
這篇文章詳細介紹了如何在Anaconda虛擬環(huán)境中安裝和配置TensorFlow,特別是針對CUDA和cuDNN的版本管理,文章首先解釋了為什么需要更新TensorFlow版本,然后指導(dǎo)如何創(chuàng)建新的虛擬環(huán)境,需要的朋友可以參考下2025-02-02

