-
Notifications
You must be signed in to change notification settings - Fork 2
/
Test-DnsRecord.ps1
195 lines (148 loc) · 5.46 KB
/
Test-DnsRecord.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using namespace System.IO
param (
[string]$Name,
[string]$RecordType = 'ANY',
[switch]$Tcp,
[switch]$Full
)
& (Join-Path $PSScriptRoot 'indented.net.dns\test\2.integration\script\Start-NameServer.ps1')
if (-not $Name.EndsWith('.')) {
$Name = ('{0}.default.indented.co.uk.' -f $Name).TrimStart('.')
}
$path = Join-Path $PSScriptRoot 'Indented.Net.Dns'
'enum', 'class' | ForEach-Object {
Get-ChildItem (Join-Path $path $_) -Filter *.ps1 -Recurse -File | ForEach-Object {
. $_.FullName
}
}
$dnsMessage = [DnsMessage]::new(
$Name,
$RecordType,
'IN'
)
$dnsMessage.SetEDnsBufferSize()
$dnsMessage.SetAcceptDnsSec()
$dnsClient = [DnsClient]::new($Tcp.IsPresent, $false)
$dnsClient.SendQuestion($dnsMessage, '127.0.0.1', 1053)
$message = $dnsClient.ReceiveBytes()
Write-Host 'Message Hexadecimal'
Write-Host '==================='
Write-Host
($message | ForEach-Object { '{0:X2}' -f $_ }) -join '' -split '(?<=\G.{60})' -replace '([0-9a-f]{2})', '$1 ' | Write-Host
Write-Host
Write-Host 'Message Bytes'
Write-Host '============='
Write-Host
($message | ForEach-Object { $_.ToString().PadLeft(4) }) -join '' -split '(?<=\G.{88})' | Write-Host
$binaryReader = [EndianBinaryReader][MemoryStream]$message
$header = [DnsHeader]$binaryReader
$header | Format-List | Out-String | Write-Host
if ($header.QuestionCount -gt 0) {
Write-Host 'Question'
Write-Host '========'
for ($i = 0; $i -lt $header.QuestionCount; $i++) {
[DnsQuestion]$binaryReader | Format-List | Out-String | Write-Host
}
}
if ($header.AnswerCount -gt 0) {
Write-Host 'Answer'
Write-Host '======'
for ($i = 0; $i -lt $header.AnswerCount; $i++) {
$answer = [DnsResourceRecord]::Parse($binaryReader)
$answer | Format-List | Out-String | Write-Host
$null = $binaryReader.BaseStream.Seek(-$answer.RecordDataLength, 'Current')
$bytes = $binaryReader.ReadBytes($answer.RecordDataLength)
Write-Host 'Answer Hexadecimal'
Write-Host '=================='
Write-Host
($bytes | ForEach-Object { '{0:X2}' -f $_ }) -join '' -split '(?<=\G.{60})' -replace '([0-9a-f]{2})', '$1 ' | Write-Host
Write-Host
Write-Host 'Answer Bytes'
Write-Host '============'
Write-Host
($bytes | ForEach-Object { $_.ToString().PadLeft(4) }) -join '' -split '(?<=\G.{88})' | Write-Host
Write-Host
Write-Host 'Answer Base64'
Write-Host '============='
Write-Host
[Convert]::ToBase64String($bytes) | Write-Host
Write-Host
Write-Host 'Answer ToString'
Write-Host '==============='
Write-Host
Write-Host $answer.RecordDataToString()
Write-Host
}
}
if ($header.AuthorityCount -gt 0) {
Write-Host 'Authority'
Write-Host '========='
for ($i = 0; $i -lt $header.AuthorityCount; $i++) {
$authority = [DnsResourceRecord]::Parse($binaryReader)
$authority | Format-List | Out-String | Write-Host
$null = $binaryReader.BaseStream.Seek(-$authority.RecordDataLength, 'Current')
$bytes = $binaryReader.ReadBytes($authority.RecordDataLength)
Write-Host 'Authority Hexadecimal'
Write-Host '====================='
Write-Host
($bytes | ForEach-Object { '{0:X2}' -f $_ }) -join '' -split '(?<=\G.{60})' -replace '([0-9a-f]{2})', '$1 ' | Write-Host
Write-Host
Write-Host 'Authority Bytes'
Write-Host '==============='
Write-Host
($bytes | ForEach-Object { $_.ToString().PadLeft(4) }) -join '' -split '(?<=\G.{88})' | Write-Host
Write-Host
Write-Host 'Authority Base64'
Write-Host '================'
Write-Host
[Convert]::ToBase64String($bytes) | Write-Host
Write-Host
Write-Host 'Authority ToString'
Write-Host '=================='
Write-Host
Write-Host $authority.RecordDataToString()
Write-Host
}
}
if ($header.AdditionalCount -gt 0) {
Write-Host 'Additional'
Write-Host '=========='
for ($i = 0; $i -lt $header.AdditionalCount; $i++) {
$additional = [DnsResourceRecord]::Parse($binaryReader)
$additional | Format-List | Out-String | Write-Host
$null = $binaryReader.BaseStream.Seek(-$additional.RecordDataLength, 'Current')
$bytes = $binaryReader.ReadBytes($additional.RecordDataLength)
Write-Host 'Additional Hexadecimal'
Write-Host '======================'
Write-Host
($bytes | ForEach-Object { '{0:X2}' -f $_ }) -join '' -split '(?<=\G.{60})' -replace '([0-9a-f]{2})', '$1 ' | Write-Host
Write-Host
Write-Host 'Additional Bytes'
Write-Host '================'
Write-Host
($bytes | ForEach-Object { $_.ToString().PadLeft(4) }) -join '' -split '(?<=\G.{88})' | Write-Host
Write-Host
Write-Host 'Additional Base64'
Write-Host '================='
Write-Host
[Convert]::ToBase64String($bytes) | Write-Host
Write-Host
Write-Host 'Additional ToString'
Write-Host '==================='
Write-Host
Write-Host $additional.RecordDataToString()
Write-Host
}
}
Write-Host 'Dig Answer'
Write-Host '=========='
Write-Host
$response = & (Join-Path $PSScriptRoot 'Indented.Net.Dns\test\2.integration\bin\dig.exe') @(
$Name
$RecordType
@('+short', $null)[$Full.IsPresent]
'+dnssec'
'-p' , 1053
'@127.0.0.1'
) | Out-String
Write-Host $response