If you have a client-side script (e.g client.py) communicating with a server-side script (e.g server.py). Using SSL certificates the identity of the server is automatically verified.

But how do you verify that the code of server.py isn't tampered with? When for example using a VPS you never know if a employee of the hosting provider with bad intentions has edited server.py to intercept data or whatever.

I was thinking of maybe using a hash to validate the code of server.py. Before deploying server.py to the server I'd generate a hash of the file. This hash is placed in client.py. When the client connects, a code in server.py is called:

# pseudo code
def return_hash_of_server_script():
    with open('server.py', 'rb') as f:
        file_content = f.read()
        hash = sha256(file_content)

The client would do something like this:

# pseudo code
def validate_hash_of_server():
    hash_of_serverpy_before_deployment = "e2ioj23njwo"
    hash = retrieve_hash_from_server()

    if hash != hash_of_serverpy_before_deployment:
        return False

    return True

But I think the problem with this is, that someone with access to the VPS could just generate a hash of server.py before he edits it and alter the function return_hash_of_server_script() like this:

# pseudo code
def get_hash_of_server_script():
    # hash generated before altering

Resulting in the client thinking the code of server.py is valid.

So I was thinking about the following:

  • The client has a hex representation of server.py before deployment in a file. We'll name this server_py_hex.
  • There is a hardcoded secret "abc123".
  • There is a RSA-keypair. The server has the private key and the client the public key.
  • The first thing the client does is generating a hash: sha256(server_py_hex + "abc123" + current_date_and_hour).
  • The client sends the hardcoded secret to the server encrypted in RSA with his public key.
  • The server decrypts the request to get the hardcoded secret and returns the hash sha256(content_of_server_py_in_hex + decrypted_secret + current_date_and_hour).

This way a malicious person with access to the VPS can't generate a hash of server.py before tampering it because he doesn't know the secret. And if he is able to intercept the response of a valid hash the server returns to the client and he tries to tamper server.py in returning that hash to fool the client, it'd only work for a hour because the current date and hour is included in the hash.

Is this a solid implementation to validate code authenticity? Of course on the condition that client.py is only distributed to a limited set of trusted users, and is not publicly accessible for download.

I bet that someone has came up with an a lot better and smarter way to tackle this issue. What is the official way to verify the code on a server?

  • Verifying the contents of server.py doesn't really mean anything if it's executing on an untrusted system - there are dozens of ways that an attacker to manipulate it without modifying the script itself. So I think you need to go back to the drawing board and rethink the whole approach.
    – Gh0stFish
    Dec 10, 2023 at 13:52

1 Answer 1


This approach assumes that the attacker has changed the original server.py without making a copy first. But if the attacker has made a copy it is easy to correctly answer the challenge from the client - just use the original and not the compromised version to compute the response.

In general, it is impossible to check the integrity solely from within a potentially compromised system. You would need instead some trusted instance which provides information about the system, i.e. something not affected by the compromise.

Also, even if you have managed to somehow verify the integrity of the script it still does not mean that it works as intended. The attacker might have modified configuration files used by the script, the python interpreter itself, the operating system ... - all affect at the end what the script does.

So you need more than validating the integrity of the script - you need to verify the integrity of the whole system at least as far as it affects what the script does. What you ask for is proper Remote attestation which is part of Trusted Computing. It's far from trivial.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .