Proving a file is unchanged: hashing with CertUtil
Generating and comparing file hashes with CertUtil on Windows 11 LTSC IoT, what a matching digest proves, and what it cannot.
In April 1992 Ronald Rivest published MD5, RFC 1321, which turned any file into a 128-bit fingerprint. In 2004 Xiaoyun Wang and her colleagues showed how to make two inputs with the same MD5 digest, and in 2012 the Flame malware used an MD5 collision to forge a Microsoft code-signing certificate. In February 2017 researchers at Google and CWI Amsterdam did the same to SHA-1 with two different PDF files.
A hash function promises that no two inputs anyone can find will share an output, and the promise ages. The workflow built on it has not changed: hash a file, keep the digest, hash again later, compare.
Initial SHA256 hash generation using CertUtil.
CertUtil is the certificate tool built into Windows, and its -hashfile verb prints a file’s digest with nothing to install. The convenience cuts both ways: attackers use the same binary to fetch and decode payloads, which is why MITRE ATT&CK lists it.
Test setup
testfile successfully created and placed.
The test file was testfile.txt, holding simple text. After creating the sample file, a copy was made so that it could be tampered with:
The two files were hashed with SHA-256:
certutil -hashfile testfile.txt SHA256
certutil -hashfile copy.txt SHA256
Identical SHA256 hash generation proven.
Both digests began 7b1b1356 and matched to the last character, whatever the files were called.
Then the copy was tampered with and hashed again:
Initial SHA256 hash is now different.
The new digest began a96e3360. A good hash function is built so that one changed character flips about half the output bits, and a small edit cannot hide behind a small difference.
SHA-1 and SHA-512 were tested on the original file as well:
certutil -hashfile testfile.txt SHA1
certutil -hashfile testfile.txt SHA512
The SHA-1 digest is 40 hex characters and the SHA-512 one 128. -hashfile also accepts MD2, MD4, MD5, SHA256 and SHA384, and with no algorithm named it uses SHA-1.
Which algorithm, and what a match proves
For catching accidents, any of them works. Against someone who wants a tampered file to pass, use SHA-256 or longer, because MD5 and SHA-1 collisions can now be manufactured.
A match proves the file is the same as whatever produced the published digest. It says nothing about who produced it. If the digest sits on the same web server as the download, anyone who can replace the file can replace the digest too. That gap is what signatures close: a GPG signature or Authenticode answers who, and the hash answers whether.
Where it goes from here
The exercise lines up with the cryptographic concepts section of Security+, but the useful part is operational. For a file from anywhere not fully trusted, the published hash and a CertUtil -hashfile against the local copy answer “did this arrive intact?” in one command.
The same trick scales. Hash the binaries that matter on a known-good system, keep the list where the system cannot write, and check again later. Gene Kim and Eugene Spafford built Tripwire on that idea at Purdue in 1992. It only has to be done before the day it is needed.
Sources
- Rivest, R. RFC 1321, “The MD5 Message-Digest Algorithm.” April 1992.
- Wang, Xiaoyun, Dengguo Feng, Xuejia Lai and Hongbo Yu. “Collisions for Hash Functions MD4, MD5, HAVAL-128 and RIPEMD.” Cryptology ePrint Archive 2004/199.
- Stevens, Marc, Elie Bursztein, Pierre Karpman, Ange Albertini and Yarik Markov. “The First Collision for Full SHA-1.” CRYPTO 2017.
- Kim, Gene H., and Eugene H. Spafford. “The Design and Implementation of Tripwire: A File System Integrity Checker.” Proceedings of the 2nd ACM Conference on Computer and Communications Security, 1994.
- MITRE ATT&CK. Software entry for certutil.
- Microsoft. “certutil” command reference.
