ISHM Blog

ishm 的電子記事本

跳至主要內容
  • 首頁
ISHM Blog

可插入式認證模組 PAM(二)

pam_unix.so 模組

pam_unix.so 是一個非常重要的模組,就是用這個模組來實現傳統的 NSS-based 認證,它可以適用 PAM 的 4 個驗證階段:

.auth : 它可以檢查使用者的名稱及密碼是否與 NSS 函式庫中雜湊演算的結果相符合。

.account : 利用 NSS 的資訊檢查密碼是否過期或是帳號是否有被鎖定。

.password : 指定密碼的相關資訊,如加密演算法 (預設是以 MD5 加密)、密碼是否以 shadow 遮蔽方式儲存 (預設是)、密碼時效功能是否啟動、是否透過 NIS 查詢修改帳密 ( 透過 rpc.yppasswdd 服務 ) 等。

.session : 指定經由 syslogd 服務來記錄 Log 檔。

pam_unix.so 有幾個參數可供設定:

md5 : 預設參數,指定密碼使用 md5 演算法加密。

bigcrypt : 使用者密碼用 DEC C2 演算法加密。

nis : 使用 NIS 來設定密碼。

shadow : 預設參數,使用 shadow 方式儲存密碼。

remember=X : 在 /etc/security/opasswd 中將使用者最近 X 次的舊密碼 (已加密) 儲存下來,並在改密碼時禁止和這些密碼重複。

其他跟帳密原則相關的模組有:

pam_cracklib.so:是一種密碼強度驗證的模組,可要求密碼的長度、數字、符號等。

password      required      pam_cracklib.so      retry=3      minlength=12      ocredit=1      dcredit=-1      lcredit=-1      ucredit=-1

範例要求:密碼錯誤重複次數 3 次、總長度最少 12 字元、其他字元 (ocredit, o 代表 other,至少一個,沒有也可以,但總長度要加 10)、數字 (dcredit, d 代表 digit)、小寫 (lcredit, l 代表 lower)、大寫(ucredit, u 代表 upper)都最少 1 個,不能沒有;如果缺少其他字元,總長度就加 10。

pam_passwdqc.so:另一種密碼強度驗證模組。

password    required    pam_passwdqc.so    retry=3    min=11,10,10,9,8

範例要求:密碼錯誤重複次數 3 次、密碼字元 1 種類別最少 11 字元、密碼字元 2 種類別最少 10 字元、通關密語 (passphrase) 最少 10 字元、密碼字元 3 種類別最少 9 字元、密碼字元 4 種類別最少 8 字元。(類別:大寫、小寫、數字、其他)

說明:

min=N0,N1,N2,N3,N4

N0 : 密碼字元類別只有 1 種時,要求的最小長度。

N1 : 密碼字元類別有 2 種時,要求的最小長度。

N2 : 通關密語使用的設定,設定通關密語的最少字元。

N3 : 密碼字元類別有 3 種時,要求的最小長度。

N4 : 密碼字元類別有 4 種時,要求的最小長度。

通關密語 (passphrase):用一句話來作為密碼,如「Mary is a pretty girl」,長度含空白就高達 21 個字元,一般的密碼是不可能這麼長的,所以強度比一般密碼還要高(這句話已經含有 3 種類別的字元,空白算其他類,再加上數字和標點符號如 , and she is 18 years old.就更難破解了),通關密語目前最長支援到 127 個字元。

pam_tally.so:登入失敗記錄模組

此模組可以記錄登入失敗的記錄至 /var/log/faillog 中,如果當它用在 auth 階段時,可中斷過多的失敗登入嘗試的使用者,但是也容易被拿來作 DOS 攻擊,例如拿正常的使用者帳號來亂試,則該帳號就會無法使用系統。

==============================

pam_securetty.so 模組

作用:指定 root 帳可以使用什麼管道登入系統。

用法:auth        required        pam_securetty.so

設定檔:/etc/securetty        (在這個檔案中指定可用的登入管道)

設定內容:

# cat /etc/securetty
console
vc/1
vc/2
tty1
tty2
tty3

==============================

pam_access.so 模組

作用:指定特定「帳號」可以從什麼「位置」存取那些「服務」。

用法1:auth      required      pam_access.so

用法2:auth      required      pam_access.so      accessfile=/PATH/FILE_NAME

設定檔:/etc/security/access.conf

設定內容:

# cat /etc/security/access.conf
# Login access control table.
# Comment line must start with “#”, no space at front.
#
# Disallow non-root logins on tty1
#-:ALL EXCEPT root:tty1
#
# Disallow console logins to all but a few accounts.
#-:ALL EXCEPT wheel shutdown sync:LOCAL
#
# Disallow non-local logins to privileged accounts (group wheel).
#-:wheel:ALL EXCEPT LOCAL .win.tue.nl
#
#-:wsbscaro wsbsecr wsbspac wsbsym wscosor wstaiwde:ALL
#
# User “root” should be allowed to get access via cron .. tty5 tty6.
#+ : root : cron crond :0 tty1 tty2 tty3 tty4 tty5 tty6
#
# User “root” should be allowed to get access from hosts with ip addresses.
#+ : root : 192.168.200.1 192.168.200.4 192.168.200.9
#+ : root : 127.0.0.1
#
# User “root” should get access from network 192.168.201.
#+ : root : 192.168.201.
#
# User “root” should be able to have access from domain.
#+ : root : .foo.bar.org
#
# User “root” should be denied to get access from all other sources.
#- : root : ALL
#
# User “foo” and members of netgroup “nis_group” should be
#+ : @nis_group foo : ALL
#
# User “john” should get access from ipv4 net/mask
#+ : john : 127.0.0.0/24
#
# User “john” should get access from ipv4 as ipv6 net/mask
#+ : john : ::ffff:127.0.0.0/127
#
# User “john” should get access from ipv6 host address
#+ : john : 2001:4ca0:0:101::1
#
# User “john” should get access from ipv6 host address (same as above)
#+ : john : 2001:4ca0:0:101:0:0:0:1
#
# User “john” should get access from ipv6 net/mask
#+ : john : 2001:4ca0:0:101::/64
#
# All other users should be denied to get access from all sources.
#- : ALL : ALL

設定檔語法:

標籤:帳號:管道

標籤:有 + 和 – ,+ 代表允許、- 代表拒絕。

帳號:可用 User Name 或 Group Name 或用 ALL 代表全部,可搭配 EXCEPT 語法使用。

管道:可用 IPv4、IPv6、網段、NetWork ID、FQDN、TTY、服務,或用 ALL 代表全部,可搭配 EXCEPT 語法使用。

設定檔採 First Match 優先比對流程,由上至下、由左至右。

==============================

網路認證模組

PAM 同時提供了其他模組以網路方式來認證:

pam_krb5.so : 以 Kerberos V ( Kerberos 版本 5 ) 為加密通訊的認證機制。

pam_ldap.so : 以 LDAP 認證的模組,帳密傳輸為非加密,應另加 TLS 加密機制。

pam_smb_auth.so : 以 Windows AD 認證的模組。

pam_winbind.so : 以 Windows AD 認證的模組。

有些服務如 NIS、Hesiod、部份 LDAP,會使用 pam_unix.so 來透過 NSS 作為 auth 階段的認證,但是依然需要相對應的模組來作為 password 階段的密碼變更機制。

==============================

pam_nologin.so 模組

作用:限制特定的帳號不可以登入系統。

用法:auth      required      pam_nologin.so

設定方式:在 /etc 目錄下 touch 一個 nologin 的空檔案即可。

#touch /etc/nologin

==============================

pam_time.so 模組

作用:限制帳號在那些「時段」之中,可透過什麼管道來存取何種服務。

用法:account      required      pam_time.so

設定檔:/etc/security/time.conf

設定檔語法:

服務;管道;使用者;時間

