Ubuntu 18.04 Bash Shell 脚本的漏洞检查

问题描述

我的代码

vuln=0 # initialize FLAG variable

test -f /etc/shadow # Check exist /etc/shadow
if [ $? == 1 ]
then
    vuln=1 # Not exist /etc/shadow File -> FLAG ON
else
    cat /etc/passwd | while read pass_protection # Read 1 Line
    do
        temp=`echo $pass_protection | cut -d':' -f2` # Parse the line
        if [ $temp != "x" ] # If password not encrypted
        then
            vuln=1 # FLAG ON
            break
        fi
    done
fi

if [ $vuln == 1 ] # Print Result
then
    echo "[4-1] Vuln"
else
    echo "[4-1] Not Vuln"
fi 

/etc/passwd 示例

man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
test:test_PASSWORD:10:10:test:/:/
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin

这段代码是检查/etc/passwd密码是​​否加密

在/etc/passwd示例文件中,测试账户没有加密密码

但是,我的代码无法捕获它

我发现初始化标志对结果有影响

请问我能得到一些建议吗?

谢谢

运行 sh -x script.sh

+ read pass_protection
+ cut -d: -f2
+ echo uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
+ temp=x
+ [ x != x ]
+ read pass_protection
+ cut -d: -f2
+ echo test:test_PASSWORD:10:10:test:/:/
+ temp=test_PASSWORD
+ [ test_PASSWORD != x ]
+ vuln=1
+ break
+ [ 0 == 1 ]
test.sh: 328: [: 0: unexpected operator
+ echo [4-1] Not Vuln
[4-1] Not Vuln

解决方法

问题是cat /etc/passwd | while read。在这里,管道的右侧在子壳中运行。子壳不能影响父壳。 while ... done 中设置的每个变量都丢失了。

要在没有子 shell 的情况下读取文件,请使用 while ... done < /etc/passwd

除此之外,您可以将整个脚本压缩为一个 grep 命令:

if grep -Evq '^[^:]*:x:' /etc/passwd; then
  echo vulnerable
else
  echo ok
fi

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...