-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathclient.ps1
348 lines (247 loc) · 9.9 KB
/
client.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#requires -version 2
<#
.SYNOPSIS
Powershell client for ThunderDNS
.DESCRIPTION
This is a Powershell client for ThunderDNS tool.
It can forward TCP traffic over DNS protocol.
.PARAMETER -domain
Your domain name, which is links to your ThunderDNS server
.PARAMETER -clientname
Your client name, it may be helpful, when you get clients list in ThunderProxy (--clients option)
.PARAMETER -server
Optional parameter. Your DNS server address. May be "localhost" to tests. If not provided, client uses standard
system DNS-server.
.NOTES
Author: FBK CyberSecurity [ by Sergey Migalin]
Creation Date: August, 2018
.EXAMPLE
./client.ps1 -domain oversec.ru -clientname test
.EXAMPLE
./client.ps1 -domain oversec.ru -clientname test -server localhost
#>
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
param([string] $domain,
[string] $clientname,
[string] $server = '',
[int] $blockLength = 200,
[int] $bufferSize = 1024,
[bool] $debug = $false)
#----------------------------------------------------------[Declarations]----------------------------------------------------------
#Script Version
$sScriptVersion = "0.1.4 alpha"
$MaximumBlockLength = $blockLength
$BufferSize = $bufferSize
$Actions = @{Register='0'; Request='1'; Reply='2'; Delete='3'}
$Dot = '.'
if ($debug)
{
$DebugPreference = 'Continue'
} else {
$DebugPreference = 'SilentlyContinue'
}
#-----------------------------------------------------------[Functions]------------------------------------------------------------
Function Random-String{
Param([int]$Length)
Process{
return ([char[]]([char]'a'..[char]'z') + 0..9 | sort {get-random})[1..$Length] -join ''
}
}
Function Get-DNS-TXT-value{
Param([string]$text)
Process{
return [regex]::Matches($text, '"(.*)"') | %{$_.groups[1].value} | %{$_ -replace '([ "\t]+)',$('') }
}
}
Function Get-TCP-Reply{
Param($reader)
Process{
Try{
$buf = [System.Byte[]]::CreateInstance([System.Byte], $BufferSize)
$count = $reader.Read($buf, 0, $BufferSize)
$to = $count - 1
return $buf[0..$to]
}
Catch {
Write-Warning "Cannot read from TCP"
return
}
}
}
Function Send-TCP-Request{
Param($writer,
$request)
Process{
Try {
$writer.Write($request, 0, $request.Length) | Out-Null
return
}
Catch {
Write-Warning "Cannot write TCP"
}
}
}
Function Connect-TCP{
Param([string]$server,
[string]$port)
Process{
Try{
return New-Object System.Net.Sockets.TcpClient($server, $port)
}
Catch{
return
}
}
}
Function Register{
Param([string] $domain,
[string] $server,
[string] $clientname)
Process{
$seed = Random-String -Length 7
$act = $Actions.Register
$text = &nslookup -q=TXT $act$seed$clientname$Dot$domain $server 2>$null
$text = Get-DNS-TXT-value -text $text
$res = @{Id=$text.Substring(0,2); Key=$text.Substring(2,3)}
return $res
}
}
Function Del-user{
Param([string] $domain,
[string] $server,
[string] $id)
Process{
$act = $Actions.Delete
$seed = Random-String -Length 7
$a = &nslookup -q=TXT $act$seed$id$Dot$domain $server 2>$null
$a = Get-DNS-TXT-value -text $a
$t1 = "12REM"
if ($a.Substring(0,2) -eq $id) {
Write-Host Success
} else {
Write-Host Not success
}
}
}
Function Reply{
Param([string] $domain,
[string] $server,
[string] $id,
[string] $key,
[string] $reply)
Process{
$act = $Actions.Reply
Write-Debug "Send Reply to DNS"
for($i = 0; $i -lt $reply.Length / $MaximumBlockLength; $i += 1){
#Start-Sleep -Milliseconds 500
$seed = Random-String -Length 4
$num = ([math]::floor($reply.Length / $MaximumBlockLength) - $i).ToString().PadLeft(3, '0')
if ($reply.Length / $MaximumBlockLength -eq 1) {
Write-Warning 'fix1'
$num = "000"
}
Write-Progress -Activity "Reply to DNS" -Status "Remaining $num packets" -PercentComplete ($i * $MaximumBlockLength / $reply.Length * 100 )
$part = $reply.Substring(($i * $MaximumBlockLength),
[math]::min( $MaximumBlockLength, $reply.Length - ($i * $MaximumBlockLength)))
if ($part.Length -gt 189){
$part = $part.Substring(0, 63) + "." + $part.Substring(63, 63) + "." + $part.Substring(126, 63) + "." + $part.Substring(189)
} else {
if ($part.Length -gt 126){
$part = $part.Substring(0, 63) + "." + $part.Substring(63, 63) + "." + $part.Substring(126)
} else {
if ($part.Length -gt 63){
$part = $part.Substring(0, 63) + "." + $part.Substring(63)
}
}
}
Write-Debug "Sending part $part . Request is $act$seed$id$num$Dot$part$Dot$domain $server"
$text = &nslookup -q=TXT $act$seed$id$num$Dot$part$Dot$domain $server 2>$null | %{Get-DNS-TXT-value -text $_}
Write-Debug "Status $text"
}
Write-Progress -Activity "Reply to DNS" -Completed -Status "Completed"
return
}
}
Function Tun{
Param([string] $domain,
[string] $server = "",
[string] $id,
[string] $key)
Process{
$forwardToHost = ""
$forwardToPort = ""
$tcpConnection = ""
$tcpStream = ""
while($true)
{
$act = $Actions.Request
#Start-Sleep -Milliseconds 500
if ($tcpStream.DataAvailable){
$reply = Get-TCP-Reply -reader $tcpStream
#$reply = Gzip-Compress -byteArray $reply
$reply = [System.Convert]::ToBase64String($reply)
Write-Debug "Reply from TCP in Base64: $reply"
Reply -domain $domain -server $server -id $id -key $key -reply $reply
}
$seed = Random-String -Length 7
$text = &nslookup -q=TXT $act$seed$id$Dot$domain $server 2>$null
$text = Get-DNS-TXT-value -text $text
if (!$text){
Write-Warning "Unable to get DNS info"
continue
}
Write-Debug $text
if ($text.StartsWith($id + "ND")){
Write-Debug "No data, waiting"
continue
}
$text = $text.Split(':')
if (($forwardToHost -ne $text[0].Substring(2)) -or ($forwardToPort -ne $text[1]) -or !$tcpConnection.Connected){
$forwardToHost = $text[0].Substring(2)
$forwardToPort = $text[1]
$tcpConnection = Connect-TCP -server $forwardToHost -port $forwardToPort
if (!$tcpConnection.Connected) {
Write-Warning "Cannot establish TCP connection"
continue
}
$tcpStream = $tcpConnection.GetStream()
}
Write-Debug "Request came: $text[2]"
$request = [System.Convert]::FromBase64String($text[2])
#$request = Gzip-Decompress -byteArray $request
Send-TCP-Request -writer $tcpStream -request $request
}
}
}
#-----------------------------------------------------------[Execution]------------------------------------------------------------
Write-Host ' ________________ _________ _______________ _________' -ForegroundColor Yellow -BackgroundColor Black
Write-Host ' ___/__ __/__/ /_____ ______________/ /______________/ __ \__/ | / //_ ___/' -ForegroundColor Yellow -BackgroundColor Black
Write-Host ' _____/ /____/ __ \ / / /_/ __ \/ __ /_/ _ \_/ __/ _/ / / /_/ |/ //____ \ ' -ForegroundColor Yellow -BackgroundColor Black
Write-Host ' ____/ /____/ / / / /_/ /_ / / / /_/ / / __// / __/ /_/ /_/ /| / ____/ / ' -ForegroundColor Yellow -BackgroundColor Black
Write-Host ' ___/_/____/_/ /_/\__,_/ /_/ /_/\__,_/__\___//_/ __/_____/ /_/ |_/ /_____/ ' -ForegroundColor Yellow -BackgroundColor Black
Write-Host ''
Write-Host
Write-Host
if (!$domain -or !$clientname){
Write-Host "Usage: ./client.ps1 -domain <DOMAIN> -clientname <CLIENTNAME> [-server <DN_SERVER>]" -ForegroundColor Yellow -BackgroundColor Black
exit 1
}
Write-Host "DNSTunnel v$sScriptVersion was started" -ForegroundColor Yellow -BackgroundColor Black
Write-Host "Domain to forward: $domain" -ForegroundColor Cyan -BackgroundColor Black
Write-Host "DN Server: $server" -ForegroundColor Cyan -BackgroundColor Black
Write-Host "Client name: $clientname" -ForegroundColor Cyan -BackgroundColor Black
$reg = Register -domain $domain -server $server -clientname $clientname
Write-Host "Client was registered: ID=" -ForegroundColor DarkGreen -BackgroundColor Black -NoNewline
Write-Host $reg.Id -ForegroundColor DarkGreen -BackgroundColor Black -NoNewline
Write-Host " KEY=" -ForegroundColor DarkGreen -BackgroundColor Black -NoNewline
Write-Host $reg.Key -ForegroundColor DarkGreen -BackgroundColor Black
try
{
Tun -domain $domain -server $server -id $reg.Id -key $reg.Key
}
finally
{
$id = $reg.Id
write-host "Deleting user " $id
Del-user -domain $domain -server $server -id $id
}