Linux Service and Security(Part 2)

接PART 1

4SSH端口轉發:SSH會自動加密和解密所有SSH客戶端與服務端之間的網絡數據。但是,SSH還能夠將其它TCP端口的網絡數據通過SSH鏈接來轉發,并且自動提供了相應的加密及解密服務,這一過程也被叫做“隧道(tunneling)”。telnet,SMTP,LDAP這些TCP應用均能夠從中得益,避免了用戶名、密碼以及隱私信息的明文傳輸。同時,如果工作環境中的防火墻限制了一些網絡端口的使用,但是允許SSH的連接,也可以通過將TCP端口轉發來使用SSH進行通信。

端口轉發功能:加密SSH Client端至SSH Server端之間的通信數據;突破防火墻的限制完成一些之前無法建立的TCP連接。

兩種方式:本地轉發和遠程轉發

A為操作機ssh -L localport:host:hostport sshserver

選項:-f 后臺啟用

-N 不開遠程shell

-g 啟用網關功能

此處以telnet服務為測試:關閉防火墻;

[root@localhost ~]# ssh -L 9527:10.1.54.250:23 10.1.252.134
root@10.1.252.134's password: 
Last login: Thu Sep 22 11:31:17 2016 from 10.1.252.66
[root@centos68 ~]# netstat -nta
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address         Foreign Address      State      
tcp    0     0 :::9527           :::*           LISTEN

鏈接A機本機端口

[root@centos68 tmp]# telnet 127.0.0.1 9527
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
^]
telnet>

blob.png

遠程轉發:ssh -R 9527:server:23 -N server

使用B主機作為操作機:讓-N后的A主機偵聽9527端口,若有訪問,就加密后轉發請求Bssh服務,再由本機解密后轉發到-R后的C主機的23端口。

[root@centos68 tmp]# ssh -R 9527:10.1.54.250:23 -N 10.1.252.28

blob.png

##ssh動態端口轉發:當用Firefox訪問internet時,本機的1080端口作為代理服務器,Firefox的訪問請求被轉發到sshserver上,由sshserver代替訪問internet。

在本機Firefox設置代理:圖形界面更改配置proxy:127.0.0.1:1080

ssh -D 1080 root@sshserver

5、配置文件:常用參數:Port;ListenAddress ipPermitRootLogin yes;ClientAliveInterval 0;UseDNS yes。

限制可登陸用戶的方法:AllowUsers user1 user2 user3

DenyUsers;AllowGroups;DenyGroups:沒寫入的就是允許的。

ssh服務的最佳實踐:

不要使用默認端口;

禁止使用protocol version 1

限制可登陸用戶;

設定空閑會話超時時長;

利用防火墻設置ssh訪問策略;

僅監聽特定的IP地址;更改配置文件,將0.0.0.0給為特定地址。

基于口令認證時,使用強密碼策略;

生成隨機數:tr -dc A-Za-z0-9_ < /dev/urandom | head -c 30 | xargs

使用基于密鑰的認證;

禁止使用空密碼;

禁止root用戶直接登錄;

限制ssh的訪問頻度和并發在線數;

做好日志分析。

一、課后練習

1、創建私有CA和申請證書:openssl的配置文件:/etc/pki/tls/openssl.cnf

步驟一:生成私鑰:

[root@centos68 ~]# (umask 077;openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
............................................................................................+++
................+++
e is 65537 (0x10001)

生成自簽證書:openssl

-new 生成新證書簽署請求

-x509 生成自簽格式證書,專用于創建私有CA

-key 生成請求時用到的私鑰路徑

-out 生成的請求文件路徑;如果自簽操作將直接生成簽署過的證書

-days指定證書的有效時長;

[root@centos68 private]# openssl req -new -x509 -key cakey.pem -out /etc/pki/CA/cacert.pem -days 3650
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:m20
Organizational Unit Name (eg, section) []:magedu
Common Name (eg, your name or your server's hostname) []:ge
Email Address []:915954814@qq.com

CA提供所需的目錄及文件:

[root@centos68 CA]# mkdir -pv /etc/pki/CA/{certs,crl,newcerts}
[root@centos68 CA]# touch /etc/pki/CA/{serial,index.txt}
[root@centos68 CA]# echo 01 > /etc/pki/CA/serial

要用到證書進行安全通信的服務器,需要向CA請求簽署證書:

步驟:以httpd為例:

用到證書的主機生成證書簽署請求:

[root@localhost ~]# mkdir  /etc/httpd/ssl
[root@localhost ~]# cd /etc/httpd/ssl
[root@localhost ssl]# ls
[root@localhost ssl]# (umask 077;openssl genrsa -out httpd.key 2048)
Generating RSA private key, 2048 bit long modulus
...............................................................+++
...............................................................................................+++
e is 65537 (0x10001)
[root@localhost ssl]# ls
httpd.key

生成證書簽署請求,國家和省和公司名稱必須和CA一致:

[root@localhost httpd]# openssl req -new -key /etc/httpd/ssl/httpd.key -out /etc/httpd.csr -days 365
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:m20
Organizational Unit Name (eg, section) []:magedu
Common Name (eg, your name or your server's hostname) []:ge
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123654
An optional company name []:magedu

將請求通過可靠方式發給CA主機:

[root@localhost etc]# scp /etc/httpd.csr  root@10.1.252.134:/tmp
root@10.1.252.134's password: 
httpd.csr                100% 1054     1.0KB/s   00:00

CA主機上簽署請求:

[root@centos68 tmp]# mkdir -p /etc/pki/CA/serts 
[root@centos68 tmp]# openssl ca -in /tmp/httpd.csr -out /etc/pki/CA/serts/httpd.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Sep 18 16:11:12 2016 GMT
            Not After : Sep 18 16:11:12 2017 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = beijing
            organizationName          = m20
            organizationalUnitName    = magedu
            commonName                = ge
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                02:80:06:15:91:23:D9:A3:43:9F:0A:C9:D2:9B:AB:CF:6F:69:48:57
            X509v3 Authority Key Identifier: 
                keyid:BC:2A:27:17:D6:D2:84:8F:1B:92:4D:71:E8:FA:CD:47:12:51:2F:A5
Certificate is to be certified until Sep 18 16:11:12 2017 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

查看證書信息,將證書發送給客戶端:

[root@centos68 serts]# openssl x509 -in httpd.crt -noout -serial -subject 
serial=01
subject= /C=CN/ST=beijing/O=m20/OU=magedu/CN=ge
[root@centos68 serts]# scp httpd.crt root@10.1.252.28:/etc/pki/CA/ 
The authenticity of host '10.1.252.28 (10.1.252.28)' can't be established.
RSA key fingerprint is e4:17:b3:40:d1:75:78:27:2b:d5:51:eb:2a:5a:f4:0e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.1.252.28' (RSA) to the list of known hosts.
root@10.1.252.28's password: 
httpd.crt            100% 4474     4.4KB/s   00:00

吊銷證書

客戶端獲取要吊銷的證書的serial序列號:

[root@localhost CA]# openssl x509 -in httpd.crt -noout -serial -subject
serial=01
subject= /C=CN/ST=beijing/O=m20/OU=magedu/CN=ge

CA端主機吊銷證書:根據客戶端提交的serialsubject信息,對比本機數據庫index.txt中存儲的是否一致。吊銷:

[root@centos68 serts]#openssl ca -revoke /etc/pki/CA/newcerts/01.pem 
Using configuration from /etc/pki/tls/openssl.cnf
Revoking Certificate 01.
Data Base Updated

生成吊銷證書的吊銷編號(第一次吊銷證書時執行)

[root@centos68 serts]# echo 01 > /etc/pki/CA/crlnumber

更新證書吊銷列表:

[root@centos68 serts]# openssl ca -gencrl -out /etc/pki/CA/crl/ca.crl
Using configuration from /etc/pki/tls/openssl.cnf

查看crl文件:

[root@centos68 serts]# openssl crl -in /etc/pki/CA/crl/ca.crl -noout -text
Certificate Revocation List (CRL):
        Version 2 (0x1)
    Signature Algorithm: sha1WithRSAEncryption
        Issuer: /C=CN/ST=beijing/L=beijing/O=m20/OU=magedu/CN=ge/emailAddress=915954814@qq.com
        Last Update: Sep 18 16:29:55 2016 GMT
        Next Update: Oct 18 16:29:55 2016 GMT
        CRL extensions:
            X509v3 CRL Number: 
                1
Revoked Certificates:
    Serial Number: 01
        Revocation Date: Sep 18 16:23:26 2016 GMT
    Signature Algorithm: sha1WithRSAEncryption
         5e:d5:ea:90:79:bd:f3:ac:a0:bf:bc:d6:00:87:b3:b8:56:2f:
         26:fb:d2:d1:10:0f:af:30:67:8b:b9:21:5c:ab:69:c3:86:db:
         ee:3e:13:e9:7d:cd:d9:04:fd:f8:dc:cf:f2:04:a8:84:34:24:
         e0:08:13:60:5c:2e:f3:46:e7:fe:c6:63:86:79:18:df:66:9a:
         c6:a6:b3:bc:47:29:af:38:50:a0:24:42:ef:6c:71:73:2f:f3:
         53:1c:df:f5:f3:6d:af:45:ee:81:0b:c4:db:7d:64:51:f4:6b:
         cf:91:f8:f3:27:eb:ad:35:d2:f9:dd:51:63:e4:ad:d5:a7:77:
         1d:2d:24:e0:2c:43:b1:fa:41:d9:53:a0:67:25:95:b5:40:fe:
         fb:78:89:2c:59:38:ef:fd:58:51:e6:0b:1c:08:71:67:52:98:
         1e:45:d3:49:38:8c:39:c3:00:8b:75:41:9e:64:aa:35:f1:a5:
         5c:9a:2d:69:be:4e:f3:d2:2f:d9:3a:8d:e6:f7:52:f7:a8:2e:
         6b:fe:05:f2:10:6b:e4:f1:6a:e7:45:c6:f8:c6:d2:2c:eb:50:
         ba:a6:cb:c3:4c:ff:61:86:85:db:4a:91:ad:d3:76:3e:9a:99:
         dd:ad:83:1c:c6:91:de:3b:07:9d:b8:ae:27:c5:49:1e:56:25:
         9a:b2:7f:27

2、ssh的兩種認證方式:passwordkey

基于密鑰(key)的認證:在linux客戶端上:

步驟一:在客戶端生成密鑰對:

[root@localhost tmp]# ssh-keygen  -t  rsa

默認生成至家目錄的.ssh/中??梢允褂?/span>-P ‘’指定不添加密鑰管理口令,設置私鑰口令命令為ssh-keygen -p。

步驟二:把公鑰文件傳輸至遠程服務器對應用戶的家目錄;

兩種傳輸方式:scp傳輸,用cat命令和管道追加家目錄.ssh/authorized_keys文件中;

可以使用如下命令,自動追加:

[root@localhost ~]# ssh-copy-id  -i  .ssh/id_rsa.pub  root@10.1.252.134:/root

注意:家目錄.ssh目錄中的authorized_keys存放的連接的主機的登錄用戶對應的公鑰,而know_hosts文件存放的是連接的主機的公鑰。

步驟三:測試:

[root@localhost ~]# ssh 10.1.252.134
Last login: Mon Sep 19 00:59:38 2016

在windows客戶端使用密鑰驗證登錄

步驟一:使用工具生成密鑰對,導出公鑰文件,并傳輸公鑰到登錄主機上:(使用xshell連接不需要轉化格式,secureCRT連接需要轉化格式),注意權限必須為600

##使用secureCRT的轉化命令:ssh-keygen -i -f Identity.pub >> .ssh/authorized_keys

blob.png

blob.png

傳輸,追加公鑰:

[root@centos68 ~]# cat id_rsa_1024\ \(2\).pub >> .ssh/authorized_keys

步驟二:測試登錄:

Connecting to 10.1.252.134:22…

Connection established.

To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Thu Sep 22 11:21:24 2016 from 10.1.54.250

[root@centos68 ~]#

當設置了密鑰的密碼后,每次登錄都需要驗證。執行代理后口令就可以只輸入一次

運行ssh-agent bash;

鑰匙通過命令添加給代理:ssh-add。

3、編譯安裝dropbear;安裝包README有詳細開啟過程。INSTALL中有安裝詳情。

./configure報錯:依賴包zlib-devel。

make PROGRAMS=dropbear dbclient dropbearkey dropbearconvert scp 

make PROGRAMS=dropbear dbclient dropbearkey dropbearconvert scp install

啟用ssh服務:

/usr/local/sbin/dropbear  –help

mkdir /etc/dropbear

dropbearkey -t rsa -f /etc/dropbear/dropbear_rsa_host_key -s 2048

dropbearkey -t dss -f /etc/dropbear/dropbear_dsa_host_key

dropbear -p :2222 -F -E (前臺運行);dropbear -p :2222(后臺運行)

[root@localhost dropbear-2013.58]# dropbear -p :22222 -F -E
[7744] Sep 22 17:28:08 Failed reading '/etc/dropbear/dropbear_dss_host_key', disabling DSS
[7744] Sep 22 17:28:08 Not backgrounding
[7782] Sep 22 17:31:35 Child connection from 127.0.0.1:33668
[7782] Sep 22 17:31:40 Bad password attempt for 'root' from 127.0.0.1:33668
[7782] Sep 22 17:31:43 Password auth succeeded for 'root' from 127.0.0.1:33668
[7782] Sep 22 17:31:47 Exit (root): Disconnect received
^C[7744] Sep 22 17:32:30 Premature exit: Terminated by signal

使用客戶端訪問:

ssh -p 2222 root@127.0.0.1(使用ssh連接)

[root@localhost ~]# ssh -p 22222 root@127.0.0.1
The authenticity of host '[127.0.0.1]:22222 ([127.0.0.1]:22222)' can't be established.
RSA key fingerprint is 3b:f0:f0:12:3c:c4:05:c4:03:25:a8:74:00:ed:06:7c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[127.0.0.1]:22222' (RSA) to the list of known hosts.
root@127.0.0.1's password: 
Permission denied, please try again.
root@127.0.0.1's password: 
-bash: /bin/bash:: No such file or directory
[root@localhost ~]# exit
logout

dbclient -p 2222 root@127.0.0.1(使用自帶工具連接)

2、AIDE:免費商業軟件:高級入侵檢測環境:

AIDE能構造一個指定文件的數據庫,aide.conf作為其配置文件。這個數據庫不應該保存哪些經常變動的文件信息,例如:日志文件、郵件、/proc文件系統,用戶其實目錄及臨時目錄等。

修改配置文件:vim /etc/aide.conf:指定對哪些文件進行檢測

!/etc/mtab:嘆號表示忽略這個文件的檢查。

NORMAL=R+rmd60+sha256

初始化默認的AIDE的庫:

[root@centos68 tmp]# aide --init
/etc/resolv.conf atime in future
/etc/gai.conf atime in future
/etc/host.conf atime in future
/etc/localtime atime in future
/etc/ld.so.cache atime in future
/etc/hosts atime in future
/etc/nsswitch.conf atime in future

生成檢查數據庫(建議初始數據備份)

將新生成的庫改名字,去掉new

[root@centos68 local]# cd /var/lib/aide/
[root@centos68 aide]# ls
aide.db.new.gz
[root@centos68 aide]# mv aide.db.new.gz aide.db.gz

檢測數據庫,會顯示更新和改變了的文件:

[root@centos68 aide]# aide --check

更新數據庫,更改配置文件之后可以進行更新,生成的新文件再改名才可以使用:

[root@centos68 aide]# aide --update

原創文章,作者:SilencePavilion,如若轉載,請注明出處:http://www.www58058.com/49814

(0)
SilencePavilionSilencePavilion
上一篇 2016-10-09
下一篇 2016-10-09

相關推薦

  • shell腳本初步

    shell腳本編程初步 程序:由數據和指令組成, 指令:由程序文件提供 數據:可以通過IO設備、文件、管道來得到, 程序:算法+數據結構 變量:變量名+變量名指向的內存空間 變量賦值: name = value變量類型:存儲格式:字符 數值(精確數值 近似數值)   表示數據范圍  &nb…

    Linux干貨 2016-08-15
  • day7作業練習

    1、顯示當前系統root、mage或wang用戶的UID和默認shell  2、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包 括下劃線)后面跟一個小括號的行  3、使用egrep取出/etc/rc.d/init.d/functions中其基名     v …

    系統運維 2016-08-08
  • 關于ip_conntrack跟蹤連接滿導致網絡丟包問題的分析

    http://ixdba.blog.51cto.com/2895551/1737642

    Linux干貨 2016-06-03
  • httpd應用舉例

    應用舉例1、基于用戶和組認證的方式訪問ftp         1、編輯配置文件/etc/httpd/conf/httpd.conf,并使用httpd -t 檢查語法,并systemctl reload httpd       &n…

    Linux干貨 2016-10-23
  • nmcli命令用法簡介

    nmcli:地址配置工具 用法:nmcli [OPTIONS] OBJECT { COMMAND | help } 一般使用的情況有: 設備:正在使用的網絡端口 鏈接:一組配置設置,對于一個單一的設備可以有多個連接,可以在鏈接之間切換 常用命令:     nmcli connection show &nbsp…

    Linux干貨 2016-09-07
  • ?N25第三周作業

    第三周 一、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。 [root@zf ~]# who | cut -d " " -f1  | sort | uniq -…

    Linux干貨 2016-12-14
欧美性久久久久