-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
win_connection_share.ps1
290 lines (248 loc) · 10.9 KB
/
win_connection_share.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
<#
.SYNOPSIS
A script that setups Internet Connection Sharing for Pwnagotchi.
.DESCRIPTION
A script that setups Internet Connection Sharing for Pwnagotchi.
Note: Internet Connection Sharing on Windows can be a bit unstable on between reboots.
You might need to run this script occasionally to disable and re-enable Internet Connection Sharing.
.PARAMETER EnableInternetConnectionSharing
Enable Internet Connection Sharing
.PARAMETER DisableInternetConnectionSharing
Disable Internet Connection Sharing
.PARAMETER SetPwnagotchiSubnet
Change the Internet Connection Sharing subnet to the Pwnagotchi subnet. The USB Gadget Interface IP will default to 10.0.0.1.
.PARAMETER ScopeAddress
Custom ScopeAddress (The IP Address of the USB Gadget Interface.)
.EXAMPLE
# Enable Internet Connection Sharing
PS C:\> .\win_connection_share -EnableInternetConnectionSharing
.EXAMPLE
# Disable Internet Connection Sharing
PS C:\> .\win_connection_share -DisableInternetConnectionSharing
.EXAMPLE
# Change the regkeys of Internet Connection Sharing to the Pwnagotchi Subnet
PS C:\> .\win_connection_share -SetPwnagotchiSubnet
.EXAMPLE
# Change the regkeys of Internet Connection Sharing to the Pwnagotchi Subnet with a custom ScopeAddress (The IP Address of the USB Gadget Interface.)
PS C:\> .\win_connection_share -SetPwnagotchiSubnet -ScopeAddress 10.0.0.10
#>
#Requires -Version 5
#Requires -RunAsAdministrator
[Cmdletbinding()]
Param (
[switch]$EnableInternetConnectionSharing,
[switch]$DisableInternetConnectionSharing,
[switch]$SetPwnagotchiSubnet,
[ipaddress]$ScopeAddress = '10.0.0.1'
)
# Load helper functions
Function Create-HNetObjects {
<#
.SYNOPSIS
A helper function that does the heavy lifting with NetCfg.HNetShare
.DESCRIPTION
A helper function that does the heavy lifting with NetCfg.HNetShare. This returns a PSObject containing the `INetSharingConfigurationForINetConnection` info of 2 Adapters.
.PARAMETER InternetAdaptor
The output of Get-NetAdaptor filtered down to the 'main' uplink interface.
.PARAMETER RNDISGadget
The output of Get-NetAdaptor filtered down to the 'USB Ethernet/RNDIS Gadget' interface.
.EXAMPLE
PS> $HNetObject = Create-HNetObjects
PS> $HNetObject
RNDISIntConfig InternetIntConfig
-------------- -----------------
System.__ComObject System.__ComObject
#>
[Cmdletbinding()]
Param (
$InternetAdaptor = $(Select-NetAdaptor -Message "Please select your main a ethernet adaptor with internet access that will be used for internet sharing."),
$RNDISGadget = $(Select-NetAdaptor -Message "Please select your 'USB Ethernet/RNDIS Gadget' adaptor")
)
Begin {
regsvr32.exe /s hnetcfg.dll
$HNetShare = New-Object -ComObject HNetCfg.HNetShare
}
Process {
if ($HNetShare.EnumEveryConnection -ne $null) {
$InternetInt = $HNetShare.EnumEveryConnection | Where-Object { $HNetShare.NetConnectionProps.Invoke($_).Name -eq ($InternetAdaptor).Name }
$InternetIntConfig = $HNetShare.INetSharingConfigurationForINetConnection.Invoke($InternetInt)
$RNDISInt = $HNetShare.EnumEveryConnection | Where-Object { $HNetShare.NetConnectionProps.Invoke($_).Name -eq ($RNDISGadget).Name }
$RNDISIntConfig = $HNetShare.INetSharingConfigurationForINetConnection.Invoke($RNDISInt)
}
}
End {
Return $(New-Object -TypeName PSObject -Property @{InternetIntConfig=$InternetIntConfig;RNDISIntConfig=$RNDISIntConfig;})
}
}
Function Enable-InternetConnectionSharing {
<#
.SYNOPSIS
Enables internet connection sharing between the 'main' uplink interface and the 'USB Ethernet/RNDIS Gadget' interface.
.DESCRIPTION
Enables internet connection sharing between the 'main' uplink interface and the 'USB Ethernet/RNDIS Gadget' interface.
.EXAMPLE
PS> Enable-InternetConnectionSharing
#>
[Cmdletbinding()]
$HNetObject = Create-HNetObjects
$HNetObject.InternetIntConfig.EnableSharing(0)
$HNetObject.RNDISIntConfig.EnableSharing(1)
Write-Output "[x] Enabled Internet Connection Sharing."
}
Function Disable-InternetConnectionSharing {
<#
.SYNOPSIS
Disables internet connection sharing between the 'main' uplink interface and the 'USB Ethernet/RNDIS Gadget' interface.
.DESCRIPTION
Disables internet connection sharing between the 'main' uplink interface and the 'USB Ethernet/RNDIS Gadget' interface.
.EXAMPLE
PS> Disable-InternetConnectionSharing
#>
[Cmdletbinding()]
$HNetObject = $(Create-HNetObjects)
$HNetObject.InternetIntConfig.DisableSharing()
$HNetObject.RNDISIntConfig.DisableSharing()
Write-Output "[x] Disabled Internet Connection Sharing."
}
Function Test-PwnagotchiSubnet {
<#
.SYNOPSIS
Tests the registry for the correct ScopeAddress.
.DESCRIPTION
Tests the registry for the correct ScopeAddress. By default windows uses a 192.168.137.x subnet for Internet Connection Sharing. This value can be changed
in the registry.
.EXAMPLE
PS> Test-PwnagotchiSubnet
[!] By default Internet Connection Sharing uses a 192.168.137.x subnet. Run Set-PwnagotchiSubnet to ensure you and your little friend are on the same subnet.
#>
[Cmdletbinding()]
$RegKeys = Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters -ErrorAction Stop
If ($RegKeys.ScopeAddress -notmatch '10.0.0.') {
Write-Error "By default Internet Connection Sharing uses a 192.168.137.x subnet. Run Set-PwnagotchiSubnet to ensure you and your little friend are on the same subnet." -ErrorAction Stop
}
If ($RegKeys.ScopeAddressBackup -notmatch '10.0.0.') {
Write-Error "By default Internet Connection Sharing uses a 192.168.137.x subnet. Run Set-PwnagotchiSubnet to ensure you and your little friend are on the same subnet." -ErrorAction Stop
}
}
Function Set-PwnagotchiSubnet {
<#
.SYNOPSIS
Set the registry for the correct ScopeAddress.
.DESCRIPTION
Set the registry for the correct ScopeAddress. By default windows uses a 192.168.137.x subnet for Internet Connection Sharing. This value can be changed
in the registry. By default it will be changed to 10.0.0.1
.PARAMETER ScopeAddress
The IP address the USB Gadget interface should use.
.EXAMPLE
Set-PwnagotchiSubnet
#>
[Cmdletbinding()]
Param (
$ScopeAddress = '10.0.0.1'
)
Try {
[void]([ipaddress]$ScopeAddress)
[void]([byte[]] $ScopeAddress.split('.'))
} Catch {
Write-Error "$ScopeAddress is not a valid IP."
}
Try {
Set-ItemProperty -Name ScopeAddress -Path "HKLM:\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\" -Value $ScopeAddress -ErrorAction Stop
Set-ItemProperty -Name ScopeAddressBackup -Path "HKLM:\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\" -Value $ScopeAddress -ErrorAction Stop
Write-Warning "The Internet Connection Sharing subnet has been updated. A reboot of windows is required !"
} Catch {
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
# Main Function
Function Setup-PwnagotchiNetwork {
<#
.SYNOPSIS
Function to setup networking.
.DESCRIPTION
Function to setup networking. Main function calls helpers functions.
.PARAMETER EnableInternetConnectionSharing
Enable Internet Connection Sharing
.PARAMETER DisableInternetConnectionSharing
Disable Internet Connection Sharing
.PARAMETER SetPwnagotchiSubnet
Change the Internet Connection Sharing subnet to the Pwnagotchi. Defaults to 10.0.0.1.
.PARAMETER ScopeAddress
Custom ScopeAddress (the ICS ip address)
.EXAMPLE
PS> Setup-PwnagotchiNetwork -EnableInternetConnectionSharing
#>
Param (
[switch]$EnableInternetConnectionSharing,
[switch]$DisableInternetConnectionSharing,
[switch]$SetPwnagotchiSubnet,
$ScopeAddress = '10.0.0.1'
)
Begin {
Try {
Write-Debug "Begin"
$ErrorSplat=@{ErrorAction="stop"}
Write-Debug "Testing subnet"
Try {
Test-PwnagotchiSubnet @ErrorSplat
} Catch {
If ($SetPwnagotchiSubnet) {
Write-Debug "Setting subnet"
Set-PwnagotchiSubnet -ScopeAddress $ScopeAddress @ErrorSplat
} Else {
Write-Error "By default Internet Connection Sharing uses a 192.168.137.x subnet. Run this script with the -SetPwnagotchiSubnet to setup the network." -ErrorAction Stop
}
}
} Catch {
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
Process {
Write-Debug "Process"
Try {
If ($EnableInternetConnectionSharing) {
Write-Debug "Enable network Sharing"
Enable-InternetConnectionSharing @ErrorSplat
} ElseIf ($DisableInternetConnectionSharing) {
Write-Debug "Disable network Sharing"
Disable-InternetConnectionSharing @ErrorSplat
}
} Catch {
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
End {
Write-Debug "End"
Try {
# Nothing to return.
} Catch {
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
}
Function Select-NetAdaptor {
<#
.SYNOPSIS
A menu function to select the correct network adaptors.
.DESCRIPTION
A menu function to select the correct network adaptors.
.PARAMETER Message
Message that will be displayed during the question.
#>
Param (
$Message
)
$Adaptors = Get-NetAdapter | Where-Object {$_.MediaConnectionState -eq 'Connected'} | Sort-Object LinkSpeed -Descending
do {
Write-Host $Message
$index = 1
foreach ($Adaptor in $Adaptors) {
Write-Host "[$index] $($Adaptor.Name), $($Adaptor.InterfaceDescription)"
$index++
}
$Selection = Read-Host "Number"
} until ($Adaptors[$selection-1])
Return $Adaptors[$selection-1]
}
# Dynamically create params for Setup-PwnagotchiNetwork function based of param input of script.
Setup-PwnagotchiNetwork @psBoundParameters