服務:為 PAM 堆疊的名稱。
管道:tty 的名稱。
使用者:帳號名稱。
時間:星期的代號 ( Mo、Tu、We、Th、Fr、Sa、Su )

四個欄位中,都可以使用 ! & |:
!: not
&: and
|: or

# cat /etc/security/time.conf
# the syntax of the lines is as follows:
#       services;ttys;users;times
# the combination of individual users/terminals etc is a logic list
# namely individual tokens that are optionally prefixed with ‘!’ (logical
# not) and separated with ‘&’ (logical and) and ‘|’ (logical or).
# services
#       is a logic list of PAM service names that the rule applies to.
# ttys
#       is a logic list of terminal names that this rule applies to.
# users
#       is a logic list of users or a netgroup of users to whom this
#       rule applies.
# times
#       The two character combinations accepted are
#               Mo Tu We Th Fr Sa Su Wk Wd Al
#       each day/time-range can be prefixed with a ‘!’ to indicate “anything
#       but”
#       The time-range part is two 24-hour times HHMM separated by a hyphen
#       indicating the start and finish time (if the finish time is smaller
#       than the start time it is deemed to apply on the following day).
#blank;tty* & !ttyp*;you|me;!Al0000-2400
#xsh;ttyp*;root;!WdMo0000-2400

==============================

pam_listfile.so 模組

作用:限制某些帳號「可以或不可以」使用服務

用法:auth      required      pam_listfile.so      item=xxxx sense=xxxx file=/PATH/FILE_NAME onerr=xxxxxx

設定檔:/PATH/FILE_NAME

說明:
item = [tty|user|rhost|ruser|group|shell]      (驗證對象)

sense = [allow|deny]      (清單中的帳號所要套用的動作)

onerr = [succeed|fail]      (模組本身發生錯誤時的處理方式)

例:

auth    required    pam_listfile.so    item=user    sense=deny    file=/etc/vsftpd/ftpusers    onerr=succeed
在 /etc/vsftpd/ftpusers 裡面所載的所有使用者都不能使用此服務,如果模組本身發生錯誤,就讓使用者通過。

==============================

SESSION 階段模組

pam_limits.so:在使用者登入時設定電腦資源的限制,如登入數、記憶體使用、CPU 使用、執行程序數量等,可在 /etc/security/limigs.conf 中設定,可使用 ulimits 命令解除柔性限制 ( soft limits )。

pam_console.so:設定由 console 登入的使用者可使用什麼本機裝置,如軟碟機、CD-ROM 等,可在 /etc/security/console.perm 中設定。如果在 auth 階段中使用此模組,可以限制使用者只能以本機 console 登入。

pam_selinux.so:在登入和登出時,協助設定 SELinux 的安全權限控管 (security context)。

pam_mkhomedir.so:在登入時如果使用者沒有家目錄,就協助建立一個,這模組在網路認證 NIS 或 LDAP 時相當有用。

==============================

應用程式的認證

登入程序並非是唯一使用 PAM 認證之處,部份應用程式,如 su、sudo 等,也需要使用到 PAM 驗證:

pam_rootok.so:使用 su 時會用到此模組,通過即可取得 root 的權限,也容許 root 不用密碼就轉換成其他身份。

pam_timestamp.so:使用在 auth 階段,一般帳號第一次使用 sudo 時會詢問密碼,通過密碼驗證後,此模組會在 /var/run/sudo 底下產生一個時間戳記 (timestamp),當第二次使用 sudo 時,它會先比對時間戳記,如果間隔小於 5 分鐘,此模組會直接回傳認證通過。

pam_xauth.so:當一般帳號使用 su 轉換身份後,此模組會轉送該帳號的 xauth cookie,讓使用者可以使用原本身份的 Xwindow。

==============================

PAM 問題解決

PAM 會將登入登出事件以及錯誤訊息記錄在 /var/log/messages 和 /var/log/secure 之中,所以當發生問題時,第一件事就是先看看這兩個 log 檔。

要注意如果使用 Xwindow 中的 system-config-authentication 程式時,它會覆蓋我們自己編輯的手動設定 /etc/pam.d/system-auth 及 /etc/nsswitch.conf 這兩個檔案。

當要變更 PAM 的設定時,最好是保留一個已經用 root 權限登入的 shell,如果 PAM 設定失敗,會連 root 也無法登入,這時保留的 shell 就可以災難復原。要是已經無法進入了,就須要在開機時用單人模式 (single-user mode) 進入修復,單人模式會繞過 PAM 認證,直接進入 shell。如果 GRUB 有設密碼而無法用單人模式時,就必須使用救援光碟 (rescue CD) 開機,再用 chroot 去將硬碟的設定檔改回來。如果 BIOS 無法進入或無法改用光碟開機,那就要把硬碟拔下來,接到其他電腦上去修改設定檔。如果硬碟壞掉了……那就買一顆新的吧……>_<|||

==============================

分類: Linux、Red Hat、RH253,標籤: login、PAM、pam_unix.so,發佈日期: 2009/03/27,作者: ishm

文章導覽

← 可插入式認證模組 PAM(一) 檔案系統管理(一) →

月曆

2023 年 4 月
日 一 二 三 四 五 六
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« 4 月    

彙整

  • 2022 年 4 月 (1)
  • 2022 年 3 月 (1)
  • 2021 年 12 月 (1)
  • 2021 年 9 月 (1)
  • 2021 年 7 月 (1)
  • 2021 年 3 月 (1)
  • 2020 年 5 月 (1)
  • 2020 年 1 月 (3)
  • 2019 年 11 月 (1)
  • 2019 年 10 月 (1)
  • 2019 年 6 月 (1)
  • 2019 年 5 月 (1)
  • 2019 年 3 月 (1)
  • 2019 年 1 月 (1)
  • 2018 年 9 月 (1)
  • 2018 年 5 月 (2)
  • 2018 年 4 月 (1)
  • 2018 年 3 月 (1)
  • 2017 年 3 月 (1)
  • 2016 年 11 月 (1)
  • 2016 年 5 月 (1)
  • 2016 年 4 月 (1)
  • 2016 年 3 月 (2)
  • 2015 年 2 月 (1)
  • 2015 年 1 月 (1)
  • 2014 年 10 月 (1)
  • 2014 年 9 月 (1)
  • 2014 年 8 月 (3)
  • 2014 年 6 月 (1)
  • 2013 年 12 月 (1)
  • 2013 年 8 月 (1)
  • 2013 年 7 月 (1)
  • 2013 年 6 月 (1)
  • 2013 年 5 月 (1)
  • 2013 年 4 月 (1)
  • 2013 年 3 月 (1)
  • 2013 年 2 月 (1)
  • 2013 年 1 月 (5)
  • 2012 年 12 月 (3)
  • 2010 年 5 月 (1)
  • 2010 年 4 月 (2)
  • 2009 年 10 月 (1)
  • 2009 年 9 月 (1)
  • 2009 年 6 月 (1)
  • 2009 年 4 月 (9)
  • 2009 年 3 月 (19)
  • 2009 年 2 月 (5)

分類

  • DB2 (1)
  • DNS (1)
  • Firefox (3)
  • Linux (54)
    • BASH (1)
    • Bind (5)
    • CentOS (13)
  • NetAPP (2)
  • Network (3)
  • Red Hat (31)
    • RH133 (13)
    • RH253 (19)
  • Security (2)
  • Storage (2)
  • VMware (12)
    • ESXi (4)
    • vCenter (3)
    • VDP (1)
    • vSphere (5)
  • Windows (8)
  • Wordpress (1)
  • 憑證 (3)
  • 未分類 (10)
  • 網頁製作 (4)
    • JavaScript (1)
    • MySQL (2)
    • php (1)

網路連結

  • Google 翻譯
  • Wiki 維基百科
  • WordPress 官方網站
  • Yahoo!奇摩字典
本站採用 WordPress 建置