HazardLib.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. * 2010-07-30 ms
  8. */
  9. class HazardLib {
  10. const URL = 'http://ha.ckers.org/xssAttacks.xml';
  11. /**
  12. * get dangerous sql strings to test with
  13. * @return array
  14. * @static
  15. * 2010-07-31 ms
  16. **/
  17. public function sqlStrings($veryDangerousToo = false) {
  18. /*
  19. $res = array(
  20. "SELECT * FROM users WHERE email = 'x'; INSERT INTO users ('username', 'password') VALUES ('x', 'y');--"
  21. );
  22. $veryDangerous = array(
  23. "SELECT * FROM users WHERE email = 'x'; DROP TABLE users; --'; -- Boom!"
  24. );
  25. */
  26. $strings = array(
  27. "x'; INSERT INTO users ('username', 'password') VALUES ('x', 'y')",
  28. );
  29. $veryDangerous = array(
  30. "x'; DROP TABLE users; --",
  31. );
  32. if ($veryDangerousToo) {
  33. $strings = array_merge($strings, $veryDangerous);
  34. }
  35. return $strings;
  36. }
  37. /**
  38. * get dangerous php strings to test with
  39. * @return array
  40. * @static
  41. * 2010-07-31 ms
  42. **/
  43. public function phpStrings() {
  44. $res = array(
  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. * @return array
  53. * @static
  54. * 2010-07-31 ms
  55. **/
  56. public function xssStrings($cache = true) {
  57. if ($cache) {
  58. $texts = Cache::read('security_lib_texts');
  59. }
  60. if (empty($texts)) {
  61. $texts = array();
  62. $contents = $this->_parseXml(self::URL);
  63. foreach ($contents as $content) {
  64. $texts[] = $content['code'];
  65. }
  66. if (empty($texts)) {
  67. trigger_error('ha.ckers.org FAILED - XML not available', E_WARNING);
  68. return array();
  69. }
  70. if ($cache) {
  71. Cache::write('security_lib_texts', $texts);
  72. }
  73. }
  74. return $texts;
  75. }
  76. /**
  77. * parse xml
  78. * 2010-02-07 ms
  79. */
  80. public function _parseXml($file) {
  81. $xml = Xml::build($file);
  82. $res = Xml::toArray($xml);
  83. if (!empty($res['xss']['attack'])) {
  84. return (array)$res['xss']['attack'];
  85. }
  86. return array();
  87. }
  88. }