{"id":79,"date":"2010-01-26T05:11:06","date_gmt":"2010-01-26T05:11:06","guid":{"rendered":"http:\/\/poojanwagh.opalstacked.com\/techblog\/?p=79"},"modified":"2010-01-26T05:12:12","modified_gmt":"2010-01-26T05:12:12","slug":"locking-down-a-publicly-exposed-ssh-server","status":"publish","type":"post","link":"https:\/\/tech.poojanblog.com\/blog\/unix-linux\/locking-down-a-publicly-exposed-ssh-server\/","title":{"rendered":"Locking down a publicly-exposed ssh server"},"content":{"rendered":"<p>I expose an ssh server to the internet. This allows me to sync up git or hg repositories under a secure connection (without requiring SSL)&#8211;but more importantly, keeping the data stored in those repositories under my control.<br \/>\nSince I&#8217;m fairly paranoid about security, I chose a random port number (my router routes the random public port to the usual internal port: 22). In addition, I disable password login, which means that I can only log-in using an ssh key. More importantly, other people can&#8217;t login using passwords&#8211;which means that there&#8217;s not possibility of a dictionary (nor simple brute-force) attack.<br \/>\nHere&#8217;s the <code>sshd_config<\/code> file for FreeBSD that does this:<br \/>\n<code><br \/>\n#\t$OpenBSD: sshd_config,v 1.74 2006\/07\/19 13:07:10 dtucker Exp $<br \/>\n#\t$FreeBSD: src\/crypto\/openssh\/sshd_config,v 1.42.2.4 2006\/11\/11 00:51:28 des Exp $<\/p>\n<p># This is the sshd server system-wide configuration file.  See<br \/>\n# sshd_config(5) for more information.<\/p>\n<p># This sshd was compiled with PATH=\/usr\/bin:\/bin:\/usr\/sbin:\/sbin<\/p>\n<p># The strategy used for options in the default sshd_config shipped with<br \/>\n# OpenSSH is to specify options with their default value where<br \/>\n# possible, but leave them commented.  Uncommented options change a<br \/>\n# default value.<\/p>\n<p># Note that some of FreeBSD&#8217;s defaults differ from OpenBSD&#8217;s, and<br \/>\n# FreeBSD has a few additional options.<\/p>\n<p>#VersionAddendum FreeBSD-20061110<\/p>\n<p>#Port 22<br \/>\n#Protocol 2<br \/>\n#AddressFamily any<br \/>\n#ListenAddress 0.0.0.0<br \/>\n#ListenAddress ::<\/p>\n<p># HostKey for protocol version 1<br \/>\n#HostKey \/etc\/ssh\/ssh_host_key<br \/>\n# HostKeys for protocol version 2<br \/>\n#HostKey \/etc\/ssh\/ssh_host_dsa_key<\/p>\n<p># Lifetime and size of ephemeral version 1 server key<br \/>\n#KeyRegenerationInterval 1h<br \/>\n#ServerKeyBits 768<\/p>\n<p># Logging<br \/>\n# obsoletes QuietMode and FascistLogging<br \/>\n#SyslogFacility AUTH<br \/>\n#LogLevel INFO<\/p>\n<p># Authentication:<\/p>\n<p>#LoginGraceTime 2m<br \/>\n#PermitRootLogin no<br \/>\n#StrictModes yes<br \/>\n#MaxAuthTries 6<\/p>\n<p>#RSAAuthentication yes<br \/>\n#PubkeyAuthentication yes<br \/>\n#AuthorizedKeysFile\t.ssh\/authorized_keys<\/p>\n<p># For this to work you will also need host keys in \/etc\/ssh\/ssh_known_hosts<br \/>\n#RhostsRSAAuthentication no<br \/>\n# similar for protocol version 2<br \/>\n#HostbasedAuthentication no<br \/>\n# Change to yes if you don&#8217;t trust ~\/.ssh\/known_hosts for<br \/>\n# RhostsRSAAuthentication and HostbasedAuthentication<br \/>\n#IgnoreUserKnownHosts no<br \/>\n# Don&#8217;t read the user&#8217;s ~\/.rhosts and ~\/.shosts files<br \/>\n#IgnoreRhosts yes<\/p>\n<p># Change to yes to enable built-in password authentication.<br \/>\nPasswordAuthentication no<br \/>\n#PermitEmptyPasswords no<\/p>\n<p># Change to no to disable PAM authentication<br \/>\nChallengeResponseAuthentication no<\/p>\n<p># Kerberos options<br \/>\n#KerberosAuthentication no<br \/>\n#KerberosOrLocalPasswd  yes<br \/>\n#KerberosTicketCleanup yes<br \/>\n#KerberosGetAFSToken no<\/p>\n<p># GSSAPI options<br \/>\n#GSSAPIAuthentication no<br \/>\n#GSSAPICleanupCredentials yes<\/p>\n<p># Set this to &#8216;no&#8217; to disable PAM authentication, account processing,<br \/>\n# and session processing. If this is enabled, PAM authentication will<br \/>\n# be allowed through the ChallengeResponseAuthentication and<br \/>\n# PasswordAuthentication.  Depending on your PAM configuration,<br \/>\n# PAM authentication via ChallengeResponseAuthentication may bypass<br \/>\n# the setting of &#8220;PermitRootLogin without-password&#8221;.<br \/>\n# If you just want the PAM account and session checks to run without<br \/>\n# PAM authentication, then enable this but set PasswordAuthentication<br \/>\n# and ChallengeResponseAuthentication to &#8216;no&#8217;.<br \/>\n#UsePAM yes<\/p>\n<p>#AllowTcpForwarding yes<br \/>\n#GatewayPorts no<br \/>\n#X11Forwarding yes<br \/>\nX11Forwarding\tno<br \/>\n#X11DisplayOffset 10<br \/>\n#X11UseLocalhost yes<br \/>\n#PrintMotd yes<br \/>\n#PrintLastLog yes<br \/>\n#TCPKeepAlive yes<br \/>\n#UseLogin no<br \/>\n#UsePrivilegeSeparation yes<br \/>\n#PermitUserEnvironment no<br \/>\n#Compression delayed<br \/>\n#ClientAliveInterval 0<br \/>\n#ClientAliveCountMax 3<br \/>\n#UseDNS yes<br \/>\n#PidFile \/var\/run\/sshd.pid<br \/>\n#MaxStartups 10<br \/>\n#PermitTunnel no<br \/>\nPermitTunnel no<\/p>\n<p># no default banner path<br \/>\n#Banner \/some\/path<\/p>\n<p># override default of no subsystems<br \/>\nSubsystem\tsftp\t\/usr\/libexec\/sftp-server<\/p>\n<p># restrict users\/IP&#8217;s<br \/>\nAllowUsers *@192.168.1.* Poojan@*<\/p>\n<p># Example of overriding settings on a per-user basis<br \/>\n#Match User anoncvs<br \/>\n#\tX11Forwarding no<br \/>\n#\tAllowTcpForwarding no<br \/>\n#\tForceCommand cvs server<br \/>\n<\/code><br \/>\nI&#8217;ve disabled all authentication types (including PAM)&#8211;at least the ones that aren&#8217;t disabled by default. Since public key authentication is on by default, I don&#8217;t have to change it.<\/p>\n<p>You&#8217;ll notice that I&#8217;ve disabled tunneling (to disable people from penetrating past the FreeBSD machine into my home network) and chosen not to enable X forwarding (no need for it in this case).<\/p>\n<div class='wp_likes' id='wp_likes_post-79'><a class='like' href=\"javascript:wp_likes.like(79);\" title='' ><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/tech.poojanblog.com\/blog\/wp-content\/plugins\/wp-likes\/images\/like.png\" alt='' border='0'\/><\/a><span class='text'>Be the first to like.<\/span><\/p>\n<div class='like' ><a href=\"javascript:wp_likes.like(79);\">Like<\/a><\/div>\n<div class='unlike' ><a href=\"javascript:wp_likes.unlike(79);\">Unlike<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve disabled all authentication types (including PAM)&#8211;at least the ones that aren&#8217;t disabled by default. Since public key authentication is on by default, I don&#8217;t have to change it.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10],"tags":[12,25,24,21,22,23],"class_list":["post-79","post","type-post","status-publish","format-standard","hentry","category-unix-linux","tag-freebsd","tag-git","tag-hg","tag-security","tag-ssh","tag-sshd"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/posts\/79","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/comments?post=79"}],"version-history":[{"count":2,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/posts\/79\/revisions"}],"predecessor-version":[{"id":81,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/posts\/79\/revisions\/81"}],"wp:attachment":[{"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/media?parent=79"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/categories?post=79"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/tags?post=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}