Advertisement
Tom_Neverwinter

adaptable script tomagatchi

Jan 7th, 2024
1,052
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Requires -Version 5
  2. #Requires -RunAsAdministrator
  3. [Cmdletbinding()]
  4. Param (
  5.     [switch]$EnableInternetConnectionSharing,
  6.     [switch]$DisableInternetConnectionSharing,
  7.     [switch]$SetPwnagotchiSubnet,
  8.     [ipaddress]$ScopeAddress = '192.168.137.1',  # Default value, can be overridden
  9.     [string]$SubnetPrefix = '192.168.137.'        # Default prefix, can be overridden
  10. )
  11.  
  12. # Load helper functions
  13. Function Create-HNetObjects {
  14.     <#
  15.     .SYNOPSIS
  16.         A helper function that does the heavy lifting with NetCfg.HNetShare
  17.     #>
  18.     [Cmdletbinding()]
  19.     Param (
  20.         $InternetAdaptor = $(Select-NetAdaptor -Message "Please select your main ethernet adaptor with internet access that will be used for internet sharing."),
  21.         $RNDISGadget = $(Select-NetAdaptor -Message "Please select your 'USB Ethernet/RNDIS Gadget' adaptor")
  22.     )
  23.     Begin {
  24.         regsvr32.exe /s hnetcfg.dll
  25.         $HNetShare = New-Object -ComObject HNetCfg.HNetShare
  26.     }
  27.     Process {
  28.         if ($HNetShare.EnumEveryConnection -ne $null) {
  29.             $InternetInt       = $HNetShare.EnumEveryConnection | Where-Object { $HNetShare.NetConnectionProps.Invoke($_).Name -eq ($InternetAdaptor).Name }
  30.             $InternetIntConfig = $HNetShare.INetSharingConfigurationForINetConnection.Invoke($InternetInt)
  31.             $RNDISInt          = $HNetShare.EnumEveryConnection | Where-Object { $HNetShare.NetConnectionProps.Invoke($_).Name -eq ($RNDISGadget).Name }
  32.             $RNDISIntConfig    = $HNetShare.INetSharingConfigurationForINetConnection.Invoke($RNDISInt)
  33.         }
  34.     }
  35.     End {
  36.         Return $(New-Object -TypeName PSObject -Property @{InternetIntConfig=$InternetIntConfig;RNDISIntConfig=$RNDISIntConfig;})
  37.     }
  38. }
  39.  
  40. Function Enable-InternetConnectionSharing {
  41.     <#
  42.     .SYNOPSIS
  43.         Enables internet connection sharing between the 'main' uplink interface and the 'USB Ethernet/RNDIS Gadget' interface.
  44.     #>
  45.     [Cmdletbinding()]
  46.     $HNetObject = Create-HNetObjects
  47.     $HNetObject.InternetIntConfig.EnableSharing(0)
  48.     $HNetObject.RNDISIntConfig.EnableSharing(1)
  49.     Write-Output "[x] Enabled Internet Connection Sharing."
  50. }
  51.  
  52. Function Disable-InternetConnectionSharing {
  53.     <#
  54.     .SYNOPSIS
  55.         Disables internet connection sharing between the 'main' uplink interface and the 'USB Ethernet/RNDIS Gadget' interface.
  56.     #>
  57.     [Cmdletbinding()]
  58.     $HNetObject = $(Create-HNetObjects)
  59.     $HNetObject.InternetIntConfig.DisableSharing()
  60.     $HNetObject.RNDISIntConfig.DisableSharing()
  61.     Write-Output "[x] Disabled Internet Connection Sharing."
  62. }
  63.  
  64. Function Test-PwnagotchiSubnet {
  65.     <#
  66.     .SYNOPSIS
  67.         Tests the registry for the correct ScopeAddress.
  68.     #>
  69.     [Cmdletbinding()]
  70.     Try {
  71.         $RegKeys = Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters -ErrorAction Stop
  72.         If ($RegKeys.ScopeAddress -notlike "$SubnetPrefix*") {
  73.             Write-Error "Internet Connection Sharing is not set to the expected subnet. Run Set-PwnagotchiSubnet with the correct parameters." -ErrorAction Stop
  74.         }
  75.     } Catch {
  76.         Write-Error "Error accessing registry to check ICS subnet: $_" -ErrorAction Stop
  77.     }
  78. }
  79.  
  80. Function Set-PwnagotchiSubnet {
  81.     <#
  82.     .SYNOPSIS
  83.         Set the registry for the correct ScopeAddress.
  84.     #>
  85.     [Cmdletbinding()]
  86.     Param (
  87.         [ipaddress]$ScopeAddress
  88.     )
  89.     Try {
  90.         [void]([ipaddress]$ScopeAddress)
  91.         [void]([byte[]] $ScopeAddress.split('.'))
  92.     } Catch {
  93.         Write-Error "$ScopeAddress is not a valid IP."
  94.     }
  95.     Try {
  96.         Set-ItemProperty -Name ScopeAddress       -Path "HKLM:\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\" -Value $ScopeAddress -ErrorAction Stop
  97.         Set-ItemProperty -Name ScopeAddressBackup -Path "HKLM:\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\" -Value $ScopeAddress -ErrorAction Stop
  98.         Write-Warning "The Internet Connection Sharing subnet has been updated. A reboot of windows is required !"
  99.     } Catch {
  100.         $PSCmdlet.ThrowTerminatingError($PSItem)
  101.     }
  102. }
  103.  
  104. # Main Function
  105. Function Setup-PwnagotchiNetwork {
  106.     <#
  107.     .SYNOPSIS
  108.         Function to setup networking.
  109.     #>
  110.     Param (
  111.         [switch]$EnableInternetConnectionSharing,
  112.         [switch]$DisableInternetConnectionSharing,
  113.         [switch]$SetPwnagotchiSubnet,
  114.         $ScopeAddress = '192.168.2.1'  # Default to 192.168.2.1 for AT&T compatibility
  115.     )
  116.     Begin {
  117.         Try {
  118.             Write-Debug "Begin"
  119.             $ErrorSplat=@{ErrorAction="stop"}
  120.             Write-Debug "Testing subnet"
  121.             Try {
  122.                 Test-PwnagotchiSubnet @ErrorSplat
  123.             } Catch {
  124.                 If ($SetPwnagotchiSubnet) {
  125.                     Write-Debug "Setting subnet"
  126.                     Set-PwnagotchiSubnet -ScopeAddress $ScopeAddress @ErrorSplat
  127.                 } Else {
  128.                     Write-Error "Internet Connection Sharing is not set to the expected subnet. Run this script with the -SetPwnagotchiSubnet to setup the network." -ErrorAction Stop
  129.                 }
  130.             }
  131.         } Catch {
  132.             $PSCmdlet.ThrowTerminatingError($PSItem)
  133.         }
  134.     }
  135.     Process {
  136.         Write-Debug "Process"
  137.         Try {
  138.             If ($EnableInternetConnectionSharing) {
  139.                 Write-Debug "Enable network Sharing"
  140.                 Enable-InternetConnectionSharing @ErrorSplat
  141.             } ElseIf ($DisableInternetConnectionSharing) {
  142.                 Write-Debug "Disable network Sharing"
  143.                 Disable-InternetConnectionSharing @ErrorSplat
  144.             }
  145.         } Catch {
  146.             $PSCmdlet.ThrowTerminatingError($PSItem)
  147.         }
  148.     }
  149.     End {
  150.         Write-Debug "End"
  151.         Try {
  152.             # Nothing to return.
  153.         } Catch {
  154.             $PSCmdlet.ThrowTerminatingError($PSItem)
  155.         }
  156.     }
  157. }
  158.  
  159. Function Select-NetAdaptor {
  160.     <#
  161.     .SYNOPSIS
  162.         A menu function to select the correct network adaptors.
  163.     #>
  164.     Param (
  165.         $Message
  166.     )
  167.     $Adaptors = Get-NetAdapter | Where-Object {$_.MediaConnectionState -eq 'Connected'} | Sort-Object LinkSpeed -Descending
  168.     do {
  169.         Write-Host $Message
  170.         $index = 1
  171.         foreach ($Adaptor in $Adaptors) {
  172.             Write-Host "[$index] $($Adaptor.Name), $($Adaptor.InterfaceDescription)"
  173.             $index++
  174.         }  
  175.         $Selection = Read-Host "Number"
  176.     } until ($Adaptors[$selection-1])
  177.      Return $Adaptors[$selection-1]
  178. }
  179.  
  180. # Dynamically create params for Setup-PwnagotchiNetwork function based on script input.
  181. Setup-PwnagotchiNetwork @psBoundParameters
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement