【问题标题】:Bash: how to check if there is only one root id and all user UIDs are unique?Bash:如何检查是否只有一个 root id 并且所有用户 UID 都是唯一的?
【发布时间】:2017-06-06 22:00:57
【问题描述】:

我在这里有这个 bash 脚本,我正在尝试修改它以检查是否只有一个 root id,它是否容易受到攻击,目前,这个脚本只检查是否有重复的 uid 并显示共享相同的用户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

【问题讨论】:

  • 我建议用uniq -c替换uniq-c,请看一下:shellcheck.net
  • 您是否有理由为cutgrepawk 等基本命令使用完整路径?
  • @codeforester 这是给我的一个例子,我是 bash 脚本的初学者,直到你指出我才意识到这一点,感谢您的输入!

标签: linux bash scripting rhel audit


【解决方案1】:

您可以大大简化您的脚本,因为您要查找的只是用户 id 0,即 root:

#!/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

【讨论】:

    【解决方案2】:

    您可以使用awk 找出答案:

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

    【讨论】:

      猜你喜欢
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 2014-10-10
      • 2020-10-24
      相关资源
      最近更新 更多