forked from bidhata/powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh_tunnel.ps1
More file actions
102 lines (81 loc) · 3.55 KB
/
Copy pathssh_tunnel.ps1
File metadata and controls
102 lines (81 loc) · 3.55 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
Param (
[String]$bindIP = "127.0.0.1",
[Int]$bindPort = 31337,
[String]$destHost = $( Read-Host "IP or Hostname: " ),
[Int]$destPort = $( Read-Host "Port: " )
)
$clientBuffer = new-object System.Byte[] 20480
$request = [System.Net.HttpWebRequest]::Create("http://" + $destHost + ":" + $destPort )
$request.Method = "CONNECT"
$proxy = [System.Net.WebRequest]::GetSystemWebProxy()
$proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$request.Proxy = $proxy
$listener = new-object System.Net.Sockets.TcpListener([System.Net.IPAddress]::Parse($bindIP), $bindPort)
$Script = {
param($state)
$serverBuffer = new-object System.Byte[] 20480
$count = 0
do {
$count = $state.serverStream.Read($serverBuffer, 0 ,$serverBuffer.length)
$state.clientStream.Write($serverBuffer, 0 , $count)
$state.clientStream.Flush()
} while ($count -gt 0)
}
$listener.start()
write-host "Waiting for a connection on port $bindPort..."
$client = $listener.AcceptTcpClient()
write-host "Connected from $($client.Client.RemoteEndPoint)"
#----------------------------------------------------------------------------------------------------
# Get the client side stream object to read/write to
$clientStream = $client.GetStream() # This is a System.Net.Sockets.NetworkStream
#----------------------------------------------------------------------------------------------------
# Get the server side response and corresponding stream object to read/write to
$serverResponse = $request.GetResponse()
$responseStream = $serverResponse.GetResponseStream()
#----------------------------------------------------------------------------------------------------
# Reflection inspection to retrieve and reuse the underlying networkStream instance
$BindingFlags= [Reflection.BindingFlags] "NonPublic,Instance"
$rsType = $responseStream.GetType()
$connectionProperty = $rsType.GetProperty("Connection", $BindingFlags)
$connection = $connectionProperty.GetValue($responseStream, $null)
$connectionType = $connection.GetType()
$networkStreamProperty = $connectionType.GetProperty("NetworkStream", $BindingFlags)
$serverStream = $networkStreamProperty.GetValue($connection, $null)
# This state object is used to pass various object by reference to the child PowerShell object (thread)
# that is created afterwards
$state = [PSCustomObject]@{"serverStream"=$serverStream;"clientStream"=$clientStream}
# Create a child PowerShell object to run the background Socket receive method.
$PS = [PowerShell]::Create()
$PS.AddScript($Script).AddArgument($state) | Out-Null
[System.IAsyncResult]$AsyncJobResult = $null
try
{
# The receive job is started asynchronously.
$AsyncJobResult = $PS.BeginInvoke()
do {
$bytesReceived = $clientStream.Read($clientBuffer, 0, $clientBuffer.length)
$serverStream.Write($clientBuffer, 0 , $bytesReceived)
#$text = [System.Text.Encoding]::ASCII.GetString($buffer, 0, $bytesReceived)
#Write-Host $text
} while ($client.Connected -or $clientStream.DataAvailable)
}
catch {
$ErrorMessage = $_.Exception.Message
Write-Host $ErrorMessage
}
finally {
# Cleanup the client socket and child PowerShell process.
if ($client -ne $null) {
$client.Close()
$client.Dispose()
$client = $null
}
if ($listener -ne $null) {
$listener.Stop()
}
write-host "Connection closed."
if ($PS -ne $null -and $AsyncJobResult -ne $null) {
$PS.EndInvoke($AsyncJobResult)
$PS.Dispose()
}
}