Protecting Python Applications Using Script Envelope

You can protect Python applications using the Script Envelope command-line tool (scriptenv.exe). This tool uses automated processes to apply advanced Sentinel LDK Envelope protection to a Python application.

NOTE   Script Envelope supports model file protection for Pytorch and TensorFlow models. For details, see Protecting Model Files for Python.

NOTE   Project files (.sprjx) are human-editable YAML files. While the legacy JSON format is still supported for backward compatibility, YAML is the preferred and recommended format.

You can perform the protection process under Windows (x64) or Linux (Intel). The protected application can run under Windows (x64) or Linux (Intel 64 or ARM 64). The corresponding runtime library needs to be present under output_root\sntlruntime. For information on using Script Envelope under Linux, see Sentinel LDK Envelope for Linux.

Using Script Envelope consists of the following steps:

1. Prepare a project file that contains all the required information for protecting your Python application, including the location of the source files and the target location for the protected files.

2.Run Script Envelope, passing the project file path as a parameter to the tool.

NOTE   You must connect a Sentinel Vendor key to the machine that you use to run Script Envelope.

Prepare the Project File

Perform the following steps on a machine where Sentinel LDK Vendor Tools are installed.

1.Create or select a project folder to contain the project file for the Python application to be protected.

2.Copy the project file %ProgramFiles(x86)%\Thales\Sentinel LDK\Samples\Envelope\Python\script_envelope\sample.sprjx (provided in the YAML format by default) to the project folder. Rename the new project file to a relevant name for your application.

3.Open your project file in a text editor.

4.Modify the parameters in your project file as described in the table below.

Property Description
"file_entries" Section of the Project File
input_root

Absolute path of the directory that contains the Python application to protect.

output_root

Absolute path of the directory where scriptenv.exe writes the protected Python application. You execute the application directly, with no additional manual steps.

entry_scripts_glob

(Optional) Parameter used to specify one or more entry scripts for your project.

The following terms are used in this section:

>Stub: A stub is a small piece of code generated to load the runtime libraries necessary to execute protected scripts. When a script file is protected, the stub is created as a separate file with the same name as the protected file.

>Prologue: When the script file is not protected, the stub code is inserted within the original script file itself and is called a prologue.

Script Envelope generates stub code to ensure that the runtime libraries load correctly. The behavior of stub generation depends on whether you specify the entry_scripts_glob parameter.

When entry_scripts_glob is specified:

>The tool generates runtime-loading stubs only for the listed entry scripts.

>The stub appears either as a prologue (if the entry script is not protected) or as a separate stub file (if the entry script is protected) with the same name as the entry script.

>If you run the protected application with a script that is not included in the entry_scripts_glob parameter, the runtime does not load and execution fails.

When entry_scripts_glob is not specified:

>The tool generates stub files for all scripts.

Usage Examples:

You can specify either a single entry script or multiple entry scripts depending on your project requirements. For example:

>Single Entry Script: Use this format when your project has only one entry script. The tool generates stub files only for the specified script.

entry_scripts_glob: main.py

>Multiple Entry Scripts: Use this format when your project has more than one entry script. The tool generates stub files only for each valid script you specify.

entry_scripts_glob:
  - file.py
  - file2.py

Considerations:

>To enable selective stub generation, entry_scripts_glob must include at least one valid .py file.

>Only files with the .py extension are valid in entry_scripts_glob. If any non-.py files are included, the system generates an error.

to_be_protected

Parameter that contains one or more sets of sub-parameters input_glob and (optionally) ignore_glob and feature_id (described later). The sub-parameters in each set identify a subset of files in the application to be protected with a specific Feature ID. For example:

to_be_protected:
  - input_glob: "*.*"
    ignore_glob: moduleC.py
    feature_id: 4
  - input_glob: "CD/*.py"
    feature_id: 14
  - input_glob:
      - "EF/*.py"
      - "GH/*.py"

The parameters in the example protect:

> All files in the root folder (excluding moduleC.py) with Feature ID 4.

> All Python files in the CD folder with Feature ID 14.

> All Python files in the EF and GH folders by default with the global_feature_id.

Script Envelope creates runtime stub modules for .py files. The tool handles all other files as data files for use at run time.

The following files are copied from the input_root location to the output_root location with no change:

>Files that are not included in any input_glob parameter.

>Files included in an input_glob parameter but excluded using the ignore_glob parameter.

To protect model files:

>To apply model file protection for TensorFlow, the to_be_protected parameter must include the TensorFlow model files. For example:

  - input_glob: model.keras
    ignore_glob: ""
    feature_id: 0

>To apply model file protection for Pytorch, the to_be_protected parameter must include the Pytorch model files. For example:

  - input_glob: model.pt
    ignore_glob: ""
    feature_id: 0
input_glob

Glob pattern that identifies the files under the input root to be protected by Envelope using the associated Feature ID (see feature_id). For example:

"**/*.py" Recursively protect all .py files under the root folder.
"ABC/*.py" Protect all Python files directly under the ABC folder.

You can specify multiple patterns. For example:

["EF/*.py", "GH/*.py"] Protect all .py files under the EF and GH folders.

For details, see https://docs.python.org/3/library/glob.html.

ignore_glob

(Optional) Files to exclude from the input glob. For example:

"input_glob": "**/*.py"
"ignore_glob": fileA.py

This example indicates that Script Envelope recursively protects all .py files under the root folder, excluding fileA.py.

feature_id

(Optional) Feature ID (in the range 0 - 65535) for the Python script (.py) files in the associated input_glob that you are protecting. If feature_id is not specified, the global Feature ID (see global_feature_id) is used.

"settings" Section of the Project File
vendor_code_file Absolute path of your vendor code file on the machine.
custom_scope

If provided, the protected program searches for a Sentinel protection key according to the custom login scope that you specify.

For information on the syntax for login scope parameters, see the topic "Scope Input XML Tags" in Sentinel Licensing API C Reference. You can also paste a login scope that was created using Sentinel LDK ToolBox in this field.

Note: In YAML, double quotes inside double-quoted strings must be escaped with a backslash (\"). Alternatively, you can enclose the entire string in single quotes to avoid escaping the inner double quotes entirely:

custom_scope: '<haspscope><hasp type="HASP-HL" /></haspscope>'
global_feature_id

(Optional) Global Feature ID (in the range 0 - 65535) for the complete application that you are protecting. This Feature ID is also applied by default for any input_glob (under to_be_protected) for which a corresponding feature_id is not specified.

Default: 0

background_check

(Optional) Specify a time interval (in seconds) of 0-65535 for periodic background checks for a Sentinel protection key with the required licenses. Default: 300 seconds.

If you specify a value of 0, a periodic background check is not performed.

Regardless of the setting for this parameter, the License Manager checks for the required license when the application is started.

message_output_mode

(Optional) Specifies how runtime user messages are delivered. Available output modes are:

>windows – If assigned the value true, displays messages in a message box.

>stderr – If assigned the value true, writes messages to the console.

Default: If no output modes are specified, windows mode is set to true and stderr mode is set to false.

message_sets

You can customize the run-time messages displayed during execution of the protected application. Sentinel LDK Envelope provides a comprehensive set of error codes, each mapped to a corresponding end-user message.

message_sets is an array, in which each message set contains the following parameters:

>language – The language identifier for each message set.

>caption – Text to appear in the title bar for the message box.

>messages – An array in which each element contains the following:

id – Predefined ID for each message.

text – Default text for each message. You can modify this text as required.

For details, see the provided sample project file.

5.Save the project file.

Run Script Envelope

Perform the following steps on the machine where your project file is located and Sentinel LDK Vendor Tools are installed.

1.Open a Windows command-line prompt.

2.In the command-line prompt, change to the following path:

%ProgramFiles(x86)%\Thales\Sentinel LDK\VendorTools\VendorSuite\

3.Enter the following command:

 scriptenv.exe projectFile [-q]

where:

projectFile

The relative or absolute path and name for the project file that you prepared earlier.

-q (or --quiet)

If specified, displays only error and warning messages.

Default: Verbose messages are displayed.

NOTE   You can enter scriptenv.exe -h or scriptenv.exe --help to display help for scriptenv.

Envelope protects the Python application that you specified in the input_root parameter and writes the application to the path that you specified in the output_root parameter.

Migrating Legacy JSON Project Files to YAML

The convert command allows you to convert your legacy JSON project file to YAML while preserving duplicate __Comment keys as standard YAML comments (#).

NOTE   The hyphen (-) in YAML represents array items. When converting JSON to YAML, map each left bracket ([) to a hyphen and omit hyphens for properties inside the item.

1.Open a Windows command-line prompt.

2.Change to the directory containing scriptenv.

3.Run either of the following commands:

scriptenv.exe convert input.sprjx output.sprjx

or

scriptenv.exe --convert input.sprjx output.sprjx

where input.sprjx is your legacy JSON project file, and output.sprjx is the name of the new YAML-formatted project file to be created.

NOTE   The tool only processes and validates files with the .sprjx extension.

Syntax Error Handling and Validation

Script Envelope validates the project file and displays syntax suggestions if validation fails.

>YAML files: The tool checks for correct indentation with spaces.

>Legacy JSON files: The tool checks for missing commas, trailing commas, and unescaped quotes.

Related Topics

Protecting Python Applications

Protecting Python Applications With Cython

Protecting Model Files for Python