Advertisement
Guest User

Untitled

a guest
Oct 11th, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.79 KB | None | 0 0
  1. <?php
  2. /*
  3.   AS 4.0 - digital signature check with colorize
  4.   version 1.0.2 - Oct 11, 2023
  5. */
  6. //var_dump($argv);
  7. $f = "";
  8. if( count($argv) >= 2 ) {
  9.   $par = "file="; $p = strpos($argv[1], $par); if( $p !== false ) { $f = substr($argv[1], strlen($par)); }
  10. }
  11. //echo $f . PHP_EOL;
  12.  
  13. function formatPrint(array $format=[],string $text = '') {
  14.   //color codes: https://misc.flogisoft.com/bash/tip_colors_and_formatting
  15.   $codes=[
  16.     'bold'=>1,'italic'=>3, 'underline'=>4, 'strikethrough'=>9,
  17.     'black'=>30, 'red'=>31, 'green'=>32, 'yellow'=>33,'blue'=>34, 'magenta'=>35, 'cyan'=>36, 'white'=>37,
  18.     'blackbg'=>40, 'redbg'=>41, 'greenbg'=>42, 'lyellowbg'=>103,'bluebg'=>44, 'magentabg'=>45, 'cyanbg'=>46, 'lightgreybg'=>47
  19.   ];
  20.   $formatMap = array_map(function ($v) use ($codes) { return $codes[$v]; }, $format);
  21.   return "\e[".implode(';',$formatMap).'m'.$text."\e[0m";
  22. }
  23.  
  24. function formatPrintLn(array $format=[], string $text='') {
  25.   return formatPrint($format, $text) . "\r\n";
  26. }
  27. /*
  28. //Examples:
  29. formatPrintLn(['bold', 'white', 'greenbg'], " Wohoo ");
  30. formatPrintLn(['bold', 'white', 'redbg'], " I'm invicible ");
  31. */
  32.  
  33. if( $f != "" && file_exists($f) ) {
  34.   //https://www.geeksforgeeks.org/php-shell_exec-vs-exec-function/
  35.   $promt = shell_exec('"C:\Program Files (x86)\SysinternalsSuite\sigcheck64.exe" "'.$f.'"');
  36.   //echo $promt;
  37.   //echo strlen($promt);
  38.   //file_put_contents("cmd-" . date("Y-m-d H-i-s") . ".log.txt", $promt);
  39.  
  40.   $signed = "Signed";
  41.   $unsigned = "Unsigned";
  42.   $verified = "Verified:";
  43.  
  44.   $bad1 = "The digital signature of the object did not verify.";
  45.   $bad2 = "A certificate chain processed, but terminated in a root certificate which is not trusted by the trust provider.";
  46.   $bad3 = "A certificate chain could not be built to a trusted root authority.";
  47.  
  48.   $lines = explode("\n", $promt);
  49.  
  50.   array_splice($lines, 2, 2); // remove Sigcheck credential details, 2 lines
  51.   $lines = array_values( $lines );
  52.  
  53.   /*
  54.   //echo count($lines);
  55.   $linesToString = implode("\n", $lines);
  56.   file_put_contents("cmd-" . date("Y-m-d H-i-s") . ".log.txt", $linesToString);
  57.   exit;
  58.   */
  59.  
  60.   for($r = 0; $r < count($lines); $r++) {
  61.     $out = "";
  62.     $l = $lines[$r];
  63.  
  64.     if (($p = strpos($l, $signed)) !== false) {
  65.       $out .= substr($l, 0, $p) . formatPrint(['bold', 'white', 'greenbg'], $signed) . substr($l, $p + strlen($signed));
  66.     } elseif (($p = strpos($l, $unsigned)) !== false) {
  67.       $out .= substr($l, 0, $p) . formatPrint(['bold', 'white', 'redbg'], $unsigned) . substr($l, $p + strlen($unsigned));
  68.     } elseif (($p = strpos($l, $verified)) !== false) {
  69.       $p += strlen($verified);
  70.       $out .= substr($l, 0, $p);
  71.  
  72.       $part2 = substr($l, $p);
  73.       $n = 0;
  74.       for ($i = 0; $i < strlen($part2); $i++) {
  75.         if ($part2[$i] != "\t") {
  76.           $n = $i;
  77.           break;
  78.         }
  79.         $out .= $part2[$i]; //tab(s)
  80.       }
  81.       $part2 = substr($part2, $n);
  82.       //echo strlen($part2)." ".$part2;
  83.       //echo strlen($bad1)." ".$bad1;
  84.  
  85.       if($part2 == $bad1) {
  86.         $out .= formatPrint(['bold', 'white', 'redbg'], $part2); //The digital signature...did not verify
  87.       }
  88.       else
  89.       if($part2 == $bad2) {
  90.         $out .= formatPrint(['bold', 'white', 'magentabg'], $part2); //A certificate chain terminated in...not trusted provider
  91.       }
  92.       else
  93.       if($part2 == $bad3) {
  94.         $out .= formatPrint(['bold', 'cyan', 'magentabg'], $part2); //A certificate could not be built to...trusted authority
  95.       }
  96.       else
  97.         $out .= formatPrint(['magenta', 'lyellowbg'], $part2); //another
  98.     }
  99.  
  100.     if( $out != "" && $out != $l )
  101.       $lines[$r] = $out;
  102.   }
  103.  
  104.   $linesToString = implode("\n", $lines);
  105.   //file_put_contents("cmd-" . date("Y-m-d H-i-s") . ".log.txt", $linesToString);
  106.   echo $linesToString;
  107. }
  108.  
  109. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement