Cubie Truck 測試 – 解決無法 ssh 連線的問題

亂玩了一下子,Lubuntu 系統被玩掛了,所以又重新使用 PhoenixSuit 燒錄系統到 Nand Flash。
系統開機後,發現竟然無法 ssh 連線
Pietty 錯誤訊息

Putty 錯誤訊息
[@more@]ssh 連線出現的錯誤訊息
# ssh linaro@192.168.1.110
Read from socket failed: Connection reset by peer

但系統 Ping 的到

ssh 服務有開放
# netstat -ant | grep :22 | grep LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN

iptables 防火牆也沒有阻擋
# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

後來檢查 ssh server 的設定檔目錄
# cd /etc/ssh
# ls -latr
total 156
-rw-r–r–  1 root root   1669 Sep  6  2012 ssh_config
-rw-r–r–  1 root root 136156 Sep  6  2012 moduli
-rw-r–r–  1 root root      0 Oct  8 13:40 sshd_config
-rw-r–r–  1 root root      0 Oct  8 13:40 ssh_host_rsa_key.pub
-rw——-  1 root root      0 Oct  8 13:40 ssh_host_rsa_key
-rw-r–r–  1 root root      0 Oct  8 13:40 ssh_host_dsa_key.pub
-rw——-  1 root root      0 Oct  8 13:40 ssh_host_dsa_key
-rw-r–r–  1 root root      0 Oct  8 13:40 ssh_host_ecdsa_key.pub
-rw——-  1 root root      0 Oct  8 13:40 ssh_host_ecdsa_key
-rw——-  1 root root    980 Oct  8 13:42 ssh_host_key
drwxr-xr-x  2 root root   4096 Oct  8 13:42 .
-rw-r–r–  1 root root    645 Oct  8 13:42 ssh_host_key.pub
drwxrwxr-x 97 root root   4096 Oct  8 13:51 ..

發現 ssh_host_* key 的檔案大小都是 0

建立 ssh_host_key
# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
Generating public/private rsa key pair.
/etc/ssh/ssh_host_rsa_key already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.
The key fingerprint is:
8c:0a:32:2a:1b:02:6f:70:a1:2e:44:50:ee:35:f3:de root@cubietruck
The key’s randomart image is:
+–[ RSA 2048]—-+
|…              |
|..               |
| .o +            |
|.o o + o         |
|*.+   o S        |
|=* . o .         |
|*.o . . E        |
|++               |
|.                |
+—————–+
# ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
Generating public/private dsa key pair.
/etc/ssh/ssh_host_dsa_key already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_dsa_key.
Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub.
The key fingerprint is:
2f:36:fc:7c:5b:d4:d9:33:f4:2a:e6:fb:9b:5f:8c:81 root@cubietruck
The key’s randomart image is:
+–[ DSA 1024]—-+
|                 |
|                 |
|               . |
|             …+|
|        S   E o++|
|       . .   . =o|
|        = . o + o|
|       . = o.o ..|
|          o.+++o.|
+—————–+

建立完成後,就可以正常連線了!
# ssh linaro@192.168.1.110
The authenticity of host ‘192.168.1.110 (192.168.1.110)’ can’t be established.
RSA key fingerprint is 8c:0a:32:2a:1b:02:6f:70:a1:2e:44:50:ee:35:f3:de.
Are you sure you want to continue connecting (yes/no)? yes

讓 Ubuntu Server 14.04 可以直接以 root 登入

Ubuntu Server 14.04 預設遠端無法以 root 直接登入,必須要先登入成一般使用者,再使用 sudo 或 su – 來取得 root 權限,但本地端則可以直接以 root 登入。
雖然使用 root 直接登入是很不好的習慣,但架設的測試 Server,主要是在內部網路及測試用途,所以不想要那麼麻煩。
預設無法以 root 登入,即使輸入的是正確的密碼

[@more@]1. 修改 /etc/ssh/sshd_config
# vim /etc/ssh/sshd_config
把 PermitRootLogin without-password 前面加上 # 註解
#PermitRootLogin without-password
加入下面這一行
PermitRootLogin yes

2. 重新啟動電腦或重新啟動 ssh Server
# sync;sync;sync;sync;reboot

3. 可以正常登入了!

利用 fail2ban 阻擋來自網路的 SSH 連線攻擊

在 /var/log/secure 中嘗試以 root 帳號登入的錯誤記錄
# grep Failed /var/log/secure | grep -v invalid | tail -5
Jan  1 01:39:54  sshd[6417]: Failed password for root from 192.168.1.1 port 53355 ssh2
Jan  1 01:39:57  sshd[6417]: Failed password for root from 192.168.1.1 port 53355 ssh2
Jan  1 12:18:38  sshd[1556]: Failed password for root from 192.168.1.1 port 54849 ssh2
Jan  1 12:18:41  sshd[1556]: Failed password for root from 192.168.1.1 port 54849 ssh2
Jan  1 12:18:44  sshd[1556]: Failed password for root from 192.168.1.1 port 54849 ssh2

設定方式:
# vim /etc/fail2ban/jail.conf
[ssh-iptables]

enabled  = true
filter   = sshd
action   = iptables[name=SSH, port=ssh, protocol=tcp]
#           sendmail-whois[name=SSH, dest=you@example.com, sender=fail2ban@example.com]
logpath  = /var/log/secure
maxretry = 3
bantime  = 86400[@more@]啟用 ssh-iptables
enabled = true

filter 是指使用 sshd 的 filter 來檢查 log 檔中是否有符合判斷規則的樣式(在 /etc/fail2ban/filter.d 目錄之下)
filter   = sshd

action 是指偵測到之後要採取的行動,這裡有二種方式,iptables 是使用 iptables 來阻擋,sendmail-whois 是寄信給管理者,這二種行動的設定,可以在 /etc/fail2ban/action.d 目錄下找到相關的設定
action   = iptables[name=SSH, port=ssh, protocol=tcp]
這裡只使用 iptables 來阻擋,不寄信給管理者

SSH Server 登錄錯誤的 Log 檔
logpath  = /var/log/secure

maxretry 是指嘗試錯誤 3 次,就阻擋,bantime = 86400 是指阻擋的時間,86400 是指秒,所以是阻擋 1 天
maxretry = 3
bantime  = 86400

重新啟動 fail2ban 服務
# server fail2ban restart

檢查 fail2ban 的狀態
# fail2ban-client status
Status
|- Number of jail:      1
`- Jail list:           ssh-iptables

列出目前的防火牆規則
# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
fail2ban-VSFTPD  tcp  —  0.0.0.0/0            0.0.0.0/0           tcp dpt:21
fail2ban-SSH  tcp  —  0.0.0.0/0            0.0.0.0/0           tcp dpt:22
ACCEPT     all  —  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
ACCEPT     icmp —  0.0.0.0/0            0.0.0.0/0
ACCEPT     all  —  0.0.0.0/0            0.0.0.0/0
ACCEPT     tcp  —  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
ACCEPT     tcp  —  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:21
REJECT     all  —  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  —  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain fail2ban-SSH (1 references)
target     prot opt source               destination
RETURN     all  —  0.0.0.0/0            0.0.0.0/0

以 Client  IP 192.168.1.1 嘗試對 Server IP 192.168.1.9 嘗試連線錯誤幾次之後
列出 fail2ban 偵測到的 SSH 攻擊
# fail2ban-client status ssh-iptables
Status for the jail: ssh-iptables
|- filter
|  |- File list:        /var/log/secure
|  |- Currently failed: 0
|  `- Total failed:     4
`- action
   |- Currently banned: 1
   |  `- IP list:       192.168.1.1
   `- Total banned:     1

列出 fail2ban-SSH 的規則
# iptables -t filter -L fail2ban-SSH -n
Chain fail2ban-SSH (1 references)
target     prot opt source               destination
DROP       all  —  192.168.1.1          0.0.0.0/0
RETURN     all  —  0.0.0.0/0            0.0.0.0/0

解除方式:
# iptables -D fail2ban-SSH  -s 192.168.1.1 -j DROP