Self Virus Check / Exec Alteration

Share your advanced PureBasic knowledge/code with the community.
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Self Virus Check / Exec Alteration

Post by J. Baker »

A way to check if a virus has infected your own program. Viruses usually attach themselves to the beginning and end of an executable. Without going into a lot of detail about that, either way, it will increase the size of your executable. Replacement code is possible but not as likely as the virus usually wants the program to behave normally, so it can run undetected. The following code is not 100% full proof but may hopefully stop a virus from doing its task or at least alert the user that their system is infected.

Code: Select all

CipherSize$ = "b5af67c75b77f50bc437618b36d0403938006fa4a1bc9d7495972498"

UseSHA2Fingerprint()

If StringFingerprint(Str(FileSize(ProgramFilename())), #PB_Cipher_SHA2, 224) <> CipherSize$
  MessageRequester("ERROR!", "The program has been altered by other means than the developer intended and will now quit.")
EndIf

;The rest of your programs code goes after the file size checking code.

;-------------------------------------------------------------------------------
;The code below should be run on an executable after it has been compiled.
;You will have to compile twice in order to get the correct cipher size.
;CipherSize$ is just a way to hide the actual file size from a virus but is not 100% protection.

;Debug StringFingerprint(Str(FileSize(ProgramFilename())), #PB_Cipher_SHA2, 224)
Be sure to check out kvitaliy's code below too. viewtopic.php?p=559198#p559198
Last edited by J. Baker on Sun Sep 06, 2020 5:25 pm, edited 1 time in total.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Self Virus Check / Exec Alteration

Post by J. Baker »

You can also file fingerprint your own executable but takes a couple compiles, a hex editor, and an offset (that depends on the executable size). So either way, a file size check would have to be done. Just thought I would mention it in case anyone wanted to go one step further.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Self Virus Check / Exec Alteration

Post by Little John »

I like the idea, thank you!

However, during development this would be quite annoying.
So I'd only include it in the final EXE file:

Code: Select all

CompilerIf #PB_Editor_CreateExecutable
   CipherSize$ = "b5af67c75b77f50bc437618b36d0403938006fa4a1bc9d7495972498"
   
   UseSHA2Fingerprint()
   
   If StringFingerprint(Str(FileSize(ProgramFilename())), #PB_Cipher_SHA2, 224) <> CipherSize$
      MessageRequester("ERROR!", "The program has been altered by other means than the developer intended and will now quit.")
      End
   EndIf
CompilerEndIf
(The constant #PB_Editor_CreateExecutable can only be used after it has been activated in the compiler options.)
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Self Virus Check / Exec Alteration

Post by Saki »

Nice Idea
Last edited by Saki on Sat Sep 05, 2020 1:30 pm, edited 1 time in total.
地球上の平和
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Self Virus Check / Exec Alteration

Post by J. Baker »

Good idea Little John. That's better than commenting and removing comments. Thanks for posting. :D
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Self Virus Check / Exec Alteration

Post by Saki »

There are several possible solutions J.Baker
It just requires the most appropriate description of the requirements for your needs.
These are standard procedures, nothing new.
I have no problems with it, these codes I have ready, not crackable and enhanced, all necessary write and read routines.
I was just about to post them now.

If you find it funny, if I try to give you an exact description of the procedure,
you just have to see for yourself how it works.
地球上の平和
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Self Virus Check / Exec Alteration

Post by J. Baker »

Saki, fill free to post. ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
kvitaliy
Enthusiast
Enthusiast
Posts: 162
Joined: Mon May 10, 2010 4:02 pm

Re: Self Virus Check / Exec Alteration

Post by kvitaliy »

Standard way for Windows using the API MapFileAndCheckSum
Protection will work even if you change 1 bit!

Code: Select all

szFullPath.s=ProgramFilename()
dwFileChecksum.l = 0: dwRealChecksum.l = 0
sz.s = "Hello" ; replace at least 1 character in EXE
MapFileAndCheckSum_(@szFullPath, @dwFileChecksum, @dwRealChecksum)
If dwFileChecksum<>dwRealChecksum 
  
 MessageRequester(sz, "the checksum is Not correct = " + Str(dwFileChecksum) +" R="+  dwRealChecksum)
Else
   MessageRequester(sz,  "the checksum is correct= " + Str(dwFileChecksum) +" R="+  dwRealChecksum)  
EndIf
For correct operation, you need to create and connect a text file (linker.txt) with the following content:
/RELEASE

Image
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Self Virus Check / Exec Alteration

Post by Saki »

MapFileAndCheckSum - Microsoft wrote :

Note The Unicode implementation of this function calls the ASCII implementation and as a result, the function can fail if the codepage does not support the characters in the path. For example, if you pass a non-English Unicode file path, and the default codepage is English, the unrecognized non-English wide chars are converted to "??" and the file cannot be opened (the function returns CHECKSUM_OPEN_FAILURE).

I'll give you a little tip. :wink:
The easiest solution is to add a small hash to the file name.
This can looks like this: MyFile[edadefcd].exe (CRC32 simplest)
This can also be very easily removed again automatically.
You can simplest check the file length or the whole content.
The more complex and password-dependent the hash, the more secure.

Entire directories can be automatically protected and checked in a few seconds.
Last edited by Saki on Sun Sep 20, 2020 10:09 am, edited 2 times in total.
地球上の平和
Bitblazer
Enthusiast
Enthusiast
Posts: 736
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: Self Virus Check / Exec Alteration

Post by Bitblazer »

The next step would be to add executable compression and code signing. Both have their uses and also disadvantages*, but sadly none of them are the ultimate solution against malware**.

*UPX and others have raised many false AV warnings on end-user machines and due to that making end-users think the executable is suspicious
**some malware can intercept and manipulate API calls to hide itself. So no virus scanner or executable check can spot them on an infected system anymore.
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Self Virus Check / Exec Alteration

Post by Little John »

Thank you, kvitaliy! This is very interesting.
BarryG
Addict
Addict
Posts: 3324
Joined: Thu Apr 18, 2019 8:17 am

Re: Self Virus Check / Exec Alteration

Post by BarryG »

kvitaliy wrote:Standard way for Windows using the API MapFileAndCheckSum
Protection will work even if you change 1 bit!

Code: Select all

szFullPath.s=ProgramFilename()
dwFileChecksum.l = 0: dwRealChecksum.l = 0
sz.s = "Hello" ; replace at least 1 character in EXE
MapFileAndCheckSum_(@szFullPath, @dwFileChecksum, @dwRealChecksum)
If dwFileChecksum<>dwRealChecksum 
  
 MessageRequester(sz, "the checksum is Not correct = " + Str(dwFileChecksum) +" R="+  dwRealChecksum)
Else
   MessageRequester(sz,  "the checksum is correct= " + Str(dwFileChecksum) +" R="+  dwRealChecksum)  
EndIf
For correct operation, you need to create and connect a text file (linker.txt) with the following content:
/RELEASE

Image
I agree with Little John; the above is a FANTASTIC tip for detecting alteration of our Windows executables. Thanks!
Joris
Addict
Addict
Posts: 885
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: Self Virus Check / Exec Alteration

Post by Joris »

I feel a bit stupid but ...
I know my Programname of course, but how to know the full path where the exe will become placed (by the user) for ProgramFilename() ?
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
BarryG
Addict
Addict
Posts: 3324
Joined: Thu Apr 18, 2019 8:17 am

Re: Self Virus Check / Exec Alteration

Post by BarryG »

That's what ProgramFilename() is for. You don't need to know where the user puts your exe. It gets the exe's path and filename at runtime.
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Self Virus Check / Exec Alteration

Post by J. Baker »

kvitaliy wrote:Standard way for Windows using the API MapFileAndCheckSum
Protection will work even if you change 1 bit!
Very nice! Thanks! :D
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
Post Reply