Newer
Older
DirtyScripts / ReportToolz / repgen.php
root on 27 Feb 2020 9 KB Tony is a informational tard
  1. #!/usr/bin/php
  2. <?php
  3. //error_reporting(0);
  4.  
  5. /***
  6. * Configuration options
  7. */
  8. $template = "templates/odt/blank_template_v1.0.odt";
  9. //$CHECKtemplate = "templates/odt/blank_template_check_v0.4.odt";
  10. $vulnTemplate = "templates/odt/vuln_template_v0.2.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.  
  45. // extract doc and get contents
  46. $rand = uniqid();
  47. mkdir("/tmp/$rand");
  48. if(unzipFolder($template, "/tmp/$rand/")) {
  49. $source = file_get_contents("/tmp/$rand/content.xml");
  50. echo "[+] doc extracted\n";
  51. } else {
  52. die("[-] unable to extract doc\n");
  53. }
  54. $source = file_get_contents("/tmp/$rand/content.xml");
  55. // add CHECK section into report if needed
  56. if(isset($config->checkRef) && trim($config->checkRef) <> ""){
  57. $check_img = '<draw:frame draw:style-name="fr9" draw:name="FPCheckLogo" text:anchor-type="frame" svg:x="0cm" svg:y="5.879cm" svg:width="6.83cm" svg:height="2.628cm" draw:z-index="40">
  58. <draw:image xlink:href="Pictures/10000000000004A3000001C98E2CC6AE1D6F811E.jpg" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad" loext:mime-type="image/jpeg"/>
  59. </draw:frame>';
  60. $check_section = '<text:p text:style-name="P26"/>
  61. <text:p text:style-name="P26"/>
  62. <text:p text:style-name="P26"/>
  63. <text:p text:style-name="P26"/>
  64. <text:p text:style-name="P26">
  65. CHECK Ref: {checkRef}
  66. </text:p>';
  67. $source = str_replace('{check_img}', $check_img, $source);
  68. $source = str_replace('{check_section}', $check_section, $source);
  69. echo "[+] Added CHECK section\n";
  70. }else{
  71. $check_section = '<text:p text:style-name="P26">
  72. <text:bookmark-start text:name="__RefHeading___Toc72207_536000782"/>
  73. <text:bookmark-end text:name="__RefHeading___Toc72207_536000782"/>
  74. </text:p>';
  75. $source = str_replace('{check_img}', "", $source);
  76. $source = str_replace('{check_section}', $check_section, $source);
  77. }
  78.  
  79. // add config into template
  80. foreach ($config as $key => $value) {
  81. $source = str_replace('{'.$key.'}', $value, $source);
  82. }
  83. file_put_contents("/tmp/$rand/content.xml", $source);
  84. echo "[+] added config values\n";
  85.  
  86. // get all vulns
  87. $vuln = array();
  88. $files = glob($filter->getParam("path")."*.json");
  89. foreach($files as $finding){
  90. $vuln[] = $found = json_decode(file_get_contents($finding), true);
  91. }
  92.  
  93. echo "[+] sorting vulns by CVSS\n";
  94. usort($vuln, 'order_by_cvss');
  95. function order_by_cvss($a, $b) {
  96. return $b['cvss_score'] > $a['cvss_score'] ? 1 : -1;
  97. }
  98.  
  99. if(empty($vuln))
  100. echo "[-] no vulns found!\n";
  101.  
  102. // create vulns for odf
  103. $templateOrig = file_get_contents($vulnTemplate);
  104. $Serious = $High = $Medium = $Low = $Informational = "";
  105. foreach ($vuln as $singlevuln) {
  106. $templateSource = $templateOrig;
  107. $togo = $singlevuln['risk'];
  108. foreach ($singlevuln as $key => $value){
  109. $value = str_replace("<", "&lt;", $value);
  110. $value = str_replace(">", "&gt;", $value);
  111. $value = str_replace("\n", "</text:p><text:p text:style-name=\"Text_20_body\">", $value);
  112. $templateSource = str_replace('{'.$key.'}', $value, $templateSource);
  113. if($key == "risk"){
  114. switch ($togo) {
  115. case 'Serious':
  116. $templateSource = str_replace('{risk_img}', '100000000000001C0000001C2B2344F988E3C014.png', $templateSource);
  117. break;
  118. case 'High':
  119. $templateSource = str_replace('{risk_img}', '100000000000001C0000001C478E326DAB1B0673.gif', $templateSource);
  120. break;
  121. case 'Medium':
  122. $templateSource = str_replace('{risk_img}', '100000000000001C0000001C08AD11DB0A5D02CD.png', $templateSource);
  123. break;
  124. case 'Low':
  125. $templateSource = str_replace('{risk_img}', '100000000000001C0000001C6CC3BB57AA64608B.gif', $templateSource);
  126. break;
  127. case 'Info':
  128. $templateSource = str_replace('{risk_img}', '100000000000001C0000001C7365C375D1750C0F.gif', $templateSource);
  129. break;
  130. }
  131. }
  132. }
  133. $$togo .= $templateSource;
  134. echo "[+] added $togo: ".$singlevuln['title']."\n";
  135. }
  136.  
  137. // squash vulns into one bbig xml
  138. $value = "";
  139. if(!empty($Serious)){
  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">Serious Risk Vulnerabilities</text:h>
  145. </text:list-item>
  146. </text:list>
  147. </text:list-item>
  148. </text:list>';
  149. $value .= $Serious;
  150. }
  151.  
  152. if(!empty($High)){
  153. $value .= '<text:list text:continue-numbering="true" text:style-name="Outline">
  154. <text:list-item>
  155. <text:list>
  156. <text:list-item>
  157. <text:h text:outline-level="2">High Risk Vulnerabilities</text:h>
  158. </text:list-item>
  159. </text:list>
  160. </text:list-item>
  161. </text:list>';
  162. $value .= $High;
  163. }
  164. if(!empty($Medium)){
  165. $value .= '<text:list text:continue-numbering="true" text:style-name="Outline">
  166. <text:list-item>
  167. <text:list>
  168. <text:list-item>
  169. <text:h text:outline-level="2">Medium Risk Vulnerabilities</text:h>
  170. </text:list-item>
  171. </text:list>
  172. </text:list-item>
  173. </text:list>';
  174. $value .= $Medium;
  175. }
  176. if(!empty($Low)){
  177. $value .= '<text:list text:continue-numbering="true" text:style-name="Outline">
  178. <text:list-item>
  179. <text:list>
  180. <text:list-item>
  181. <text:h text:outline-level="2">Low Risk Vulnerabilities</text:h>
  182. </text:list-item>
  183. </text:list>
  184. </text:list-item>
  185. </text:list>';
  186. $value .= $Low;
  187. }
  188. if(!empty($Informational)){
  189. $value .= '<text:list text:continue-numbering="true" text:style-name="Outline">
  190. <text:list-item>
  191. <text:list>
  192. <text:list-item>
  193. <text:h text:outline-level="2">Informational Risk Vulnerabilities</text:h>
  194. </text:list-item>
  195. </text:list>
  196. </text:list-item>
  197. </text:list>';
  198. $value .= $Informational;
  199. }
  200. // add to template
  201. $source = file_get_contents("/tmp/$rand/content.xml");
  202. $source = str_replace('{vuln}', $value, $source);
  203. file_put_contents("/tmp/$rand/content.xml", $source);
  204.  
  205. // create report and tidying
  206. $repOutName = $config->ref.".3 ".$config->client ." ". $config->title1;
  207. zipFolder("/tmp/$rand", $filter->getParam("path").$repOutName.".odt");
  208. echo "[=] generated report: ".$filter->getParam("path").$repOutName.".odt\n";
  209. delTree("/tmp/$rand");
  210. echo "[+] temp files removed\n";
  211.  
  212. function unzipFolder($zipInputFile, $outputFolder) {
  213. $zip = new ZipArchive;
  214. $res = $zip->open($zipInputFile);
  215. if ($res === true) {
  216. $zip->extractTo($outputFolder);
  217. $zip->close();
  218. return true;
  219. }
  220. else {
  221. return false;
  222. }
  223. }
  224.  
  225. function XML2Array(SimpleXMLElement $parent){
  226. $array = array();
  227.  
  228. foreach ($parent as $name => $element) {
  229. ($node = & $array[$name])
  230. && (1 === count($node) ? $node = array($node) : 1)
  231. && $node = & $node[];
  232.  
  233. $node = $element->count() ? XML2Array($element) : trim($element);
  234. }
  235.  
  236. return $array;
  237. }
  238.  
  239. function delTree($dir){
  240. $files = array_diff(scandir($dir), array('.', '..'));
  241.  
  242. foreach ($files as $file) {
  243. (is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
  244. }
  245.  
  246. return rmdir($dir);
  247. }
  248.  
  249. function zipFolder($inputFolder, $zipOutputFile) {
  250. if (!extension_loaded('zip') || !file_exists($inputFolder)) {
  251. return false;
  252. }
  253.  
  254. $zip = new ZipArchive();
  255. if (!$zip->open($zipOutputFile, ZIPARCHIVE::CREATE)) {
  256. return false;
  257. }
  258.  
  259. $inputFolder = str_replace('\\', "/", realpath($inputFolder));
  260.  
  261. if (is_dir($inputFolder) === true) {
  262. $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($inputFolder), RecursiveIteratorIterator::SELF_FIRST);
  263.  
  264. foreach ($files as $file) {
  265. $file = str_replace('\\', "/", $file);
  266.  
  267. if (in_array(substr($file, strrpos($file, '/')+1), array('.', '..'))) {
  268. continue;
  269. }
  270.  
  271. $file = realpath($file);
  272.  
  273. if (is_dir($file) === true) {
  274. $dirName = str_replace($inputFolder."/", '', $file."/");
  275. $zip->addEmptyDir($dirName);
  276. }
  277. else if (is_file($file) === true) {
  278. $fileName = str_replace($inputFolder."/", '', $file);
  279. $zip->addFromString($fileName, file_get_contents($file));
  280. }
  281. }
  282. }
  283. else if (is_file($inputFolder) === true) {
  284. $zip->addFromString(basename($inputFolder), file_get_contents($inputFolder));
  285. }
  286.  
  287. return $zip->close();
  288. }
  289.  
  290. ?>
Buy Me A Coffee