2017-01-22 52 views
-1

我在这里有这个bash脚本,我试图修改,以检查是否只有一个root id,是否易受攻击,目前,此脚本只检查是否存在重复的uid并显示共享相同uid的用户。提前致谢! :)Bash:如何检查是否只有一个root ID和所有用户UID是唯一的?

bash脚本:

#!/bin/bash 
/bin/cat /etc/passwd| /bin/cut -f3 -d":" | /bin/sort -n | /usr/bin/uniq-c | while 
read x ; do 
    [ -z "${x}" ] && break 
    set -$x 
    if [ $1 -gt1 ]; then 
     users=`/bin/gawk -F: '($3 == n) { print $1 }' n=$2 /etc/passwd| /usr/bin/xargs` 
     echo "Duplicate UID ($2): ${users}" 
    fi 
done 

预期输出:

Audit criteria: There is only one root id 

Vulnerability: Yes 

Details: See below 


root:!:0:0::/:/usr/bin/bash 

jdoe:*:0:1:John Doe:/home/jdoe:/usr/bin/bash 
+0

我建议用'的uniq -c'更换'uniq的-C'并请看看:http://www.shellcheck.net/ – Cyrus

+0

有你正在使用完整路径来处理'cut','grep','awk'等基本命令的原因? – codeforester

+0

@codeforester这是一个给我的例子,因为我是一个初学者,在bash脚本编写之前,我没有意识到它,直到你指出它,谢谢你的输入! –

回答

0

您可以大大简化你的脚本,因为所有你所追求的是用户ID 0,这是根:

#!/bin/bash 
root_count=$(cut -f3 -d":" /etc/passwd | grep -wc 0) 
if [[ $root_count > 1 ]]; then 
    users=$(awk -F: '($3 == 0) { print $1 }' /etc/passwd | xargs) 
    echo "Duplicate roots: ${users}" 
fi 
0

您可以使用awk找到了:

if ! awk -F: '$3==0{c++}END{exit !(c<2)}' /etc/passwd ; then 
    echo "More than one user with uid 0" 
fi 
相关问题