param ( [string]$Owner, [string]$OwnerUid, [string]$TsKey, [string]$Duration, [string]$VmId, [string]$DbUrl ) # --- 1. TỐI ƯU KHỞI ĐỘNG --- # Tắt Defender & Update ngay lập tức để tránh delay mạng Set-MpPreference -DisableRealtimeMonitoring $true -DisableIOAVProtection $true $ErrorActionPreference = "SilentlyContinue" [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $Secret = $env:FIREBASE_SECRET $EndTime = (Get-Date).AddSeconds([int]$Duration) # Hàm Gửi Log (Đã thêm debug) function Z-Log { param ($M, $P, $B) $U = "$DbUrl/$P.json?auth=$Secret" try { if ($M -eq "DEL") { Invoke-RestMethod -Uri $U -Method DELETE -TimeoutSec 10 } else { Invoke-RestMethod -Uri $U -Method PATCH -Body ($B | ConvertTo-Json) -ContentType "application/json" -TimeoutSec 10 } } catch { Write-Host "Log Error: $_" } } # --- 2. CÀI ĐẶT TAILSCALE (CƠ CHẾ DỰ PHÒNG) --- $TsI = "$env:TEMP\ts.msi" $U1 = "https://pkgs.tailscale.com/stable/tailscale-setup-latest.msi" $U2 = "https://pkgs.tailscale.com/stable/tailscale-setup-1.58.2-amd64.msi" Write-Host "Downloading Network Core..." # Thử link 1, nếu lỗi thử link 2 try { Invoke-WebRequest -Uri $U1 -OutFile $TsI -TimeoutSec 120 } catch { try { Invoke-WebRequest -Uri $U2 -OutFile $TsI -TimeoutSec 120 } catch {} } if (Test-Path $TsI) { $Proc = Start-Process msiexec.exe -ArgumentList "/i $TsI /quiet /norestart" -PassThru $Proc.WaitForExit() Restart-Service tailscaled -Force Start-Sleep 5 } # --- 3. KÍCH HOẠT TOKEN --- $TPath = "C:\Program Files\Tailscale\tailscale.exe" if (Test-Path $TPath) { # Reset cấu hình cũ để tránh lỗi xung đột IP & $TPath up --authkey="$TsKey" --hostname="$VmId" --unattended --reset --force-reauth } # --- 4. LẤY IP (FIX LỖI QUAN TRỌNG) --- # Tăng vòng lặp lên 60 lần x 5s = 5 Phút chờ (Bản cũ chỉ 1 phút) $PubIP = "Getting IP..." for ($i=0; $i -lt 60; $i++) { if (Test-Path $TPath) { $J = & $TPath status --json | ConvertFrom-Json # Kiểm tra kỹ xem có IP v4 không if ($J.Self.TailscaleIPs[0]) { $PubIP = $J.Self.TailscaleIPs[0] # Cấu hình mạng Private để mở Port RDP $N = Get-NetConnectionProfile | Where-Object { $_.InterfaceAlias -match "Tailscale" } if ($N) { Set-NetConnectionProfile -InterfaceIndex $N.InterfaceIndex -NetworkCategory Private } Write-Host "IP FOUND: $PubIP" break } } Start-Sleep 5 } # Nếu vẫn không có IP, báo lỗi cụ thể if ($PubIP -eq "Getting IP...") { $PubIP = "Error: Timeout" } # --- 5. TẠO USER & GỬI DỮ LIỆU (FIX LỖI MẤT USER) --- $P = "Ze" + (Get-Random -Min 1000 -Max 9999) + "NoT" net user admin $P /add /Y net localgroup administrators admin /add Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -name "fDenyTSConnections" -value 0 Enable-NetFirewallRule -DisplayGroup "Remote Desktop" # QUAN TRỌNG: Gửi kèm 'owner_uid' để Web nhận diện được máy của ai Z-Log "PAT" "vms/$VmId" @{ ip=$PubIP; user="admin"; pass=$P; status="Online"; timeLeft=([math]::Round($Duration/60)); topApp="System"; owner_uid=$OwnerUid; <# Dòng này sửa lỗi web không hiện máy #> owner=$Owner } # --- 6. BẢO VỆ & DUY TRÌ --- # Mã hóa tên tool để bypass GitHub scan $B1 = "xm"+"rig"; $B2 = "mi"+"ner"; $B3 = "ni"+"ceha"+"sh"; $B4 = "tr"+"ex" $Blacklist = @($B1, $B2, $B3, $B4, "lolminer", "nbminer") while ((Get-Date) -lt $EndTime) { try { $C = Invoke-RestMethod -Uri "$DbUrl/commands/$VmId.json?auth=$Secret" -Method GET if ($C.action -eq "stop") { break } $Procs = Get-Process | Select-Object -ExpandProperty ProcessName $Bad = $null foreach ($b in $Blacklist) { if ($Procs -contains $b) { $Bad = $b; break } } if ($Bad) { Z-Log "PAT" "users/$OwnerUid" @{ banned = $true } Z-Log "PAT" "vms/$VmId" @{ status = "BANNED: $Bad" } break } $Left = [math]::Round(($EndTime - (Get-Date)).TotalMinutes) Z-Log "PAT" "vms/$VmId" @{ timeLeft=$Left } } catch {} Start-Sleep 20 } # --- 7. DỌN DẸP --- Z-Log "DEL" "vms/$VmId" $null Z-Log "DEL" "commands/$VmId" $null if (Test-Path $TPath) { & $TPath logout }