--- - name: Install OpenSSH Server ansible.windows.win_powershell: script: | [CmdletBinding(SupportsShouldProcess)] param() $cap = Get-WindowsCapability -Online -Name 'OpenSSH.Server*' if ($cap.State -eq 'Installed') { $Ansible.Changed = $false; return } if ($PSCmdlet.ShouldProcess($cap.Name, 'install')) { Add-WindowsCapability -Online -Name $cap.Name | Out-Null } # The first start writes the host keys and the stock configuration. The test # further down needs both to exist. - name: Start sshd once so it creates its host keys ansible.windows.win_service: name: sshd start_mode: auto state: started - name: Check there is a key to log in with when: win_ssh_hardening_require_admin_key | bool block: - name: Look for the administrators' key file ansible.windows.win_stat: path: "{{ win_ssh_hardening_config_dir }}\\administrators_authorized_keys" register: win_ssh_hardening_admin_keys - name: Stop if there is none ansible.builtin.assert: that: - win_ssh_hardening_admin_keys.stat.exists - win_ssh_hardening_admin_keys.stat.size > 0 fail_msg: >- {{ inventory_hostname }} has no keys in administrators_authorized_keys. Add at least one administrator's public key first, or set win_ssh_hardening_require_admin_key to false if you have another way in. # sshd ignores the file, silently, unless only Administrators and SYSTEM can # reach it. With passwords off, that is a lockout. - name: Restrict the administrators' key file to Administrators and SYSTEM ansible.windows.win_powershell: parameters: Path: "{{ win_ssh_hardening_config_dir }}\\administrators_authorized_keys" script: | [CmdletBinding(SupportsShouldProcess)] param([string]$Path) if (-not (Test-Path $Path)) { $Ansible.Changed = $false; return } $acl = Get-Acl $Path $who = $acl.Access | ForEach-Object { $_.IdentityReference.Translate([Security.Principal.SecurityIdentifier]).Value } | Sort-Object -Unique if ($acl.AreAccessRulesProtected -and (($who -join ',') -eq 'S-1-5-18,S-1-5-32-544')) { $Ansible.Changed = $false; return } if ($PSCmdlet.ShouldProcess($Path, 'restrict to Administrators and SYSTEM')) { icacls.exe $Path /inheritance:r /grant:r '*S-1-5-32-544:F' /grant:r '*S-1-5-18:F' | Out-Null if ($LASTEXITCODE -ne 0) { throw "icacls could not set permissions on $Path" } # /grant:r replaces those two entries. Anyone else granted access # directly keeps it unless removed by name. foreach ($sid in $who) { if ('S-1-5-18', 'S-1-5-32-544' -notcontains $sid) { icacls.exe $Path /remove "*$sid" | Out-Null } } } # win_template has no validate option, so the file is written beside the live # one, tested, and only then copied over it. - name: Write the new configuration next to the live one ansible.windows.win_template: src: sshd_config.j2 dest: "{{ win_ssh_hardening_config_dir }}\\sshd_config.new" - name: Test the new configuration before it goes live ansible.windows.win_command: >- "{{ win_ssh_hardening_sshd }}" -t -f "{{ win_ssh_hardening_config_dir }}\sshd_config.new" changed_when: false when: not ansible_check_mode - name: Put it live ansible.windows.win_copy: src: "{{ win_ssh_hardening_config_dir }}\\sshd_config.new" dest: "{{ win_ssh_hardening_config_dir }}\\sshd_config" remote_src: true notify: Restart sshd # In a preview the new file was never written, so there is nothing to copy. when: not ansible_check_mode - name: Set the shell sessions open in ansible.windows.win_regedit: path: HKLM:\SOFTWARE\OpenSSH name: DefaultShell data: "{{ win_ssh_hardening_default_shell }}" type: string when: win_ssh_hardening_default_shell | length > 0 # The rule Windows creates allows SSH from anywhere. Narrowing it here means a # stolen key is only useful from a network you already watch. The rule is # found by its ID, OpenSSH-Server-In-TCP, which is not the name the older # firewall interfaces show. - name: Narrow the inbound SSH rule to the management networks ansible.windows.win_powershell: parameters: Networks: "{{ win_ssh_hardening_allowed_networks }}" script: | [CmdletBinding(SupportsShouldProcess)] param([string[]]$Networks) $rule = Get-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -ErrorAction SilentlyContinue if (-not $rule) { throw 'The OpenSSH-Server-In-TCP firewall rule is missing. Reinstall the OpenSSH Server capability.' } # Windows stores 10.0.0.0/24 as 10.0.0.0/255.255.255.0, so the lists are # compared after setting them, in the form Windows reports. $before = (@(($rule | Get-NetFirewallAddressFilter).RemoteAddress) | Sort-Object) -join ',' if ($PSCmdlet.ShouldProcess('OpenSSH-Server-In-TCP', "allow only $($Networks -join ', ')")) { Set-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -RemoteAddress $Networks -Enabled True $after = (@((Get-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' | Get-NetFirewallAddressFilter).RemoteAddress) | Sort-Object) -join ',' $Ansible.Changed = ($before -ne $after) -or ($rule.Enabled -ne 'True') } when: win_ssh_hardening_allowed_networks | length > 0 - name: Flush handlers so the check below reads the running configuration ansible.builtin.meta: flush_handlers - name: Read back the settings sshd will actually use ansible.windows.win_command: '"{{ win_ssh_hardening_sshd }}" -T' changed_when: false register: win_ssh_hardening_effective when: not ansible_check_mode - name: Confirm password logins are off ansible.builtin.assert: that: - "'passwordauthentication no' in win_ssh_hardening_effective.stdout | lower" fail_msg: sshd still accepts passwords on {{ inventory_hostname }}. success_msg: Key-only logins confirmed. when: - not ansible_check_mode - not win_ssh_hardening_password_authentication | bool