Linux 系统安全规范
转载自:http://zoukejian.blog.51cto.com/131276/56631
#/bin/bash
#
export
# ---------------------------------------------------------------------------------------------------------#
# define vars
script_base_dir=/root/system_check
today=$(date +'%Y-%m-%d')
today_perm_777=$script_base_dir/"$today"_perm_777.txt
today_sugid=$script_base_dir/"$today"_perm_sugid.txt
today_owner_none=$script_base_dir/"$today"_owner_none.txt
today_files_jsp=$script_base_dir/"$today"_files_jsp.txt
today_result=$script_base_dir/"$today"_result.txt
yesterday=$(date +'%Y-%m-%d' --date '1 days ago')
yesterday_perm_777=$script_base_dir/"$yesterday"_perm_777.txt
yesterday_sugid=$script_base_dir/"$yesterday"_perm_sugid.txt
yesterday_owner_none=$script_base_dir/"$yesterday"_owner_none.txt
yesterday_files_jsp=$script_base_dir/"$yesterday"_files_jsp.txt
ip=`ip addr l dev eth0 | grep 'inet ' | awk '{ print $2 }' | awk -F/ '{ print $1 }' | awk -F. '{ print $4 }'`
mail_subject="Check report from $ip on $today"
mail_recept="mgroup@xx-xxx.com"
# ---------------------------------------------------------------------------------------------------------#
# search for abnormal dirs and files
echo begin to find world-writable directories, please wait... > $today_perm_777
find / \( -path /usr/local/v2.marry5.com/files -o -path /proc \) -prune -o \
-type d \( -perm 777 \) -exec ls -lda {} \; >> $today_perm_777
echo finished finding world-writable directories >> $today_perm_777
echo begin to find suid/sgid files, please wait... > $today_sugid
find / \( -path /usr/local/v2.marry5.com/files -o -path /proc \) -prune -o \
-type f \( -perm -2000 -o -perm -4000 \) -exec ls -la {} \; >> $today_sugid
echo finished finding suid/sgid files >> $today_sugid
echo begin to find files without owner, please wait... > $today_owner_none
find / \( -path /usr/local/v2.marry5.com/files -o -path /proc \) -prune -o \
-nouser -o -nogroup -exec -ls -lda {} \; >> $today_owner_none
echo finished finding files without owner >> $today_owner_none
# this line is for 81 only
echo begin to find jsp files in directory /usr/local/v2.marry5.com, please wait... > $today_files_jsp
if [ "$ip""A" == "81A" ] ; then
find /usr/local/v2.marry5.com/files -type f -name *.jsp -exec ls -la {} \; >> $today_files_jsp
fi
echo finished finding jsp files >> $today_files_jsp
# ---------------------------------------------------------------------------------------------------------#
# compare the search result of today to yesterday's result
echo "Date: " $(date +'%Y-%m-%d') > $today_result
echo "IP: " $ip >> $today_result
echo "Findings:" >> $today_result
echo >> $today_result
if [ ! -f $yesterday_perm_777 ] ; then
echo "The file $yesterday_perm_777 has lost! " >> $today_result
else
echo "World-writable directories:" >> $today_result
diff $yesterday_perm_777 $today_perm_777 >> $today_result
fi
echo >> $today_result
if [ ! -f $yesterday_sugid ] ; then
echo "The file $yesterday_sugid has lost! " >> $today_result
else
echo "SUID/SGID files:" >> $today_result
diff $yesterday_sugid $today_sugid >> $today_result
fi
echo >> $today_result
if [ ! -f $yesterday_owner_none ] ; then
echo "The file $yesterday_owner_none has lost!" >> $today_result
else
echo "Files without owner:" >> $today_result
diff $yesterday_owner_none $today_owner_none >> $today_result
fi
echo >> $today_result
if [ "$ip""A" == "81A" ] ; then
if [ ! -f $yesterday_files_jsp ] ; then
echo "The file $yesterday_files_jsp has lost!" >>$today_result
else
echo "Files without owner:" >> $today_result
diff $yesterday_files_jsp $today_files_jsp >> $today_result
fi
fi
# ---------------------------------------------------------------------------------------------------------#
# mail the finding to monitor group and remove old files
mail -s "$mail_subject" "$mail_recept" < $today_result
rm $(date +'%Y-%m-%d' --date '1 days ago')*.txt
# ---------------------------------------------------------------------------------------------------------#
exit 0