/kloxo/httpdocs/htmllib/lib/lxguardincludelib.php

https://bitbucket.org/Nemcio/kloxo-mr · PHP · 154 lines · 130 code · 20 blank · 4 comment · 28 complexity · 16a51142cbc7cc7a617eb1c20f66ad3e MD5 · raw file

  1. <?php
  2. function get_deny_list($total)
  3. {
  4. $lxgpath = "__path_home_root/lxguard";
  5. $rmt = lfile_get_unserialize("$lxgpath/config.info");
  6. $wht = lfile_get_unserialize("$lxgpath/whitelist.info");
  7. if ($wht) {$wht = $wht->data;}
  8. $disablehit = null;
  9. if ($rmt) {
  10. $disablehit = $rmt->data['disablehit'];
  11. }
  12. if (!($disablehit > 0)) { $disablehit = 20 ;}
  13. $deny = null;
  14. if ($total) {
  15. foreach($total as $k => $v) {
  16. if ($wht) {
  17. if (array_search_bool($k, $wht)) {
  18. dprint("$k found in whitelist... not blocking..\n");
  19. continue;
  20. }
  21. }
  22. if ($v > $disablehit) {
  23. $deny[$k] = $v;
  24. }
  25. }
  26. }
  27. return $deny;
  28. }
  29. function get_total($list, &$total)
  30. {
  31. $lxgpath = "__path_home_root/lxguard";
  32. $rmt = lfile_get_unserialize("$lxgpath/hitlist.info");
  33. if ($rmt) { $total = $rmt->hl; }
  34. if ($list) {
  35. foreach($list as $k => $v) {
  36. if (!isset($total[$k])) { $total[$k] = 0 ; }
  37. $c = count_fail($v);
  38. $total[$k] += $c;
  39. }
  40. }
  41. }
  42. function count_fail($v)
  43. {
  44. $count = 0;
  45. foreach($v as $vv) {
  46. if ($vv['access'] === 'fail') {
  47. $count++;
  48. }
  49. }
  50. return $count;
  51. }
  52. function parse_sshd_and_ftpd($fp, &$list)
  53. {
  54. $count = 0;
  55. while(!feof($fp)) {
  56. $count++;
  57. if ($count > 10000) { break; }
  58. $string = fgets($fp);
  59. sshLogString($string, $list);
  60. ftpLogString($string, $list);
  61. }
  62. }
  63. function parse_ftp_log($fp, &$list)
  64. {
  65. $count = 0;
  66. while(!feof($fp)) {
  67. $count++;
  68. if ($count > 10000) { break; }
  69. $string = fgets($fp);
  70. }
  71. }
  72. function sshLogString($string, &$list)
  73. {
  74. //'refuse' => "refused connection",
  75. $str = array('success' => "Accepted password", 'fail' => "Failed password");
  76. $match = false;
  77. foreach($str as $k => $v) {
  78. if (!csa($string, "sshd")) { continue; }
  79. if (csa($string, $v)) {
  80. $match = true;
  81. $access = $k;
  82. break;
  83. }
  84. }
  85. if (!$match) { return; }
  86. $time = getTimeFromSysLogString($string);
  87. preg_match("/.*Failed password for( invalid user)? (.*) from ([^ ]*).*/", $string, $match);
  88. if (!$match) { return; }
  89. $ip = $match[3];
  90. if (csb($ip, "::ffff:")) {
  91. $ip = strfrom($ip, "::ffff:");
  92. }
  93. $user = $match[2];
  94. if (csb($ip, "127")) { return; }
  95. $list[$ip][$time] = array('service' => 'ssh', 'user' => $user, 'access' => $access);
  96. }
  97. function ftpLogString($string, &$list)
  98. {
  99. $str = array('fail' => "Authentication failed", 'success' => "is now logged in");
  100. $match = false;
  101. foreach($str as $k => $v) {
  102. if (!csa($string, "pure-ftpd")) { continue; }
  103. if (csa($string, $v)) {
  104. $match = true;
  105. $access = $k;
  106. break;
  107. }
  108. }
  109. if (!$match) { return; }
  110. $time = getTimeFromSysLogString($string);
  111. if ($access === 'fail') {
  112. preg_match("/.*\(?@([^\)]*)\) \[WARNING\] Authentication failed for user \[([^\]]*)\].*/", $string, $match);
  113. } else {
  114. preg_match("/.*\(?@([^\)]*)\) \[INFO\] ([^ ]*) is now logged in.*/", $string, $match);
  115. }
  116. if (!$match) { return; }
  117. $ip = $match[1];
  118. $user = $match[2];
  119. if (csb($ip, "127")) { return; }
  120. $list[$ip][$time] = array('service' => 'ftp', 'user' => $user, 'access' => $access);
  121. }
  122. function getTimeFromSysLogString($line)
  123. {
  124. $line = trimSpaces($line);
  125. $year = @ date('Y');
  126. list($month, $day, $time) = explode(" ", $line);
  127. $month = get_num_for_month($month);
  128. list($hour, $min, $sec) = explode(':' , $time);
  129. //$s = mktime($hour , $min , $sec , monthToInt($month), str_pad($day , 2, 0, STR_PAD_LEFT) , $year);
  130. $s = @ mktime($hour, $min, $sec, $month, $day, $year);
  131. //dprint(" $date $time $hour, $min $sec $month, $day , $year, Time: $s\n");
  132. // Return date and size. The size param is not important. Our aim is to find the right position.
  133. return $s;
  134. }