• Shell脚本的安全性


    Shell脚本给管理和使用系统都带来了巨大的便利,然后在多用户系统中,也带来了许多安全性问题。在脚本中可能存在的安全性问题如下:

    (1)在脚本中使用cd命令,切换到不安全的目录执行脚本。

    (2)在脚本中修改了环境变量的值,从而导致系统产生了变化。

    (3)在某个目录中创建了非法的文件。例如使用重定向在目录/etc中创建了文件nologin,这将导致其他用户无法登陆。

    在Shell脚本中,提供了一种受限模式。脚本在受限模式中运行时,可以极大地保护系统的安全性。当脚本中出现cd命令、重定向、修改环境变量等不安全的行为时,Bash将会拒绝执行。

    1、调用Shell时启动受限模式

    要启动Bash的受限模式,可以在调用Shell语句后加上选项r,这时如果脚本中出现不安全的命令,会被系统拒绝。

    [root@localhost shell]# cat strict_mode.sh 
    #!/bin/bash -r
    #上面的选项r表示开启受限模式
    
    # this is an example script.
    # 2013.12.20
    
    cd /
    echo "`pwd`"
    
    echo "test" >~/test.tmp
    cat ~/test.tmp
    
    SHELL=/bin/ksh
    echo "SHELL="$SHELL

    [root@localhost shell]# ./strict_mode.sh 
    ./strict_mode.sh: line 6: cd: restricted
    /home/zhu/shell
    ./strict_mode.sh: line 9: /root/test.tmp: restricted: cannot redirect output
    cat: /root/test.tmp: No such file or directory
    ./strict_mode.sh: line 12: SHELL: readonly variable
    SHELL=/bin/bash

    从上面的执行结果可以看出,在Shell的受限模式下切换工作目录、重定向及修改环境变量等操作都被拒绝。

    2、使用set命令启动受限模式

    [root@localhost shell]# cat strict_mode.sh 
    #!/bin/bash 
    
    # this is an example script.
    # 2013.12.20
    
    set -r
    
    cd /
    echo "`pwd`"
    
    echo "test" >~/test.tmp
    cat ~/test.tmp
    
    SHELL=/bin/ksh
    echo "SHELL="$SHELL
    [root@localhost shell]# ./strict_mode.sh 
    ./strict_mode.sh: line 8: cd: restricted
    /home/zhu/shell
    ./strict_mode.sh: line 11: /root/test.tmp: restricted: cannot redirect output
    cat: /root/test.tmp: No such file or directory
    ./strict_mode.sh: line 14: SHELL: readonly variable
    SHELL=/bin/bash

    3、临时文件的安全性

    除了受限模式之外,如果在脚本中使用了临时文件,这些临时文件也可能会造成安全性问题。为此建议不要将临时文件放入系统临时目录/tmp中,因为任何登录系统的用户都可以看见系统临时目录中的文件。除此之外,脚本运行完成或由系统产生的中断退出时,建议删除脚本使用的临时文件。

  • 相关阅读:
    Java--环境变量(jdk、jre)配置
    Jsoup爬取数据设置代理IP
    数据库连接的莫名问题:create connection error
    springBoot使用过程的小感悟
    Activiti工作流引擎数据库表结构
    IDEA创建Activiti工作流开发
    Java开源爬虫框架WebCollector设置代理
    使用OkHttpClient爬取json数据
    雪中的杭州
    StringBuffer中的append方法
  • 原文地址:https://www.cnblogs.com/nufangrensheng/p/3484493.html
Copyright © 2020-2023  润新知