{"id":775,"date":"2023-01-14T12:21:44","date_gmt":"2023-01-14T10:21:44","guid":{"rendered":"https:\/\/www.gccdat.be\/main\/?p=775"},"modified":"2023-01-14T12:22:59","modified_gmt":"2023-01-14T10:22:59","slug":"installing-openssl-to-enable-an-sftp-server-on-windows-server-2022","status":"publish","type":"post","link":"https:\/\/www.gccdat.be\/main\/2023\/01\/14\/installing-openssl-to-enable-an-sftp-server-on-windows-server-2022\/","title":{"rendered":"Installing OpenSSL to enable an SFTP server on Windows Server 2022"},"content":{"rendered":"\n<p>Windows Server for a long time only supported an FTPS server (FTP over SSL\/TLS) via the included Internet Information Server. But FTPS in not very firewall friendly and is rather difficult to configure.<br>SFTP (FTP over SSH) is currently considered the best option to use, and is recognized by (nearly) all firewalls out of the box. If not, only port 22 needs to be opened up.<\/p>\n\n\n\n<p>You can install OpenSSL on a computer using the GUI, but for easy repicability we will use powershell commands. Second, we will install everything form the online source. You could adapt the commands to use the windows server 2022 installation media.<\/p>\n\n\n\n<p>First lets check if it&#8217;s not already installed:<\/p>\n\n\n\n<p><br><strong>Get-Service -Name &#8220;sshd&#8221;<\/strong><\/p>\n\n\n\n<p>If the answer is like below, it&#8217;s already installed, so skip the installation part.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Status   Name               DisplayName                           \n------   ----               -----------                           \nRunning  sshd               OpenSSH SSH Server    <\/code><\/pre>\n\n\n\n<p><strong>Retrieve installed an not intalled capabilities\/options<\/strong><\/p>\n\n\n\n<p><strong>Get-WindowsCapability -Online | Where Name -Like &#8220;OpenSSH*&#8221;<\/strong><\/p>\n\n\n\n<p>This will reveal the real names we must use in further commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Name  : OpenSSH.Client~~~~0.0.1.0\nState : Installed\n\nName  : OpenSSH.Server~~~~0.0.1.0\nState : Installed<\/code><\/pre>\n\n\n\n<p>If it&#8217;s not installed you can install it by:<br><strong>Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0<\/strong><br>if you also want the clienttools:<br><strong>dd-WindowsCapability -Online -Name<\/strong> OpenSSH.Client~~~~0.0.1.0<\/p>\n\n\n\n<p>The installed service will not automatically start if you do not tell it so. So let&#8217;s do that, and also start it for the very first time in one go:<br><br><strong>Get-Service -Name &#8220;sshd&#8221; | Set-Service -StartupType Automatic -PassThru | Start-Service -PassThru<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common changes to the OpenSSL config file.<\/h2>\n\n\n\n<p>The OpenSSL config file, is located in %ProgramData%\\ssh\\sshd_config (being c:\\ProgramData\\ssh\\sshd_config.)<br>You can make changes in this file like changing commented out #Port 22 to Port 2223 (removing the # at the front), to tell OpenSSH to use port 2223 for example.<br>After every change to this file, you will need to restart OpenSSH with the powershell command:<br><strong>Restart-Service &#8220;sshd&#8221;<\/strong><\/p>\n\n\n\n<p>Inspecting the usage of port 22 to see if opensll is running and listening, we use a powershell command. It returns the processid owning the port, and we translate that to the process name for readability:<br><br><strong>Get-NetTCPConnection -LocalPort 22 | select Local*, State, @{n=&#8221;ProcessName&#8221;;e={(Get-Process -Id $_.OwningProcess).ProcessName}}, @{n=&#8221;ProcessPath&#8221;;e={(Get-Process -Id $_.OwningProcess).Path}} | ft -Auto<\/strong><\/p>\n\n\n\n<p>It returns:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>LocalAddress LocalPort       State ProcessName ProcessPath                         \n------------ ---------       ----- ----------- -----------                         \n::                  22      Listen sshd        C:\\Windows\\System32\\OpenSSH\\sshd.exe\n192.168.1.75        22 Established sshd        C:\\Windows\\System32\\OpenSSH\\sshd.exe\n0.0.0.0             22      Listen sshd        C:\\Windows\\System32\\OpenSSH\\sshd.exe<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Only Allow SFTP and not SSH into a system<\/h2>\n\n\n\n<p>After a default installation, both sftp and ssh have been enabled. So, after setting up security (see further) a users may use both. In our case, we want to ristrict this to only allow SFTP, and only allow access to one folder on the system, to which sftp access will be possible::<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Locate this line and place below:\n\n# override default of no subsystems\n#Added to allow not ssh and only sftp\n#Force the use of the openssl internal sftp server\r\nForceCommand internal-sftp\n\r#Restricht the sftp server to c:\\sftproot\nSubsystem sftp sftp-server.exe -d \"c:\\sftproot\"\n#Make this the rootdirectory, when accessing by SFtp\r\nChrootDirectory c:\\sftproot \n#<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Allow or deny connections<\/h2>\n\n\n\n<p>The OpenSSH server in windows is configured in this file. It also allows you to connect it&#8217;s unix style of configuration to the windows styte of security. You can use windows security to tie op windows groups to this file based security. To do so you will use the directives<strong> DenyUsers, AllowUsers, DenyGroups and AllowGroups<\/strong> in de configuration file.<br>Note that these directives are processed top to bottom in the configuration file.  If one of them is processed, and you will get access or are denied access, other directives will not be processed anymore.<\/p>\n\n\n\n<p>Also, place <strong>DenyUsers, AllowUsers, DenyGroups and AllowGroups<\/strong> before the <strong>Match Group Administrators<\/strong> directive for the same reason.<br><br>Examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DenyUsers     contoso\\admin@192.168.2.23\r\nDenyUsers     contoso\\*\r\nAllowGroups   contoso\\sshusers contoso\\serveroperators\n\nOr:\n\nAllowUsers    localuser@192.168.2.23\r\nAllowGroups   sshusers<\/code><\/pre>\n\n\n\n<p><strong>Remark<\/strong>: Specify account name in lowercase only. Specify domain accounts in UPN format, replacing @ with ? to avoid conflcts with regular Linux expressions (Example: mve@aca.nl -> mve?aca*)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Restrict SFPT acces to a subfolder only<\/h2>\n\n\n\n<p>Use the ChrootDirectory directive to reach this goal.<br>This directive is only supported with sftp sessions. A remote session into cmd.exe wouldn&#8217;t honor the ChrootDirectory. To set up a sftp-only chroot server, set ForceCommand to internal-sftp. You may also set up scp with chroot, by implementing a custom shell that would only allow scp and sftp.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Logging<\/h2>\n\n\n\n<p>It&#8217;s considered good practice to use logging of the connections to your server. OpenSSH supports ETW (Event Tracing for Windows) based logging and file based logging. If you want to use file based logging, It&#8217;s controlled by the <strong>SyslogFacility <\/strong>and <strong>LogLevel <\/strong>directives in the configuration file.  The logfiles will be save to the <strong>%ProgramData%\\ssh\\logs<\/strong> directory if you add below lines to your windows OpenSSL configuration file. If you leave out the Syslogfacility line, it will use ETW. But when using file based logging, you are responsible for cleanup!<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SyslogFacility LOCAL0\nLogLevel Debug 3<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">More Information<\/h2>\n\n\n\n<p>See <a rel=\"noreferrer noopener\" href=\"https:\/\/learn.microsoft.com\/en-us\/windows-server\/administration\/openssh\/openssh_server_configuration\" target=\"_blank\">https:\/\/learn.microsoft.com\/en-us\/windows-server\/administration\/openssh\/openssh_server_configuration<\/a> for more information.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Windows Server for a long time only supported an FTPS server (FTP over SSL\/TLS) via the included Internet Information Server. But FTPS in not very firewall friendly and is rather difficult to configure.SFTP (FTP over SSH) is currently considered the best option to use, and is recognized by (nearly) all firewalls out of the box.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[69,72,71,70],"class_list":["post-775","post","type-post","status-publish","format-standard","hentry","category-algemeen","tag-openssl","tag-sftp","tag-windows-11","tag-windows-server-2022"],"_links":{"self":[{"href":"https:\/\/www.gccdat.be\/main\/wp-json\/wp\/v2\/posts\/775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gccdat.be\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gccdat.be\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gccdat.be\/main\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gccdat.be\/main\/wp-json\/wp\/v2\/comments?post=775"}],"version-history":[{"count":4,"href":"https:\/\/www.gccdat.be\/main\/wp-json\/wp\/v2\/posts\/775\/revisions"}],"predecessor-version":[{"id":779,"href":"https:\/\/www.gccdat.be\/main\/wp-json\/wp\/v2\/posts\/775\/revisions\/779"}],"wp:attachment":[{"href":"https:\/\/www.gccdat.be\/main\/wp-json\/wp\/v2\/media?parent=775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gccdat.be\/main\/wp-json\/wp\/v2\/categories?post=775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gccdat.be\/main\/wp-json\/wp\/v2\/tags?post=775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}