How can I write a program that can detect by itself that it has been changed?

穿精又带淫゛_ 提交于 2021-02-19 08:12:20

问题


I need to write a small program that can detect that it has been changed. Please give me a suggestion!

Thank you.


回答1:


The short answer is to create a hash or key of the program and have the program encrypt and store that key within itself. From time to time the program would make a checksum of itself and compare it against that hash/key. If there is a difference then handle it accordingly.

There are lots and lots of ways to go about this. There are lots of very smart engineers out there that know how to work around it if that is what you are trying to avoid.




回答2:


The simplest way would be to use a hash function to generate a short code which is a digest of the whole program and then check this.

It would be fairly easy to debug the code and replace the hash value to subvert this.


A better way would be to generate a digital signature using your private key and with the public key in the program to check it.

This would then require changing the public key and the hash as well as understanding the program, or changing the program code itself to subvert the check.


All you can do in the case described so far is make it more difficult to subvert but it will be possible with a certain amount of effort. I'd suggest looking into cryptographic techniques and copy protection for more information to suit your specific case.




回答3:


Do you mean that program 'foo' should be able to tell if some part of it was modified prior to / during run time? That's not the responsibility of the program, its the responsibility of the security hooks in the target OS.

For instance, if the installed and trusted 'foo' has signature "xyz1234" , the kernel should refuse to run a modified (or completely new) 'foo'. The same goes for 'foo' while its currently running in memory. Look up 'Trusted Path Of Execution', aka TPE to start.

A better question to ask would be how to sign your released version of 'foo', which depends upon your target platform.




回答4:


try searching for "code signing"




回答5:


The easiest way would be for the program to detect its own md5 and store that in a separate file, but this isn't totally secure. An MD5 + CRC might work slightly better.

Or as others here have suggested, a sha1, sha2 or sha3 which are much more secure than md5 currently.




回答6:


I'd ask an external tool to do the check. This problem reminds me of the challenge to write a program that prints itself. In Bash you could do something like this:

#!/bin/bash
cat $0

which really asks for an external tool to do the job. It's kind of solving the problem by getting away from solving the problem...




回答7:


The best option is going to be code signing -- either using a tool supplied by your local friendly OS (For example, If you're targeting Windows, you probably want to take a look at Authenticode where the Operating System handles the tampering), or by rolling your own option storing MD5 hashes and comparing

It is important to remember that bets are off if someone injects a thread into your process (to potentially kill your ongoing checks, etc.), or if they tamper with your compiled application to bypass said checks.




回答8:


An alternative way which wasn't mentioned is to use a binary packer such as UPX.
If the binary gets changed on the disk then the unpacking code is likely to fail.
This however doesn't protect you if someone changes the binary while it is in memory.



来源:https://stackoverflow.com/questions/406179/how-can-i-write-a-program-that-can-detect-by-itself-that-it-has-been-changed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!