免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3695 | 回复: 4
打印 上一主题 下一主题

[学习共享] Linux帐号活动审计脚本 [复制链接]

论坛徽章:
2
白羊座
日期:2013-11-18 19:52:42辰龙
日期:2014-09-07 07:46:06
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-09-19 13:20 |只看该作者 |倒序浏览
本帖最后由 damcool 于 2012-09-21 09:16 编辑

为了防止用户滥用root等敏感帐号,并且便于管理上追踪相应的sudo活动造成的问题。简单的根据/etc/bashrc的功能实现了帐号活动记录和存档的功能。下面列出一些实现的主要步骤:
  • 首先,设置/etc/bashrc,在文件最后添加下列语句,用以建立帐号活动记录
    1. mkdir -p $HOME/.audit &>/dev/null
    2. export AUDIT_FILE=$HOME/.audit/$(who am i|awk '{printf("%s_%s_%s_%s.aud",$1,$2,$3,$4)}'|tr '/:' '-') &>/dev/null
    3. export PROMPT_COMMAND='(ts=`date "+[%Y%m%d %H:%M:%S]"` && cmd=$(echo ">>`history 1|{ read y x; echo -e "$x";}`<<@`who am i |awk \"{print \\$5}\"`") && lst_cmd=`tail -10 $AUDIT_FILE|grep "]"|tail -1|cut -d "]" -f 2-` && [[ "$lst_cmd" != "$cmd" ]] && echo "$ts$cmd") >> "$AUDIT_FILE"'
    复制代码
  • 然后,创建目录/usr/local/admin/,创建如下脚本audit.sh
    1. #!/bin/bash

    2. ################################################################################
    3. # Note:
    4. # Please add below lines into /etc/bashrc first
    5. # mkdir -p $HOME/.audit &>/dev/null
    6. # export AUDIT_FILE=$HOME/.audit/$(who am i|awk '{printf("%s_%s_%s_%s.aud",$1,$2,$3,$4)}'|tr '/:' '-') &>/dev/null
    7. # export PROMPT_COMMAND='(ts=`date "+[%Y%m%d %H:%M:%S]"` && cmd=$(echo ">>`history 1|{ read y x; echo -e "$x";}`<<@`who am i |awk \"{print \\$5}\"`") && lst_cmd=`tail -10 $AUDIT_FILE|grep "]"|tail -1|cut -d "]" -f 2-` && [[ "$lst_cmd" != "$cmd" ]] && echo "$ts$cmd") >> "$AUDIT_FILE"'
    8. ################################################################################

    9. ################################################################################
    10. #  Function Name:        HELP_USAGE
    11. #  Description:                Function to display the usage of the script
    12. #  Parameters:                 None
    13. #  Return:                        Help messages
    14. #  Called By:                Script Main Loop->Script Parameters' Handler
    15. #  History:                        2012-SEP-17        Initial Edition                                                RobinHoo
    16. ################################################################################
    17. function help_usage(){
    18. cat <<EOF
    19. Usage: $PROGNAME [OPTION]
    20. Audit files maintenance functions according to /etc/bashrc settings

    21.   -a, --archive        Archive the audit logging files older than 7 days and compress
    22.                                   the archive file of last month as well as delete the archive
    23.                                   over a year 365 days.
    24.   -h, --help   Show current help message of the script usages
    25.   
    26. Notes:
    27.   
    28. Please Report Script Bugs to $AUTHOR_MAIL


    29. EOF
    30. exit 1
    31. }

    32. ################################################################################
    33. #  Function Name:        ARCHIVE_LOGGING
    34. #  Description:                Function to archive the audit logging files
    35. #  Parameters:                 None
    36. #  Return:                        None
    37. #  Called By:                Script Main Loop->Script Parameters' Handler
    38. #  History:                        2012-SEP-17        Initial Edition                                                RobinHoo
    39. ################################################################################
    40. function archive_logging(){
    41.         local pwd_line
    42.         local user_id
    43.         local home_dir
    44.         local tar_files
    45.         local tar_file
    46.         for pwd_line in $(cat /etc/passwd|tr ' ' '_'); do
    47.                 user_id=$(echo "$pwd_line"|awk -F ":" '{print $1}')
    48.                 home_dir=$(echo "$pwd_line"|awk -F ":" '{print $6}')
    49.                 if [ -d "$home_dir/.audit" ]; then
    50.                         cd $home_dir/.audit
    51.                         for tar_files in $(find -type f -name "*aud" -mtime +7 -exec basename {} \;); do
    52.                                 tar_file="audit_"$(echo $tar_files|awk -F "_" '{print $3}'|cut -d "-" -f 1-2)".tar"
    53.                                 [ -f "$tar_file" ] && tar -rf "$tar_file" $tar_files || tar -cf "$tar_file" $tar_files
    54.                                 rm -f $tar_files &>/dev/null
    55.                         done
    56.        
    57.                         find -type f -name "*tar" -mtime +31 | xargs -i basename {} | xargs -i bzip2 -zq9 {} &>/dev/null
    58.                        
    59.                         find -type f -name "*bz2" -mtime +365 | xargs -i basename {} | xargs -i rm -f {} &>/dev/null
    60.                        
    61.                         chown $user_id.$user_id *tar *bz2 &>/dev/null
    62.                         chmod 644 *tar *bz2 &>/dev/null
    63.                 fi
    64.         done       
    65. }

    66. ################################################################################
    67. #  Function Name:        Script Main Loop
    68. #  History:                        2012-SEP-16        Initial Edition                                                RobinHoo
    69. ################################################################################
    70. BASE_DIR=$(cd "$(dirname "$0")" && pwd)
    71. PROGNAME=$(basename "$0")
    72. AUTHOR_MAIL="robin.hoo@pginw.com"
    73. ACRCHIVE=0
    74. HELP=0
    75. [ $# -eq 0 ] && help_usage
    76. while [ $# -gt 0 ]
    77. do
    78.     case "$1" in
    79.     (-a)                ACRCHIVE=1;shift;break;;
    80.     (-h)                HELP=1;shift;break;;
    81.     (--archive)        ACRCHIVE=1;shift;break;;
    82.     (--help)        HELP=1;shift;break;;
    83.     (*)                        echo "$PROGNAME: error - unrecognized option or parameter $1" 1>&2; HELP=1;break;;
    84.     esac
    85.     shift
    86. done
    87. [ $# -gt 0 ] && HELP=1
    88. [ $HELP -eq 1 ] && help_usage
    89. [ $ACRCHIVE -eq 1 ] && archive_logging && exit 0

    复制代码
  • 最后,创建如下cron job负责定期归档审计记录文件
    1. 0 0 * * * sh /usr/local/admin/audit.sh --archive
    复制代码

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
2 [报告]
发表于 2012-09-19 13:22 |只看该作者
感谢分享

论坛徽章:
0
3 [报告]
发表于 2012-09-19 14:21 |只看该作者
楼主是个用心之人啊  安全策略考虑的比较周到  感谢分享!

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
4 [报告]
发表于 2012-09-19 17:04 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
1
2015亚冠之柏太阳神
日期:2015-09-29 10:00:55
5 [报告]
发表于 2012-09-20 10:00 |只看该作者
我也弄个过一个,跟大家分享一下。
http://backkom.blog.51cto.com/3939458/976608
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP