Quick Note: This code is quickly put together to prompt for the Cloudflare API key and email, then will list all the domains in cloudflare and also prompt for an ip to search for within all domains. Please ignore the debugging and extra code
$ CloudflareAPIKey = Read-Host -Prompt 'Enter the API Key' ;
$ CloudflareAPIEmail = Read-Host -Prompt 'Enter the API email e.g. user@domain' ;
$ headers = @ {
"X-Auth-Key" = $ CloudflareAPIKey ;
"X-Auth-Email" = $ CloudflareAPIEmail ;
"Content-Type" = 'application/json'
}
#this may need changing when Cloudflare update the api
[ string ] $ cloudFlareApiBaseUrl = "https://api.cloudflare.com/client/v4"
#Force TLS version to be 1.2 (cloudflare only allows tls1.2 now)
[ Net.ServicePointManager ]::SecurityProtocol = [ Net.SecurityProtocolType ]::Tls12
# Get Zones from cloudflare
$ request = Invoke-WebRequest -Uri " $ { cloudFlareApiBaseUrl }/zones" -Method "GET" -Headers $ headers
$ CloudflarePagedDomains = ConvertFrom-Json $ request .Content
[ int ] $ pageCount = $ CloudflarePagedDomains .result_info [ 0 ].total_pages
$ CloudflareAllDomains = @ ()
$ IpSearchValue = Read-Host -Prompt 'IP address to search for in Cloudflare' ;
write-host "List of all domains in cloudflare" -foregroundcolor "Yellow"
for ( $ i = 1 ; $ i -le $ pageCount ; $ i ++)
{
$ Parameters = @ {
page = $ i
}
$ request = Invoke-WebRequest -Uri " $ { cloudFlareApiBaseUrl }/zones" -Method "GET" -Headers $ headers -Body $ Parameters
$ CloudflarePagedDomains = ConvertFrom-Json $ request .Content
#loop through $request.result array...
Foreach ( $ CloudflareDomain IN $ CloudflarePagedDomains .result ) {
$ CloudflareAllDomains += $ CloudflareDomain .name ;
write-host " $ ( $ CloudflareDomain .name ) `r " -foregroundcolor "DarkCyan"
}
}
Foreach ( $ CloudflareDomain IN $ CloudflareAllDomains ) {
$ request = Invoke-WebRequest -Uri " $ { cloudFlareApiBaseUrl }/zones/?name= $ { CloudflareDomain }" -Method "GET" -Headers $ headers
$ zoneId = $ ( ConvertFrom-Json $ request .Content ).result[ 0 ].id
write-host "Searching $ (( $ CloudflareDomain ).padright( 75 )) $ ( $ zoneId )" -foregroundcolor "Yellow"
$ Parameters = @ {
type = 'A'
content = $ IpSearchValue
}
$ request = Invoke-WebRequest -Uri " $ { cloudFlareApiBaseUrl }/zones/ $ { zoneId }/dns_records" -Method "GET" -Headers $ headers -Body $ Parameters
Foreach ( $ RecordFound IN $ ( ConvertFrom-Json $ request .Content ).result) {
write-host " $ (( $ RecordFound .name ).padright( 75 )) $ ( $ RecordFound .content )" -foregroundcolor "Green"
}
}
Powershell: Cloudflare List all domains and search for ip