加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl邮件报警 for PHPIDS

发布时间:2020-12-16 00:39:07 所属栏目:大数据 来源:网络整理
导读:?PHPIDS缺省支持的邮件报警存在一些不足之处: 1、对页面的访问速度有一定影响。 2、邮件发送以一条告警日志为单位,如果日志比较多的话,我们可能会收到上百封邮件。(可能有某个选项可以设置,但我没找到) 设计一个perl脚本来发送邮件,原理如下: 首先读

?PHPIDS缺省支持的邮件报警存在一些不足之处:

1、对页面的访问速度有一定影响。

2、邮件发送以一条告警日志为单位,如果日志比较多的话,我们可能会收到上百封邮件。(可能有某个选项可以设置,但我没找到)

设计一个perl脚本来发送邮件,原理如下:
首先读取phpids的log文件(tmp/phpids_log.txt),然后利用正则对日志内容进行简单的格式话,最后把格式化的内容发送出去。

 
 
  1. #!/usr/bin/perl?-w?
  2. use?strict;?
  3. use?warnings;?
  4. use?MIME::Lite;?
  5. #?set?up?email?
  6. my?$mailto?=?"you@example.com";?
  7. my?$mailfrom?=?"phpids@example.com";?
  8. my?$Cc?=?"";?
  9. my?$subject?=?"PHPIDS?detected?an?intrusion?attempt!";?
  10. my?$message?=?"The?following?attack?has?been?detected?by?PHPIDS";?
  11. my?$content?=?$message."nn";?
  12. my?$count;?
  13. get_content();?
  14. if($content?eq?$message."nn")?{?
  15. ????????print?"No?data?to?mail!nByeBye!n";?
  16. }else{?
  17. ????????print?"Sending?mail?now!n";?
  18. ????????email($mailto,?$mailfrom,?$Cc,?$subject,?$message,?$content);?
  19. }?
  20. ?
  21. sub?email?
  22. {?
  23. ????????#?get?incoming?parameters?
  24. ????????my?($mailto,?$content)?=?@_;?
  25. ?
  26. ????????#create?a?new?message?
  27. ????????my?$msg?=?MIME::Lite->new(?
  28. ????????????????From?=>?$mailfrom,?
  29. ????????????????To?=>?$mailto,?
  30. ????????????????Cc?=>?$Cc,?
  31. ????????????????Subject?=>?$subject,?
  32. ????????????????Data?=>?"message",?
  33. ????????????????Type?=>?'multipart/mixed'?
  34. ????????);?
  35. ?
  36. ????????$msg->attach(?
  37. ????????Type?=>?'text/plain',?
  38. ????????Data?=>?$content,?
  39. ????????);?
  40. ????????#?send?the?mail?
  41. ????????MIME::Lite->send('smtp',?'example.com',?Debug?=>0,?Timeout?=>?60);??
  42. ????????$msg->send();??
  43. }?
  44. ?
  45. #?get?log?content?from?log?file??
  46. sub?get_content{?
  47. ????????read_count();?
  48. ????????format_log();?
  49. ????????read_result();?
  50. ????????note_count();?
  51. }?
  52. ?
  53. #?read?count?number?from?the?count?file.?
  54. sub?read_count{?
  55. ????????if?(-e?"count"){?
  56. ????open(FILE,?"<count")?or?die?"Can't?open?countn";?
  57. ????while(<FILE>){?
  58. ????chomp;?
  59. ????$count?=?int($_);?
  60. ????}?
  61. ????close?FILE;?
  62. }else{?
  63. ????????$count?=?1;?
  64. }?
  65. }?
  66. ?
  67. #?format?log?content.??
  68. sub?format_log{?
  69. ????open(LOG,?"<phpids_log.txt")?or?die?"Can't?open?phpids_log.txtn";?
  70. ????????open(O,">result.log")?or?die?"can't?open?result.logn";?
  71. ????????my?$number?=?1;?
  72. ????while(<LOG>)?{?
  73. ????????if($.?>=?$count){?
  74. ????????????????????????chomp;?
  75. ????????????????????????if($_?=~?/"(.*?)",(.*?),(d+)."(.*?)","(.*?)","(.*?)"/){?
  76. ????????????????????????????????????????printf?O?("NO.%snIP:?%snDate:?%snImpact:?%snAffected?tags:?%snAffected?parameters:?%snRequest?URI:?%snOrigin:?%snn",?$number,?$1,?$2,?$3,?$4,?$5,?$6,?$7);?
  77. ????????????????????????}else{?
  78. ????????????????????????????????????????if($_?=?~/"(.*?)","(.*?)"/)?{?
  79. ????????????????????????????????????????????????????????printf?O?("NO.%snIP:?%snDate:?%snImpact:?%snAffected?tags:?%snAffected?parameters:?%snRequest?URI:?%snn",?$6);?
  80. ????????????????????????????????????????}else{?
  81. ????????????????????????????????????????????????print?O?"NO.".$number."n"."$_";?
  82. ????????????????????????????????????????}?
  83. ????????????????????????}?
  84. ????????????????????????$number++;?
  85. ????????????$count++;?
  86. ????????}????
  87. ????}????
  88. ????close?LOG;?
  89. ????????close?O;?
  90. }????
  91. ?
  92. #?read?formatted?result?from?result.log?
  93. sub?read_result?{?
  94. ????????open(R,"<result.log")?or?die?"Can't?open?result.logn";?
  95. ????????while(<R>){?
  96. ????????????????$content?.=?$_;?
  97. }?
  98. ????????unlink?"result.log";?
  99. close?R;?
  100. }?
  101. ?
  102. #?note?current?count?number?to?count.?
  103. sub?note_count?{?
  104. ????open(OUT,?">count")?or?die?"Can't?open?countn";?
  105. ????print?OUT?$count;?
  106. ????close?OUT;?
  107. }?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读