PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/SugarSecurity.php

https://github.com/vincentamari/SuperSweetAdmin
PHP | 167 lines | 98 code | 23 blank | 46 comment | 9 complexity | 4d8d0d7caf9aa942ff0f7ab61b8bfaae MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, AGPL-3.0, LGPL-2.1
  1. <?PHP
  2. /*********************************************************************************
  3. * SugarCRM is a customer relationship management program developed by
  4. * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  24. * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  25. *
  26. * The interactive user interfaces in modified source and object code versions
  27. * of this program must display Appropriate Legal Notices, as required under
  28. * Section 5 of the GNU Affero General Public License version 3.
  29. *
  30. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  31. * these Appropriate Legal Notices must retain the display of the "Powered by
  32. * SugarCRM" logo. If the display of the logo is not reasonably feasible for
  33. * technical reasons, the Appropriate Legal Notices must display the words
  34. * "Powered by SugarCRM".
  35. ********************************************************************************/
  36. class SugarSecure{
  37. var $results = array();
  38. function display(){
  39. echo '<table>';
  40. foreach($this->results as $result){
  41. echo '<tr><td>' . nl2br($result) . '</td></tr>';
  42. }
  43. echo '</table>';
  44. }
  45. function save($file=''){
  46. $fp = fopen($file, 'a');
  47. foreach($this->results as $result){
  48. fwrite($fp , $result);
  49. }
  50. fclose($fp);
  51. }
  52. function scan($path= '.', $ext = '.php'){
  53. $dir = dir($path);
  54. while($entry = $dir->read()){
  55. if(is_dir($path . '/' . $entry) && $entry != '.' && $entry != '..'){
  56. $this->scan($path .'/' . $entry);
  57. }
  58. if(is_file($path . '/'. $entry) && substr($entry, strlen($entry) - strlen($ext), strlen($ext)) == $ext){
  59. $contents = file_get_contents($path .'/'. $entry);
  60. $this->scanContents($contents, $path .'/'. $entry);
  61. }
  62. }
  63. }
  64. function scanContents($contents){
  65. return;
  66. }
  67. }
  68. class ScanFileIncludes extends SugarSecure{
  69. function scanContents($contents, $file){
  70. $results = array();
  71. $found = '';
  72. /*preg_match_all("'(require_once\([^\)]*\\$[^\)]*\))'si", $contents, $results, PREG_SET_ORDER);
  73. foreach($results as $result){
  74. $found .= "\n" . $result[0];
  75. }
  76. $results = array();
  77. preg_match_all("'include_once\([^\)]*\\$[^\)]*\)'si", $contents, $results, PREG_SET_ORDER);
  78. foreach($results as $result){
  79. $found .= "\n" . $result[0];
  80. }
  81. */
  82. $results = array();
  83. preg_match_all("'require\([^\)]*\\$[^\)]*\)'si", $contents, $results, PREG_SET_ORDER);
  84. foreach($results as $result){
  85. $found .= "\n" . $result[0];
  86. }
  87. $results = array();
  88. preg_match_all("'include\([^\)]*\\$[^\)]*\)'si", $contents, $results, PREG_SET_ORDER);
  89. foreach($results as $result){
  90. $found .= "\n" . $result[0];
  91. }
  92. $results = array();
  93. preg_match_all("'require_once\([^\)]*\\$[^\)]*\)'si", $contents, $results, PREG_SET_ORDER);
  94. foreach($results as $result){
  95. $found .= "\n" . $result[0];
  96. }
  97. $results = array();
  98. preg_match_all("'fopen\([^\)]*\\$[^\)]*\)'si", $contents, $results, PREG_SET_ORDER);
  99. foreach($results as $result){
  100. $found .= "\n" . $result[0];
  101. }
  102. $results = array();
  103. preg_match_all("'file_get_contents\([^\)]*\\$[^\)]*\)'si", $contents, $results, PREG_SET_ORDER);
  104. foreach($results as $result){
  105. $found .= "\n" . $result[0];
  106. }
  107. if(!empty($found)){
  108. $this->results[] = $file . $found."\n\n";
  109. }
  110. }
  111. }
  112. class SugarSecureManager{
  113. var $scanners = array();
  114. function registerScan($class){
  115. $this->scanners[] = new $class();
  116. }
  117. function scan(){
  118. while($scanner = current($this->scanners)){
  119. $scanner->scan();
  120. $scanner = next($this->scanners);
  121. }
  122. reset($this->scanners);
  123. }
  124. function display(){
  125. while($scanner = current($this->scanners)){
  126. echo 'Scan Results: ';
  127. $scanner->display();
  128. $scanner = next($this->scanners);
  129. }
  130. reset($this->scanners);
  131. }
  132. function save(){
  133. //reset($this->scanners);
  134. $name = 'SugarSecure'. time() . '.txt';
  135. while($this->scanners = next($this->scanners)){
  136. $scanner->save($name);
  137. }
  138. }
  139. }
  140. $secure = new SugarSecureManager();
  141. $secure->registerScan('ScanFileIncludes');
  142. $secure->scan();
  143. $secure->display();