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