CertUtil -hashfile Output to File and Error Handling

1

I need to generate a file with hashfile tags from a list of files that is generated within the same batch file. Here is the code that I have so far:

@echo off
setlocal enabledelayedexpansion

:: Set the variables for this script.
set testfolder=c:\test\test folder
set listfile=c:\test\output\file list.txt
set hashfile=c:\test\output\hashes.txt

:: Delete any of the files that were created the last time this script was ran.
del "%hashfile%"
del "%listfile%"
cls

:: Generate a file with a list of all of the files (with path) in designated folder and subdirectories.
:: Directory and subdirectory names are not included in the list. Only files.
dir /s /b /a-d "%testfolder%" > "%listfile%"

:: Assign each line of the file above to its own variable.
set counter=1
for /f "usebackq delims=" %%x in ("%listfile%") do (
  set "line_!counter!=%%x"
  set /a counter+=1
)

:: Count the number of lines in the above file to use as a reference point.
set /a numlines=counter - 1

:: Generate an MD5 hash for each variable and write it to a file with a blank space between each.
for /l %%x in (1,1,%numlines%) do (
  certutil -hashfile "!line_%%x!" MD5 >> "%hashfile%"
  echo( >> "%hashfile%"
)
eof

For most of the files that I generate a hashfile for, I get something like:

MD5 hash of file c:\test\test folder\Citrix 2.bmp:
31 34 d6 04 cd b0 4b ef a7 63 c3 e9 ae a8 3d 01
CertUtil: -hashfile command completed successfully.

But there are times where I get an error like:

CertUtil: -hashfile command FAILED: 0x800703ee (WIN32: 1006)
CertUtil: The volume for a file has been externally altered so that the opened file is no longer valid.
  1. Why would some files be giving this error?
  2. How can I delete any line that begins with CertUtil: so I don't have unnecessary lines or is there a way to only write the first 2 lines of the CertUtil command to the file.
  3. After the %hashfile% is in its final form, I want to run certutil -hashfile "%hashfile% MD5 and assign just the hash code to a variable. What is the syntax for that?
batch-file
certutil
asked on Stack Overflow Nov 29, 2016 by indy-pc

2 Answers

0

Slightly modified code snippet shows that 0x800703ee (WIN32: 1006 ERROR_FILE_INVALID) error message just for zero length files (cf. rem comments).

:: Generate an MD5 hash for each variable and write it to a file with a blank line...
>"%hashfile%" (
  for /l %%x in (1,1,%numlines%) do (

        rem debugging: show file length for each file
    for %%G in ("!line_%%x!") do echo(%%~zG "%%~G"

        rem operational: skip certutil for zero length files (requires elaboration)   
    certutil -hashfile "!line_%%x!" MD5
    echo(
  )
)

See Command Line arguments (Parameters) for %%~zG and "%%~G" explanation.

answered on Stack Overflow Nov 29, 2016 by JosefZ
0

IMO the count step in your batch is simpler to accomplish with a find /v /N ""

for /f "tokens=3 delims=: " %%A in (
  'find /V /N /C "" %listfile%'
) Do set NumLines=%%A

But sincs the list and the hashfile are recreated every time the batch is run, the intermediate steps aren't necessary at all.
This stacked for - if - for will do it in one run:

@echo off
setlocal enabledelayedexpansion

:: Set the variables for this script.
set testfolder=c:\test\test folder
set hashfile=c:\test\output\md5-hashes.csv

>%hashfile% (
  echo "File","MD5-hash"
  For /f "delims=" %%A in (
    'dir /s /b /a-d "%testfolder%"'
  ) Do If %%~sA Gtr 0 For /f "delims=" %%B in (
    'certutil -hashfile %%A MD5 ^|findstr /i ^[0-9a-f][0-9a-f].[0-9a-f][0-9a-f].[0-9a-f][0-9a-f]'
  ) Do Echo:"%%~A","%%~B"
)

This partial output happens to reference a tool which may be more suited to this task :-)

> type md5-hashes.csv
"File","MD5-hash"
"c:\test\md5\hashdeep.exe","23 03 ea 53 52 03 c7 93 05 49 0d 6c 20 be 84 54"
"c:\test\md5\hashdeep64.exe","66 5c cc 57 a8 4c 56 39 c4 e5 15 16 86 cc 04 32"
"c:\test\md5\md5deep.exe","23 03 ea 53 52 03 c7 93 05 49 0d 6c 20 be 84 54"
"c:\test\md5\md5deep64.exe","66 5c cc 57 a8 4c 56 39 c4 e5 15 16 86 cc 04 32"
answered on Stack Overflow Nov 30, 2016 by (unknown user)

User contributions licensed under CC BY-SA 3.0