Newer
Older
DirtyScripts / CSharpConfChecker.php
  1. <?php
  2. /***
  3. * $>php CSharpConfChecker.php /path/to/src/
  4. * will output csv with status of packages (out of dat, known vulnerable)
  5. */
  6.  
  7. $total = 0;
  8.  
  9. function findConfFiles($folderPath) {
  10. $confFiles = array();
  11. $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($folderPath));
  12. foreach($iterator as $file) {
  13. //echo "$file \r";
  14. if ($file->isFile() && $file->getExtension() == 'config') {
  15. $confFiles[] = $file->getPathname();
  16. }
  17. }
  18. return $confFiles;
  19. }
  20.  
  21. function parseConfFiles($confFiles) {
  22. global $total;
  23. $result = array();
  24. foreach($confFiles as $file) {
  25. $lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  26. foreach($lines as $line) {
  27. if(strpos($line, 'id=') !== false && strpos($line, 'version=') !== false) {
  28. $id = '';
  29. $version = '';
  30. preg_match('/id=(\S+)/', $line, $idMatch);
  31. if(!empty($idMatch)) {
  32. $id = $idMatch[1];
  33. }
  34. preg_match('/version=(\S+)/', $line, $versionMatch);
  35. if(!empty($versionMatch)) {
  36. $version = $versionMatch[1];
  37. }
  38. if(!empty($id) && !empty($version)) {
  39. if(empty($result[$id][$version])){
  40. $total++;
  41. }
  42. $result[$id][$version][] = $file;
  43. }
  44. }
  45. }
  46. }
  47.  
  48. // sort by id and then by version number
  49. uksort($result, function($a, $b) use ($result) {
  50. $aVersions = array_keys($result[$a]);
  51. $bVersions = array_keys($result[$b]);
  52. $aVersion = $aVersions[count($aVersions) - 1];
  53. $bVersion = $bVersions[count($bVersions) - 1];
  54. $aVersion = preg_replace('/[^0-9.]/', '', $aVersion);
  55. $bVersion = preg_replace('/[^0-9.]/', '', $bVersion);
  56. if($a == $b) {
  57. return version_compare($aVersion, $bVersion);
  58. } else {
  59. return strcmp($a, $b);
  60. }
  61. });
  62. return $result;
  63. }
  64.  
  65. function downloadUrls($parsedConf) {
  66. global $total;
  67. $count = 0;
  68. foreach ($parsedConf as $id => $versions) {
  69. foreach ($versions as $version => $files) {
  70. $url = "https://www.nuget.org/packages/" . str_replace('"', '', trim($id)) . "/" . str_replace('"', '', trim($version));
  71. $count++;
  72. echo "downloading: $count of $total \r";
  73. $response = getdataz($url);
  74.  
  75. if (strpos($response, 'This package has at least one') !== false) {
  76. $parsedConf[$id][$version]['status'] = 'vulnerable';
  77. } elseif (strpos($response, 'There is a newer version of this package') !== false) {
  78. $parsedConf[$id][$version]['status'] = 'outdated';
  79. }
  80.  
  81. }
  82. }
  83. echo "downloaded all\n";
  84. return $parsedConf;
  85. }
  86.  
  87. function getdataz($target){
  88. $ch = curl_init($target);
  89. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  90. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  91. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  92. $result = curl_exec($ch);
  93. curl_close($ch);
  94.  
  95. return $result;
  96. }
  97.  
  98. $folderPath = isset($argv[1]) ? $argv[1] : '';
  99. if (!empty($folderPath)) {
  100. echo "searching $folderPath\n";
  101. $confFiles = findConfFiles($folderPath);
  102. echo "parsing\n";
  103. $parsedConfFiles = parseConfFiles($confFiles);
  104. echo "found: $total\n";
  105. $parsedConfFiles = downloadUrls($parsedConfFiles);
  106. $csvString = "id, version, status, file\n";
  107. foreach ($parsedConfFiles as $id => $versions) {
  108.  
  109. foreach ($versions as $version => $files) {
  110. $status = isset($files['status']) ? $files['status'] : '';
  111. $file = isset($files[0]) ? $files[0] : '';
  112. // Add a row to the CSV
  113. $csvString .= "$id, $version, $status, $file\n";
  114. }
  115. }
  116. echo $csvString;
  117. }
  118. ?>
Buy Me A Coffee