TL;DR
Yes, an attacker *can* replace a download’s hash and public key if they compromise the distribution channel. However, strong verification methods like signatures and trusted sources significantly reduce this risk. This guide explains how attackers do it and what you can do to protect yourself.
Understanding the Attack
An attacker aims to trick you into installing malicious software by making it appear legitimate. They achieve this by:
- Replacing the Download: Substituting a fake file for the real one.
- Manipulating the Hash: Changing the advertised hash value to match the malicious file.
- Compromising the Public Key: Replacing the legitimate public key with their own, allowing them to sign malicious files that appear valid.
If all three are successful, you’ll download a fake file, verify it against a manipulated hash, and potentially trust it because of a compromised signature.
How an Attacker Replaces the Hash
- Man-in-the-Middle (MITM) Attack: If the website isn’t using HTTPS properly, or you’re on an insecure network, an attacker can intercept the download link and change it to point to their server.
- Website Compromise: An attacker gains access to the website hosting the file and directly modifies the hash value displayed on the page. This is often done through vulnerabilities in the website’s code (e.g., SQL injection, cross-site scripting).
- Social Engineering: Tricking someone with access to the website into changing the hash manually.
Once they control the download source or the displayed information, they calculate the hash of their malicious file (using tools like sha256sum on Linux/macOS or Get-FileHash in PowerShell) and replace the original hash with this new value.
How an Attacker Replaces the Public Key
- Compromise the Developer’s System: Gaining access to the developer’s computer or signing server.
- Phishing: Tricking the developer into revealing their private key (used to sign files).
- Website Compromise: If the website hosts the public key, an attacker can replace it with their own.
With access to the legitimate public key, they can then substitute it with their own malicious key.
Protecting Yourself: Verification Steps
- Use HTTPS: Always download files from websites using
https://. This encrypts communication and prevents MITM attacks.curl -I https://example.com/downloadfile.exe(Check for a valid certificate)
- Verify the Download Hash Independently: Don’t rely on hashes provided *on* the download website. Find the hash from a trusted source (e.g., official documentation, developer’s social media).
sha256sum downloadfile.exe - Use Digital Signatures: Download files that are digitally signed by the software vendor.
- Windows: Right-click the file, go to Properties, and check the ‘Digital Signatures’ tab.
- Linux/macOS: Use tools like
gpgor package managers (e.g.,apt verifyfor Debian packages).
- Trusted Sources Only: Download software only from official websites or reputable app stores. Avoid third-party download sites.
- Two-Factor Authentication (2FA): If you’re a developer, enable 2FA on all accounts related to your signing infrastructure.
- Code Signing Certificates: Use hardware security modules (HSMs) to protect your code signing keys.
What if the Public Key is Compromised?
If you suspect a public key has been compromised:
- Revoke the Old Key: Immediately revoke the compromised key.
- Issue a New Key: Generate and distribute a new public key.
- Communicate to Users: Inform your users about the compromise and instruct them to use only the new key for verification.

