Using EXECryptor with PureBasic

Share your advanced PureBasic knowledge/code with the community.
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Using EXECryptor with PureBasic

Post by MrMat »

This is an example for using the software protector EXECryptor with PureBasic. To follow this guide you'll need:
To run the example file execryptor.pb follow these steps:
  • Compile execryptor.pb as an exe (with or without using PBCoffee)
  • Load EXECryptor and create a new project (make a note of where you create it).
  • Go to Options -> General settings and select the exe created above as the application file.
  • Go to Options -> Protection options and select the options you want. Make sure 'dynamic imports' is not selected (doesn't seem to work with PureBasic).
  • Go to Options -> Serial number support and select manual protection as the configuration and use built-in serials.
  • Go to Serials -> Manager, enter a registration name, select some options for testing and click on create.
  • Take note of the serial number generated and we will test it later. For example:
    Image
  • Exit EXECryptor and return to the example PureBasic file execryptor.pb.
  • Start PBCoffee and create new settings to work with EXECryptor (we can save these settings and reuse them)
  • Set it to compile an exe on the main tab, select FIXED No in the linker tab and select EXECryptor in the post-compile options (see highlighted section):
    Image
  • Click on the button next to EXECrypt and find the EXECrypt.exe file (not the EXECryptor.exe frontend - EXECrypt.exe is the command line version)
  • In the params box find the ep2 project you created above in EXECryptor.
  • Tick the "Auto create mode def file" box and save the settings for future use.
  • Compile the program. The output should be something like the following:

    Code: Select all

    EXECryptor 2.1.9
    Copyright (c) 2002-2005 SoftComplete Development
    UNREGISTERED!
    Found 3 marks.
    Randomize: 1679461627, 2041191522, 148967460, 811779114
    Mark at 0x00001BF2:  Size 432 bytes; 90 instructions; 3 jcc.
    Mark at 0x00001DA2:  Size 519 bytes; 113 instructions; 1 jcc.
    Mark at 0x00001FA9:  Size 36 bytes; 6 instructions; 0 jcc.
    Detect EXECryptor API usage: VerifySerialNumber, IsRegistered, RegConst_0, RegConst_1, RegConst_2, RegConst_3, RegConst_4, RegConst_5, RegConst_6, RegConst_7, GetHardwareID, EncryptStr, DecryptStr, SecureWrite, SecureRead, MessageBox, GetProcAddr
    Destroy export directory
    Process import ...
    Compile EXECryptor API library 60867 bytes; 15663 instructions; 1508 jcc.
    Local transform: 83638 bytes; 2478 instructions; 86 jcc.
    Convert and hide 1 calls to imported procs/EXECryptor API
    Global transform: 86809 bytes; 3150 instructions; 128 jcc.
    Compile VM core 23487 bytes; 6457 instructions; 606 jcc.
    Total: 171163 bytes; 25270 instructions; 2242 jcc.
    Virtualization: 4469 instructions
    Total: 219507 bytes; 32982 instructions; 1721 jcc.
    Create extra code section at 0x0000A000. Size 343573 bytes.
    Assemble
    Reduce extra code section size: 343573 --> 211957 bytes
    Strip relocation info.
    Create TLS directory
    JCALG1 Compression Library. Copyright (C) Jeremy Collake.
    Compress code ...
    Compress resource ...
    Resource is incompressible.
    Done: 237557 --> 195838 bytes (82%)
    Decompressor size: 388 bytes
    Compile loader 4118 bytes; 1087 instructions; 62 jcc.
    Create backup
    Save
    Done.
  • Run the compiled program and enter the username/password created earlier to test it is working:
    Image
And that's it! You can see how the serial number and expiry date created above was read correctly by the app.

Now, whenever you change your code and want to protect it with EXECryptor simply select the settings you saved in PBCoffee and click compile. If you want to change serials or other EXECryptor settings, run EXECryptor and edit the project file as needed. There is no need to protect the exe during testing so you can skip that stage and compile it as a regular exe as normal (but of course it won't be protected then).

More details

In PBCoffee, the "Auto create mode def file" tick box will generate the following on the linker tab for the example file:

Code: Select all

EXPORTS
EXECryptor_MessageBox
EXECryptor_GetHardwareID
EXECryptor_EncryptStr
EXECryptor_DecryptStr
EXECryptor_SecureWrite
EXECryptor_SecureRead
EXECryptor_GetProcAddr
EXECryptor_VerifySerialNumber
EXECryptor_IsRegistered
EXECryptor_RegConst_0
EXECryptor_RegConst_1
EXECryptor_RegConst_2
EXECryptor_RegConst_3
EXECryptor_RegConst_4
EXECryptor_RegConst_5
EXECryptor_RegConst_6
EXECryptor_RegConst_7
This is a list of all EXECryptor API calls that are used in the main file. Only the main file is scanned by PBCoffee (included files are ignored, and no distinction is made between code and comments) so for more complicated projects untick "Auto create mode def file" and enter the mod def file text manually. In all cases the linker option FIXED must be set to No.

How the code works

The PureBasic code calls an include file that contains the EXECryptor procedures, macros and structures. The code consists of the following procedures:
  • ! CRYPT_START and ! CRYPT_END - Code in between the markers is protected. Do not nest code markers and do not jump out of the code.
  • ! CRYPT_REG and ! CRYPT_END - Code in between the markers only runs if the username/password is valid, other it is skipped.
  • ! CRYPT_UNREG and ! CRYPT_END - Code in between the markers only runs if the username/password is invalid, other it is skipped.
  • EXECryptor_MessageBox(hWnd.l, Text.s, Caption.s, Type.l) - This is like the standard MessageBox but has additional debugger protection and breakpoint checking.
  • EXECryptor_SafeGetDate(@DateDay.l, @DateMonth.l, @DateYear.l) - Gets the current date whilst avoiding date substitution tricks.
  • EXECryptor_GetHardwareID() - Returns a hardware id based on the hard drive. Remains the same unless hard drive is formatted or replaced. If the value is 0 the app has not been protected.
  • EXECryptor_EncryptStr(Source.s, @Dest.s) and EXECryptor_DecryptStr(Source.s, @Dest.s) - These procedures encrypt and decrypt a string, using a key based on the hardware id and the unique project key. Ensure that the string Dest is at least twice the size of the string Source.
  • EXECryptor_SecureWrite(Name.s, WriteValue.s)
    and EXECryptor_SecureRead(Name.s, @ReadValue.s) - Securely (encrypted) read and write values to and from the registry. Ensure ReadValue is big enough to hold the data.
  • EXECryptor_VerifySerialNumber(Username.s, Serial.s, Year.l, Month.l, SNInfo.l, HardwareID.s) - Check if the specified username/serial is valid. Pass the curren year, month and the address of a structure to receive information about the serial (license type, features, etc.). HardwareID is optional. Returns #vrInvalid if the username/serial is invalid, #vrExpired is the username/serial has expired (license type #1) and #vrOK if the username/serial is valid.
  • EXECryptor_IsRegistered() - Returns the value of the last EXECryptor_VerifySerialNumber call.
  • EXECryptor_RegConst_x() where x=0 to 7 - Returns the value x if username/serial is correct, otherwise the values are undefined.
  • EXECryptor_GetProcAddr(hModule.l, ProcName.s) - Similar to the WinAPI GetProcAddress function but with added protection.
The EXECryptor manual contains more details and the example .pb file shows them being used with PureBasic. Note that PureBasic isn't officially supported by EXECryptor and that i've only tested the demo (so not all features have been tested).

Edit: Updated links
Last edited by MrMat on Sun Jan 30, 2005 6:56 am, edited 1 time in total.
Mat
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Post by Armoured »

Hi MrMat.
I can't download "execryptor.pb" and "execryptor.pbi" from your link :(
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post by MrMat »

Sorry about that. I hadn't updated the links but it should be working now! If you have any problems let me know :)
Mat
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Post by Armoured »

Thanks :)
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Post by Armoured »

Hi
There are problems with the version 2.1.13 of EXECryptor.
The test between serial and user name is always wrong and the HardwareID is always zero. :cry:
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post by MrMat »

It sounds like the app isn't protected. Are you using PBCoffee? Can you post the EXECryptor output and we'll see whats happening :)
Mat
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Post by Armoured »

