Tuesday 18 October 2016

recurcat - 循环高亮

Bash  代码:
#recursvie which access subdir/*, use {,**/}*, rf: http://stackoverflow.com/questions/1690809/what-expands-to-all-files-in-current-directory-recursively
#shift, rf: http://askubuntu.com/questions/521192/how-to-pass-wildcard-to-path-parameter-of-find-command-via-a-variable-in-scr
function recurcat () { 
    [ -z "$2" ] && echo "Missing 1st arg [Usage: recurcat extension (don't use single quotes bcoz nid expand)path] e.g. recurcat java **/*.java " && return 1;
    shopt -q globstar && : || {                                                                                                                                                                                    
        echo "globstar not enabled. Simply run again it will works or put it in ~/.bashrc for permanent change." && shopt -s globstar && return 1
    };
    lang="$1";
    shift;
    find "$@" -type f -exec bash -c 'echo; echo -e "\e[41m Filename: \e[40m\e[93m {}"; tput sgr0; echo' \; -exec bash -c "highlight -O xterm256 -S "$lang" {}" \;
}


使用方法

C 例子:


Python 例子:


Java 例子:


注:

[0] 安装方式:把那个 bash 代码放在 ~/.bashrc 文件, source ~/.bashrc 后即可享用,比煮杯面还快哟~

[1]  recurcat python *.py, python 是程序语言名字, py 是文件后缀。代码没有处理自动检测, 毕竟有些文件如 ~/.bashrc 也不是 .sh 后缀, 所以我觉得不重要。不局限于代码,如果是普通文件也是可以用的(高亮数字好看很多),只要程序语言放 sh 即可。

[2] recurcat python *.py 只搜索当下目录, recurcat python **/*.py 则搜索循环目录。

[3] 确保你有安装 highlight utility 先。

[4] 使用这个命令(或任何命令)的时候,不需要重新输入,只要你 bash history size 够大(我的是 unlimited),只需输入 recu 再按 Page Up 很轻易就可以重新使用命令。

[5] 当然,也要调整 Terminal scrollback,我本人是调 100000 行。

[6] 用 Shift+Page Up/Down 在 terminal 快速上下浏览。

[7] 如果你要把文件名的红色改成其它颜色,如青色,就把最后一行那个 41m 换成 42m。

[8] 不局限于当前目录和全部目录, 你完全可以发挥你的 regex 想象力,比如说用 recurcat c ./*m*.c 只找当前目录含有字眼的文件, 如 ./myls.c , 或 recurcat c **/*m*.c 搜索全部目录含有 m 字眼的文件。这也是为什么我不打算把这个代码改成接受 parameter 的原因,regex 应该是用户自己输入的。

[9] 如果想改代码的输出颜色, 用 `touch /tmp/dummy.c; strace -ff -e open highlight -O xterm256 -S c /tmp/dummy.c` 命令即可找出相关 config 文件。

[10] 如果代码能改进,欢迎留言哟~


zombie symlink

无端发现很好玩的 symlink 举止。



这个有点关系,`sudo sysctl -w fs.protected_symlinks=0` 就可以避免这个问题,但我仍然认为很好玩 hor。

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 这是严谨的检验,做到很完美干啥,达到目的才是重点。