{"id":6453,"date":"2017-02-01T21:00:38","date_gmt":"2017-02-01T13:00:38","guid":{"rendered":"http:\/\/rmohan.com\/?p=6453"},"modified":"2017-02-01T21:00:38","modified_gmt":"2017-02-01T13:00:38","slug":"secure-secure-shell","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=6453","title":{"rendered":"Secure Secure Shell"},"content":{"rendered":"<header class=\"post-header\">\n<h1 class=\"post-title\">Secure Secure Shell<\/h1>\n<p class=\"post-meta\">\n<\/header>\n<article class=\"post-content\">You may have heard that the NSA can decrypt SSH at least some of the time. If you have not, then read the <a href=\"https:\/\/www.spiegel.de\/international\/germany\/inside-the-nsa-s-war-on-internet-security-a-1010361.html\">latest batch of Snowden documents<\/a> now. All of it. This post will still be here when you finish. My goal with this post here is to make NSA analysts sad.<\/p>\n<p>TL;DR: Scan this post for fixed width fonts, these will be the config file snippets and commands you have to use.<\/p>\n<p><em>Warning<\/em>: You will need a recent OpenSSH version. It should work with 6.5 but I have only tested 6.7 and connections to Github. Here is a good <a href=\"http:\/\/ssh-comparison.quendi.de\/comparison.html\">compatibility matrix<\/a>.<\/p>\n<h1 id=\"the-crypto\">The crypto<\/h1>\n<p>Reading the documents, I have the feeling that the NSA can 1) decrypt weak crypto and 2) steal keys. Let\u2019s focus on the crypto first. SSH supports different key exchange algorithms, ciphers and message authentication codes. The server and the client choose a set of algorithms supported by both, then proceed with the key exchange. Some of the supported algorithms are not so great and should be disabled completely. This hurts interoperability but everyone uses OpenSSH anyway. Fortunately, downgrade attacks are not possible because the supported algorithm lists are included in the key derivation. If a man in the middle were to change the lists, then the server and the client would calculate different keys.<\/p>\n<h2 id=\"key-exchange\">Key exchange<\/h2>\n<p>There are basically two ways to do key exchange: <a href=\"https:\/\/en.wikipedia.org\/wiki\/Diffie%E2%80%93Hellman_key_exchange\">Diffie-Hellman<\/a> and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Elliptic_curve_Diffie%E2%80%93Hellman\">Elliptic Curve Diffie-Hellman<\/a>. Both provide <a href=\"https:\/\/en.wikipedia.org\/wiki\/Forward_secrecy\">forward secrecy<\/a> which the NSA hates because they can\u2019t use passive collection and key recovery later. The server and the client will end up with a shared secret number at the end without a passive eavesdropper learning anything about this number. After we have a shared secret we have to derive a cryptographic key from this using a key derivation function. In case of SSH, this is a hash function. <a href=\"https:\/\/www.mitls.org\/downloads\/transcript-collisions.pdf\">Collision attacks<\/a> on this hash function have been proven to allow downgrade attacks.<\/p>\n<p>DH works with a multiplicative group of integers modulo a prime. Its security is based on the hardness of the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Discrete_logarithm_problem\">discrete logarithm problem<\/a>.<\/p>\n<pre><code id=\"diffie-hellman\">Alice           Bob\r\n---------------------------\r\nSa = random\r\nPa = g^Sa   --&gt; Pa\r\n                Sb = random\r\nPb          &lt;-- Pb = g^Sb\r\ns = Pb^Sa       s = Pa^Sb\r\nk = KDF(s)      k = KDF(s)<\/code><\/pre>\n<p>ECDH works with elliptic curves over finite fields. Its security is based on the hardness of the elliptic curve discrete logarithm problem.<\/p>\n<pre><code id=\"elliptic-curve-diffie-hellman\">Alice           Bob\r\n---------------------------\r\nSa = random\r\nPa = Sa * G --&gt; Pa\r\n                Sb = random\r\nPb          &lt;-- Pb = Sb * G\r\ns = Sa * Pb     s = Sb * Pa\r\nk = KDF(s)      k = KDF(s)<\/code><\/pre>\n<p>OpenSSH supports 8 key exchange protocols:<\/p>\n<ol>\n<li><a href=\"http:\/\/git.libssh.org\/projects\/libssh.git\/tree\/doc\/curve25519-sha256@libssh.org.txt\">curve25519-sha256<\/a>: ECDH over <a href=\"http:\/\/cr.yp.to\/ecdh.html\">Curve25519<\/a> with SHA2<\/li>\n<li><a href=\"https:\/\/www.ietf.org\/rfc\/rfc4253.txt\">diffie-hellman-group1-sha1<\/a>: 1024 bit DH with SHA1<\/li>\n<li><a href=\"https:\/\/www.ietf.org\/rfc\/rfc4253.txt\">diffie-hellman-group14-sha1<\/a>: 2048 bit DH with SHA1<\/li>\n<li><a href=\"https:\/\/www.ietf.org\/rfc\/rfc4419.txt\">diffie-hellman-group-exchange-sha1<\/a>: Custom DH with SHA1<\/li>\n<li><a href=\"https:\/\/www.ietf.org\/rfc\/rfc4419.txt\">diffie-hellman-group-exchange-sha256<\/a>: Custom DH with SHA2<\/li>\n<li>ecdh-sha2-nistp256: ECDH over NIST P-256 with SHA2<\/li>\n<li>ecdh-sha2-nistp384: ECDH over NIST P-384 with SHA2<\/li>\n<li>ecdh-sha2-nistp521: ECDH over NIST P-521 with SHA2<\/li>\n<\/ol>\n<p>We have to look at 3 things here:<\/p>\n<ul>\n<li><em>ECDH curve choice<\/em>: This eliminates 6-8 because <a href=\"http:\/\/blog.cr.yp.to\/20140323-ecdsa.html\">NIST curves suck<\/a>. They leak secrets through timing side channels and off-curve inputs. Also, <a href=\"https:\/\/projectbullrun.org\/dual-ec\/vulnerability.html\">NIST is considered harmful<\/a> and cannot be trusted.<\/li>\n<li><em>Bit size of the DH modulus<\/em>: This eliminates 2 because the NSA has supercomputers and possibly unknown attacks. 1024 bits simply don\u2019t offer sufficient security margin.<\/li>\n<li><em>Security of the hash function<\/em>: This eliminates 2-4 because SHA1 is broken. We don\u2019t have to wait for a second preimage attack that takes 10 minutes on a cellphone to disable it right now.<\/li>\n<\/ul>\n<p>We are left with 1 and 5. 1 is better and it\u2019s perfectly OK to only support that but for interoperability (with Eclipse, WinSCP), 5 can be included.<\/p>\n<p>Recommended <code class=\"highlighter-rouge\">\/etc\/ssh\/sshd_config<\/code> snippet:<\/p>\n<pre><code id=\"server-kex\">KexAlgorithms curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256<\/code><\/pre>\n<p>Recommended <code class=\"highlighter-rouge\">\/etc\/ssh\/ssh_config<\/code> snippet:<\/p>\n<pre><code id=\"client-kex\"># Github needs diffie-hellman-group-exchange-sha1 some of the time but not always.\r\n#Host github.com\r\n#    KexAlgorithms curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1\r\n    \r\nHost *\r\n    KexAlgorithms curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256<\/code><\/pre>\n<p>If you chose to enable 5, open <code class=\"highlighter-rouge\">\/etc\/ssh\/moduli<\/code> if exists, and delete lines where the 5th column is less than 2000.<\/p>\n<pre><code id=\"server-moduli-filter\">awk '$5 &gt; 2000' \/etc\/ssh\/moduli &gt; \"${HOME}\/moduli\"\r\nwc -l \"${HOME}\/moduli\" # make sure there is something left\r\nmv \"${HOME}\/moduli\" \/etc\/ssh\/moduli<\/code><\/pre>\n<p>If it does not exist, create it:<\/p>\n<pre><code id=\"server-moduli\">ssh-keygen -G \/etc\/ssh\/moduli.all -b 4096\r\nssh-keygen -T \/etc\/ssh\/moduli.safe -f \/etc\/ssh\/moduli.all\r\nmv \/etc\/ssh\/moduli.safe \/etc\/ssh\/moduli\r\nrm \/etc\/ssh\/moduli.all<\/code><\/pre>\n<p>This will take a while so continue while it\u2019s running.<\/p>\n<h2 id=\"authentication\">Authentication<\/h2>\n<p>The key exchange ensures that the server and the client shares a secret no one else knows. We also have to make sure that they share this secret with each other and not an NSA analyst.<\/p>\n<h3 id=\"server-authentication\">Server authentication<\/h3>\n<p>The server proves its identity to the client by signing the key resulting from the key exchange. There are 4 public key algorithms for authentication:<\/p>\n<ol>\n<li>DSA with SHA1<\/li>\n<li>ECDSA with SHA256, SHA384 or SHA512 depending on key size<\/li>\n<li><a href=\"http:\/\/ed25519.cr.yp.to\/\">Ed25519<\/a> with SHA512<\/li>\n<li>RSA with SHA1<\/li>\n<\/ol>\n<p>DSA keys must be exactly 1024 bits so let\u2019s disable that. Number 2 here involves NIST suckage and should be disabled as well. Another important disadvantage of DSA and ECDSA is that it uses randomness for each signature. If the random numbers are not the best quality, then it is <a href=\"https:\/\/security.stackexchange.com\/a\/46781\">possible to recover<\/a> the <a href=\"https:\/\/events.ccc.de\/congress\/2010\/Fahrplan\/attachments\/1780_27c3_console_hacking_2010.pdf\">secret key<\/a>. Fortunately, RSA using SHA1 is not a problem here because the value being signed is actually a SHA2 hash. The hash function SHA1(SHA2(x)) is just as secure as SHA2 (it has less bits of course but no better attacks).<\/p>\n<pre><code id=\"server-auth\">Protocol 2\r\nHostKey \/etc\/ssh\/ssh_host_ed25519_key\r\nHostKey \/etc\/ssh\/ssh_host_rsa_key<\/code><\/pre>\n<p>The first time you connect to your server, you will be asked to accept the new fingerprint.<\/p>\n<p>This will also disable the horribly broken v1 protocol that you should not have enabled in the first place. We should remove the unused keys and only generate a large RSA key and an Ed25519 key. Your init scripts may recreate the unused keys. If you don\u2019t want that, remove any <code class=\"highlighter-rouge\">ssh-keygen<\/code> commands from the init script.<\/p>\n<pre><code id=\"server-keygen\">cd \/etc\/ssh\r\nrm ssh_host_*key*\r\nssh-keygen -t ed25519 -f ssh_host_ed25519_key -N \"\" &lt; \/dev\/null\r\nssh-keygen -t rsa -b 4096 -f ssh_host_rsa_key -N \"\" &lt; \/dev\/null<\/code><\/pre>\n<h3 id=\"client-authentication\">Client authentication<\/h3>\n<p>The client must prove its identity to the server as well. There are various methods to do that.<\/p>\n<p>The simplest is password authentication. This should be disabled immediately <em>after<\/em> setting up a more secure method because it allows compromised servers to steal passwords. Password authentication is also more vulnerable to online bruteforce attacks.<\/p>\n<p>Recommended <code class=\"highlighter-rouge\">\/etc\/ssh\/sshd_config<\/code> snippet:<\/p>\n<pre><code id=\"server-auth-password\">PasswordAuthentication no\r\nChallengeResponseAuthentication no<\/code><\/pre>\n<p>Recommended <code class=\"highlighter-rouge\">\/etc\/ssh\/ssh_config<\/code> snippet:<\/p>\n<pre><code id=\"client-auth-password\">Host *\r\n    PasswordAuthentication no\r\n    ChallengeResponseAuthentication no<\/code><\/pre>\n<p>The most common and secure method is public key authentication, basically the same process as the server authentication.<\/p>\n<p>Recommended <code class=\"highlighter-rouge\">\/etc\/ssh\/sshd_config<\/code> snippet:<\/p>\n<pre><code id=\"server-auth-pubkey\">PubkeyAuthentication yes<\/code><\/pre>\n<p>Recommended <code class=\"highlighter-rouge\">\/etc\/ssh\/ssh_config<\/code> snippet:<\/p>\n<pre><code id=\"client-auth-pubkey\">Host *\r\n    PubkeyAuthentication yes\r\n    HostKeyAlgorithms ssh-ed25519-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-ed25519,ssh-rsa<\/code><\/pre>\n<p>Generate client keys using the following commands:<\/p>\n<pre><code id=\"client-keygen\">ssh-keygen -t ed25519 -o -a 100\r\nssh-keygen -t rsa -b 4096 -o -a 100<\/code><\/pre>\n<p>You can deploy your new client public keys using <code class=\"highlighter-rouge\">ssh-copy-id<\/code>.<\/p>\n<p>It is also possible to use OTP authentication to reduce the consequences of lost passwords. <a href=\"https:\/\/code.google.com\/p\/google-authenticator\/wiki\/PamModuleInstructions\">Google Authenticator<\/a> is a nice implementation of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Time-based_One-time_Password_Algorithm\">TOTP<\/a>, or Timebased One Time Password. You can also use a <a href=\"https:\/\/www.cl.cam.ac.uk\/~mgk25\/otpw.html\">printed list of one time passwords<\/a> or any other <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pluggable_authentication_module\">PAM<\/a> module, really, if you enable <code class=\"highlighter-rouge\">ChallengeResponseAuthentication<\/code>.<\/p>\n<h3 id=\"user-authentication\">User Authentication<\/h3>\n<p>Even with Public Key authentication, you should only allow incoming connections from expected users. The <code class=\"highlighter-rouge\">AllowUsers<\/code> setting in <code class=\"highlighter-rouge\">sshd_config<\/code> lets you specify users who are allowed to connect, but this can get complicated with a large number of ssh users. Additionally, when deleting a user from the system, the username is <a href=\"https:\/\/bugs.debian.org\/cgi-bin\/bugreport.cgi?bug=779880\">not removed<\/a> from <code class=\"highlighter-rouge\">sshd_config<\/code>, which adds to maintenance requirements. The solution is to use the <code class=\"highlighter-rouge\">AllowGroups<\/code> setting instead, and add users to an <code class=\"highlighter-rouge\">ssh-user<\/code> group.<\/p>\n<p>Recommended <code class=\"highlighter-rouge\">\/etc\/ssh\/sshd_config<\/code> snippet:<\/p>\n<pre><code id=\"client-auth-allowgroups\">AllowGroups ssh-user<\/code><\/pre>\n<p>Create the ssh-user group with <code class=\"highlighter-rouge\">sudo groupadd ssh-user<\/code>, then add each ssh user to the group with <code class=\"highlighter-rouge\">sudo usermod -a -G ssh-user &lt;username&gt;<\/code>.<\/p>\n<h2 id=\"symmetric-ciphers\">Symmetric ciphers<\/h2>\n<p>Symmetric ciphers are used to encrypt the data after the initial key exchange and authentication is complete.<\/p>\n<p>Here we have quite a few algorithms:<\/p>\n<ol>\n<li>3des-cbc<\/li>\n<li>aes128-cbc<\/li>\n<li>aes192-cbc<\/li>\n<li>aes256-cbc<\/li>\n<li>aes128-ctr<\/li>\n<li>aes192-ctr<\/li>\n<li>aes256-ctr<\/li>\n<li>aes128-gcm@openssh.com<\/li>\n<li>aes256-gcm@openssh.com<\/li>\n<li>arcfour<\/li>\n<li>arcfour128<\/li>\n<li>arcfour256<\/li>\n<li>blowfish-cbc<\/li>\n<li>cast128-cbc<\/li>\n<li>chacha20-poly1305@openssh.com<\/li>\n<\/ol>\n<p>We have to consider the following:<\/p>\n<ul>\n<li><em>Security of the cipher algorithm<\/em>: This eliminates 1 and 10-12 &#8211; both DES and RC4 are broken. Again, no need to wait for them to become even weaker, disable them now.<\/li>\n<li><em>Key size<\/em>: At least 128 bits, the more the better.<\/li>\n<li><em>Block size<\/em>: Does not apply to stream ciphers. At least 128 bits. This eliminates 13 and 14 because those have a 64 bit block size.<\/li>\n<li><em>Cipher mode<\/em>: The recommended approach here is to prefer <a href=\"https:\/\/en.wikipedia.org\/wiki\/Authenticated_encryption\">AE<\/a> modes and optionally allow CTR for compatibility. CTR with Encrypt-then-MAC is provably secure.<\/li>\n<\/ul>\n<p>Chacha20-poly1305 is preferred over AES-GCM because the SSH protocol <a href=\"http:\/\/blog.djm.net.au\/2013\/11\/chacha20-and-poly1305-in-openssh.html\">does not encrypt message sizes<\/a> when GCM (or EtM) is in use. This allows some traffic analysis even without decrypting the data. We will deal with that soon.<\/p>\n<p>Recommended <code class=\"highlighter-rouge\">\/etc\/ssh\/sshd_config<\/code> snippet:<\/p>\n<pre><code id=\"server-ciphers\">Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr<\/code><\/pre>\n<p>Recommended <code class=\"highlighter-rouge\">\/etc\/ssh\/ssh_config<\/code> snippet:<\/p>\n<pre><code id=\"client-ciphers\">Host *\r\n    Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr<\/code><\/pre>\n<h2 id=\"message-authentication-codes\">Message authentication codes<\/h2>\n<p>Encryption provides <em>confidentiality<\/em>, message authentication code provides <em>integrity<\/em>. We need both. If an AE cipher mode is selected, then extra MACs are not used, the integrity is already given. If CTR is selected, then we need a MAC to calculate and attach a tag to every message.<\/p>\n<p>There are multiple ways to combine ciphers and MACs &#8211; not all of these are useful. The 3 most common:<\/p>\n<ul>\n<li><em>Encrypt-then-MAC<\/em>: encrypt the message, then attach the MAC of the ciphertext.<\/li>\n<li><em>MAC-then-encrypt<\/em>: attach the MAC of the plaintext, then encrypt everything.<\/li>\n<li><em>Encrypt-and-MAC<\/em>: encrypt the message, then attach the MAC of the plaintext.<\/li>\n<\/ul>\n<p>Only Encrypt-then-MAC should be used, period. Using MAC-then-encrypt have lead to many attacks on TLS while Encrypt-and-MAC have lead to not quite that many attacks on SSH. The reason for this is that the more you fiddle with an attacker provided message, the more chance the attacker has to gain information through side channels. In case of Encrypt-then-MAC, the MAC is verified and if incorrect, discarded. Boom, one step, no timing channels. In case of MAC-then-encrypt, first the attacker provided message has to be decrypted and only then can you verify it. Decryption failure (due to invalid CBC padding for example) may take less time than verification failure. Encrypt-and-MAC also has to be decrypted first, leading to the same kind of potential side channels. It\u2019s even worse because no one said that a MAC\u2019s output can\u2019t leak what its input was. SSH by default, uses this method.<\/p>\n<p>Here are the available MAC choices:<\/p>\n<ol>\n<li>hmac-md5<\/li>\n<li>hmac-md5-96<\/li>\n<li>hmac-ripemd160<\/li>\n<li>hmac-sha1<\/li>\n<li>hmac-sha1-96<\/li>\n<li>hmac-sha2-256<\/li>\n<li>hmac-sha2-512<\/li>\n<li>umac-64<\/li>\n<li>umac-128<\/li>\n<li>hmac-md5-etm@openssh.com<\/li>\n<li>hmac-md5-96-etm@openssh.com<\/li>\n<li>hmac-ripemd160-etm@openssh.com<\/li>\n<li>hmac-sha1-etm@openssh.com<\/li>\n<li>hmac-sha1-96-etm@openssh.com<\/li>\n<li>hmac-sha2-256-etm@openssh.com<\/li>\n<li>hmac-sha2-512-etm@openssh.com<\/li>\n<li>umac-64-etm@openssh.com<\/li>\n<li>umac-128-etm@openssh.com<\/li>\n<\/ol>\n<p>The selection considerations:<\/p>\n<ul>\n<li><em>Security of the hash algorithm<\/em>: No MD5 and SHA1. Yes, I know that HMAC-SHA1 does not need collision resistance but why wait? Disable weak crypto today.<\/li>\n<li><em>Encrypt-then-MAC<\/em>: I am not aware of a security proof for CTR-and-HMAC but I also don\u2019t think CTR decryption can fail. Since there are no downgrade attacks, you can add them to the end of the list. You can also do this on a host by host basis so you know which ones are less safe.<\/li>\n<li><em>Tag size<\/em>: At least 128 bits. This eliminates umac-64-etm.<\/li>\n<li><em>Key size<\/em>: At least 128 bits. This doesn\u2019t eliminate anything at this point.<\/li>\n<\/ul>\n<p>Recommended <code class=\"highlighter-rouge\">\/etc\/ssh\/sshd_config<\/code> snippet:<\/p>\n<pre><code id=\"server-macs\">MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-ripemd160-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512,hmac-sha2-256,hmac-ripemd160,umac-128@openssh.com<\/code><\/pre>\n<p>Recommended <code class=\"highlighter-rouge\">\/etc\/ssh\/ssh_config<\/code> snippet:<\/p>\n<pre><code id=\"client-macs\">Host *\r\n    MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-ripemd160-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512,hmac-sha2-256,hmac-ripemd160,umac-128@openssh.com<\/code><\/pre>\n<h1 id=\"preventing-key-theft\">Preventing key theft<\/h1>\n<p>Even with forward secrecy the secret keys must be kept secret. The NSA has a database of stolen keys &#8211; you do not want your key there.<\/p>\n<h2 id=\"system-hardening\">System hardening<\/h2>\n<p>OpenSSH has some undocumented, and rarely used features. UseRoaming is one such feature with a known vulnerability.<\/p>\n<p>Recommended <code class=\"highlighter-rouge\">\/etc\/ssh\/ssh_config<\/code> snippet:<\/p>\n<pre><code id=\"client-features\">Host *\r\n   UseRoaming no<\/code><\/pre>\n<p>This post is not intended to be a comprehensive system security guide. Very briefly:<\/p>\n<ul>\n<li><em>Don\u2019t install what you don\u2019t need<\/em>: Every single line of code has a chance of containing a bug. Some of these bugs are security holes. Fewer lines, fewer holes.<\/li>\n<li><em>Use free software<\/em>: As in speech. You want to use code that\u2019s actually reviewed or that you can review yourself. There is no way to achieve that without source code. Someone may have reviewed proprietary crap but who knows.<\/li>\n<li><em>Keep your software up to date<\/em>: New versions often fix critical security holes.<\/li>\n<li><em>Exploit mitigation<\/em>: Sad but true &#8211; there will always be security holes in your software. There are things you can do to prevent their exploitation, such as GCC\u2019s -fstack-protector. One of the best security projects out there is <a href=\"https:\/\/grsecurity.net\/\">Grsecurity<\/a>. Use it or use OpenBSD.<\/li>\n<\/ul>\n<h2 id=\"traffic-analysis-resistance\">Traffic analysis resistance<\/h2>\n<p>Set up <a href=\"https:\/\/www.torproject.org\/docs\/hidden-services.html.en\">Tor hidden services<\/a> for your SSH servers. This has multiple advantages. It provides an additional layer of encryption and server authentication. People looking at your traffic will not know your IP, so they will be unable to scan and target other services running on the same server and client. Attackers can still attack these services but don\u2019t know if it has anything to do with the observed traffic until they actually break in.<\/p>\n<p>Now this is only true if you don\u2019t disclose your SSH server\u2019s fingerprint in any other way. You should only accept connections from the hidden service or from LAN, if required.<\/p>\n<p>If you don\u2019t need LAN access, you can add the following line to <code class=\"highlighter-rouge\">\/etc\/ssh\/sshd_config<\/code>:<\/p>\n<pre><code id=\"localhost-only\">ListenAddress 127.0.0.1:22<\/code><\/pre>\n<p>Add this to <code class=\"highlighter-rouge\">\/etc\/tor\/torrc<\/code>:<\/p>\n<pre><code id=\"hidden-service\">HiddenServiceDir \/var\/lib\/tor\/hidden_service\/ssh\r\nHiddenServicePort 22 127.0.0.1:22<\/code><\/pre>\n<p>You will find the hostname you have to use in <code class=\"highlighter-rouge\">\/var\/lib\/tor\/hidden_service\/ssh\/hostname<\/code>. You also have to configure the client to use Tor. For this, socat will be needed. Add the following line to <code class=\"highlighter-rouge\">\/etc\/ssh\/ssh_config<\/code>:<\/p>\n<pre><code id=\"onion-proxy\">Host *.onion\r\n    ProxyCommand socat - SOCKS4A:localhost:%h:%p,socksport=9050\r\n\r\nHost *\r\n    ...<\/code><\/pre>\n<p>If you want to allow connections from LAN, don\u2019t use the <code class=\"highlighter-rouge\">ListenAddress<\/code> line, configure your firewall instead.<\/p>\n<h2 id=\"key-storage\">Key storage<\/h2>\n<p>You should encrypt your client key files using a strong password. Additionally, you can use <code class=\"highlighter-rouge\">ssh-keygen -o -a $number<\/code> to slow down cracking attempts by iterating the hash function many times. You may want to store them on a pendrive and only plug it in when you want to use SSH. Are you more likely to lose your pendrive or have your system compromised? I don\u2019t know.<\/p>\n<p>Unfortunately, you can\u2019t encrypt your server key and it must be always available, or else sshd won\u2019t start. The only thing protecting it is OS access controls.<\/p>\n<h1 id=\"the-end\">The end<\/h1>\n<p>It\u2019s probably a good idea to test the changes. <code class=\"highlighter-rouge\">ssh -v<\/code> will print the selected algorithms and also makes problems easier to spot. Be extremely careful when configuring SSH on a remote host. Always keep an active session, never restart sshd. Instead you can send the <code class=\"highlighter-rouge\">SIGHUP<\/code> signal to reload the configuration without killing your session. You can be even more careful by starting a new sshd instance on a different port and testing that.<\/p>\n<p>Can you make these changes? If the answer is yes, then\u2026<\/p>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p> Secure Secure Shell <\/p>\n<p class=\"post-meta\"> You may have heard that the NSA can decrypt SSH at least some of the time. If you have not, then read the latest batch of Snowden documents now. All of it. This post will still be here when you finish. My goal with this post here is to [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6453"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6453"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6453\/revisions"}],"predecessor-version":[{"id":6454,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6453\/revisions\/6454"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}