非常教程

Erlang 20参考手册

ssh

3.在SSH中配置算法 | 3. Configuring algorithms in SSH

3.1游戏攻略

要充分理解如何配置算法,必须对ssh协议和osp ssh应用程序如何处理相应的项有基本的了解。

第一小节将给出ssh协议的简短背景,而后面的部分将描述实现并提供一些示例。

ssh协议算法处理的基础知识

SSH在会话的不同阶段使用不同的算法集。在会话开始时,客户端和服务器会协商使用哪些算法。有关详细信息RFC 4253,请参阅“安全外壳(SSH)传输层协议”。

谈判很简单:两个同伴都将支持的算法列表发送给其他部分。客户列表上的第一个算法也在服务器列表中被选中。所以这是客户对列表的排序,给出了算法的优先级。

在连接设置中交换了五个列表。其中的三个也被分为两个方向,往返于服务器。

这些列表(以SSH应用程序的选项命名):

kex

钥匙交换。

选择算法来计算秘密加密密钥。其中的例子有:今日的旧时代'diffie-hellman-group-exchange-sha1',非常强大和现代的时代'ecdh-sha2-nistp512'

public_key

服务器主机密钥

服务器的公私密钥对中使用的非对称加密算法。例子包括众所周知的RSA 'ssh-rsa'和椭圆曲线'ecdsa-sha2-nistp521'

cipher

用于有效载荷加密的对称密码算法。该算法将使用在kex阶段计算的密钥(连同其他信息)来生成使用的实际密钥。例子是tripple-DES '3des-cbc'和许多AES变体之一'aes192-ctr'

这个列表实际上是每个方向服务器到客户端和客户端到服务器的两个一个。因此,在一个连接的两个方向上有不同的算法是可能的,但很少有。

mac

消息认证码

对等节点之间发送的每条消息的“校验和”。例子是SHA 'hmac-sha1'和SHA2 'hmac-sha2-512'

这个列表也分为两个方向。

compression

如何以及如何压缩消息。例子none是,没有压缩和zlib

这个列表也分为两个方向。

SSH应用程序的机制

SSH应用程序默认使用的算法集取决于

  • crypto阿普,
  • 密码OTP通常与操作系统使用的加密OTP链接,可能是OpenSSL,
  • 最后,SSH应用程序实现了什么?

因此,不可能在文档中列出某些安装中可用的算法。

有一个重要的命令列出实际算法及其排序:ssh:default_algorithms/0

0> ssh:default_algorithms().
[{kex,['ecdh-sha2-nistp384','ecdh-sha2-nistp521',
       'ecdh-sha2-nistp256','diffie-hellman-group-exchange-sha256',
       'diffie-hellman-group16-sha512',
       'diffie-hellman-group18-sha512',
       'diffie-hellman-group14-sha256',
       'diffie-hellman-group14-sha1',
       'diffie-hellman-group-exchange-sha1']},
 {public_key,['ecdsa-sha2-nistp384','ecdsa-sha2-nistp521',
              'ecdsa-sha2-nistp256','ssh-rsa','rsa-sha2-256',
              'rsa-sha2-512','ssh-dss']},
 {cipher,[{client2server,['aes256-gcm@openssh.com',
                          'aes256-ctr','aes192-ctr','aes128-gcm@openssh.com',
                          'aes128-ctr','aes128-cbc','3des-cbc']},
          {server2client,['aes256-gcm@openssh.com','aes256-ctr',
                          'aes192-ctr','aes128-gcm@openssh.com','aes128-ctr',
                          'aes128-cbc','3des-cbc']}]},
 {mac,[{client2server,['hmac-sha2-256','hmac-sha2-512',
                       'hmac-sha1']},
       {server2client,['hmac-sha2-256','hmac-sha2-512',
                       'hmac-sha1']}]},
 {compression,[{client2server,[none,'zlib@openssh.com',zlib]},
               {server2client,[none,'zlib@openssh.com',zlib]}]}]

      

若要更改算法列表,可以在ssh:connect/2,3,4ssh:daemon/2,3当然,这些选项可以用于启动连接的所有其他功能。

选择如下:preferred_algorithmsmodify_algorithms二替换默认集,而后者修改默认集。

3.2替换默认集:首选[医]算法

Reference Manual详情

下面是一系列从简单到更复杂的示例。

为了预见选项的影响,有一个实验功能ssh:chk_algos_opts(Opts)。它轧液的选项preferred_algorithms,并modify_algorithms以同样的方式ssh:dameonssh:connect和自己的朋友一样。

例1

用单一算法替换kex算法列表'diffie-hellman-group14-sha256'

1> ssh:chk_algos_opts(
               [{preferred_algorithms,
                     [{kex, ['diffie-hellman-group14-sha256']}
                     ]
                }
              ]).
