telnet & ssh

telnet

telnet: 23/tcp

明文

C/S:

默认:禁止管理直接登录

总结:

telnet

telnet-server

xinetd

ssh

ssh: secure shell, 22/tcp

sshv1, sshv2

sshv1基于CRC-32做MAC,不安全;

sshv2基于双方主机的协商选择使用最安全的MAC方式

加密机制及MAC机制由双方协商选定;

基于DH实现密钥交换,基于RSA或DSA实现身份认证;

客户端通过检查服务器端的主机密钥来判断是否能够继续通信;

OpenSSH 是一组用于安全地访问远程计算机的连接工具。 它可以作为 rlogin、 rsh rcp 以及 telnet 的直接替代品使用。 更进一步, 其他任何 TCP/IP 连接都可以通过 SSH 安全地进行隧道/转发。 OpenSSH 对所有的传输进行加密, 从而有效地阻止了窃听、 连接劫持, 以及其他网络级的攻击。OpenSSH 由 OpenBSD project 维护。

登录过程和使用 rlogin 或 telnet 建立的会话非常类似。 在连接时, SSH 会利用一个密钥指纹系统来验证服务器的真实性。 只有在第一次连接时, 用户会被要求输入 yes。 之后的连接将会验证预先保存下来的密钥指纹。 如果保存的指纹与登录时接收到的不符, 则将会给出警告。 指纹保存在 ~/.ssh/known_hosts 中, 对于 SSH v2 指纹, 则是 ~/.ssh/known_hosts2。

默认情况下, 较新版本的 OpenSSH 只接受 SSH v2 连接。 如果能用版本 2 则客户程序会自动使用, 否则它会返回使用版本 1 的模式。 此外, 也可以通过命令行参数 -1 或 -2 来相应地强制使用版本 1 或 2。 保持客户端的版本 1 能力是为了考虑较早版本的兼容性。

openssh:

C/S

S: sshd

C: ssh

windows客户端:

putty,xshell,securecrt,sshshellclient

openssh的客户端组件:

ssh: 配置/etc/ssh/ssh_config

ssh [username@]host [COMMAND]

ssh -l username host [COMMAND]

-p PORT

scp: 利用ssh协议在主机之间实现安全文件传输的工具

scp SRC1... DEST

分两种情形:

1、源文件在本机,目标为远程

# scp /path/to/somefile... USERNAME@HOST:/path/to/somewhere

[root@coffee zzz]# scp /zzz/manpages-zh-1.5.2.tar.bz2 [email protected]:/test

[email protected]'s password:

manpages-zh-1.5.2.tar.bz2 

2、源文件在远程,本地为目标

# scp USERNAME@HOST:/path/to/somewhere /path/to/somewhere

[root@coffee zzz]# scp [email protected]:/test/zhang /zzz

[email protected]'s password:

zhang 

-r: 复制目录时使用

-p: 保持源文件的元数据信息,包括mode和timestamp

-q: 静默模式

-p PORT: 指定ssh协议监听的端口

sftp: 基于ssh的ftp服务

用法:sftp USERNAME@HOST

openssh的服务器端:sshd

配置文件:/etc/ssh/sshd_config

服务脚本:/etc/rc.d/init.d/sshd

脚本配置文件:/etc/sysconfig/sshd

配置参数:

# man sshd_config

经常需要修改的参数:

Port: 修改默认监听的端口

ListenAddress

sshd认证方式:

1、基于口令的认证;

2、基于密钥的认证;

# ssh-keygen -t rsa

默认密钥为id_rsa, id_rsa.pub

-f /path/to/somefile: 密钥文件保存位置

-P '': 指定oldpassword

# ssh-copy-id -i .ssh/id_rsa.pub USERNAME@HOST

[root@coffee ~]# ssh [email protected]                                       用root身份连接到151主机 exit为退出

[email protected]'s password:

Last login: Tue Jun 18 05:06:03 2019 from 192.168.0.161

[root@coffee ~]# exit

logout

Connection to 192.168.0.151 closed.

[root@coffee ~]#

[root@coffee ~]# ssh [email protected] 'uname -r'                                  用root身份到151主机上执行uname命令后返回

[email protected]'s password:

2.6.32-754.14.2.el6.x86_64

[root@coffee ~]# uname -r

3.10.0-957.12.2.el7.x86_64

最佳实践:

1、不要使用默认的22号端口;

2、不要使用protocol 1;

3、限制可登录的用户

白名单:

AllowUsers user1 user2 ...

AllowGroups grp1 grp2...

黑名单:

DenyUsers

DenyGroups

4、设定空闲会话超时时长

ClientAliveInterval 300

ClientAliveCountMax 0

5、利用防火墙设置ssh访问策略;

6、仅监听在特定的IP地址,而非本机所有的IP地址;

7、使用强密码策略

# tr -dc A-Za-z0-9_ < /dev/urandom | head -c 30 | xargs

8、使用基于密钥的认证;

9、禁止使用空密码;

10、禁止root用户直接登录;

11、限制ssh的访问频度

12、做好日志,经常分析;

总结:

ssh: /etc/ssh/ssh_config

sshd: /etc/ssh/sshd_config

ssh, scp, sftp

lastb: 最近失败登录的尝试

dropbear

客户端配置文件

[root@coffee zzz]# vim /etc/ssh/ssh_config

#  Tunnel no

#  Protocol 2                                  协议版本

#  Cipher 3des                                对称加密

#  Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc  所有加密方式

#  MACs hmac-md5,hmac-sha1,[email protected],hmac-ripemd160      消息摘认证

#  EscapeChar ~

#  Tunnel no

#  TunnelDevice any:any

#  PermitLocalCommand no

#  VisualHostKey no

#  ProxyCommand ssh -q -W %h:%p gateway.example.com

#  RekeyLimit 1G 1h

#

# Uncomment this if you want to use .local domain

# Host *.local

#  CheckHostIP no

上面的为默认值,把要启用的选项,可以写到下面

Host *

        GSSAPIAuthentication yes

# If this option is set to yes then remote X11 clients will have full access

# to the original X11 display. As virtually no X11 client supports the untrusted

# mode correctly we set this to yes.


服务器端

修改22端口时关闭 selinux 和防火墙

[root@coffee ~]# getenforce

Enforcing

[root@coffee ~]# setenforce 0

[root@coffee ~]# service iptables stop

[root@coffee .ssh]# rpm -q openssh                                          检查服务器端是否安装

/var/log/secure                                                    日志保存位置,root才能查看     

[root@coffee ~]# vim /etc/ssh/sshd_config


#      $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $

# This is the sshd server system-wide configuration file.  See

# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with

# OpenSSH is to specify options with their default value where

# possible, but leave them commented.  Uncommented options change a

# default value.

#Port 22                                                       监听端口 建议修改

#AddressFamily any                                      "any"(默认)、"inet"(仅IPv4)、"inet6"(仅IPv6)。

#ListenAddress 0.0.0.0                                    监听ipv4本机所以网络地址

#ListenAddress ::                                             监听ipv6的本机所有网络地址

# Disable legacy (protocol version 1) support in the server for new

# installations. In future the default will change to require explicit

# activation of protocol 1

Protocol 2

# HostKey for protocol version 1

#HostKey /etc/ssh/ssh_host_key

# HostKeys for protocol version 2

#HostKey /etc/ssh/ssh_host_rsa_key

#HostKey /etc/ssh/ssh_host_dsa_key

# Lifetime and size of ephemeral version 1 server key

#KeyRegenerationInterval 1h                     秘钥有效时间1小时

#ServerKeyBits 1024                                   主机秘钥长度

# Logging

# obsoletes QuietMode and FascistLogging

#SyslogFacility AUTH

SyslogFacility AUTHPRIV                       日志记录方式

#LogLevel INFO

# Authentication:

#LoginGraceTime 2m                              登录超时时间2分钟

#PermitRootLogin yes                       是否允许管理员登录 建议关闭

#StrictModes yes                             

#MaxAuthTries 6                  密码允许错误次数

#MaxSessions 10                 最多允许同时登录数量

#RSAAuthentication yes

#PubkeyAuthentication yes

#AuthorizedKeysFile    .ssh/authorized_keys

#AuthorizedKeysCommand none

#AuthorizedKeysCommandRunAs nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts

#RhostsRSAAuthentication no

# similar for protocol version 2

#HostbasedAuthentication no

# Change to yes if you don't trust ~/.ssh/known_hosts for

# RhostsRSAAuthentication and HostbasedAuthentication

#IgnoreUserKnownHosts no

# Don't read the user's ~/.rhosts and ~/.shosts files

#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!

#PasswordAuthentication yes

#PermitEmptyPasswords no

PasswordAuthentication yes                    是否允许密码登录  建议关闭

# Change to no to disable s/key passwords

#ChallengeResponseAuthentication yes

ChallengeResponseAuthentication no

# Kerberos options                 大公司统一账号认证

#KerberosAuthentication no

#KerberosOrLocalPasswd yes

#KerberosTicketCleanup yes

#KerberosGetAFSToken no

#KerberosUseKuserok yes

# GSSAPI options

#GSSAPIAuthentication no

GSSAPIAuthentication yes

#GSSAPICleanupCredentials yes

GSSAPICleanupCredentials yes

#GSSAPIStrictAcceptorCheck yes

#GSSAPIKeyExchange no

# Set this to 'yes' to enable PAM authentication, account processing,

# and session processing. If this is enabled, PAM authentication will

# be allowed through the ChallengeResponseAuthentication and

# PasswordAuthentication.  Depending on your PAM configuration,

# PAM authentication via ChallengeResponseAuthentication may bypass

# the setting of "PermitRootLogin without-password".

# If you just want the PAM account and session checks to run without

# PAM authentication, then enable this but set PasswordAuthentication

# and ChallengeResponseAuthentication to 'no'.

#UsePAM no

UsePAM yes

# Accept locale-related environment variables

AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES

AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT

AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE

AcceptEnv XMODIFIERS

#AllowAgentForwarding yes

#AllowTcpForwarding yes

#GatewayPorts no

#X11Forwarding no

X11Forwarding yes          X11图形转发  建议开启

#X11DisplayOffset 10

#X11UseLocalhost yes

#PrintMotd yes

#PrintLastLog yes

#TCPKeepAlive yes

#UseLogin no

#UsePrivilegeSeparation yes

#PermitUserEnvironment no

#Compression delayed

#ClientAliveInterval 0

#ClientAliveCountMax 3

#ShowPatchLevel no

#UseDNS yes       是否连接成功后反解析DNS  建议关闭不然卡很久

#PidFile /var/run/sshd.pid

#MaxStartups 10:30:100

#PermitTunnel no

#ChrootDirectory none

# no default banner path

#Banner none

# override default of no subsystems

Subsystem      sftp    /usr/libexec/openssh/sftp-server     是否开启sftp连接

# Example of overriding settings on a per-user basis

#Match User anoncvs

#      X11Forwarding no

#      AllowTcpForwarding no

#      ForceCommand cvs server

   

生成秘钥对

[root@coffee .ssh]# ssh-keygen -t rsa                                        在客户端生成。拷贝

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):                       保存位置

Enter passphrase (empty for no passphrase):                                   私钥是否加密,直接回车不用加密

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

SHA256:UTyhCttSN+sw2zUezSH0LaukS0ix0zRULLfgI6NILTs root@coffee

The key's randomart image is:

+---[RSA 2048]----+

|        .+=.    |

|      .o+=. .  |

|  .. o.B+.o+ .  |

|  o .=oBo=.+ +  |

| . +o.OoS.= +    |

|  E .o O = +    |

|  .  o = o      |

|      . .      |

|        .        |

+----[SHA256]-----+

[root@coffee .ssh]# ls

id_rsa  id_rsa.pub  known_hosts                     id_rsa为私钥     id_rsa.pub为公钥

[root@coffee .ssh]# 

[root@coffee .ssh]# ssh-copy-id -i id_rsa.pub [email protected]           把公钥拷贝到需要连接的主机上

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

[email protected]'s password:

Number of key(s) added: 1

Now try logging into the machine, with:  "ssh '[email protected]'"

and check to make sure that only the key(s) you wanted were added.

默认为对方主机的~/.ssh/authorized_keys 文件

你可能感兴趣的:(telnet & ssh)