ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Windows 中的 OpenSSH 详解

2022-06-02 15:31:24  阅读:212  来源: 互联网

标签:私钥 Windows OpenSSH 详解 ssh 服务器 SSH


Windows 中的 OpenSSH

OpenSSH 是安全 Shell (SSH) 工具的开放源代码版本,Linux 及其他非 Windows 系统的管理员使用此类工具跨平台管理远程系统。 OpenSSH 在 2018 年秋季已添加至 Windows,并包含在 Windows 10 和 Windows Server 2019 中。

SSH 基于客户端-服务器体系结构,用户在其中工作的系统是客户端,所管理的远程系统是服务器。 OpenSSH 包含一系列组件和工具,用于提供一种安全且简单的远程系统管理方法,其中包括:

  • sshd.exe,它是远程所管理的系统上必须运行的 SSH 服务器组件
  • ssh.exe,它是在用户的本地系统上运行的 SSH 客户端组件
  • ssh-keygen.exe,为 SSH 生成、管理和转换身份验证密钥
  • ssh-agent.exe,存储用于公钥身份验证的私钥
  • ssh-add.exe,将私钥添加到服务器允许的列表中
  • ssh-keyscan.exe,帮助从许多主机收集公用 SSH 主机密钥
  • sftp.exe,这是提供安全文件传输协议的服务,通过 SSH 运行
  • scp.exe 是在 SSH 上运行的文件复制实用工具

本部分中的文档重点介绍了如何在 Windows 上使用 OpenSSH,包括安装以及特定于 Windows 的配置和用例。

  • 有关常见 OpenSSH 功能的其他详细文档,请参阅 OpenSSH.com

  • OpenSSH 开源项目是由 OpenBSD 项目的开发人员管理的。 此项目的 Microsoft 分支在 GitHub中。 欢迎提供有关 Windows OpenSSH 的反馈,可以通过在我们的 OpenSSH GitHub 存储库中创建 GitHub 问题来提供反馈。

 

 

出处:https://docs.microsoft.com/zh-cn/windows-server/administration/openssh/openssh_overview

=======================================================================================

OpenSSH Manual Pages


Web manual pages are available from OpenBSD for the following commands. These manual pages reflect the latest development release of OpenSSH.

  • ssh(1) — The basic rlogin/rsh-like client program
  • sshd(8) — The daemon that permits you to log in
  • ssh_config(5) — The client configuration file
  • sshd_config(5) — The daemon configuration file
  • ssh-agent(1) — An authentication agent that can store private keys
  • ssh-add(1) — Tool which adds keys to in the above agent
  • sftp(1) — FTP-like program that works over SSH1 and SSH2 protocol
  • scp(1) — File copy program that acts like rcp
  • ssh-keygen(1) — Key generation tool
  • sftp-server(8) — SFTP server subsystem (started automatically by sshd)
  • ssh-keyscan(1) — Utility for gathering public host keys from a number of hosts
  • ssh-keysign(8) — Helper program for host-based authentication

The SSH2 protocol implemented in OpenSSH is standardized by the IETF secsh working group and is specified in several RFCs and drafts. The overall structure of SSH2 is described in the architecture RFC. It is composed of three layered components:

  • The transport layer provides algorithm negotiation and a key exchange. The key exchange includes server authentication and results in a cryptographically secured connection: it provides integrity, confidentiality and optional compression.
  • The user authentication layer uses the established connection and relies on the services provided by the transport layer. It provides several mechanisms for user authentication. These include traditional password authentication as well as public-key or host-based authentication mechanisms.
  • The connection layer multiplexes many different concurrent channels over the authenticated connection and allows tunneling of login sessions and TCP-forwarding. It provides a flow control service for these channels. Additionally, various channel-specific options can be negotiated.

Additional documents specify:

  • The interactive authentication RFC provides support for new authentication schemes like S/Key or TIS authentication.
  • The SFTP file transfer protocol is specified in the filexfer draft. OpenSSH implements a SFTP client and server.
  • A file format for public keys is specified in the publickeyfile draft. The command ssh-keygen(1) can be used to convert an OpenSSH public key to this file format.
  • The Diffie-Hellman Group Exchange allows clients to request more secure groups for the Diffie-Hellman key exchange.
  • OpenSSH implemented a compression method "zlib@openssh.com" that delays starting compression until after user authentication, to eliminate the risk of pre-authentication attacks against the compression code. It is described in draft-miller-secsh-compression-delayed-00.txt.
  • OpenSSH implements an additional MAC (Message Authentication Code) "umac-64@openssh.com", which has superior performance to the ones specified in RFC 4253. It is described in draft-miller-secsh-umac-01.txt.
  • The authentication agent protocol used by ssh-agent is documented in the PROTOCOL.agent file.
  • OpenSSH makes various other minor extensions to and divergences from the standard SSH protocols. These are documented in the PROTOCOL file.

 

出处:https://www.openssh.com/manual.html

=======================================================================================

Windows使用OpenSSH

如何在 Windows 上使用 OpenSSH,包括安装以及特定于 Windows 的配置和用例?

    OpenSSH 是安全 Shell (SSH) 工具的开放源代码版本,Linux 及其他非 Windows 系统的管理员使用此类工具跨平台管理远程系统。   OpenSSH 在 2018 年秋季已添加至 Windows,并包含在 Windows 10 和 Windows Server 2019 中。

 

有关常见 OpenSSH 功能的其他详细文档,请参阅

 

主  OpenSSH 开源项目是由 OpenBSD 项目的开发人员管理的。   此项目的 Microsoft 分支在  GitHub中。  

 

维基:(Building OpenSSH for Windows (using LibreSSL crypto))

 

  

    SSH 基于客户端-服务器体系结构,用户在其中工作的系统是客户端,所管理的远程系统是服务器。  OpenSSH 包含一系列组件和工具,用于提供一种安全且简单的远程系统管理方法,其中包括:

  • sshd.exe,它是远程所管理的系统上必须运行的 SSH 服务器组件
  • ssh.exe,它是在用户的本地系统上运行的 SSH 客户端组件
  • ssh-keygen.exe,为 SSH 生成、管理和转换身份验证密钥
  • ssh-agent.exe,存储用于公钥身份验证的私钥
  • ssh-add.exe,将私钥添加到服务器允许的列表中
  • ssh-keyscan.exe,帮助从许多主机收集公用 SSH 主机密钥
  • sftp.exe,这是提供安全文件传输协议的服务,通过 SSH 运行
  • scp.exe 是在 SSH 上运行的文件复制实用工具

1、从 Windows Server 2019 或 Windows 10 1809 (surface7 Pro windows10 version2004 20H2)上的“设置”UI 安装 OpenSSH

OpenSSH 客户端和服务器是 Windows 10 1809 的可安装功能。

若要安装 OpenSSH,请启动“设置”,然后转到“应用”>“应用和功能”>“管理可选功能”。

扫描此列表,查看 OpenSSH 客户端是否已安装。  如果没有,则在页面顶部选择“添加功能”,然后:

  • 若要安装 OpenSSH 客户端,请找到“OpenSSH 客户端”,然后单击“安装”。
  • 若要安装 OpenSSH 服务器,请找到“OpenSSH 服务器”,然后单击“安装”。

安装完成后,请返回“应用”>“应用和功能”>“管理可选功能”,你应当会看到列出的 OpenSSH 组件。

安装 OpenSSH 服务器将创建并启用名为“OpenSSH-Server-In-TCP”的防火墙规则。   这允许端口 22 上的入站 SSH 流量。

PS C:\Users\mi_zy> get-netfirewallrule -name *ssh*

Name                  : OpenSSH-Server-In-TCP

DisplayName           : OpenSSH SSH Server (sshd)

Description           : Inbound rule for OpenSSH SSH Server (sshd)

DisplayGroup          : OpenSSH Server

Group                 : OpenSSH Server

Enabled               : True

Profile               : Any

Platform              : {}

Direction             : Inbound

Action                : Allow

EdgeTraversalPolicy   : Block

LooseSourceMapping    : False

LocalOnlyMapping      : False

Owner                 :

PrimaryStatus         : OK

Status                : 已从存储区成功分析规则。 (65536)

EnforcementStatus     : NotApplicable

PolicyStoreSource     : PersistentStore

PolicyStoreSourceType : Local

 

2.1、 通过 PowerShell 安装 OpenSSH

(含步骤和安装包链接)

若要使用 PowerShell 安装 OpenSSH,请首先以管理员身份启动 PowerShell。  若要确保 OpenSSH 功能可以安装,请执行以下操作:

Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'

然后,安装服务器和/或客户端功能:

# Install the OpenSSH Client

Add-WindowsCapability -Online -Name OpenSSH.Client~~~~ 0.0 . 1.0

# Install the OpenSSH Server

Add-WindowsCapability -Online -Name OpenSSH.Server~~~~ 0.0 . 1.0

2.2、 就像 Linux 有 apt-get, mac 有 homebrew 一样,windows 也有自己的 package manager:  chocolatey, This package was approved as   on 14 Jan 2020

https://blog.csdn.net/hffyyg/article/details/83990873(安装  chocolatey-OK)

管理员权限打开PowerShell:( 执行iex(invoke expression Hook)函数)

iex ((new-object net.webclient).DownloadString('))

 

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('))

cmd运行:

 

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

(卸载chocolatey-OK)

 

用 choco install 或 cinst(choco install的简写)加上软件包的名字:
choco install <packagename>
cinst <packagename>
通过传统方式安装软件,如果配置了环境变量,例如JAVA_HOME,PATH等等,都需要重启命令行工具,甚至要等一会才能生效。Chocolatey提供了一个refreshenv命令,可以快速生效环境变量。

To install Win32 OpenSSH (Universal Installer), run the following command from the command line or from PowerShell:

    PS>choco install openssh --pre 

 

3、 SSH 服务器的初始配置

若要配置 OpenSSH 服务器以在 Windows 上首次使用,请以管理员身份启动 PowerShell,然后运行以下命令来启动 SSHD 服务:

 

Start-Service sshd

# OPTIONAL but recommended:

Set-Service -Name sshd -StartupType 'Automatic'

# Confirm the Firewall rule is configured. It should be created automatically by setup.

Get-NetFirewallRule -Name *ssh*

# There should be a firewall rule named "OpenSSH-Server-In-TCP", which should be enabled

# If the firewall does not exist, create one

New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22

首次使用 SSH

在 Windows 上安装 OpenSSH 服务器后,可以从安装了 SSH 客户端的任何 Windows 设备上使用 PowerShell 来快速测试它。  在 PowerShell 中,键入以下命令:

ssh username@servername

到任何服务器的第一个连接都将生成类似以下内容的消息:

The authenticity of host 'servername (10.00.00.001)' can't be established.
ECDSA key fingerprint is SHA256:(<a large string>).
Are you sure you want to continue connecting (yes/no)?

回答必须是“yes”或“no”。  回答 Yes 会将该服务器添加到本地系统的已知 ssh 主机列表中。

系统此时会提示你输入密码。  作为安全预防措施,密码在键入的过程中不会显示。

在连接后,你将看到类似于以下内容的命令 shell 提示符:

domain\username@SERVERNAME C:\Users\username>

Windows OpenSSH 服务器使用的默认 shell 是 Windows 命令行解释器。

 

4、 Sshd_config 中的 Windows 配置

    在 Windows 中,sshd 默认情况下从 %programdata%\ssh\sshd_config 中读取配置数据,也可以通过使用 -f 参数启动 sshd 来指定不同的配置文件。  如果该文件不存在,则在启动该服务时,sshd 将使用默认配置生成一个文件。

下面列出的元素提供了通过 sshd_config 中的条目可以实现的特定于 Windows 的配置。  可以在其中实现的其他一些配置设置在此处没有列出, 因为在线  Win32 OpenSSH 文档 中详细介绍了它们。

 

使用 AllowGroups、AllowUsers、DenyGroups 和 DenyUsers 指令控制哪些用户和组可以连接到服务器。  allow/deny 指令按以下顺序处理:DenyUsers、AllowUsers、DenyGroups,最后是 AllowGroups。  必须以小写形式指定所有帐户名称。  有关通配符模式的详细信息,请参阅 ssh_config 中的 PATTERNS。

当使用域用户或组配置基于用户/组的规则时,请使用以下格式: user?domain*。  Windows 允许使用多种格式来指定域主体,但许多格式与标准 Linux 模式冲突。  因此,添加了 * 来涵盖 FQDN。  此外,此方法使用了 "?"(而非 @)来避免与 username@host 格式发生冲突。

 

5、 Windows Server 2019 和 Windows 10 1809 中附带的 OpenSSH 版本中未提供以下配置选项:

 

  • AllowStreamLocalForwarding
  • 压缩
  • X11DisplayOffset
  • X11Forwarding

  • —— Specifies whether X11 connections will be automatically redirected over the secure channel and  DISPLAY  set. The argument must be  yes  or  no  (the default).

  • X11 forwarding should be enabled with caution. Users with the ability to bypass file permissions on the remote host (for the user's X11 authorization database) can access the local X11 display through the forwarded connection. An attacker may then be able to perform activities such as keystroke monitoring if the  ForwardX11Trusted option is also enabled( If this option is set to  yes , remote X11 clients will have full access to the original X11 display.)

  • X11UseLocalhost
  • XAuthLocation

  • IgnoreRhosts

  • IgnoreUserKnownHosts


6、 Windows 环境中的大多数身份验证都是使用用户名-密码对完成的。  这适用于共享公共域的系统。  跨域工作时(例如在本地和云托管的系统之间),很容易受到暴力攻击入侵。

相比之下,Linux 环境通常使用公钥/私钥对来驱动身份验证,这不要求使用可推测的密码。  OpenSSH 提供了工具来帮助支持此用途,具体如下:

  • ssh-keygen ,用于生成安全的密钥
  • ssh-agent 和  ssh-add ,用于安全地存储私钥
  • scp 和  sftp ,在首次使用服务器时安全地复制公钥文件

本文档概述了如何在 Windows 上使用这些工具开始使用 SSH 进行密钥身份验证。  如果你不熟悉 SSH 密钥管理,我们强烈建议你查看  NIST 文档 IR 7966,标题为“使用安全外壳 (SSH) 的交互和自动化访问管理的安全性”。

    SSH 公钥身份验证使用不对称加密算法来生成两个密钥文件 – 一个为“私钥”文件,一个为“公钥”文件。   私钥文件等效于密码,在所有情况下都应当保护它们。   如果有人获取了你的私钥,则他们可以像你一样登录到你有权登录的任何 SSH 服务器。   公钥放置在 SSH 服务器上,并且可以共享,不会危害私钥的安全。

—— 将密钥身份验证用于 SSH 服务器时,SSH 服务器和客户端会依据私钥来比较所提供的用户名的公钥。   如果无法依据客户端私钥验证服务器端公钥,则身份验证失败。

 

若要使用基于密钥的身份验证,首先需要为客户端生成一些公钥/私钥对。  通过 PowerShell 或 cmd,使用 ssh-keygen 生成一些密钥文件。

cd ~\.ssh\ssh-keygen

有了一个公钥/私钥 ED25519 密钥对(.pub 文件是公钥 id_ed25519.pub,其余的是私钥 id_ed25519: 强烈建议你将私钥备份到一个安全位置,将其添加到 ssh-agent,然后将其从本地系统中删除。)

请记住,私钥文件等效于密码,应当采用与保护密码相同的方式来保护它。   为了实现此目的,请使用 ssh-agent 来将私钥安全地存储在与你的 Windows 登录关联的 Windows 安全上下文中。   为执行该操作,请以管理员身份启动 ssh-agent 服务并使用 ssh-add 来存储私钥。

# Now load your key files into ssh-agent

ssh-add ~\.ssh\id_ed25519

 

部署公钥:

若要使用上面创建的用户密钥,需要将公钥放置在服务器上的一个文本文件中,该文件名为 authorized_keys,位于 users\username\.ssh\ 下。  OpenSSH 工具包括了 scp 来帮助实现此目的,这是一个安全的文件传输实用工具。

将公钥 (~.ssh\id_ed25519.pub) 的内容移动到服务器/主机上 ~.ssh 中名为 authorized_keys 的文本文件中。

此示例使用了之前在上面的说明中在主机上安装的 OpenSSHUtils 模块中的 Repair-AuthorizedKeyPermissions 函数。

# Make sure that the .ssh directory exists in your server's home folder

ssh user1@domain1@contoso.com mkdir C:\users\user1\.ssh\

# Use scp to copy the public key file generated previously to authorized_keys on your server

scp C:\Users\user1\.ssh\id_ed25519.pub user1@domain1@contoso.com:C:\Users\user1\.ssh\authorized_keys

# Appropriately ACL the authorized_keys file on your server

ssh --% user1@domain1@contoso.com powershell -c $ConfirmPreference = 'None'; Repair-AuthorizedKeyPermission C:\Users\user1\.ssh\authorized_keys

这些步骤完成了对 Windows 上的 SSH 使用基于密钥的身份验证所需的配置。  完成此项后,用户可以从具有私钥的任何客户端连接到 sshd 主机。


7、ssh命令参数官方说明:https://man.openbsd.org/ssh

 

 

出处:http://blog.itpub.net/7970627/viewspace-2757011/

标签:私钥,Windows,OpenSSH,详解,ssh,服务器,SSH
来源: https://www.cnblogs.com/mq0036/p/16337890.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有