Advertisement
Tom_Neverwinter

pwnagatchi

Jan 7th, 2024
1,101
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.2.1'  # Default to 192.168.2.1 for AT&T compatibility
  9. )
  10.  
  11. # Load helper functions
  12. Function Create-HNetObjects {
  13.     <#
  14.     .SYNOPSIS
  15.         A helper function that does the heavy lifting with NetCfg.HNetShare
  16.    
  17.     .DESCRIPTION
  18.         A helper function that does the heavy lifting with NetCfg.HNetShare. This returns a PSObject containing the `INetSharingConfigurationForINetConnection` info of 2 Adapters.
  19.    
  20.     .PARAMETER InternetAdaptor
  21.         The output of Get-NetAdaptor filtered down to the 'main' uplink interface.
  22.    
  23.     .PARAMETER RNDISGadget
  24.         The output of Get-NetAdaptor filtered down to the 'USB Ethernet/RNDIS Gadget' interface.
  25.    
  26.     .EXAMPLE
  27.         PS> $HNetObject = Create-HNetObjects
  28.         PS> $HNetObject
  29.             RNDISIntConfig     InternetIntConfig
  30.             --------------     -----------------
  31.             System.__ComObject System.__ComObject
  32.     #>
  33.     [Cmdletbinding()]
  34.     Param (
  35.         $InternetAdaptor = $(Select-NetAdaptor -Message "Please select your main ethernet adaptor with internet access that will be used for internet sharing."),
  36.         $RNDISGadget = $(Select-NetAdaptor -Message "Please select your 'USB Ethernet/RNDIS Gadget' adaptor")
  37.     )
  38.     Begin {
  39.         regsvr32.exe /s hnetcfg.dll
  40.         $HNetShare = New-Object -ComObject HNetCfg.HNetShare
  41.     }
  42.     Process {
  43.         if ($HNetShare.EnumEveryConnection -ne $null) {
  44.             $InternetInt       = $HNetShare.EnumEveryConnection | Where-Object { $HNetShare.NetConnectionProps.Invoke($_).Name -eq ($InternetAdaptor).Name }
  45.             $InternetIntConfig = $HNetShare.INetSharingConfigurationForINetConnection.Invoke($InternetInt)
  46.             $RNDISInt          = $HNetShare.EnumEveryConnection | Where-Object { $HNetShare.NetConnectionProps.Invoke($_).Name -eq ($RNDISGadget).Name }
  47.             $RNDISIntConfig    = $HNetShare.INetSharingConfigurationForINetConnection.Invoke($RNDISInt)
  48.         }
  49.     }
  50.     End {
  51.         Return $(New-Object -TypeName PSObject -Property @{InternetIntConfig=$InternetIntConfig;RNDISIntConfig=$RNDISIntConfig;})
  52.     }
  53. }
  54.  
  55. Function Enable-InternetConnectionSharing {
  56.     <#
  57.     .SYNOPSIS
  58.         Enables internet connection sharing between the 'main' uplink interface and the 'USB Ethernet/RNDIS Gadget' interface.
  59.    
  60.     .DESCRIPTION
  61.         Enables internet connection sharing between the 'main' uplink interface and the 'USB Ethernet/RNDIS Gadget' interface.
  62.    
  63.     .EXAMPLE
  64.         PS> Enable-InternetConnectionSharing
  65.    
  66.     #>
  67.     [Cmdletbinding()]
  68.     $HNetObject = Create-HNetObjects
  69.     $HNetObject.InternetIntConfig.EnableSharing(0)
  70.     $HNetObject.RNDISIntConfig.EnableSharing(1)
  71.     Write-Output "[x] Enabled Internet Connection Sharing."
  72. }
  73.  
  74. Function Disable-InternetConnectionSharing {
  75.     <#
  76.     .SYNOPSIS
  77.         Disables internet connection sharing between the 'main' uplink interface and the 'USB Ethernet/RNDIS Gadget' interface.
  78.    
  79.     .DESCRIPTION
  80.         Disables internet connection sharing between the 'main' uplink interface and the 'USB Ethernet/RNDIS Gadget' interface.
  81.  
  82.     .EXAMPLE
  83.         PS> Disable-InternetConnectionSharing
  84.    
  85.     #>
  86.     [Cmdletbinding()]
  87.     $HNetObject = $(Create-HNetObjects)
  88.     $HNetObject.InternetIntConfig.DisableSharing()
  89.     $HNetObject.RNDISIntConfig.DisableSharing()
  90.     Write-Output "[x] Disabled Internet Connection Sharing."
  91. }
  92.  
  93. Function Test-PwnagotchiSubnet {
  94.     <#
  95.     .SYNOPSIS
  96.         Tests the registry for the correct ScopeAddress.
  97.    
  98.     .DESCRIPTION
  99.         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
  100.         in the registry.
  101.    
  102.     .EXAMPLE
  103.         PS> Test-PwnagotchiSubnet
  104.         [!] 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.
  105.     #>
  106.     [Cmdletbinding()]
  107.     $RegKeys = Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters -ErrorAction Stop
  108.     If ($RegKeys.ScopeAddress -notmatch '192.168.2.') {  # Updated to check for 192.168.2.x subnet
  109.         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
  110.     }
  111.     If ($RegKeys.ScopeAddressBackup -notmatch '192.168.2.') {
  112.         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
  113.     }
  114. }
  115.  
  116. Function Set-PwnagotchiSubnet {
  117.     <#
  118.     .SYNOPSIS
  119.         Set the registry for the correct ScopeAddress.
  120.    
  121.     .DESCRIPTION
  122.         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
  123.         in the registry. By default it will be changed to 192.168.2.1
  124.    
  125.     .PARAMETER ScopeAddress
  126.         The IP address the USB Gadget interface should use.
  127.    
  128.     .EXAMPLE
  129.     Set-PwnagotchiSubnet
  130.    
  131.     #>
  132.     [Cmdletbinding()]
  133.     Param (
  134.         $ScopeAddress = '192.168.2.1'  # Changed to 192.168.2.1 for AT&T compatibility
  135.     )
  136.     Try {
  137.         [void]([ipaddress]$ScopeAddress)
  138.         [void]([byte[]] $ScopeAddress.split('.'))
  139.     } Catch {
  140.         Write-Error "$ScopeAddress is not a valid IP."
  141.     }
  142.     Try {
  143.         Set-ItemProperty -Name ScopeAddress       -Path "HKLM:\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\" -Value $ScopeAddress -ErrorAction Stop
  144.         Set-ItemProperty -Name ScopeAddressBackup -Path "HKLM:\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\" -Value $ScopeAddress -ErrorAction Stop
  145.         Write-Warning "The Internet Connection Sharing subnet has been updated. A reboot of windows is required !"
  146.     } Catch {
  147.         $PSCmdlet.ThrowTerminatingError($PSItem)
  148.     }
  149.  
  150. }
  151.  
  152. # Main Function
  153. Function Setup-PwnagotchiNetwork {
  154.     <#
  155.     .SYNOPSIS
  156.         Function to setup networking.
  157.    
  158.     .DESCRIPTION
  159.         Function to setup networking. Main function calls helpers functions.
  160.    
  161.     .PARAMETER EnableInternetConnectionSharing
  162.         Enable Internet Connection Sharing
  163.    
  164.     .PARAMETER DisableInternetConnectionSharing
  165.         Disable Internet Connection Sharing
  166.    
  167.     .PARAMETER SetPwnagotchiSubnet
  168.         Change the Internet Connection Sharing subnet to the Pwnagotchi. Defaults to 10.0.0.1.
  169.    
  170.     .PARAMETER ScopeAddress
  171.         Custom ScopeAddress (the ICS ip address)
  172.    
  173.     .EXAMPLE
  174.         PS> Setup-PwnagotchiNetwork -EnableInternetConnectionSharing
  175.    
  176.     #>
  177.    
  178.     Param (
  179.         [switch]$EnableInternetConnectionSharing,
  180.         [switch]$DisableInternetConnectionSharing,
  181.         [switch]$SetPwnagotchiSubnet,
  182.         $ScopeAddress = '10.0.0.1'
  183.     )
  184.     Begin {
  185.         Try {
  186.             Write-Debug "Begin"
  187.             $ErrorSplat=@{ErrorAction="stop"}
  188.             Write-Debug "Testing subnet"
  189.             Try {
  190.                 Test-PwnagotchiSubnet @ErrorSplat
  191.             } Catch {
  192.                 If ($SetPwnagotchiSubnet) {
  193.                     Write-Debug "Setting subnet"
  194.                     Set-PwnagotchiSubnet -ScopeAddress $ScopeAddress @ErrorSplat
  195.                 } Else {
  196.                     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
  197.                 }
  198.             }
  199.         } Catch {
  200.             $PSCmdlet.ThrowTerminatingError($PSItem)
  201.         }
  202.     }
  203.     Process {
  204.         Write-Debug "Process"
  205.         Try {
  206.             If ($EnableInternetConnectionSharing) {
  207.                 Write-Debug "Enable network Sharing"
  208.                 Enable-InternetConnectionSharing @ErrorSplat
  209.             } ElseIf ($DisableInternetConnectionSharing) {
  210.                 Write-Debug "Disable network Sharing"
  211.                 Disable-InternetConnectionSharing @ErrorSplat
  212.             }
  213.         } Catch {
  214.             $PSCmdlet.ThrowTerminatingError($PSItem)
  215.         }
  216.     }
  217.     End {
  218.         Write-Debug "End"
  219.         Try {
  220.             # Nothing to return.
  221.         } Catch {
  222.             $PSCmdlet.ThrowTerminatingError($PSItem)
  223.         }
  224.     }
  225. }
  226. Function Select-NetAdaptor {
  227.     <#
  228.     .SYNOPSIS
  229.         A menu function to select the correct network adaptors.
  230.    
  231.     .DESCRIPTION
  232.         A menu function to select the correct network adaptors.
  233.    
  234.     .PARAMETER Message
  235.         Message that will be displayed during the question.
  236.    
  237.     #>
  238.    
  239.     Param (
  240.         $Message
  241.     )
  242.     $Adaptors = Get-NetAdapter | Where-Object {$_.MediaConnectionState -eq 'Connected'} | Sort-Object LinkSpeed -Descending
  243.     do {
  244.         Write-Host $Message
  245.         $index = 1
  246.         foreach ($Adaptor in $Adaptors) {
  247.             Write-Host "[$index] $($Adaptor.Name), $($Adaptor.InterfaceDescription)"
  248.             $index++
  249.         }  
  250.         $Selection = Read-Host "Number"
  251.     } until ($Adaptors[$selection-1])
  252.      Return $Adaptors[$selection-1]
  253. }
  254. # Dynamically create params for Setup-PwnagotchiNetwork function based of param input of script.
  255. Setup-PwnagotchiNetwork @psBoundParameters
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement