forked from llkat/rsockstun
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpowershell_client.ps1
More file actions
403 lines (337 loc) · 17.4 KB
/
Copy pathpowershell_client.ps1
File metadata and controls
403 lines (337 loc) · 17.4 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
param (
[string]$server = "localhost",
[int]$port = 4888,
[string]$pass = "microsoft"
)
$buffer = New-Object System.Byte[] 2048
[void][byte[]] $ymxbuffer
[void][byte[]] $databuffer
$tmpbuffer = New-Object System.Byte[] 12
$wrnum = 0
$bufsize = 8192
[ScriptBlock]$socksScript = {
#do socks server
param($state)
function Get-IpAddress{
param($ip)
IF ($ip -as [ipaddress]){
return $ip
}else{
$ip2 = [System.Net.Dns]::GetHostAddresses($ip)[0].IPAddressToString;
}
return $ip2
}
$buffer = New-Object System.Byte[] 1500
#"Debug : inside socks thread. StreamID: " | out-file -Append c:\work\log.txt
#$state.StreamID | out-file -Append c:\work\log.txt
try{
#get socks ver
$state.inputStream.Read($buffer,0,2) | Out-Null
$socksVer=$buffer[0]
if ($socksVer -eq 5){
#"Debug : inside socks thread. Socksver = 5 " | out-file -Append c:\work\log.txt
$state.inputStream.Read($buffer,2,$buffer[1]) | Out-Null
for ($i=2; $i -le $buffer[1]+1; $i++) {
if ($buffer[$i] -eq 0) {break}
}
if ($buffer[$i] -ne 0){
$buffer[1]=255
$state.outputStream.Write($buffer,0,2)
}else{
$buffer[1]=0
$state.outputStream.Write($buffer,0,2)
}
$state.inputStream.Read($buffer,0,4) | Out-Null
$cmd = $buffer[1]
$atyp = $buffer[3]
if($cmd -ne 1){
$buffer[1] = 7
$state.outputStream.Write($buffer,0,2)
#"Debug : inside socks thread. Not a connect: " | out-file -Append c:\work\log.txt
$state.outputStream.Write($buffer,0,2)
$StopFlag[$state.StreamID] = 2
throw "Not a connect"
}
if($atyp -eq 1){
$ipv4 = New-Object System.Byte[] 4
$state.inputStream.Read($ipv4,0,4) | Out-Null
$ipAddress = New-Object System.Net.IPAddress(,$ipv4)
$hostName = $ipAddress.ToString()
#"Debug : inside socks thread. ipaddress: $ipaddress, hostname: $hostname " | out-file -Append c:\work\log.txt
}elseif($atyp -eq 3){
$state.inputStream.Read($buffer,4,1) | Out-Null
$hostBuff = New-Object System.Byte[] $buffer[4]
$state.inputStream.Read($hostBuff,0,$buffer[4]) | Out-Null
$hostName = [System.Text.Encoding]::ASCII.GetString($hostBuff)
#"Debug : inside socks thread. hostname: $hostname " | out-file -Append c:\work\log.txt
}
else{
$buffer[1] = 8
$state.outputStream.Write($buffer,0,2)
#"Debug : inside socks thread. Not a valid destination address " | out-file -Append c:\work\log.txt
$buffer[1]=4
$state.outputStream.Write($buffer,0,2)
$StopFlag[$state.StreamID] = 2
throw "Not a valid destination address"
}
$state.inputStream.Read($buffer,4,2) | Out-Null
$destPort = $buffer[4]*256 + $buffer[5]
#"Debug : inside socks thread. Call IP address func. Hostname: $hostname " | out-file -Append c:\work\log.txt
$destHost = Get-IpAddress($hostName)
#"Debug : inside socks thread. Done. DestHost: $DestHost " | out-file -Append c:\work\log.txt
if($destHost -eq $null){
$buffer[1]=4
$state.outputStream.Write($buffer,0,2)
#"Debug : inside socks thread. Cant resolve destination address " | out-file -Append c:\work\log.txt
throw "Cant resolve destination address"
}
#"Debug : inside socks thread. Connecting to... $desthost, $destport " | out-file -Append c:\work\log.txt
$tmpServ = New-Object System.Net.Sockets.TcpClient($destHost, $destPort)
#$tmpServ = New-Object System.Net.Sockets.TcpClient('127.0.0.1', 8889)
if($tmpServ.Connected){
#"Debug : inside socks thread. $state.StreamID connected " | out-file -Append c:\work\log.txt
$buffer[1]=0
$buffer[3]=1
$buffer[4]=0
$buffer[5]=0
$state.outputStream.Write($buffer,0,10)
$state.outputStream.Flush()
$srvStream = $tmpServ.GetStream()
#$AsyncJobResult2 = $srvStream.WriteAsync([byte[]][char[]]"GET / HTTP/1.1`n`n",0,16)
$AsyncJobResult2 = $state.inputStream.CopyToAsync($srvstream)
#"Debug : inside thread $state.StreamID . state.inputStream.CopyToAsync done " | out-file -Append c:\work\log.txt
$AsyncJobResult = $srvStream.CopyToAsync($state.outputStream)
#"Debug : inside thread $state.StreamID . srvStream.CopyToAsync done " | out-file -Append c:\work\log.txt
#$AsyncJobResult.AsyncWaitHandle.WaitOne();
#$AsyncJobResult2.AsyncWaitHandle.WaitOne();
while ($StopFlag[$state.StreamID] -eq 0 -and $tmpServ.Connected ){
#"Debug : inside thread. EndFlag is NOT set " | out-file -Append c:\work\log.txt
#$StopFlag[$state.StreamID] | out-file -Append c:\work\log.txt
start-sleep -Milliseconds 5
}
if ($tmpServ.Connected){
#"Debug : inside thread. finishing by yamux... " | out-file -Append c:\work\log.txt
$tmpServ.close()
}else{
#"Debug : inside thread. finishing by socks... " | out-file -Append c:\work\log.txt
$StopFlag[$state.StreamID] = 2
}
}else{
#"Debug : inside thread. Can not connect " | out-file -Append c:\work\log.txt
$buffer[1]=4
$state.outputStream.Write($buffer,0,2)
$StopFlag[$state.StreamID] = 2
throw "thread extension: can not a connect"
}
}
}
catch{
$StopFlag[$state.StreamID] = 2
throw "thread extension: some error"
}
finaly{
if ($tmpServ -ne $null) {
$tmpServ.Dispose()
}
#"Debug : inside socks thread. Closing thread. StreamID: " | out-file -Append c:\work\log.txt
#$state.StreamID | out-file -Append c:\work\log.txt
Exit;
}
}
[ScriptBlock]$yamuxScript = {
#going walktrough a streams list, quering them and puting a results to yamux with corresponding yamux stream id
param($state)
#"Debug : inside yamux thread. Hello ! " | out-file -Append c:\work\log.txt
while($true){
if ($StopFlag[0] -ge 0){
#got yamux keepalive. we have to reply
$outbuf = [byte[]](0x00,0x02,0x00,0x02,0x00,0x00,0x00,0x00) + [bitconverter]::getbytes([int32]$StopFlag[0])[3..0]
$state.tcpstream.Write($outbuf,0,12)
$state.tcpstream.flush()
$StopFlag[0] = -1
}
#"Debug : inside yamux thread. Hello ! " | out-file -Append c:\work\log.txt
foreach ($stream in $state.streams){
#"Debug : inside yamux thread. working with stream: " | out-file -Append c:\work\log.txt
#$stream.ymxId | out-file -Append c:\work\log.txt
if ($stream.readjob -eq $null){
$stream.readjob = $stream.sinputStream.ReadAsync($stream.readbuffer,0,$state.bufsize)
}elseif ( $stream.readjob.IsCompleted ){
#if read asyncjob completed
#"Debug : inside yamux thread. got data from socks thread " | out-file -Append c:\work\log.txt
#$stream.ymxId | out-file -Append c:\work\log.txt
$outbuf = [byte[]](0x00,0x00,0x00,0x00)+ [bitconverter]::getbytes([int32]$stream.ymxId)[3..0]+ [bitconverter]::getbytes([int32]$stream.readjob.Result)[3..0]
#"Debug : inside yamux thread. writing YMX 12 byte DATA header to tcpstream " | out-file -Append c:\work\log.txt
$state.tcpstream.Write($outbuf,0,12)
#write raw data from socks thread to yamux
#"Debug : inside yamux thread. writing raw DATA header to tcpstream " | out-file -Append c:\work\log.txt
$state.tcpstream.Write($stream.readbuffer,0,$stream.readjob.Result)
$state.tcpstream.flush()
#create new readasync job
$stream.readjob = $stream.sinputStream.ReadAsync($stream.readbuffer,0,$state.bufsize)
}else{
#write-host "Not readed"
}
#$stopflag | out-file -Append c:\work\log.txt
#check StopFlag for FYN from socks
if ($StopFlag[$stream.ymxId] -eq 2){
#"Debug : inside yamux thread. got STOP flag from socks " | out-file -Append c:\work\log.txt
#$stream.ymxId | out-file -Append c:\work\log.txt
$StopFlag[$stream.ymxId] = 3
$outbuf = [byte[]](0x00,0x01,0x00,0x04)+ [bitconverter]::getbytes([int32]$stream.ymxId)[3..0]+ [byte[]](0x00,0x00,0x00,0x00)
$state.tcpstream.Write($outbuf,0,12)
$state.tcpstream.flush()
}
#check RcvBytes for 256K size
if ($RcvBytes[$stream.ymxId] -ge 256144){
#out win update ymx packet with 256K size
$outbuf = [byte[]](0x00,0x01,0x00,0x00)+ [bitconverter]::getbytes([int32]$stream.ymxId)[3..0]+ (0x00,0x04,0x00,0x00)
$state.tcpstream.Write($outbuf,0,12)
$RcvBytes[$stream.ymxId] = 0
}
}
start-sleep -Milliseconds 5
}
}
"Debug : begin" | out-file c:\work\log.txt
[System.Collections.ArrayList]$streams = @{}
$StopFlag = [hashtable]::Synchronized(@{})#Shared array of flags. 0 - normal;1 - got FIN from yamux; 2 - got FIN from socks; 3 - got FIN from socks and FIN packet was sent to yamux
$RcvBytes = [hashtable]::Synchronized(@{})#Shared array of num of received bytes. When its eq or more than 256144 - then we have to send ymx window update packet, otherwise files > 256k will not be transfered by yamux
$StopFlag[0] = -1
$tcpConnection = New-Object System.Net.Sockets.TcpClient($server, $port)
#$tcpStream = $tcpConnection.GetStream() #uncomment for cleartext connection
#comment 2 lines for cleartext connection
$tcpStream = New-Object System.Net.Security.SslStream($tcpConnection.GetStream(),$false,({$True} -as [Net.Security.RemoteCertificateValidationCallback]))
$tcpStream.AuthenticateAsClient('127.0.0.1')
if ($tcpConnection.Connected)
{
write-host "conneccted"
$connected = $true
$tcpstream.Write([byte[]][char[]]$pass,0,$pass.length)
#create runspase for shared arrays StopFlag, RcvBytes
$ymxrunspace = [runspacefactory]::CreateRunspace()
$ymxrunspace.Open()
$ymxrunspace.SessionStateProxy.SetVariable("StopFlag",$StopFlag)
$ymxrunspace.SessionStateProxy.SetVariable("RcvBytes",$RcvBytes)
#start async yamux stript(thread) -
write-host "Debug: staring yamux thread"
$state = [PSCustomObject]@{"streams"=$streams;"tcpStream"=$tcpstream;"bufsize"=$bufsize}
$PS1 = [PowerShell]::Create()
$PS1.Runspace = $ymxrunspace
$PS1.AddScript($yamuxScript).AddArgument($state) | Out-Null
[System.IAsyncResult]$AsyncJobResult = $null
$ymxAsyncJob = $PS1.BeginInvoke()
while($tcpConnection.Connected -and $connected){
$ymxbuffer = $null
$tnum = 0
#read 12 bytes of ymx header; we have to use cycle, because there may be multiple read attempts...
do {
try { $num = $tcpStream.Read($tmpbuffer,0,12) } catch {$connected=$false; break;}
if ($num -eq 0 ) {$connected=$false; break;}
$tnum += $num
$ymxbuffer += $tmpbuffer[0..($num-1)]
}while ($tnum -lt 12 -and $tcpConnection.Connected)
if ($ymxbuffer[1] -eq 1 -and $ymxbuffer[3] -eq 1){
write-host "Debug: got ymx SYN"
$ymxstream = [bitconverter]::ToInt32($ymxbuffer[7..4],0)
#write-host "Yamux stream ID: $ymxstream"
#we do not need send reply for ymx SYN, and we do not send it...
#$outbuf = [byte[]](0x00,0x01,0x00,0x02,$ymxstream[3],$ymxstream[2],$ymxstream[1],$ymxstream[0],0x00,0x00,0x00,0x00)
#$tcpstream.Write($outbuf,0,12)
#create and start thread
#write-host "Debug: creating thread"
#create 2 pipes: IN and OUT. Create 4 streams: Server and Client stream for every pipe
$sipipe = new-object System.IO.Pipes.AnonymousPipeServerStream(1)
$sopipe = new-object System.IO.Pipes.AnonymousPipeServerStream(2,1)
$sipipe_clHandle = $sipipe.GetClientHandleAsString()
$sopipe_clHandle = $sopipe.GetClientHandleAsString()
$cipipe = new-object System.IO.Pipes.AnonymousPipeClientStream(1,$sopipe_clHandle)
$copipe = new-object System.IO.Pipes.AnonymousPipeClientStream(2,$sipipe_clHandle)
$readbuffer = New-Object System.Byte[] $bufsize
$state = [PSCustomObject]@{"StreamID"=$ymxstream;"inputStream"=$cipipe;"outputStream"=$copipe}
$PS = [PowerShell]::Create()
#create runspase for shared variable StopFlag
$socksrunspace = [runspacefactory]::CreateRunspace()
$socksrunspace.Open()
$socksrunspace.SessionStateProxy.SetVariable("StopFlag",$StopFlag)
$PS.Runspace = $socksrunspace
$PS.AddScript($socksScript).AddArgument($state) | Out-Null
[System.IAsyncResult]$AsyncJobResult = $null
$StopFlag[$ymxstream] = 0
$RcvBytes[$ymxstream] = 0
$AsyncJobResult = $PS.BeginInvoke()
#add created streams and PS object to streams list
$streams.add(@{ymxId=$ymxstream;cinputStream=$cipipe;sinputStream=$sipipe;coutputStream=$copipe;soutputStream=$sopipe;asyncobj=$AsyncJobResult;psobj=$PS;readjob=$null;readbuffer=$readbuffer}) | out-null
$readbuffer.clear()
}elseif ($ymxbuffer[1] -eq 2){
#write-host "got ymx keepalive"
$pingval = [bitconverter]::ToInt32($ymxbuffer[11..8],0)
#set $StopFlag[0] to recieved ping value. This instruct yamuxScript to send this value back in ymx keepalive message
$StopFlag[0] = $pingval
}elseif ($ymxbuffer[1] -eq 0) {
#write-host "Debug: got ymx DATA"
$ymxstream = [bitconverter]::ToInt32($ymxbuffer[7..4],0)
$ymxcount = [bitconverter]::ToInt32($ymxbuffer[11..8],0)
#search in streamlist inpsteam and outstream by ymx Id and get streams from streams list
if ($streams.Count -gt 1){$streamind = $streams.ymxId.IndexOf($ymxstream)}
else {$streamind = 0}
$inpStream = $streams[$streamind].sinputStream
$outStream = $streams[$streamind].soutputStream
if($inpStream -and $outStream ){
#read raw data
#write-host "Debug: read raw ymx DATA"
$databuffer = $null
$tnum = 0
do {
if ($buffer.length -le ($ymxcount-$tnum)) { $num = $tcpStream.Read($buffer,0,$buffer.Length) }else
{ $num = $tcpStream.Read($buffer,0,($ymxcount-$tnum)) }
$tnum += $num
$databuffer += $buffer[0..($num-1)]
}while ($tnum -lt $ymxcount -and $tcpConnection.Connected)
#write-host "out " $ymxcount" bytes to outstream at index:" $streamind
$outStream.Write($databuffer,0,$ymxcount)
#update $RcvBytes
$RcvBytes[$ymxstream] += $ymxcount
}else{
write-host "Debug: can't find streams. StreamId: $streamid, inpStream: $inpStream, outstream: $outStream"
}
}elseif ($ymxbuffer[1] -eq 1 -and $ymxbuffer[3] -eq 0) {
write-host "Debug: got ymx win update"
Write-Host $ymxbuffer
}
elseif ($ymxbuffer[1] -eq 1 -and $ymxbuffer[3] -eq 4) {
write-host "Debug: got ymx FIN"
$ymxstream = [bitconverter]::ToInt32($ymxbuffer[7..4],0)
write-host "Yamux stream ID: $ymxstream"
#search in streamlist inpsteam and outstream by ymx Id and get asyncjobresult
if ($streams.Count -gt 1){$streamind = $streams.ymxId.IndexOf($ymxstream)}
else {$streamind = 0}
$AsyncJobResult = $streams[$streamind].asyncobj
$PS = $streams[$streamind].psobj
write-host "Debug: stoping thread"
if ($StopFlag[$ymxstream] -eq 0){
$StopFlag[$ymxstream] = 1
}
start-sleep -milliseconds 200 #wait for thread check flag
#$streams[$streamind].cinputStream.close()
#$streams[$streamind].coutputStream.close()
#$streams[$streamind].sinputStream.close()
#$streams[$streamind].soutputStream.close()
$streams[$streamind].psobj.Runspace.close()
$streams[$streamind].psobj.Dispose()
$streams[$streamind].readbuffer.clear()
write-host "Debug: removing streams from streams list"
$streams.RemoveAt($streamind)
[System.GC]::Collect()#clear garbage to minimize memory usage
}else{
write-host "got something wrong"
$wrnum += 1
if ($wrnum -gt 10) { write-host "too many errors! Exiting..."; break;}
}
}
write-host "Debug: stoping ymx thread"
$PS1.EndStop($PS1.BeginStop($null,$ymxAsyncJob))
$tcpConnection.Close()
}else{
write-host "not connected"
}