HazardLib.php 2.2 KB

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