Red Hat Enterprise Linux Network Services and Security Administration Unit 8 - Securing Data
學習目標
The Need For Encryption
telnet, FTP, POP3, 等密碼都是明碼
sendmail, NFS, NIS, 等資料也都是明碼
rsh, rcp, 等不安全的身份認證
Random Number Generator
亂數產生器:
/dev/random, 如果系統沒有其他動作,random值就停止產生
/dev/urandom, 可永遠提取
# openssl rand [base64] num,用 openssl 產生亂數
One-Way Hashes
編編前的字串經過演算法hash之後,得到一串新的字串,新的字串無法推回原來的字串
md2,md5,mdc2,rmd160,sha,sha1,/etc/passwd 是用md5編碼
# sha1sum [--check] file
# md5sum [--check] file
openssl, gpg
# rpm -V
Symmetric Encryption 對稱式的加密演算法
加密與解密都用同一把key (key可以視為一串字串)
Asymmetric Encryption 非對稱加密演算法
二把配對的key, 一把加密, 另一把解密,public/private key
另外一種應用:數位簽章
1.對稱式演算法比非對稱演算法效率高
2.實務上常見用非對稱演算法把對稱式演算法的key安全地傳給對方,之後的傳資料都用效率高的對稱式演算法
OpenSSH Overview
SSH1,第一代
SSH2,第二代更安全,key是雙方協調出來的
OpenSSH Authentication
Service Profile: SSH
Type: System V
套件package: openssh, openssh-clients, openssh-server
執行檔: /usr/sbin/sshd
啟動script: /etc/init.d/sshd
port: 22
設定檔: /etc/ssh/*, $HOME/.ssh/
相關套件: openssl, openssh-askpass, openssh-askpass-gnome, tcp_wrappers
# vi /etc/ssh/sshd_config
#Protocol 2,1 ← 先試第二版,再試第一版
Protocol 2 ← 只接受第二版
#Banner /some/path ← 登入訊息
The OpenSSH Client
# ssh hostname ← 連遠端主機,user預設同自己
# ssh user@hostname ← 指定user跟host
# ssh hostname remote-command ← 直接在遠端下命令,把結果傳回來
# scp file user@host:remote-dir ← 與遠端互相copy資料
# scp -r user@host:remote-dir localdir ← 遞迴COPY整個目錄
# sftp host
# sftp -C user@host ← -C 進行網路傳遞壓縮
Protecting Your Keys
ssh-agent
ssh-add
Applications: RPM
# rpm --verify package_name ( or -V)
# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat*
# rpm --checksig package_file_name(or -K)
Lab 8 實作
在 stationX 新增使用者 alice
[root@stationX]# useradd alice
[root@stationX]# echo alice | passwd --stdin alice
在 statiionY 新增使用者 bob
[root@stationY]# useradd bob
[root@stationY]# echo bob | passwd --stdin bob
[root@stationY]# service sshd start
在 stationX,用alice 身份登入
[alice@stationX]$ ssh bob@stationY ls /tmp ← 用ssh直接在stationY下指令,需輸入bob密碼
[alice@stationX]$ ssh bob@stationY ← ssh 以bob身份登入stationY
[bob@stationY]$ exit
[alice@stationX]$ scp bob@stationY:/etc/services . ← COPY stationY的/etc/services檔案回來stationX
[alice@stationX]$ scp -r bob@stationY:/etc/xinetd.d . ← COPY 整個stationY的/etc/xinetd.d/目錄回來stationX
[alice@stationX]$ ssh-keygen -t dsa ← 製造一對key,私鑰 id_dsa與公鑰 id_dsa.pub
Generating public/private dsa key pair.
Enter file in which to save the key (/home/alice/.ssh/id_dsa): ← Enter
Created directory '/home/alice/.ssh'.
Enter passphrase (empty for no passphrase): ← Enter
Enter same passphrase again: ← Enter
Your identification has been saved in /home/alice/.ssh/id_dsa.
Your public key has been saved in /home/alice/.ssh/id_dsa.pub.
The key fingerprint is:
59:a0:dd:31:0f:34:fd:96:f6:fd:02:68:92:6b:08:23 alice@stationX
[alice@stationX]$ ls ~/.ssh
[alice@stationX]$ cat ~/.ssh/id_dsa ← 私鑰 private key
[alice@stationX]$ cat ~/.ssh/id_dsa.pub ← 公鑰 public key
[alice@stationX ~]$ scp id_dsa.pub bob@stationY:/home/bob/ ← copy 公鑰到 stationY
在 stationY 以 bob 身份登入
[bob@stationY]$ mkdir -p ~/.ssh
[bob@stationY]$ chmod 700 ~/.ssh ← 設好目錄權限,保護好key
[bob@stationY]$ mv id_dsa.pub ~/.ssh/authorized_keys ← 將key更名成authorized_keys放入.ssh目錄中
[bob@stationY]$ chmod 600 ~/.ssh/authorized_keys ← 設好權限,不讓其他人讀到key
在 stationX中以alice身份登入
[alice@stationX]$ ssh bob@stationY id ← 此時在對stationY下指令都不需輸入密碼了
[alice@stationX]$ ssh bob@stationY tar czvf - /home/bob > /tmp/bob.stationY.tgz ← 把stationY 的/home/bob 目錄壓縮成bob.stationY.tgz之後,copy到stationX的/tmp
實作 ssh-agent, ssh-add
[alice@stationX tmp]$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/alice/.ssh/id_dsa): ← Enter
/home/alice/.ssh/id_dsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): ← 輸入密碼 passphrase
Enter same passphrase again: ← 輸入密碼 passphrase
Your identification has been saved in /home/alice/.ssh/id_dsa.
Your public key has been saved in /home/alice/.ssh/id_dsa.pub.
The key fingerprint is:
bf:f8:6d:4d:78:5c:46:ec:aa:a4:94:10:1d:3e:59:74 alice@stationX
[alice@stationX ~]$ scp .ssh/id_dsa.pub bob@stationY:/home/bob/.ssh/authorized_keys
[alice@stationX ~]$ ssh bob@stationY
此時要ssh到stationY也要輸入密碼,除非利用ssh-add把key加入鑰匙圈
[alice@stationX ~]$ ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-rGvkCa7415/agent.7415; export SSH_AUTH_SOCK;
SSH_AGENT_PID=7416; export SSH_AGENT_PID;
echo Agent pid 7416;
如果失敗的話,再試 eval 'ssh-agent' or ssh-agent bash
[alice@stationX ~]$ ssh-agent bash
[alice@stationX ~]$ ssh-add ← 掛上鑰匙圈
Enter passphrase for /home/alice/.ssh/id_dsa: ← 輸入密碼 passphrase
Identity added: /home/alice/.ssh/id_dsa (/home/alice/.ssh/id_dsa)
[alice@stationX ~]$ ssh-add -l ← list 出所有掛上的鑰匙圈
[alice@stationX ~]$ ssh bob@stationY ← 此時 ssh 到stationY都不需要密碼了
ssh tunnel 實作
[root@stationX ~]# links http://stationY ← 看得到內容
[root@stationX ~]# ssh bob@stationY -L 12345:stationY:80 ← 挖一個密道入口從本機的localhost:12345 → stationY:80
[root@stationX ~]# links http://localhost:12345 ← 在本機的12345 port看到的內容,實際是stationY:80的內容
[root@stationX ~]# ssh -Nf bob@stationY -L 12345:stationY:80 ← -Nf ,ssh 連線可不用一直hold在stationY機器上
By SmallKen