-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from mission-apprentissage/feat(devops-119)-s…
…sh-hardening feat: ssh config hardening
- Loading branch information
Showing
6 changed files
with
133 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
--- | ||
# recommandation from https://www.sshaudit.com/hardening_guides.html | ||
# keys configuration is skipped (first 2 steps of the documentation above) | ||
|
||
- name: Regen host keys | ||
command: /opt/app/tools/ssh/regen-host-key.sh | ||
args: | ||
creates: /etc/ssh/ssh_host_keys_generated | ||
|
||
- name: Create safe moduli file | ||
command: /opt/app/tools/ssh/moduli_file.sh | ||
args: | ||
creates: /etc/ssh/moduli.safe | ||
|
||
- name: Get if moduli file has been modified | ||
stat: | ||
path: /etc/ssh/moduli | ||
register: moduli_stat | ||
|
||
- name: Get if moduli safe file has been modified | ||
stat: | ||
path: /etc/ssh/moduli.safe | ||
register: moduli_safe_stat | ||
|
||
- name: Move moduli.safe to moduli | ||
command: mv /etc/ssh/moduli.safe /etc/ssh/moduli | ||
when: moduli_stat.stat.checksum != moduli_safe_stat.stat.checksum | ||
notify: | ||
- restart-ssh | ||
|
||
- name: Create SSH hardening configuration Ubuntu 22 | ||
copy: | ||
dest: /etc/ssh/sshd_config.d/ssh-audit_hardening.conf | ||
content: | | ||
# Restrict key exchange, cipher, and MAC algorithms, as per sshaudit.com | ||
# hardening guide. | ||
KexAlgorithms [email protected],curve25519-sha256,[email protected],gss-curve25519-sha256-,diffie-hellman-group16-sha512,gss-group16-sha512-,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256 | ||
Ciphers [email protected],[email protected],aes256-ctr,aes192-ctr,[email protected],aes128-ctr | ||
MACs [email protected],[email protected],[email protected] | ||
HostKeyAlgorithms [email protected],[email protected],[email protected],[email protected],[email protected],ssh-ed25519,rsa-sha2-512,rsa-sha2-256 | ||
CASignatureAlgorithms [email protected],ssh-ed25519,rsa-sha2-512,rsa-sh2-256 | ||
GSSAPIKexAlgorithms gss-curve25519-sha256-,gss-group16-sha512- | ||
HostbasedAcceptedAlgorithms [email protected],[email protected],[email protected],ssh-ed25519,[email protected],rsa-sha2-512,[email protected],rsa-sha2-256 | ||
PubkeyAcceptedAlgorithms [email protected],[email protected],[email protected],ssh-ed25519,[email protected],rsa-sha2-512,[email protected],rsa-sha2-256 | ||
when: ansible_facts['distribution_major_version'] == '22' | ||
notify: | ||
- restart-ssh | ||
|
||
- name: Create SSH hardening configuration Ubuntu 24 | ||
copy: | ||
dest: /etc/ssh/sshd_config.d/ssh-audit_hardening.conf | ||
content: | | ||
# Restrict key exchange, cipher, and MAC algorithms, as per sshaudit.com | ||
# hardening guide. | ||
KexAlgorithms [email protected],gss-curve25519-sha256-,curve25519-sha256,[email protected],diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256,gss-group16-sha512-,diffie-hellman-group16-sha512 | ||
Ciphers [email protected],[email protected],aes256-ctr,aes192-ctr,[email protected],aes128-ctr | ||
MACs [email protected],[email protected],[email protected] | ||
RequiredRSASize 3072 | ||
HostKeyAlgorithms [email protected],[email protected],[email protected],[email protected],[email protected],ssh-ed25519,rsa-sha2-512,rsa-sha2-256 | ||
CASignatureAlgorithms [email protected],ssh-ed25519,rsa-sha2-512,rsa-sha2-256 | ||
GSSAPIKexAlgorithms gss-curve25519-sha256-,gss-group16-sha512- | ||
HostbasedAcceptedAlgorithms [email protected],[email protected],[email protected],[email protected],[email protected],ssh-ed25519,rsa-sha2-512,rsa-sha2-256 | ||
PubkeyAcceptedAlgorithms [email protected],[email protected],[email protected],[email protected],[email protected],ssh-ed25519,rsa-sha2-512,rsa-sha2-256 | ||
when: ansible_facts['distribution_major_version'] == '24' | ||
notify: | ||
- restart-ssh | ||
|
||
- name: IPtable | ||
ansible.builtin.shell: | ||
cmd: /opt/app/tools/ssh/iptables.sh | ||
|
||
# Do not use iptables-persistant due to conflict with fail2ban and docker policies | ||
- name: Run IPtable script after reboot | ||
ansible.builtin.cron: | ||
name: "iptables" | ||
special_time: "reboot" | ||
job: "/opt/app/tools/ssh/iptables.sh >> /var/log/cron.log 2>&1; /opt/app/tools/monitoring/export-cron-status-prom.sh -c 'Restore SSH iptables' -v $?" | ||
|
||
- name: Validate sshd Config | ||
shell: | ||
cmd: sshd -t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
#Needs to be run as sudo | ||
|
||
iptables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --set | ||
iptables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 10 --hitcount 10 -j DROP | ||
ip6tables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --set | ||
ip6tables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 10 --hitcount 10 -j DROP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
#Needs to be run as sudo | ||
|
||
awk '$5 >= 3071' /etc/ssh/moduli > /etc/ssh/moduli.safe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
#Needs to be run as sudo | ||
|
||
rm /etc/ssh/ssh_host_* | ||
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" | ||
ssh-keygen -t rsa -b 4096 -f /etc/ssh/ssh_host_rsa_key -N "" | ||
touch /etc/ssh/ssh_host_keys_generated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters