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