python實現(xiàn)獲取aws route53域名信息的方法
最近由于工作原因接觸到aws的服務,我需要實時獲取所有的域名信息,用于對其進行掃描,因此寫了一個自動化爬取腳本 給需要的人分享。
1.基礎(chǔ)準備
代碼環(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ū)域,不寫就默認全部
- private區(qū)域的域名只能在aws內(nèi)網(wǎ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
正常的響應結(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請求,如果有響應則證明解析有效。
#測試網(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,默認第一行為表頭 def save_dns_to_xlsx(web_list, path): workbook = openpyxl.load_workbook(path) sheet =workbook["Sheet1"] # 默認存到第一頁 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)文章
Python3.6實現(xiàn)根據(jù)電影名稱(支持電視劇名稱),獲取下載鏈接的方法
這篇文章主要介紹了Python3.6實現(xiàn)根據(jù)電影名稱(支持電視劇名稱),獲取下載鏈接的方法,涉及Python爬蟲與正則相關(guān)操作技巧,需要的朋友可以參考下2019-08-08解決jupyter notebook顯示不全出現(xiàn)框框或者亂碼問題
這篇文章主要介紹了解決jupyter notebook顯示不全出現(xiàn)框框或者亂碼問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04利用Python實現(xiàn)生成顏色表(color chart)
在做色彩相關(guān)的算法分析時候,經(jīng)常需要使用規(guī)則的顏色表來進行輔助,本文就來利用numpy和opencv生成顏色表并保存為圖片,需要的可以參考一下2023-05-05