HazardLib.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. App::uses('Xml', 'Utility');
  3. /**
  4. * Get dangerous strings for various security checks
  5. *
  6. * used in configurations controller + debug helper
  7. *
  8. * @author Mark Scherer
  9. * @license MIT
  10. * 2010-07-30 ms
  11. */
  12. class HazardLib {
  13. const URL = 'http://ha.ckers.org/xssAttacks.xml';
  14. /**
  15. * get dangerous sql strings to test with
  16. * @return array
  17. * @static
  18. * 2010-07-31 ms
  19. */
  20. public static function sqlStrings($veryDangerousToo = false) {
  21. /*
  22. $res = array(
  23. "SELECT * FROM users WHERE email = 'x'; INSERT INTO users ('username', 'password') VALUES ('x', 'y');--"
  24. );
  25. $veryDangerous = array(
  26. "SELECT * FROM users WHERE email = 'x'; DROP TABLE users; --'; -- Boom!"
  27. );
  28. */
  29. $strings = array(
  30. "x'; INSERT INTO users ('username', 'password') VALUES ('x', 'y')",
  31. );
  32. $veryDangerous = array(
  33. "x'; DROP TABLE users; --",
  34. );
  35. if ($veryDangerousToo) {
  36. $strings = array_merge($strings, $veryDangerous);
  37. }
  38. return $strings;
  39. }
  40. /**
  41. * get dangerous php strings to test with
  42. * @return array
  43. * 2010-07-31 ms
  44. */
  45. public static function phpStrings() {
  46. $res = array(
  47. 'a:100000000:{}', # serialized objects run the magic _ _wakeup() function when they're unserialized
  48. ':3:"PDO":0:{}' # If the PDO extension is enabled -- and it is by default in PHP 5 -- you can cause a fatal error
  49. );
  50. return $res;
  51. }
  52. /**
  53. * get dangerous html strings to test with
  54. * @return array
  55. * 2010-07-31 ms
  56. */
  57. public static function xssStrings($cache = true) {
  58. if ($cache) {
  59. $texts = Cache::read('hazard_lib_texts');
  60. }
  61. if (empty($texts)) {
  62. $texts = array();
  63. $contents = self::_parseXml(self::URL);
  64. foreach ($contents as $content) {
  65. if ($content['code'] === 'See Below') {
  66. continue;
  67. }
  68. $texts[] = $content['code'];
  69. }
  70. if (empty($texts)) {
  71. trigger_error('ha.ckers.org FAILED - XML not available', E_WARNING);
  72. return array();
  73. }
  74. if ($cache) {
  75. Cache::write('hazard_lib_texts', $texts);
  76. }
  77. }
  78. return $texts;
  79. }
  80. /**
  81. * Parse xml
  82. *
  83. * @return array
  84. * 2010-02-07 ms
  85. */
  86. protected static function _parseXml($file) {
  87. $xml = Xml::build($file);
  88. $res = Xml::toArray($xml);
  89. if (!empty($res['xss']['attack'])) {
  90. return (array)$res['xss']['attack'];
  91. }
  92. return array();
  93. }
  94. }