dotfiles/pwsh7/Microsoft.PowerShell_profile.ps1

76 lines
3.9 KiB
PowerShell

function Start-SshReverseTunnel {
<#
.SYNOPSIS
Inicia un túnel SSH reverso de forma segura ocultando parámetros sensibles.
#>
# 1. Deshabilitar el historial para esta sesión de forma inmediata
Set-PSReadLineOption -HistorySaveStyle SaveNothing
Write-Host "`n[!] SSH REVERSE TUNNEL - SECURE INITIALIZATION" -ForegroundColor Cyan
Write-Host "[*] El historial de comandos ha sido desactivado para esta sesión." -ForegroundColor Gray
# 2.MALA-PRACTICA (esta hardcodeado) Definición de valores hardcodeados (Valores estáticos) (MALA PRACTICA)
$static_r = "2229"
$static_s = "..."
$static_t = "..."
$static_t_u = "..."
# Inicialización como SecureString (Para compatibilidad con tu lógica de conversión)
# Usamos -AsPlainText -Force para convertir el texto plano en objeto SecureString en memoria
$p_relay = ConvertTo-SecureString $static_r -AsPlainText -Force
$p_ssh = ConvertTo-SecureString $static_s -AsPlainText -Force
#> $target = ConvertTo-SecureString $static_t -AsPlainText -Force
$target_u = ConvertTo-SecureString $static_t -AsPlainText -Force
# 2. Captura de datos con enmascaramiento (asteriscos) (MEJOR PRACTICA, NO HARDCODEADO COMO LO ANTERIOR)
#> $p_relay = Read-Host "[-] Ingrese Puerto Relay (R)" -AsSecureString
$p_ssh = Read-Host "[-] Ingrese Puerto SSH NAS (S)" -AsSecureString
#>#> $target = Read-Host "[-] Ingrese Usuario Endpoint (User@Host)" -AsSecureString
$target_u = Read-Host "[-] Ingrese Usuario (Target) Endpoint (User@)" -AsSecureString
# 3. Conversión segura en memoria
$r = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($p_relay))
$s = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($p_ssh))
#>#> $t = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($target))
$u = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($target_u))
# 4. Construcción y ejecución
# Nota: Se usa el puerto 22 local por defecto según tu SOP anterior
#>#> $sshArgs = "-Command ssh -R ${r}:localhost:22 -N -C -i $HOME\.ssh\id_ed25519 -p ${s} ${u}@${d} -o 'ServerAliveInterval 30' -o 'ExitOnForwardFailure yes'"
$sshArgs = "-Command ssh -R ${r}:localhost:22 -N -C -i $HOME\.ssh\id_ed25519 -p ${s} ${u}@dzamo.duckdns.org -o 'ServerAliveInterval 30' -o 'ExitOnForwardFailure yes' -o 'ServerAliveCountMax 3' -o 'StreamLocalBindUnlink=yes'"
Write-Host "[+] Lanzando proceso SSH en segundo plano (Hidden)..." -ForegroundColor Yellow
try {
Start-Process pwsh -ArgumentList $sshArgs -WindowStyle Hidden
Write-Host "[OK] Túnel reverso inicializado correctamente." -ForegroundColor Green
} catch {
Write-Host "[ERROR] No se pudo lanzar el proceso." -ForegroundColor Red
}
# 5. Limpieza agresiva de memoria
$r = $s = $t = $null
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($p_relay))
[System.GC]::Collect()
}
function Get-SshTunnels {
Get-CimInstance Win32_Process -Filter "Name = 'ssh.exe'" |
Select-Object @{N='PID';E={$_.ProcessId}}, @{N='Command';E={$_.CommandLine}} |
Out-GridView -Title "Túneles SSH Activos" # Abre una ventana visual para verlos
}
function Stop-SshTunnels {
Write-Host "Deteniendo todos los procesos ssh.exe..." -ForegroundColor Yellow
Get-Process ssh -ErrorAction SilentlyContinue | Stop-Process -Force
Write-Host "[OK] Túneles cerrados." -ForegroundColor Green
}
function View-SshExeRunning{
Get-CimInstance Win32_Process -Filter "Name = 'ssh.exe'" | Select-Object ProcessId, CommandLine | Format-List
}
# Crear un alias corto para uso rápido (opcional)
Set-Alias -Name stn -Value Start-SshReverseTunnel