-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathInvoke-SocksProxy.psm1
281 lines (252 loc) · 8.32 KB
/
Invoke-SocksProxy.psm1
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
<#
.SYNOPSIS
Powershell Socks5 Proxy
Author: p3nt4 (https://twitter.com/xP3nt4)
License: MIT
.DESCRIPTION
Creates a Socks proxy using powershell.
It only supports Socks5 at the moment although socks4 will be easy to implement. (Windows system proxy is Socks4).
This is only a subset of the full Socks5 protocol: It does not support authentication, It does not support UDP or bind requests.
New features will be implemented in the future. PR are welcome.
.EXAMPLE
Create a Socks5 proxy on port 1234:
Invoke-SocksProxy -bindPort 1234
Create a simple tcp port forward:
Invoke-PortFwd -bindPort 33389 -destHost 127.0.0.1 -destPort 3389
#>
[ScriptBlock]$TcpConnectionMgr = {
param($vars)
$srvConnection=$vars.srvConnection
$cliConnection=$vars.cliConnection
$Script = {
param($vars)
$vars.inStream.CopyToAsync($vars.outStream)
}
$cliStream = $cliConnection.GetStream()
$srvStream = $srvConnection.GetStream()
$vars2 = [PSCustomObject]@{"inStream"=$cliStream;"outStream"=$srvStream}
$PS = [PowerShell]::Create()
$PS.AddScript($Script).AddArgument($vars2) | Out-Null
[System.IAsyncResult]$AsyncJobResult = $null
$vars3 = [PSCustomObject]@{"inStream"=$srvStream;"outStream"=$cliStream}
$PS2 = [PowerShell]::Create()
$PS2.AddScript($Script).AddArgument($vars3) | Out-Null
[System.IAsyncResult]$AsyncJobResult2 = $null
try
{
$AsyncJobResult = $PS.BeginInvoke()
$AsyncJobResult2 = $PS2.BeginInvoke()
while($cliConnection.Connected -and $srvConnection.Connected){
sleep -m 100;
}
}
catch {
}
finally {
if ($cliConnection -ne $null) {
$cliConnection.Close()
$cliConnection.Dispose()
$cliConnection = $null
}
if ($srvConnection -ne $null) {
$srvConnection.Close()
$srvConnection.Dispose()
$srvConnection = $null
}
if ($PS -ne $null -and $AsyncJobResult -ne $null) {
$PS.EndInvoke($AsyncJobResult) | Out-Null
$PS.Dispose()
}
if ($PS2 -ne $null -and $AsyncJobResult2 -ne $null) {
$PS2.EndInvoke($AsyncJobResult2) | Out-Null
$PS2.Dispose()
}
}
}
[ScriptBlock]$SocksConnectionMgr = {
param($vars)
function Get-IpAddress{
param($ip)
IF ($ip -as [ipaddress]){
return $ip
}else{
$ip2 = [System.Net.Dns]::GetHostAddresses($ip)[0].IPAddressToString;
Write-Host "$ip resolved to $ip2"
}
return $ip2
}
$client=$vars.cliConnection
$TcpConnectionMgr=$vars.TcpConnectionMgr
$buffer = New-Object System.Byte[] 32
try
{
$cliStream = $client.GetStream()
$cliStream.Read($buffer,0,2) | Out-Null
$cliStream.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
$cliStream.Write($buffer,0,2)
}else{
$buffer[1]=0
$cliStream.Write($buffer,0,2)
}
$cliStream.Read($buffer,0,4) | Out-Null
$cmd = $buffer[1]
$atyp = $buffer[3]
if($cmd -ne 1){
$buffer[1] = 7
$cliStream.Write($buffer,0,2)
$client.Close()
$client.Dispose()
throw "Not a connect"
}
if($atyp -eq 1){
$ipv4 = New-Object System.Byte[] 4
$cliStream.Read($ipv4,0,4) | Out-Null
$ipAddress = [System.Net.IPAddress]::new($ipv4)
$hostName = $ipAddress.ToString()
}elseif($atyp -eq 3){
$cliStream.Read($buffer,4,1) | Out-Null
$hostBuff = New-Object System.Byte[] $buffer[4]
$cliStream.Read($hostBuff,0,$buffer[4]) | Out-Null
$hostName = [System.Text.Encoding]::ASCII.GetString($hostBuff)
}
else{
$buffer[1] = 8
$cliStream.Write($buffer,0,2)
$client.Close()
$client.Dispose()
throw "Not a valid destination address"
}
$cliStream.Read($buffer,4,2) | Out-Null
$destPort = $buffer[4]*256 + $buffer[5]
$destHost = Get-IpAddress($hostName)
if($destHost -eq $null){
$buffer[1]=4
$cliStream.Write($buffer,0,2)
$client.Close()
$client.Dispose()
throw "Cant resolve destination address"
}
$tmpServ = New-Object System.Net.Sockets.TcpClient($destHost, $destPort)
if($tmpServ.Connected){
$buffer[1]=0
$buffer[3]=1
$buffer[4]=0
$buffer[5]=0
$cliStream.Write($buffer,0,10)
$cliStream.Flush()
$vars = [PSCustomObject]@{"cliConnection"=$client;"srvConnection"= $tmpServ}
$PS3 = [PowerShell]::Create()
$PS3.AddScript($TcpConnectionMgr).AddArgument($vars) | Out-Null
[System.IAsyncResult]$AsyncJobResult3 = $null
$AsyncJobResult3 = $PS3.BeginInvoke()
while($client.Connected -and $tmpServ.Connected){
sleep -m 100;
}
}
else{
$buffer[1]=4
$cliStream.Write($buffer,0,2)
$client.Close()
$client.Dispose()
throw "Cant connect to host"
}
}
catch {
}
finally {
if ($client -ne $null) {
$client.Close()
$client.Dispose()
$client = $null
}
if ($PS3 -ne $null -and $AsyncJobResult3 -ne $null) {
$PS3.EndInvoke($AsyncJobResult3) | Out-Null
$PS3.Dispose()
}
}
}
function Invoke-SocksProxy{
param (
[String]$bindIP = "0.0.0.0",
[Int]$bindPort
)
try{
$listener = new-object System.Net.Sockets.TcpListener([System.Net.IPAddress]::Parse($bindIP), $bindPort)
$listener.start()
write-host "Listening on port $bindPort..."
while($true){
$client = $listener.AcceptTcpClient()
Write-Host "New Connection from " $client.Client.RemoteEndPoint
$vars = [PSCustomObject]@{"cliConnection"=$client;"TcpConnectionMgr"=$TcpConnectionMgr}
$PS3 = [PowerShell]::Create()
$PS3.AddScript($SocksConnectionMgr).AddArgument($vars) | Out-Null
[System.IAsyncResult]$AsyncJobResult3 = $null
$AsyncJobResult3 = $PS3.BeginInvoke()
}
}
catch{
write-host $_.Exception.Message
}
finally{
write-host "Server closed."
if ($listener -ne $null) {
$listener.Stop()
}
if ($client -ne $null) {
$client.Close()
$client.Dispose()
$client = $null
}
if ($PS3 -ne $null -and $AsyncJobResult3 -ne $null) {
$PS3.EndInvoke($AsyncJobResult3) | Out-Null
$PS3.Dispose()
}
}
}
function Invoke-PortFwd{
param (
[String]$destHost,
[Int]$destPort,
[String]$bindIP = "0.0.0.0",
[Int]$bindPort
)
$destIp = Get-IpAddress $destHost
$listener = new-object System.Net.Sockets.TcpListener([System.Net.IPAddress]::Parse($bindIP), $bindPort)
$listener.start()
write-host "Listening on port $bindPort..."
while($true){
$client = $listener.AcceptTcpClient()
Write-Host "New Connection"
$serv = New-Object System.Net.Sockets.TcpClient($destIp, $destPort)
$vars = [PSCustomObject]@{"cliConnection"=$client;"srvConnection"= $serv}
$PS3 = [PowerShell]::Create()
$PS3.AddScript($TcpConnectionMgr).AddArgument($vars) | Out-Null
[System.IAsyncResult]$AsyncJobResult3 = $null
$AsyncJobResult3 = $PS3.BeginInvoke()
}
if ($listener -ne $null) {
$listener.Stop()
}
if ($PS3 -ne $null -and $AsyncJobResult3 -ne $null) {
$PS3.EndInvoke($AsyncJobResult3) | Out-Null
$PS3.Dispose()
}
write-host "Connection closed."
}
function Get-IpAddress{
param($ip)
IF ($ip -as [ipaddress]){
return $ip
}else{
$ip2 = [System.Net.Dns]::GetHostAddresses($ip)[0].IPAddressToString;
Write-Host "$ip resolved to $ip2"
}
return $ip2
}
export-modulemember -function Invoke-SocksProxy
export-modulemember -function Invoke-PortFwd