{"id":14283,"date":"2017-05-17T08:42:02","date_gmt":"2017-05-17T15:42:02","guid":{"rendered":"http:\/\/ainslies.net\/?p=14283"},"modified":"2017-05-17T09:02:02","modified_gmt":"2017-05-17T16:02:02","slug":"socat-sslh-and-stunnel-to-share-https-port-443","status":"publish","type":"post","link":"https:\/\/ainslies.net\/?p=14283","title":{"rendered":"Socat, sslh and stunnel to share https port 443"},"content":{"rendered":"<p>The instructions below assume you are using Ubuntu 16.04 but they will work for other Linuxes with minor modifications. The instructions below will also conflict with a webserver listening on port 443 ( https ) so you&#8217;ll need to disable it. Once the setup below is complete your https connections will get seamlessly forwarded to port 80. <\/p>\n<h2>Setup letsencrypt keys<\/h2>\n<p>For the SSL connection to be secure and trusted by browsers and other software you need to have a certificate signed by a recognised certificate authority. The easiest way to do this is to use <a href=\"https:\/\/letsencrypt.org\/\">letsencypt&#8217;s<\/a> certbot. I&#8217;m not going to go into how to get the certificate as there are too many ways depending on your configuration. Just follow letsencrypt&#8217;s documentation to generate a key for your &quot;servernname.com&quot; that will get used in the rest of these instructions.<\/p>\n<p>You could also use a self signed key but that may cause you problems with stateful firewalls.<\/p>\n<h2>Setup stunnel<\/h2>\n<p>With your certificate installed on the server you can now setup <a href=\"http:\/\/stunnel.org\">stunnel<\/a> to use it. crow shows a partial setup <a href=\"https:\/\/community.letsencrypt.org\/t\/configure-stunnel\/3611\">here<\/a>. I think he&#8217;s limited the ciphers for increased security but I found it was not necessary. <\/p>\n<p>Install stunnel4<\/p>\n<pre>\r\nsudo apt-get install stunnel4\r\n<\/pre>\n<p>So the setup in \/etc\/stunnel\/stunnel.conf I am using looks like this<\/p>\n<pre>\r\npid = \/var\/run\/stunnel.pid\r\ncert = \/etc\/letsencrypt\/live\/servername.com\/fullchain.pem\r\nkey = \/etc\/letsencrypt\/live\/servername.com\/privkey.pem\r\n[ssh]\r\naccept = servername.com:443\r\nconnect = localhost:80\r\n<\/pre>\n<p>You also need to enable stunnel in \/etc\/default\/stunnel4.conf by setting ENABLED=1<\/p>\n<p>restart stunnel to use the new configuration.<\/p>\n<pre>\r\nsystemctl restart stunnel4\r\n<\/pre>\n<p>At this point you can test the stunnel setup by going to &#8220;http:\/\/servername.com&#8221; with your browser and you will have a secure connection to your http server.<\/p>\n<p>To prep for the sslh configuration change <\/p>\n<pre>\r\nconnect = localhost:80\r\n<\/pre>\n<p>in \/etc\/stunnel\/stunnel.conf to <\/p>\n<pre>\r\nconnect = localhost:1022\r\n<\/pre>\n<p> and then restart stunnel again.<\/p>\n<h2>Setup sslh<\/h2>\n<p><a href=\"http:\/\/www.rutschle.net\/sslh\">sslh<\/a> will redirect the sessions decrypted by stunnel to the correct port on your server. <\/p>\n<p>You need to install sslh<\/p>\n<pre>\r\nsudo apt-get install sslh\r\n<\/pre>\n<p>The minimum services I wanted are ssh and http so my configuration in \/etc\/default\/sslh looks like this.<\/p>\n<pre>\r\nRUN=yes\r\n\r\n# binary to use: forked (sslh) or single-thread (sslh-select) version\r\nDAEMON=\/usr\/sbin\/sslh\r\n\r\nDAEMON_OPTS=\"--user sslh --listen 127.0.0.1:1022 --http 127.0.0.1:80 --ssh 127.0.0.1:22 --pidfile \/var\/run\/sslh\/sslh.pid\"\r\n<\/pre>\n<p>The sslh documentation says that OpenVPN, tinc, XMPP are also supported but I didn&#8217;t need those so my configuration doesn&#8217;t support them. You can now restart sslh <\/p>\n<pre>\r\nsudo systemctl restart sslh\r\n<\/pre>\n<p>This would be another good time to test the stunnel -> sslh -> httpd redirection by visiting  &#8220;http:\/\/servername.com&#8221; in your browser.<\/p>\n<h2>Client side ssh setup<\/h2>\n<p>Once all of the above is complete and assuming that you have an ssh server that you can connect to on port 22 of your server the ssh client can be setup to use the ssl tunnel. The ssh session needs to wrapped in the ssl session to be able to connect to the server so I used the ssh ProxyCommand to accomplish this. Add the section below to your ~\/.ssh\/config on your client machine<\/p>\n<pre>\r\nHost servername.com\r\nProxyCommand \/usr\/bin\/socat - OPENSSL:servername.com:443\r\n<\/pre>\n<p>From the client you should now be able to connect to your server by doing <\/p>\n<pre>\r\nssh servername.com\r\n<\/pre>\n<p>If you get errors from ProxyCommand about your keys or if you used a self signed certificate you will need to turn off key verification.<\/p>\n<pre>\r\nHost servername.com\r\nProxyCommand \/usr\/bin\/socat - OPENSSL:servername.com:443,verify=0\r\n<\/pre>\n<p>There is usually one other modification I have in my ssh config and that is a DynamicProxy so that stateful packet inspection doesn&#8217;t interfere. So the final configuration looks like this.<\/p>\n<pre>\r\nHost servername.com\r\nDynamicForward localhost:2121\r\nProxyCommand \/usr\/bin\/socat - OPENSSL:servername.com:443\r\n<\/pre>\n<p>The interested reader should look into FoxyProxy to see how this might be used.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The instructions below assume you are using Ubuntu 16.04 but they will work for other Linuxes with minor modifications. The instructions below will also conflict with a webserver listening on port 443 ( https ) so you&#8217;ll need to disable &hellip; <a href=\"https:\/\/ainslies.net\/?p=14283\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-14283","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/ainslies.net\/index.php?rest_route=\/wp\/v2\/posts\/14283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ainslies.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ainslies.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ainslies.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ainslies.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=14283"}],"version-history":[{"count":7,"href":"https:\/\/ainslies.net\/index.php?rest_route=\/wp\/v2\/posts\/14283\/revisions"}],"predecessor-version":[{"id":14290,"href":"https:\/\/ainslies.net\/index.php?rest_route=\/wp\/v2\/posts\/14283\/revisions\/14290"}],"wp:attachment":[{"href":"https:\/\/ainslies.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ainslies.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ainslies.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}