Ok

EXECryptor 2.1.13
Copyright (c) 2002-2005 SoftComplete Development
UNREGISTERED!
Found 3 marks.
WARNING: Relocation info not found.
Randomize: 3476115137, 389524774, 231748031, 3330814077
Mark at 0x00001C29: Size 432 bytes; 90 instructions; 3 jcc.
Mark at 0x00001DD9: Size 519 bytes; 113 instructions; 1 jcc.
Mark at 0x00001FE0: Size 36 bytes; 6 instructions; 0 jcc.
Process import ...
Compile EXECryptor API library 12412 bytes; 2855 instructions; 291 jcc.
Local transform: 8066 bytes; 2220 instructions; 62 jcc.
Global transform: 11957 bytes; 2954 instructions; 119 jcc.
Compile VM core 23130 bytes; 6327 instructions; 636 jcc.
Total: 47499 bytes; 12136 instructions; 1046 jcc.
Virtualization: 3698 instructions
Total: 79058 bytes; 15718 instructions; 910 jcc.
Create extra code section at 0x00008000. Size 209335 bytes.
Assemble
Reduce extra code section size: 209335 --> 78185 bytes
Strip relocation info.
Create TLS directory
JCALG1 Compression Library. Copyright (C) Jeremy Collake.
Compress code ...
Done: 97641 --> 70138 bytes (71%)
Decompressor size: 388 bytes
Compile loader 5868 bytes; 1464 instructions; 72 jcc.
Create backup
Save
Done.

P.S.
I don't use PBCoffee
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post by MrMat »

It might work in the "One-touch trial" mode (select it in configuration on the serial number support tab) but i don't know how to get the EXECryptor API to work unless you use PBCoffee (maybe it is possible but i don't know how). The output is missing the part:
Detect EXECryptor API usage: VerifySerialNumber, IsRegistered, RegConst_0, RegConst_1, RegConst_2, RegConst_3, RegConst_4, RegConst_5, RegConst_6, RegConst_7, GetHardwareID, EncryptStr, DecryptStr, SecureWrite, SecureRead, MessageBox, GetProcAddr
If that was present it would mean EXECryptor found the API calls but it cannot do that unless they are listed in the export table, which PBCoffee takes care of. Sorry!
Mat
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Post by Armoured »

Thanks Mat
I will try with your program.
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post by MrMat »

Ok. I hope you can get it working! Once it is working with PBCoffee you only need to use PBCoffee for the final compile when you want to protect your app, so during development you can compile it how you want (of course the EXECryptor API won't work as you saw earlier).
Mat
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Post by Armoured »

ok it works.
I have only a question I can insert the version info in a protected exe?


Thanks.
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post by MrMat »

Yes sure! In PBCoffee make sure "Link resources..." is ticked on the Resources tab and "Include in resources" is ticked on the Version info tab then enter the version info as you'd like it and compile. I need to write a decent guide for PBCoffee... :)
Mat
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Post by Armoured »

Hi MrMat.
Your program is wonderful!
Thanks.

P.S.
When your program start I have a warning message from windows.
It's normal?
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post by MrMat »

Thanks! What warning message do you get and which Windows are you using? There shouldn't be any message but i've only tested it on XP and it may still be a bit buggy.
Mat
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Post by Armoured »

Hi MrMat.
In the request I can read that it's impossible to verify the author of the software.


P.S.
I have windowsXP Pro
Post Reply