Using EXECryptor with PureBasic
Posted: Wed Jan 19, 2005 5:53 pm
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:
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:
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:
Edit: Updated links
- EXECryptor (can test using a demo)
- PBCoffee (frontend for PureBasic compiler that has some options we need, see this thread for more details on it)
- Example PureBasic file execryptor.pb and include file execryptor.pbi (thanks Beach for hosting)
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:
- 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):
- 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:
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
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.
Edit: Updated links