zoukankan      html  css  js  c++  java
  • 一起来学linux:PAM模块

    Linux中执行有些程序时,这些程序在执行前首先要对启动它的用户进行认证,符合一定的要求之后才允许执行,例如login, su

    Linux中进行身份或是状态的验证程序是由PAM来进行的,PAMPluggable Authentication Modules)可动态加载验证模块,因为可以按需要动态的对验证的内容进行变更,所以可以大大提高验证的灵活性。

    Linux各个发行版中,PAM使用的验证模块一般存放在/lib/security/目录下,可以使用ls命令进行查看本计算机支持哪些验证控制方式,一般的PAM模块名字例如pam_unix.so,模块可以随时在这个目录下添加和删除,这不会直接影响程序运行,具体的影响在PAM的配置目录下。

    PAM的配置文件位于/etc/pam.d文件下面。ls命令可以看到所有的文件配置

    root@zhf-maple:/etc/pam.d# ls -al

    总用量 124

    drwxr-xr-x   2 root root  4096 11月 27 21:31 .

    drwxr-xr-x 133 root root 12288 11月 27 21:31 ..

    -rw-r--r--   1 root root   384 11月 20  2014 chfn

    -rw-r--r--   1 root root    92 11月 20  2014 chpasswd

    -rw-r--r--   1 root root   581 11月 20  2014 chsh

    -rw-r--r--   1 root root  1208 11月 26 20:23 common-account

    -rw-r--r--   1 root root  1249 11月 26 20:23 common-auth

    -rw-r--r--   1 root root  1480 11月 26 20:23 common-password

    -rw-r--r--   1 root root  1470 11月 26 20:23 common-session

    -rw-r--r--   1 root root  1435 11月 26 20:23 common-session-noninteractive

    -rw-r--r--   1 root root   606 8月  21 07:54 cron

    -rw-r--r--   1 root root    69 9月  14 00:53 cups

    -rw-r--r--   1 root root   884 9月  30  2015 lightdm

    -rw-r--r--   1 root root   551 9月  30  2015 lightdm-autologin

    -rw-r--r--   1 root root   727 8月  24  2015 lightdm-greeter

    -rw-r--r--   1 root root  4905 11月 27 21:31 login

    -rw-r--r--   1 root root    92 11月 20  2014 newusers

    -rw-r--r--   1 root root   520 4月  22  2017 other

    -rw-r--r--   1 root root    92 11月 20  2014 passwd

    -rw-r--r--   1 root root   270 5月  24  2017 polkit-1

    -rw-r--r--   1 root root   168 1月  28  2016 ppp

    -rw-r--r--   1 root root   143 7月  11 09:19 runuser

    -rw-r--r--   1 root root   138 7月  11 09:19 runuser-l

    -rw-r--r--   1 root root  2257 11月 20  2014 su

    -rw-r--r--   1 root root   239 6月  13 03:51 sudo

    -rw-r--r--   1 root root   317 10月  3 11:24 systemd-user

    -rw-r--r--   1 root root    57 9月  13 09:14 ukui-screensaver

    -rw-r--r--   1 root root   319 5月   8  2014 vsftpd

    我们看下文件里面的格式是什么:

    root@zhf-maple:/etc/pam.d# cat ./login

    #

    # The PAM configuration file for the Shadow `login' service

    #

    # Enforce a minimal delay in case of failure (in microseconds).

    # (Replaces the `FAIL_DELAY' setting from login.defs)

    # Note that other modules may require another minimal delay. (for example,

    # to disable any delay, you should add the nodelay option to pam_unix)

    auth       optional   pam_faildelay.so  delay=3000000

    auth       required   pam_securetty.so

    配置文件的格式主要有4列组成:

    第一列代表模块类型,类型分为4种:

    auth: 用来对用户的身份进行识别.如:提示用户输入密码,或判断用户是否为root

    account:对帐号的各项属性进行检查.如:是否允许登录,是否达到最大用户数,或是root用户是否允许在这个终端登录等

    session:这个模块用来定义用户登录前的,及用户退出后所要进行的操作.如:登录连接信息,用户数据的打开与关闭,挂载文件系统等.

    passwd:使用用户信息来更新.如:修改用户密码.

    第二列代表控制标记:也有4种类型:

    required: 表示即使某个模块对用户的验证失败,也要等所有的模块都执行完毕后,PAM 才返回错误信息。这样做是为了不让用户知道被哪个模块拒绝。如果对用户验证成功,所有的模块都会返回成功信息

    requisite: 与required相似,但是如果这个模块返回失败,则立刻向应用程序返回失败,表示此类型失败.不再进行同类型后面的操作.

    sufficient:表示如果一个用户通过这个模块的验证,PAM结构就立刻返回验证成功信息(即使前面有模块fail了,也会把 fail结果忽略掉),把控制权交回应用程序。后面的层叠模块即使使用requisite或者required 控制标志,也不再执行。如果验证失败,sufficient 的作用和 optional 相同

    optional:表示即使本行指定的模块验证失败,也允许用户接受应用程序提供的服务,一般返回PAM_IGNORE(忽略).

    第三列代表模块路径:要调用模块的位置。一般是在/lib/security文件中。但是每个版本都不太一样,比如我是用的ubuntun17.10。模块位于/lib/x86_64-linux-gnu/security文件夹下面

    zhf@zhf-maple:/lib/x86_64-linux-gnu/security$ ls -al

    总用量 1064

    drwxr-xr-x 2 root root   4096 11月 26 20:05 .

    drwxr-xr-x 3 root root  12288 11月 26 20:08 ..

    -rw-r--r-- 1 root root  18672 4月  22  2017 pam_access.so

    -rw-r--r-- 1 root root  10080 10月 11 16:26 pam_cap.so

    -rw-r--r-- 1 root root  10376 4月  22  2017 pam_debug.so

    -rw-r--r-- 1 root root   6000 4月  22  2017 pam_deny.so

    -rw-r--r-- 1 root root  10336 4月  22  2017 pam_echo.so

    -rw-r--r-- 1 root root  14536 4月  22  2017 pam_env.so

    -rw-r--r-- 1 root root  14728 4月  22  2017 pam_exec.so

    -rw-r--r-- 1 root root  60368 4月  22  2017 pam_extrausers.so

    -rw-r--r-- 1 root root  10368 4月  22  2017 pam_faildelay.so

    -rw-r--r-- 1 root root  14576 4月  22  2017 pam_filter.so

    -rw-r--r-- 1 root root  10304 4月  22  2017 pam_ftp.so

    -rw-r--r-- 1 root root  42984 7月  31 21:21 pam_gnome_k

    第四列是模块参数:参数可以用多个,之间用空格隔开。例如:password   required   pam_unix.so nullok obscure min=4 max=8 md5

    我们再来看下配置文件里面的内容:比如下面两种。其中红色的字体注释已经写明了对应的模块需要做那些配置

    Uncomment and edit /etc/security/access.conf if you need to

    # set access limits.

    # (Replaces /etc/login.access file)

    # account  required       pam_access.so

    Sets up user limits according to /etc/security/limits.conf

    # (Replaces the use of /etc/limits in old login)

    session    required   pam_limits.so

    下面我们介绍集中常用的PAM模块,如下表:

    PAM模块

    结合管理类型

    说明

    pam_unix.so

    auth

    提示用户输入密码,并与/etc/shadow文件相比对.匹配返回0

    account

    检查用户的账号信息(包括是否过期等).帐号可用时,返回0.

    password

    修改用户的密码. 将用户输入的密码,作为用户的新密码更新shadow文件

    pam_shells.so

    auth

    account 

    如果用户想登录系统,那么它的shell必须是在/etc/shells文件中之一的shell

    pam_deny.so

    account

    auth

    password

    session

    该模块可用于拒绝访问

    pam_permit.so

    auth

    account

    password

    session

    模块任何时候都返回成功.

    pam_securetty.so

    auth

    如果用户要以root登录时,则登录的tty必须在/etc/securetty之中.

    pam_listfile.so

    auth

    account

    password session

    访问应用程的控制开关

    pam_cracklib.so

    password

    这个模块可以插入到一个程序的密码栈中,用于检查密码的强度.

    pam_limits.so

    session

    定义使用系统资源的上限,root用户也会受此限制,可以通过/etc/security/limits.conf或/etc/security/limits.d/*.conf来设定

    我们用pam_securetty.so这个模块来做个测试。这个模块的作用是如果用户要以root登录时,则登录的tty必须在/etc/securetty之中。我们知道我们在键盘上可以用ctrl+alt+f1-ctrl+alt+f6来登录各个终端。现在我们来限制root的登录终端

    第一步:首先在/etc/pam.d/login文件中配置如下,表明当用户为root的时候需要用pam_securetty.so来进行验证

    auth       required   pam_securetty.so

    第二步:在/etc/securetty中将tty2给注释掉

    # Virtual consoles

    tty1

    #tty2

    第三步:在用root用户登录tty2的时候则会提示login incorrect.

  • 相关阅读:
    【python+selenium自动化】图像识别技术在UI自动化测试中的实际运用
    【python+selenium自动化】使用pytest+allure2完成自动化测试报告的输出
    【python+selenium自动化】设置Chrome启动参数
    迭代器 和 生成器
    Python 第三方包上传至 PyPI 服务器
    python 模块导入全局变量
    装饰器
    set 集合数据类型
    lambda 、 map 、filter 、reduce 及 reversed 常用函数
    AVA + Spectron + JavaScript 对 JS 编写的客户端进行自动化测试
  • 原文地址:https://www.cnblogs.com/zhanghongfeng/p/7906390.html
Copyright © 2011-2022 走看看