Files

66 lines
3.7 KiB
Batchfile

@echo off
REM Enable LAN-only SSH access on this PC for docchecker remote debugging. v2
REM v2 changes: 1) portable OpenSSH from GitHub fallback when Windows Update
REM refuses the FoD capability (error 0x80242005); 2) ACL via language-neutral
REM SIDs (local name mapping broke on ru-Windows); 3) honest final check -
REM DONE only when the sshd service is actually RUNNING and port 22 listens.
REM Scope stays LAN-only: firewall rule allows TCP 22 from 192.168.0.0/24, key auth only.
REM Revert with disable_ssh.bat from the same folder.
net session >nul 2>&1
if %errorlevel% neq 0 (
echo Need admin rights. Relaunching elevated...
powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
exit /b
)
echo === 1. OpenSSH Server: try Windows capability ===
powershell -NoProfile -Command "$c = Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Server*'; if ($c.State -ne 'Installed') { try { $c | Add-WindowsCapability -Online | Out-Null } catch {}; $s = (Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Server*').State; Write-Output ('STATE=' + $s) } else { Write-Output 'STATE=Installed' }"
powershell -NoProfile -Command "if ((Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Server*').State -eq 'Installed') { exit 0 } else { exit 1 }"
if %errorlevel% equ 0 goto :svc
echo === 1b. Windows Update refused - portable OpenSSH from GitHub ===
if not exist "C:\ProgramData\sshportable" mkdir "C:\ProgramData\sshportable"
powershell -NoProfile -Command "try { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -UseBasicParsing -Uri 'https://github.com/PowerShell/Win32-OpenSSH/releases/download/v9.5.0.0p1-Beta/OpenSSH-Win64.zip' -OutFile 'C:\ProgramData\sshportable\ssh.zip'; exit 0 } catch { Write-Output $_.Exception.Message; exit 1 }"
if %errorlevel% neq 0 goto :err
powershell -NoProfile -Command "Expand-Archive -Force 'C:\ProgramData\sshportable\ssh.zip' 'C:\ProgramData\sshportable'"
powershell -NoProfile -ExecutionPolicy Bypass -File "C:\ProgramData\sshportable\OpenSSH-Win64\install-sshd.ps1"
if %errorlevel% neq 0 goto :err
:svc
echo === 2. Public key for administrators - ACL via SIDs ===
if not exist "C:\ProgramData\ssh" mkdir "C:\ProgramData\ssh"
echo ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKdrZtRRqRwyQYHHJTepJnUFS159250wMVQxnVnfjC5H docchecker-debug@macbookair>"C:\ProgramData\ssh\administrators_authorized_keys"
icacls "C:\ProgramData\ssh\administrators_authorized_keys" /inheritance:r /grant *S-1-5-18:F /grant *S-1-5-32-544:F >nul
if %errorlevel% neq 0 goto :err
if exist "C:\ProgramData\sshportable\OpenSSH-Win64\sshd_default_config" if not exist "C:\ProgramData\ssh\sshd_config" copy /y "C:\ProgramData\sshportable\OpenSSH-Win64\sshd_default_config" "C:\ProgramData\ssh\sshd_config" >nul
echo === 3. Service sshd: automatic + start ===
sc config sshd start= auto >nul
net start sshd 2>nul
sc query sshd | findstr "STATE"
echo === 4. Firewall: TCP 22 only from 192.168.0.0/24 ===
netsh advfirewall firewall delete rule name="docdebug-ssh" >nul 2>&1
netsh advfirewall firewall add rule name="docdebug-ssh" dir=in action=allow protocol=TCP localport=22 remoteip=192.168.0.0/24 profile=any >nul
echo === 5. Verify ===
powershell -NoProfile -Command "$svc = Get-Service sshd -ErrorAction SilentlyContinue; if ($svc -and $svc.Status -eq 'Running' -and (netstat -ano | Select-String ':22.*LISTENING')) { Write-Output 'CHECK=OK' } else { Write-Output 'CHECK=FAILED'; exit 1 }"
if %errorlevel% neq 0 goto :err
echo.
echo === DONE ===
echo OPERATOR LINE - copy or photo this:
whoami
hostname
echo.
pause
exit /b 0
:err
echo.
echo === FAILED - take a photo of this window ===
pause
exit /b 1