脆弱性を作り易い関数を調べるスクリプトを書いてみた.

セキュアソフトウェアを買って読書中なので,P147に載ってるスクリプトLinux版を作って見ました.
誤用しやすい関数(strcpy,sprintfとか)を探すのに使うスクリプトです。

#!/bin/sh
#set -x

if [ $# != 3 ]; then
    echo "Usage: " $0 " [dir] [function list] [output file]"
    exit 0
fi

target_dir=$1
func_list=$2
out_file=$3

tmp=`date '+%Y%m%d%H%M%S'`

files=`find $target_dir -perm -o+x -type f`
for f in $files
do
    echo "Checking $f"
    file $f > $tmp
    exe=`cut -f2 -d' ' $tmp`
    if [ "$exe" = "ELF" ]; then
	echo "------------------------------------------\n"$f >> $out_file
	objdump -d $f | grep -n -f $func_list >> $out_file
	echo "------------------------------------------\n\n\n" >> $out_file
    fi
    rm -f $tmp
done

echo "Done."

exit 0

検索したい関数名を書いたファイルを用意して,

[masami@moonlight:~]% cat grep_str.txt
sprintf
strcpy
strcat

こんな感じで使うと・・・

[masami@moonlight:~]% ./find_func.sh /usr/sbin/  grep_str.txt test.log
Checking /usr/sbin/grpck
Checking /usr/sbin/hibernate
Checking /usr/sbin/chpasswd
Checking /usr/sbin/kerneloops
Checking /usr/sbin/unregister-common-lisp-source
Checking /usr/sbin/dpkg-preconfigure
Checking /usr/sbin/nfsstat
〜中略〜
Checking /usr/sbin/update-rc.d
Checking /usr/sbin/avahi-autoipd
Checking /usr/sbin/remove-default-ispell
Done.
[masami@moonlight:~]% 

こういう感じで,ログを出力します.

------------------------------------------
/usr/sbin/grpck
302:0000000000401ce0 <strcpy@plt>:
2655:  404085:  e8 56 dc ff ff          callq  401ce0 <strcpy@plt>
5572:  406b50:  e8 8b b1 ff ff          callq  401ce0 <strcpy@plt>
5580:  406b6f:  e8 6c b1 ff ff          callq  401ce0 <strcpy@plt>
5592:  406b9b:  e8 40 b1 ff ff          callq  401ce0 <strcpy@plt>
5624:  406c1b:  e8 c0 b0 ff ff          callq  401ce0 <strcpy@plt>
------------------------------------------



------------------------------------------
/usr/sbin/chpasswd
------------------------------------------



------------------------------------------
/usr/sbin/kerneloops
222:00000000004018f8 <strcat@plt>:
1448:  402ad5:  e8 1e ee ff ff          callq  4018f8 <strcat@plt>
1570:  402ccd:  e8 26 ec ff ff          callq  4018f8 <strcat@plt>
------------------------------------------