Check if exe was modified

Share your advanced PureBasic knowledge/code with the community.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Check if exe was modified

Post by Joakim Christiansen »

As I'm experimenting a little with anti cracking methods, I made this easy way of checking if a exe file was modified:

The code you should have in your app to check if it was modified:

Code: Select all

If ReadFile(0,ProgramFilename())
  *Buffer = AllocateMemory(Lof(0)-4)
  ReadData(0,*Buffer,MemorySize(*Buffer))
  If CRC32Fingerprint(*Buffer,MemorySize(*Buffer)) <> ReadLong(0)
    MessageRequester("Error","Exe has been tampered with...",#MB_ICONERROR)
    End
  EndIf
  FreeMemory(*Buffer)
  CloseFile(0)
EndIf
The code used to add the checksum at the end of the exe file:

Code: Select all

If OpenFile(0,OpenFileRequester("Add checksum to exe","","Executable|*.exe",0))
  *Buffer = AllocateMemory(Lof(0))
  ReadData(0,*Buffer,MemorySize(*Buffer))
  WriteLong(0,CRC32Fingerprint(*Buffer,MemorySize(*Buffer)))
  FreeMemory(*Buffer)
  CloseFile(0)
EndIf
Of course this is very little protection and anyone could very easily crack this, but it might be handy.
I like logic, hence I dislike humans but love computers.
HarryO
User
User
Posts: 42
Joined: Wed May 07, 2003 4:25 am
Location: Palatine,IL.,USA

RE: Check if '.exe' was modified....

Post by HarryO »

So if I understand this process correctly:

1. Create/compile program ('.exe') with your first procedure in it.
2. You would use the second procedure (in a separate program) to add the fingerprint at the end of the '.exe' created in step 1.
3. Then you could run your program (with the first procedure) to check the fingerprint of itself (the '.exe').

Is this correct?

That also means that everytime you change your program (compile) your have to rerun the second procedure/program to re-fingerprint your '.exe' file.

I think the process has merit.


Harry0
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: RE: Check if '.exe' was modified....

Post by Joakim Christiansen »

HarryO wrote:Is this correct?

That also means that everytime you change your program (compile) your have to rerun the second procedure/program to re-fingerprint your '.exe' file.
That is correct!
I like logic, hence I dislike humans but love computers.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Re: Check if exe was modified

Post by Derek »

Joakim Christiansen wrote:The code used to add the checksum at the end of the exe file:

Code: Select all

If OpenFile(0,OpenFileRequester("Add checksum to exe","","Executable|*.exe",0))
  *Buffer = AllocateMemory(Lof(0))
  ReadData(0,*Buffer,MemorySize(*Buffer))
  WriteLong(0,CRC32Fingerprint(*Buffer,MemorySize(*Buffer)))
  FreeMemory(*Buffer)
  CloseFile(0)
EndIf
Correct me if I'm wrong but it looks like you are storing the CRC at the end of the file so shouldn't you allocate lof(0)+4 bytes to the buffer before reading in the data, also if this is the case then the readdata() command will also need a -4 at the end or need to be changed to lof().
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

Derek wrote:Correct me if I'm wrong
with joy, dear friend. :D ;)

the proc you quoted is for appending the checksum to a file without.
the other proc that checks the checksum uses the demanded

Code: Select all

*Buffer = AllocateMemory(Lof(0)-4)
oh... and have a nice day.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

You're right. :oops:

Got my wires crossed, of course the file is being appened to.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

I wouldn't post code without testing it would I? :P
I like logic, hence I dislike humans but love computers.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

@Joakim Christiansen, just me having a brain meltdown. Don't know why but for some reason I thought the whole program was being read into a buffer, checksummed and then the buffer being written out.

I have these days where my brain just mis-fires hence my putting "correct me if I'm wrong", I am quite often!!
kinglestat
Enthusiast
Enthusiast
Posts: 746
Joined: Fri Jul 14, 2006 8:53 pm
Location: Malta
Contact:

Post by kinglestat »

very nice Joakim
thanks
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Post by ricardo »

If a crecker is smart enough to crack our software, i guess will be able to trick this kind of check.

Maybe need to be stored in some more complex way to avoid this possibilitie.
Matt
Enthusiast
Enthusiast
Posts: 447
Joined: Sat May 21, 2005 1:08 am
Location: USA

Post by Matt »

If someone really wanted to, they would be able to change the check sum at the end of the file, with no problem.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

Matt wrote:If someone really wanted to, they would be able to change the check sum at the end of the file, with no problem.
But it's not too hard for the programmer to tweak/encrypt that checksum a little so the cracker doesn't know what to change it into.
I like logic, hence I dislike humans but love computers.
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

I wouldn't touch the checksum, rather go for the check itself.
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Good idea.


As thefool said, a cracker will take out the check (not the checksum) but it is another little thing that makes life a smidgen harder for the cracker.


Aside:
Trouble is that for a large number of the real crackers it is the challenge that counts. Harder it is, more obsfucation and proofing there is, the smarter the protection, then the more motivation there is to crack it.

Bit like crossword puzzles and chess problems. The solution is the reward.
Dare2 cut down to size
Post Reply