Page 1 of 1

Capture dynamic value as a variable

Posted: 21 Oct 2010 13:03
by A_Bobby
Hi,

I am using certutil command to dump the local certificate store and then have to pick up the skid value of the certificate. The output looks something like this

Key Id Hash(sha1): d8 51 cf b1 45 e7 cc a6 55 91 79 0b 6b e6 b1 ee e7 4f 81 f
Cert Hash(md5): c1 9c 56 23 d5 26 43 39 6b 6e 13 3d a1 0b 03 04
Cert Hash(sha1): 6f cd 7a c7 14 fe a6 fd fb 33 52 7d a9 b3 fb 27 0d 62 85 f8

CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID(24):
1f 35 d6 0e 91 61 df 29 b7 40 7f 91 58 4e 63 a9

CERT_SIGNATURE_HASH_PROP_ID(15):
4e 52 9b db dc cd 84 cb 95 f3 17 1f 39 b5 76 27 91 30 83 a3

CERT_KEY_IDENTIFIER_PROP_ID(20):
27 94 4d 86 38 ea a4 22 7c f2 17 bf 6d 0d 77 fe 10 c3 73 66


The above is only a part of output. I want to capture the string 27 94 4d 86 38 ea a4 22 7c f2 17 bf 6d 77 fe 10 c3 73 66 but it is dynamic in all machines. I have been able to use the for /f successfully to capture the line above it which is unique in every output using hte following command..

for /f %i in ('certutil -v -store My ^| findstr /i "_ID(20)"') do @echo %i
which gives me the output CERT_KEY_IDENTIFIER_PROP_ID(20):
but that is not what i want, i want the line right after that as a variable.

Can someone help. Thanks

Re: Capture dynamic value as a variable

Posted: 21 Oct 2010 15:17
by aGerman
Try something like that:

Code: Select all

@echo off &setlocal enabledelayedexpansion
for /f "delims=: tokens=1*" %%a in ('certutil -v -store My^|findstr /n .') do (
  for /f %%c in ('echo\"%%b"^|findstr /i "_ID(20)"') do set /a n=%%a+1
  for /f %%d in ('echo\%%a_^|findstr /x "!n!_"') do (
    set "hash=%%b"
    goto exitFor
  )
)
:exitFor
echo\%hash%
pause

Regards
aGerman

Re: Capture dynamic value as a variable

Posted: 21 Oct 2010 17:01
by A_Bobby
Brilliant!!

So with this I am able to extract the hash for one cert, but what if there are multiple certs (maximum 5) in the store "My" and all need to be captured?

Also I am trying to eliminate spaces in between the characters in the hash.

Thanks a bunch for this. I am going to try this myself before I cheat :wink:

Re: Capture dynamic value as a variable

Posted: 21 Oct 2010 18:12
by ghostmachine4
if you want to process files/text , use a better programming language like vbscript, or gawk (or Perl/Python etc). the batch solution provided is slow to execute since it makes many extra calls to external findstr command unnecessarily.

An eg in Vbscript

Code: Select all

Set StdIn = WScript.StdIn
Set StdOut = WScript.StdOut
Do While Not StdIn.AtEndOfStream
     str = StdIn.ReadLine
     If InStr(str,"_ID(20)" ) > 0 Then
           what_i_want = Join(Split(StdIn.ReadLine," "),"")     ' remove the spaces
           Stdout.Write(what_i_want)
     End If
Loop


how to use:

Code: Select all

certutil -v -store ..... |  cscript //nologo myscript.vbs 



Or if you could afford to download stuff, you can use gawk for windows

Code: Select all

C:\test>    certutil -v -store ..... | gawk "/ID\(20\)/{getline;$1=$1;print }" OFS="" 


If you have 5 stores to process , then use a for loop.