PDA

View Full Version : Self Checking Checksum



Gadget
05-02-2003, 02:38 PM
A couple of questions here... Firstly, how can a Delphi exe read the exe file when it has exclusive access to the file already due to it loading?

The second problem is that the Delphi compiler sticks random data in various parts of the exe to pad it. So how can you work out what the checksum will be, if you see what I mean?

Alimonster
05-02-2003, 07:25 PM
You can read a running exe by using one of the mode flags in TFileStream.Create, I believe - open in "fmOpenRead or fmShareDenyWrite" [or maybe "fmOpenRead or fmShareDenyNone"]. A quick test suggests that they work fine reading from a running exe file.

Can't answer your second question, though I'll give you some speculation. Are you trying to include a checksum onto an exe to check whether it's been altered? If so, write your standard "CheckCRC32" function inside of your main code (or other suitable checksum function). Compile your project into an exe file.

Use an external utility w/ the same function (just copy and paste it into another project) to generate a fixed-size checksum based on the compiled exe. Append this checksum value to the end of the exe. When running the exe, you would read all the exe data up to the start of the checksum. You'd do the calculation and compare to the externally generated checksum.

Bear in mind that this will be very fiddly - you'll have to manually generate a new checksum each you recompile, which will be a royal pain in the ass. Also, you have to remember that the checksum value shouldn't be read in with the rest of the exe when calculating at run-time - it wasn't involved in the first place!

Gadget
05-02-2003, 09:29 PM
You can read a running exe by using one of the mode flags in TFileStream.Create, I believe - open in "fmOpenRead or fmShareDenyWrite" [or maybe "fmOpenRead or fmShareDenyNone"]. A quick test suggests that they work fine reading from a running exe file.

Can't answer your second question, though I'll give you some speculation. Are you trying to include a checksum onto an exe to check whether it's been altered? If so, write your standard "CheckCRC32" function inside of your main code (or other suitable checksum function). Compile your project into an exe file.

Use an external utility w/ the same function (just copy and paste it into another project) to generate a fixed-size checksum based on the compiled exe. Append this checksum value to the end of the exe. When running the exe, you would read all the exe data up to the start of the checksum. You'd do the calculation and compare to the externally generated checksum.

Bear in mind that this will be very fiddly - you'll have to manually generate a new checksum each you recompile, which will be a royal pain in the ass. Also, you have to remember that the checksum value shouldn't be read in with the rest of the exe when calculating at run-time - it wasn't involved in the first place!

Your reply is as genius as always :) Thanks, just what I was looking for! I had previously thought about hard coding a dummy checkum 'inside' the exe (eg. $FFFFFFFF) then calculate the actual checksum of the exe without the 2 words where the checksum is stored, then manually edit that location in the exe to include the actual checksum. It's a bit easier adding it to the end of the exe.

Sly
05-02-2003, 11:33 PM
There are ways of including the checksum in the calculation of the checksum. I'm not familiar with these methods myself, but I have read articles about copy protection in games such as Spyro the Dragon where that is what they used. The article is on GamaSutra (http://www.gamasutra.com/features/20011017/dodd_01.htm).

Generating a checksum for an executable each time it is compiled would be very easy if the Borland IDE gave us the opportunity to run arbitrary commands before and after a build process. Visual Studio has this feature and we use it extensively here at work. If Borland did have this feature, all you would need is a small program that generates the checksum to insert into the executable and call this checksum generator after the build process has completed. Easy! Every time you recompiled the project a new checksum would be generated and inserted into the executable. But alas, Borland's project management features in the IDE have always been woefully inadequate.