[{kex,['diffie-hellman-group14-sha256']},
 {public_key,['ecdsa-sha2-nistp384','ecdsa-sha2-nistp521',
              'ecdsa-sha2-nistp256','ssh-rsa','rsa-sha2-256',
              'rsa-sha2-512','ssh-dss']},
 {cipher,[{client2server,['aes256-gcm@openssh.com',
                          'aes256-ctr','aes192-ctr','aes128-gcm@openssh.com',
                          'aes128-ctr','aes128-cbc','3des-cbc']},
          {server2client,['aes256-gcm@openssh.com','aes256-ctr',
                          'aes192-ctr','aes128-gcm@openssh.com','aes128-ctr',
                          'aes128-cbc','3des-cbc']}]},
 {mac,[{client2server,['hmac-sha2-256','hmac-sha2-512',
                       'hmac-sha1']},
       {server2client,['hmac-sha2-256','hmac-sha2-512',
                       'hmac-sha1']}]},
 {compression,[{client2server,[none,'zlib@openssh.com',zlib]},
               {server2client,[none,'zlib@openssh.com',zlib]}]}]
      

需要注意的是,未提及的列表(public_keyciphermaccompression)是未改变。

例2

在两个方向分为两个的列表(cf cipher)中,可以一次改变两个方向:

2> ssh:chk_algos_opts(
               [{preferred_algorithms,
                     [{cipher,['aes128-ctr']}
                     ]
                }
              ]).
[{kex,['ecdh-sha2-nistp384','ecdh-sha2-nistp521',
       'ecdh-sha2-nistp256','diffie-hellman-group-exchange-sha256',
       'diffie-hellman-group16-sha512',
       'diffie-hellman-group18-sha512',
       'diffie-hellman-group14-sha256',
       'diffie-hellman-group14-sha1',
       'diffie-hellman-group-exchange-sha1']},
 {public_key,['ecdsa-sha2-nistp384','ecdsa-sha2-nistp521',
              'ecdsa-sha2-nistp256','ssh-rsa','rsa-sha2-256',
              'rsa-sha2-512','ssh-dss']},
 {cipher,[{client2server,['aes128-ctr']},
          {server2client,['aes128-ctr']}]},
 {mac,[{client2server,['hmac-sha2-256','hmac-sha2-512',
                       'hmac-sha1']},
       {server2client,['hmac-sha2-256','hmac-sha2-512',
                       'hmac-sha1']}]},
 {compression,[{client2server,[none,'zlib@openssh.com',zlib]},
               {server2client,[none,'zlib@openssh.com',zlib]}]}]
      

请注意,两个列表cipher都已更改为提供的值('aes128-ctr')。

例3

在两个方向上分为两列的列表(cf cipher)中,只能更改其中一个方向:

3> ssh:chk_algos_opts(
               [{preferred_algorithms,
                     [{cipher,[{client2server,['aes128-ctr']}]}
                     ]
                }
              ]).
[{kex,['ecdh-sha2-nistp384','ecdh-sha2-nistp521',
       'ecdh-sha2-nistp256','diffie-hellman-group-exchange-sha256',
       'diffie-hellman-group16-sha512',
       'diffie-hellman-group18-sha512',
       'diffie-hellman-group14-sha256',
       'diffie-hellman-group14-sha1',
       'diffie-hellman-group-exchange-sha1']},
 {public_key,['ecdsa-sha2-nistp384','ecdsa-sha2-nistp521',
              'ecdsa-sha2-nistp256','ssh-rsa','rsa-sha2-256',
              'rsa-sha2-512','ssh-dss']},
 {cipher,[{client2server,['aes128-ctr']},
          {server2client,['aes256-gcm@openssh.com','aes256-ctr',
                          'aes192-ctr','aes128-gcm@openssh.com','aes128-ctr',
                          'aes128-cbc','3des-cbc']}]},
 {mac,[{client2server,['hmac-sha2-256','hmac-sha2-512',
                       'hmac-sha1']},
       {server2client,['hmac-sha2-256','hmac-sha2-512',
                       'hmac-sha1']}]},
 {compression,[{client2server,[none,'zlib@openssh.com',zlib]},
               {server2client,[none,'zlib@openssh.com',zlib]}]}]
      

例4

当然,有可能改变一个以上的清单:

4> ssh:chk_algos_opts(
               [{preferred_algorithms,
                     [{cipher,['aes128-ctr']},
		      {mac,['hmac-sha2-256']},
                      {kex,['ecdh-sha2-nistp384']},
		      {public_key,['ssh-rsa']},
		      {compression,[{server2client,[none]},
		                    {client2server,[zlib]}]}
                     ]
                }
              ]).
[{kex,['ecdh-sha2-nistp384']},
 {public_key,['ssh-rsa']},
 {cipher,[{client2server,['aes128-ctr']},
          {server2client,['aes128-ctr']}]},
 {mac,[{client2server,['hmac-sha2-256']},
       {server2client,['hmac-sha2-256']}]},
 {compression,[{client2server,[zlib]},
               {server2client,[none]}]}]

      

请注意,列表中元组的顺序并不重要。

3.3修改默认设置:modify_algorithms

添加算法可能有用的情况是,需要使用受支持但禁用的算法。一个例子是'diffie-hellman-group1-sha1'现在已经很不安全了,因此是残疾的。然而,它仍然得到支持,并可能被使用。

选择preferred_algorithms用于添加或删除单个算法可能比较复杂。二必须列出ssh:default_algorithms()然后在列表中做修改。

为方便加入或移除演算法,选择modify_algorithms是可用的。见Reference Manual关于细节。

该选项接受一个列表,其中包含附加、预先添加或删除算法的指令:

{modify_algorithms, [{append,  ...},
                     {prepend, ...},
		     {rm,      ...}
		    ]}
    

每个...可以是algs_list()作为对preferred_algorithms选择。

例5

作为一个例子,我们先在kex列表中添加Diffie-Hellman Group1。它根据Supported algoritms。支持。

5> ssh:chk_algos_opts(
         [{modify_algorithms,
	       [{prepend,
	           [{kex,['diffie-hellman-group1-sha1']}]
		   }
	       ]
          }
        ]).
[{kex,['diffie-hellman-group1-sha1','ecdh-sha2-nistp384',
       'ecdh-sha2-nistp521','ecdh-sha2-nistp256',
       'diffie-hellman-group-exchange-sha256',
       'diffie-hellman-group16-sha512',
       'diffie-hellman-group18-sha512',
       'diffie-hellman-group14-sha256',
       'diffie-hellman-group14-sha1',
       'diffie-hellman-group-exchange-sha1']},
 {public_key,['ecdsa-sha2-nistp384','ecdsa-sha2-nistp521',
              'ecdsa-sha2-nistp256','ssh-rsa','rsa-sha2-256',
              'rsa-sha2-512','ssh-dss']},
 {cipher,[{client2server,['aes256-gcm@openssh.com',
                          'aes256-ctr','aes192-ctr','aes128-gcm@openssh.com',
                          'aes128-ctr','aes128-cbc','3des-cbc']},
          {server2client,['aes256-gcm@openssh.com','aes256-ctr',
                          'aes192-ctr','aes128-gcm@openssh.com','aes128-ctr',
                          'aes128-cbc','3des-cbc']}]},
 {mac,[{client2server,['hmac-sha2-256','hmac-sha2-512',
                       'hmac-sha1']},
       {server2client,['hmac-sha2-256','hmac-sha2-512',
                       'hmac-sha1']}]},
 {compression,[{client2server,[none,'zlib@openssh.com',zlib]},
               {server2client,[none,'zlib@openssh.com',zlib]}]}]

      

结果表明,Diffie-Hellman群1被添加到kex列表的首。

例6

在这个例子中,我们先把'diffie-hellman-group1-sha1' 'ecdh-sha2-nistp521'放到kex列表中,也就是append它。

6> ssh:chk_algos_opts(
         [{modify_algorithms,
	       [{prepend,
	           [{kex, ['diffie-hellman-group1-sha1']}
		   ]},
		{append,
                   [{kex, ['ecdh-sha2-nistp521']}
                   ]}
	       ]
          }
        ]).
[{kex,['diffie-hellman-group1-sha1','ecdh-sha2-nistp384',
       'ecdh-sha2-nistp256','diffie-hellman-group-exchange-sha256',
       'diffie-hellman-group16-sha512',
       'diffie-hellman-group18-sha512',
       'diffie-hellman-group14-sha256',
       'diffie-hellman-group14-sha1',
       'diffie-hellman-group-exchange-sha1','ecdh-sha2-nistp521']},
 {public_key,['ecdsa-sha2-nistp384','ecdsa-sha2-nistp521',
   .....
] 
      

请注意,追加的算法将从原来的位置移除,然后追加到相同的列表中。

例7

在这个例子中,我们使用了两个选项(preferred_algorithmsmodify_algorithms),并尝试预先支持不支持的算法。任何不受支持的算法都会悄悄删除。

7> ssh:chk_algos_opts(
         [{preferred_algorithms,
               [{cipher,['aes128-ctr']},
	        {mac,['hmac-sha2-256']},
                {kex,['ecdh-sha2-nistp384']},
		{public_key,['ssh-rsa']},
		{compression,[{server2client,[none]},
		              {client2server,[zlib]}]}
               ]
           },
          {modify_algorithms,
	       [{prepend,
	           [{kex, ['some unsupported algorithm']}
		   ]},
		{append,
                   [{kex, ['diffie-hellman-group1-sha1']}
                   ]}
	       ]
          }
        ]).
[{kex,['ecdh-sha2-nistp384','diffie-hellman-group1-sha1']},
 {public_key,['ssh-rsa']},
 {cipher,[{client2server,['aes128-ctr']},
          {server2client,['aes128-ctr']}]},
 {mac,[{client2server,['hmac-sha2-256']},
       {server2client,['hmac-sha2-256']}]},
 {compression,[{client2server,[zlib]},
               {server2client,[none]}]}]

      

当然,有谁愿意同时使用这两种选择,这是有疑问的,但如果出现意外的需要,这是可能的。

Erlang 20

Erlang 是一种通用的面向并发的编程语言,可应付大规模开发活动的程序设计语言和运行环境。

主页 https://www.erlang.org/
源码 https://github.com/erlang/otp
版本 20
发布版本 20.1