HazardLib.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. *
  17. * @return array
  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. *
  43. * @return array
  44. * 2010-07-31 ms
  45. */
  46. public static function phpStrings() {
  47. $res = array(
  48. 'a:100000000:{}', # serialized objects run the magic _ _wakeup() function when they're unserialized
  49. ':3:"PDO":0:{}' # If the PDO extension is enabled -- and it is by default in PHP 5 -- you can cause a fatal error
  50. );
  51. return $res;
  52. }
  53. /**
  54. * get dangerous html strings to test with
  55. *
  56. * @return array
  57. * 2010-07-31 ms
  58. */
  59. public static function xssStrings($cache = true) {
  60. if ($cache) {
  61. $texts = Cache::read('hazard_lib_texts');
  62. }
  63. if (empty($texts)) {
  64. $texts = array();
  65. $contents = self::_parseXml(self::URL);
  66. foreach ($contents as $content) {
  67. if ($content['code'] === 'See Below') {
  68. continue;
  69. }
  70. $texts[] = $content['code'];
  71. }
  72. if (empty($texts)) {
  73. trigger_error('ha.ckers.org FAILED - XML not available', E_WARNING);
  74. return array();
  75. }
  76. if ($cache) {
  77. Cache::write('hazard_lib_texts', $texts);
  78. }
  79. }
  80. return $texts;
  81. }
  82. /**
  83. * Parse xml
  84. *
  85. * @return array
  86. * 2010-02-07 ms
  87. */
  88. protected static function _parseXml($file) {
  89. $xml = Xml::build($file);
  90. $res = Xml::toArray($xml);
  91. if (!empty($res['xss']['attack'])) {
  92. return (array)$res['xss']['attack'];
  93. }
  94. return array();
  95. }
  96. }