Blog | G5 Cyber Security

Download Security: Protecting Against Hash & Key Replacement

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:

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

  1. 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.
  2. 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).
  3. 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

  1. Compromise the Developer’s System: Gaining access to the developer’s computer or signing server.
  2. Phishing: Tricking the developer into revealing their private key (used to sign files).
  3. 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

  1. 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)

  2. 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
  3. 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 gpg or package managers (e.g., apt verify for Debian packages).
  4. Trusted Sources Only: Download software only from official websites or reputable app stores. Avoid third-party download sites.
  5. Two-Factor Authentication (2FA): If you’re a developer, enable 2FA on all accounts related to your signing infrastructure.
  6. 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:

Exit mobile version