Sunday 16 October 2016

root 文件小检验

无端醒觉 ln -s /root/.bash_aliases /home/xiaobai/.bash_aliases 方便备份的安全隐患,这种举动基本是破坏掉 sudo 的安全政策。所以就写了一个 bash function 为 Kali 和 Fedora 做个小检验,看下以前的我犯了多少错误。同时也发现我常用的 exsudo (exsudo is aliased to `sudo ') 其实是蛮危险的,减少危险性的方法是把预载的 ~/.bashrc, ~/.bash_aliases 等文件全部 symlink 去  root,只让 root 改预载的 alias/function/PATH。

代码(我刚吃饱更新了一下,su log 现在包括 fork 之前):

function nonroot_checker  () ( 
    w=$(whoami);
    tmpf="/tmp/su.log_by_$w.sh";
    echo -e "\e[40m\e[97m\n[Basic non-root file checker]\nUsage: Press Ctrl+D to quit right after loggedin to su session\n";
    script -qc 'su -c "strace -ff -e open su"' "$tmpf" && echo -e "\e[40m\e[93m\n\n[Checking su preload PATH]:\n\e[97m" && \grep -a --color=never -E -e '^\[pid[ ]+[0-9]+\] open\("' -e '^open\("' "$tmpf" | awk -F '"' '{print $2}' | while read -r f; do
        \ls -larthiFL --context --color=always "$f" 2> /dev/null | \grep -av --color=never -E " root[ ]+root |^total ";
    done;
    tput sgr0;
    echo -e "\n";
    target_PATH="/root:$PATH:$(sudo printenv PATH)";
    uniq_target_PATH=$(echo "$target_PATH" | awk -v RS=':' -v ORS=":" '!a[$1]++');
    export IFS=":";
    for p in $uniq_target_PATH;
    do
        tput sgr0;
        echo -e "\e[40m\e[93m[Checking PATH $p ]:\e[97m";
        sudo ls -RlarthiFL --context --color=always "$p" 2> /dev/null | \grep -av --color=never -E " root[ ]+root |^total |^$p:";
    done;
    tput sgr0;
    export GREP_COLORS='mt=01;36';
    echo -e "\n[User id reference]:";
    getent passwd | cut -d: -f1,3 | sort -t: -k2n | sed ':a;N;$!ba;s/\n/ /g' | \grep -a --color -E '[0-9]+ |[0-9]+$';
    echo -e "\n[Group id reference]:";
    getent group | cut -d: -f1,3 | sort -t: -k2n | sed ':a;N;$!ba;s/\n/ /g' | \grep -a --color -E '[0-9]+ |[0-9]+$';
    echo;
    tput sgr0 )

<<"pitfall"
[1] ls -L is the key to see the symlink target is normal user which will normally hide the fact if not use -L
[2] \grep, \ls(if no sudo infront) to avoid unintent alias, lazy do for echo though. `sudo ls` no nid \
[3] ls+grep http://stackoverflow.com/questions/867877/preserve-ls-colouring-after-greping
[4] use subshell () to reduce use of export IFS (got problem though), GREP_COLORS, ... etc
[5] keep in mind paste latest code to vim for ~/.bash_aliases might failed and revert back to orig func (my personal problem)
[6] use sudo ls to get more files when ls, except for su preload PATH then no nid
[7] cp symlink will resolve to target, so no nid worry put ~/.bash_aliases at root dir but forgot cp backup root punya version
[8] Ensure saved inside /tmp file for su's log instead of default is curr directory to avoid  this file created by root user and not able to access by normal user when run the code again //you might say can use sudo, i prefer avoid mass use of sudo
[9]  don't be fool,  0 below is not gid, but it's --context outcome
root@dnxb:/tmp# sudo ls -RlarthiFL --context --color=always "/root/.config/pulse" 2> /dev/null
/root/.config/pulse:
total 8.0K
     ? l?????????  ? ?    ?    0    ?            ? d7329d76e555dca6868c6000fed5c033-runtime
134628 drwxr-xr-x 14 root root ? 4.0K Oct 14 02:16 ../

[9.1]
but still, don't know why it will show this broken link, while other will simply throws to 2>/dev/null [onhold:0]

[10] grep, [0-9]\+ when no -E, [0-9]+ when -E

[11] If failed to strip newline for bottom uid/gid list, might nid do `IFS=' '` bcoz already corrupted, happen even when running `typea echo`(my advanced version of `type -a`), but still not stable even i already put infront, better use `sed ':a;N;$!ba;s/\n/ /g'`

[12] always use "" for echo easier to extends if want to add $var

pitfall


例子:

用法很简单,先把上述代码 paste 在你的 ~/.bashrc (Fedora) 或 ~/.bash_aliases (Kali 普通用户, 想讲,install kali 不表示你需要用 root 裸奔)。然后在 source 那个那个 file 或开新的 terminal tab 加载 `nonroot_checker` function, 然后在 terminal 输入 nonroot_checker:



然后要输入 su 密码,那个 strace 大量的 log 停顿后(我没加入 `clear` 那个 log 是因为避免用户看不见输入密码失败的信息 ),用 Ctrl+D 或  exit 出去 su session (有更好的方法拿 su log 欢迎指正)。然后要输入 sudo (如果之前已经输入过 sudo,短时间内就不需要)。然后黄色下面开始的就是检测结果 (strace log 下面,如果太多 PATH, 黄色就可能 scroll 在很上面, 你可能要提升 terminal Scrollback,本人是设置 100000 行 ):

如上图,我检测到 /root/.vimrc 是属于我的 xiaobai 普通用户和用户组,是我不懂事犯下的失误,ls -l 可以更清楚,别只看 /root/.vimrc 显示是 root 的, symlink target 才是重点。

我也在 Fedora 检测了一下。发现 sopcast 的那个 sp-sc-auth 文件,好像有点亮眼...


本人 linux 小白用用下会开始质疑 sudo 重要性,可是 alias/function 是用户 level 的文件负责的,sudo 可以避免直接运行那些 alias/function。不过不该用 sudo 的时候就别用,不然你分不清输入密码给 root 是运行正常程序还是恶意程序 (酱想,当你运行 hello_world.c 的 a.out 却要求你输入密码时,你就知道  something wrong 了,可是如果你用 sudo ./a.out, sudo 要输入密码不是很正常吗 ? 你就不懂 something wrong 了 ) 。

[更新]
[1] 为什么不用 find,其实我也是过后才想到 "是 hor",但是 find 如果没有我想要的 output 过后也是要 exec ls,倒不如省下一个步骤。这个 parsing 不严谨,因为文件名如果叫 "root root" 也算上去了, 我也没有 expect 这是严谨的检验,做到很完美干啥,达到目的才是重点。








3 comments: