Newer
Older
DirtyScripts / ReportToolz / repgen.php
root on 16 Nov 2019 6 KB vdb.php added
  1. #!/usr/bin/php
  2. <?php
  3. //error_reporting(0);
  4.  
  5. /***
  6. * Configuration options
  7. */
  8. $template = "templates/odt/blank_template.odt";
  9. $vulnTemplate = "templates/odt/vuln_template.xml";
  10.  
  11. /***
  12. * Main program - Don't edit below
  13. */
  14. echo "_____ _____ _____ Gen\n||_// ||== ||_// \n|| \\ ||___ || \n\n";
  15.  
  16. foreach (glob("classes/*.php") as $filename)
  17. include $filename;
  18.  
  19. $definitions = new \Clapp\CommandLineArgumentDefinition(
  20. array(
  21. "help|h" => "Shows help message",
  22. "path|p=s" => "/path/to/configs/", // should contain config.json and all vuln.json files
  23. )
  24. );
  25.  
  26. $filter = new \Clapp\CommandArgumentFilter($definitions, $argv);
  27.  
  28. if ($filter->getParam('h') === true || $argc < 2) {
  29. fwrite(STDERR, $definitions->getUsage());
  30. exit(0);
  31. }
  32.  
  33. // see if doc exists
  34. if ($filter->getParam("path") == false)
  35. die("[-] no path set\n");
  36.  
  37. echo "[!] path: ".$filter->getParam("path")."\n";
  38. if(!is_dir($filter->getParam("path")))
  39. die("[-] no such folder! \n");
  40.  
  41. // extract doc and get contents
  42. $rand = uniqid();
  43. mkdir("/tmp/$rand");
  44. if(unzipFolder($template, "/tmp/$rand/")) {
  45. $source = file_get_contents("/tmp/$rand/content.xml");
  46. echo "[+] doc extracted\n";
  47. } else {
  48. die("[-] unable to extract doc\n");
  49. }
  50. $config = json_decode(file_get_contents($filter->getParam("path")."config.conf"));
  51.  
  52. // add config into template
  53. $source = file_get_contents("/tmp/$rand/content.xml");
  54. foreach ($config as $key => $value) {
  55. $source = str_replace('{'.$key.'}', $value, $source);
  56. }
  57. file_put_contents("/tmp/$rand/content.xml", $source);
  58. echo "[+] added config values\n";
  59.  
  60. // get all vulns
  61. $vuln = array();
  62. $files = glob($filter->getParam("path")."*.json");
  63. foreach($files as $finding){
  64. $vuln[] = $found = json_decode(file_get_contents($finding), true);
  65. }
  66.  
  67. echo "[+] sorting vulns by CVSS\n";
  68. usort($vuln, 'order_by_cvss');
  69. function order_by_cvss($a, $b) {
  70. return $b['cvss_score'] > $a['cvss_score'] ? 1 : -1;
  71. }
  72.  
  73. if(empty($vuln))
  74. echo "[-] no vulns found!\n";
  75.  
  76. // create vulns for odf
  77. $templateOrig = file_get_contents($vulnTemplate);
  78. $Serious = $High = $Medium = $Low = "";
  79. foreach ($vuln as $singlevuln) {
  80. $templateSource = $templateOrig;
  81. $togo = $singlevuln['risk'];
  82. foreach ($singlevuln as $key => $value){
  83. $value = str_replace("\n", "</text:p><text:p text:style-name=\"P173\">", $value);
  84. $templateSource = str_replace('{'.$key.'}', $value, $templateSource);
  85. }
  86. $$togo .= $templateSource;
  87. echo "[+] added $togo: ".$singlevuln['title']."\n";
  88. }
  89.  
  90. // squash vulns into one bbig xml
  91. $value = "";
  92. if(!empty($Serious)){
  93. $value .= '<text:list xml:id="list215514604433265" text:continue-numbering="true" text:style-name="Outline">
  94. <text:list-item>
  95. <text:list>
  96. <text:list-item>
  97. <text:h text:style-name="P156" text:outline-level="2">Serious Risk Vulnerabilities</text:h>
  98. </text:list-item>
  99. </text:list>
  100. </text:list-item>
  101. </text:list>';
  102. $value .= $Serious;
  103. }
  104.  
  105. if(!empty($High)){
  106. $value .= '<text:list xml:id="list215514604433265" text:continue-numbering="true" text:style-name="Outline">
  107. <text:list-item>
  108. <text:list>
  109. <text:list-item>
  110. <text:h text:style-name="P156" text:outline-level="2">High Risk Vulnerabilities</text:h>
  111. </text:list-item>
  112. </text:list>
  113. </text:list-item>
  114. </text:list>';
  115. $value .= $High;
  116. }
  117. if(!empty($Medium)){
  118. $value .= '<text:list xml:id="list215514604433265" text:continue-numbering="true" text:style-name="Outline">
  119. <text:list-item>
  120. <text:list>
  121. <text:list-item>
  122. <text:h text:style-name="P156" text:outline-level="2">Medium Risk Vulnerabilities</text:h>
  123. </text:list-item>
  124. </text:list>
  125. </text:list-item>
  126. </text:list>';
  127. $value .= $Medium;
  128. }
  129. if(!empty($Low)){
  130. $value .= '<text:list xml:id="list215514604433265" text:continue-numbering="true" text:style-name="Outline">
  131. <text:list-item>
  132. <text:list>
  133. <text:list-item>
  134. <text:h text:style-name="P156" text:outline-level="2">Low Risk Vulnerabilities</text:h>
  135. </text:list-item>
  136. </text:list>
  137. </text:list-item>
  138. </text:list>';
  139. $value .= $Low;
  140. }
  141. // add to template
  142. $source = file_get_contents("/tmp/$rand/content.xml");
  143. $source = str_replace('{vuln}', $value, $source);
  144. file_put_contents("/tmp/$rand/content.xml", $source);
  145.  
  146. // create report and tidying
  147. zipFolder("/tmp/$rand", $filter->getParam("path")."repgen.odt");
  148. echo "[=] generated report: ".$filter->getParam("path")."repgen.odt\n";
  149. delTree("/tmp/$rand");
  150. echo "[+] temp files removed\n";
  151.  
  152. function unzipFolder($zipInputFile, $outputFolder) {
  153. $zip = new ZipArchive;
  154. $res = $zip->open($zipInputFile);
  155. if ($res === true) {
  156. $zip->extractTo($outputFolder);
  157. $zip->close();
  158. return true;
  159. }
  160. else {
  161. return false;
  162. }
  163. }
  164.  
  165. function XML2Array(SimpleXMLElement $parent){
  166. $array = array();
  167.  
  168. foreach ($parent as $name => $element) {
  169. ($node = & $array[$name])
  170. && (1 === count($node) ? $node = array($node) : 1)
  171. && $node = & $node[];
  172.  
  173. $node = $element->count() ? XML2Array($element) : trim($element);
  174. }
  175.  
  176. return $array;
  177. }
  178.  
  179. function delTree($dir){
  180. $files = array_diff(scandir($dir), array('.', '..'));
  181.  
  182. foreach ($files as $file) {
  183. (is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
  184. }
  185.  
  186. return rmdir($dir);
  187. }
  188.  
  189. function zipFolder($inputFolder, $zipOutputFile) {
  190. if (!extension_loaded('zip') || !file_exists($inputFolder)) {
  191. return false;
  192. }
  193.  
  194. $zip = new ZipArchive();
  195. if (!$zip->open($zipOutputFile, ZIPARCHIVE::CREATE)) {
  196. return false;
  197. }
  198.  
  199. $inputFolder = str_replace('\\', "/", realpath($inputFolder));
  200.  
  201. if (is_dir($inputFolder) === true) {
  202. $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($inputFolder), RecursiveIteratorIterator::SELF_FIRST);
  203.  
  204. foreach ($files as $file) {
  205. $file = str_replace('\\', "/", $file);
  206.  
  207. if (in_array(substr($file, strrpos($file, '/')+1), array('.', '..'))) {
  208. continue;
  209. }
  210.  
  211. $file = realpath($file);
  212.  
  213. if (is_dir($file) === true) {
  214. $dirName = str_replace($inputFolder."/", '', $file."/");
  215. $zip->addEmptyDir($dirName);
  216. }
  217. else if (is_file($file) === true) {
  218. $fileName = str_replace($inputFolder."/", '', $file);
  219. $zip->addFromString($fileName, file_get_contents($file));
  220. }
  221. }
  222. }
  223. else if (is_file($inputFolder) === true) {
  224. $zip->addFromString(basename($inputFolder), file_get_contents($inputFolder));
  225. }
  226.  
  227. return $zip->close();
  228. }
  229.  
  230. ?>
Buy Me A Coffee