PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/lib/bad-behavior/common_tests.inc.php

http://pixie-cms.googlecode.com/
PHP | 143 lines | 83 code | 20 blank | 40 comment | 41 complexity | b25a320a0fe40d2b8cefb98f12137b93 MD5 | raw file
  1. <?php if (!defined('BB2_CORE')) die('I said no cheating!');
  2. // Enforce adherence to protocol version claimed by user-agent.
  3. function bb2_protocol($settings, $package)
  4. {
  5. // Is it claiming to be HTTP/1.0? Then it shouldn't do HTTP/1.1 things
  6. // Always run this test; we should never see Expect:
  7. if (array_key_exists('Expect', $package['headers_mixed']) && stripos($package['headers_mixed']['Expect'], "100-continue") !== FALSE) {
  8. return "a0105122";
  9. }
  10. // Is it claiming to be HTTP/1.1? Then it shouldn't do HTTP/1.0 things
  11. // Blocks some common corporate proxy servers in strict mode
  12. if ($settings['strict'] && !strcmp($package['server_protocol'], "HTTP/1.1")) {
  13. if (array_key_exists('Pragma', $package['headers_mixed']) && strpos($package['headers_mixed']['Pragma'], "no-cache") !== FALSE && !array_key_exists('Cache-Control', $package['headers_mixed'])) {
  14. return "41feed15";
  15. }
  16. }
  17. return false;
  18. }
  19. function bb2_cookies($settings, $package)
  20. {
  21. // Enforce RFC 2965 sec 3.3.5 and 9.1
  22. // Bots wanting new-style cookies should send Cookie2
  23. // FIXME: Amazon Kindle is broken; Amazon has been notified 9/24/08
  24. if (@strpos($package['headers_mixed']['Cookie'], '$Version=0') !== FALSE && !array_key_exists('Cookie2', $package['headers_mixed']) && strpos($package['headers_mixed']['User-Agent'], "Kindle/") === FALSE) {
  25. return '6c502ff1';
  26. }
  27. return false;
  28. }
  29. function bb2_misc_headers($settings, $package)
  30. {
  31. @$ua = $package['headers_mixed']['User-Agent'];
  32. if (!strcmp($package['request_method'], "POST") && empty($ua)) {
  33. return "f9f2b8b9";
  34. }
  35. // Broken spambots send URLs with various invalid characters
  36. // Some broken browsers send the #vector in the referer field :(
  37. // if (strpos($package['request_uri'], "#") !== FALSE || strpos($package['headers_mixed']['Referer'], "#") !== FALSE) {
  38. if (strpos($package['request_uri'], "#") !== FALSE) {
  39. return "dfd9b1ad";
  40. }
  41. // A pretty nasty SQL injection attack on IIS servers
  42. if (strpos($package['request_uri'], ";DECLARE%20@") !== FALSE) {
  43. return "dfd9b1ad";
  44. }
  45. // Range: field exists and begins with 0
  46. // Real user-agents do not start ranges at 0
  47. // NOTE: this blocks the whois.sc bot. No big loss.
  48. // Exceptions: MT (not fixable); LJ (refuses to fix; may be
  49. // blocked again in the future)
  50. if ($settings['strict'] && array_key_exists('Range', $package['headers_mixed']) && strpos($package['headers_mixed']['Range'], "=0-") !== FALSE) {
  51. if (strncmp($ua, "MovableType", 11) && strncmp($ua, "URI::Fetch", 10) && strncmp($ua, "php-openid/", 11)) {
  52. return "7ad04a8a";
  53. }
  54. }
  55. // Content-Range is a response header, not a request header
  56. if (array_key_exists('Content-Range', $package['headers_mixed'])) {
  57. return '7d12528e';
  58. }
  59. // Lowercase via is used by open proxies/referrer spammers
  60. // Exceptions: Clearswift uses lowercase via (refuses to fix;
  61. // may be blocked again in the future)
  62. if (array_key_exists('via', $package['headers']) &&
  63. strpos($package['headers']['via'],'Clearswift') === FALSE &&
  64. strpos($ua,'CoralWebPrx') === FALSE) {
  65. return "9c9e4979";
  66. }
  67. // pinappleproxy is used by referrer spammers
  68. if (array_key_exists('Via', $package['headers_mixed'])) {
  69. if (stripos($package['headers_mixed']['Via'], "pinappleproxy") !== FALSE || stripos($package['headers_mixed']['Via'], "PCNETSERVER") !== FALSE || stripos($package['headers_mixed']['Via'], "Invisiware") !== FALSE) {
  70. return "939a6fbb";
  71. }
  72. }
  73. // TE: if present must have Connection: TE
  74. // RFC 2616 14.39
  75. // Blocks Microsoft ISA Server 2004 in strict mode. Contact Microsoft
  76. // to obtain a hotfix.
  77. if ($settings['strict'] && array_key_exists('Te', $package['headers_mixed'])) {
  78. if (!preg_match('/\bTE\b/', $package['headers_mixed']['Connection'])) {
  79. return "582ec5e4";
  80. }
  81. }
  82. if (array_key_exists('Connection', $package['headers_mixed'])) {
  83. // Connection: keep-alive and close are mutually exclusive
  84. if (preg_match('/\bKeep-Alive\b/i', $package['headers_mixed']['Connection']) && preg_match('/\bClose\b/i', $package['headers_mixed']['Connection'])) {
  85. return "a52f0448";
  86. }
  87. // Close shouldn't appear twice
  88. if (preg_match('/\bclose,\s?close\b/i', $package['headers_mixed']['Connection'])) {
  89. return "a52f0448";
  90. }
  91. // Keey-Alive shouldn't appear twice either
  92. if (preg_match('/\bkeep-alive,\s?keep-alive\b/i', $package['headers_mixed']['Connection'])) {
  93. return "a52f0448";
  94. }
  95. }
  96. // Headers which are not seen from normal user agents; only malicious bots
  97. if (array_key_exists('X-Aaaaaaaaaaaa', $package['headers_mixed']) || array_key_exists('X-Aaaaaaaaaa', $package['headers_mixed'])) {
  98. return "b9cc1d86";
  99. }
  100. // Proxy-Connection does not exist and should never be seen in the wild
  101. if (array_key_exists('Proxy-Connection', $package['headers_mixed'])) {
  102. return "b7830251";
  103. }
  104. if (array_key_exists('Referer', $package['headers_mixed'])) {
  105. // Referer, if it exists, must not be blank
  106. if (empty($package['headers_mixed']['Referer'])) {
  107. return "69920ee5";
  108. }
  109. // Referer, if it exists, must contain a :
  110. // While a relative URL is technically valid in Referer, all known
  111. // legit user-agents send an absolute URL
  112. if (strpos($package['headers_mixed']['Referer'], ":") === FALSE) {
  113. return "45b35e30";
  114. }
  115. }
  116. // "uk" is not a language (ISO 639) nor a country (ISO 3166)
  117. // oops, yes it is :( Please shoot any Ukrainian spammers you see.
  118. # if (preg_match('/\buk\b/', $package['headers_mixed']['Accept-Language'])) {
  119. # return "35ea7ffa";
  120. # }
  121. return false;
  122. }
  123. ?>