1

我的代码

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 示例文件中,测试帐户未加密密码

但是,我的代码无法捕捉到它

我发现 Initialize FLAG 对结果有影响

请问我可以得到一些建议吗?

谢谢

运行 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
4

1 回答 1

1

问题是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
于 2021-02-19T03:17:48.870 回答