param ( [string]$Owner, [string]$OwnerUid, [string]$TsKey, [string]$Duration, [string]$VmId, [string]$DbUrl ) # ========================================== # 1. KHỞI TẠO & TỐI ƯU HÓA (INIT SYSTEM) # ========================================== # Ép buộc dùng giao thức bảo mật mới nhất để tải file không lỗi [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $ErrorActionPreference = "SilentlyContinue" $ProgressPreference = 'SilentlyContinue' # Tắt Windows Defender ngay lập tức để tăng tốc độ mạng và CPU Write-Host ">>> [INIT] Disabling Defender..." Set-MpPreference -DisableRealtimeMonitoring $true -DisableIOAVProtection $true -DisableScriptScanning $true $Secret = $env:FIREBASE_SECRET $EndTime = (Get-Date).AddSeconds([int]$Duration) # Hàm Gửi Dữ Liệu Về Web (Firebase Sync) function Sync-Web { param ($Path, $Data) $Api = "$DbUrl/$Path.json?auth=$Secret" try { if ($null -eq $Data) { Invoke-RestMethod -Uri $Api -Method DELETE -TimeoutSec 5 } else { Invoke-RestMethod -Uri $Api -Method PATCH -Body ($Data | ConvertTo-Json) -ContentType "application/json" -TimeoutSec 5 } } catch { Write-Host ">>> [SYNC ERROR] $_" } } # ========================================== # 2. CÀI ĐẶT MẠNG (NETWORK CORE) # ========================================== $MsiPath = "$env:TEMP\tailscale.msi" $LinkMain = "https://pkgs.tailscale.com/stable/tailscale-setup-latest.msi" $LinkBack = "https://pkgs.tailscale.com/stable/tailscale-setup-1.58.2-amd64.msi" Write-Host ">>> [NET] Downloading Core..." try { Invoke-WebRequest -Uri $LinkMain -OutFile $MsiPath -TimeoutSec 60 } catch { Invoke-WebRequest -Uri $LinkBack -OutFile $MsiPath -TimeoutSec 60 } if (Test-Path $MsiPath) { # Cài đặt im lặng (Passive Mode) $Install = Start-Process msiexec.exe -ArgumentList "/i $MsiPath /quiet /norestart" -PassThru $Install.WaitForExit() # CHỜ DỊCH VỤ KHỞI ĐỘNG (Fix lỗi không nhận Key) Write-Host ">>> [NET] Waiting for Service..." $RetrySvc = 0 while ((Get-Service "tailscaled").Status -ne "Running" -and $RetrySvc -lt 20) { Start-Service "tailscaled" Start-Sleep 2 $RetrySvc++ } } # ========================================== # 3. KẾT NỐI & LẤY IP (AUTH & DISCOVERY) # ========================================== $TsExe = "C:\Program Files\Tailscale\tailscale.exe" if (Test-Path $TsExe) { Write-Host ">>> [AUTH] Authenticating..." # Thử kết nối 5 lần (Retry Logic) for ($k=1; $k -le 5; $k++) { & $TsExe up --authkey="$TsKey" --hostname="$VmId" --unattended --reset --force-reauth Start-Sleep 3 $Check = & $TsExe status --json | ConvertFrom-Json if ($Check.BackendState -eq "Running") { break } Write-Host ">>> [AUTH] Retry $k..." } } # Lấy IP (Vòng lặp chờ 3 phút) $MyIP = "Init..." for ($i=0; $i -lt 36; $i++) { if (Test-Path $TsExe) { $Info = & $TsExe status --json | ConvertFrom-Json # Kiểm tra kỹ mảng IP có dữ liệu không if ($Info.Self.TailscaleIPs.Count -gt 0) { $MyIP = $Info.Self.TailscaleIPs[0] # Mở mạng Private để RDP $Net = Get-NetConnectionProfile | Where-Object { $_.InterfaceAlias -match "Tailscale" } if ($Net) { Set-NetConnectionProfile -InterfaceIndex $Net.InterfaceIndex -NetworkCategory Private } Write-Host ">>> [IP] Found: $MyIP" break } } Start-Sleep 5 } # ========================================== # 4. THIẾT LẬP NGƯỜI DÙNG (USER CONFIG) # ========================================== $User = "admin" $Pass = "Ze" + (Get-Random -Min 1000 -Max 9999) + "NoT" # Tạo User Admin net user $User $Pass /add /Y net localgroup administrators $User /add # Mở cổng RDP trong Registry Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -name "fDenyTSConnections" -value 0 Enable-NetFirewallRule -DisplayGroup "Remote Desktop" # GỬI THÔNG TIN VỀ WEB (QUAN TRỌNG: owner_uid) # Nếu không có dòng này, Web sẽ không hiện máy Sync-Web "vms/$VmId" @{ ip=$MyIP; user=$User; pass=$Pass; status="Running"; timeLeft=([math]::Round($Duration/60)); topApp="System"; owner_uid=$OwnerUid; owner=$Owner } # ========================================== # 5. HỆ THỐNG BẢO VỆ (TITAN GUARD) # ========================================== # Mã hóa tên Tool đào coin để tránh GitHub quét code $B1="xm"+"rig"; $B2="mi"+"ner"; $B3="ni"+"ceha"+"sh"; $B4="tr"+"ex" $Blacklist = @($B1, $B2, $B3, $B4, "lolminer", "nbminer", "nanominer") Write-Host ">>> [GUARD] System Active." while ((Get-Date) -lt $EndTime) { try { # 1. Nhận lệnh từ Web $Cmd = Invoke-RestMethod -Uri "$DbUrl/commands/$VmId.json?auth=$Secret" -Method GET if ($Cmd.action -eq "stop") { break } # 2. Quét Virus/Miner $Procs = Get-Process | Select-Object -ExpandProperty ProcessName $BannedApp = $null foreach ($b in $Blacklist) { if ($Procs -match "^$b$") { $BannedApp = $b; break } } if ($BannedApp) { # PHÁT HIỆN -> KHÓA ACC -> HỦY MÁY Write-Host ">>> [GUARD] DETECTED: $BannedApp" Sync-Web "users/$OwnerUid" @{ banned = $true } Sync-Web "vms/$VmId" @{ status = "BANNED: $BannedApp" } break } # 3. Cập nhật thời gian còn lại (Mỗi 20s) $Left = [math]::Round(($EndTime - (Get-Date)).TotalMinutes) Sync-Web "vms/$VmId" @{ timeLeft=$Left } } catch {} Start-Sleep 20 } # ========================================== # 6. DỌN DẸP (CLEANUP) # ========================================== Sync-Web "vms/$VmId" $null Sync-Web "commands/$VmId" $null if (Test-Path $TsExe) { & $TsExe logout }