Code protection method and device, equipment, medium and product
By distinguishing encrypted and non-encrypted codes in Python source code files and using target objects to check file integrity, the compatibility issues caused by all encryption are solved, and more efficient code protection is achieved.
Patent Information
- Application Number
- CN202411854294.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-16
- Publication Date
- 2025-04-22
AI Technical Summary
Encrypting all Python source code files may cause compatibility issues and affect the code protection effect.
By distinguishing encrypted code from non-encrypted code in the target file and using the target object to check the integrity of the target file, ensuring that the non-encrypted code has not been tampered with, thereby improving the code protection effect.
It avoids compatibility issues caused by all encryption, ensures data security, and improves code protection effect.
Smart Images

Figure CN119862592A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, specifically to technical fields such as data security, content compliance, and smart cloud, and in particular to a code protection method, device, equipment, medium, and product. Background Art
[0002] When Python programs are deployed, they usually contain core code. When Python code is delivered as a product, in order to protect the intellectual property rights of the core code and prevent the core code from being cracked, the Python source code files, that is, the py files, are usually fully encrypted. However, there are some limitations to fully encrypting source code files, for example, it may cause compatibility issues. Summary of the invention
[0003] The present disclosure provides a code protection method, apparatus, device, medium and product.
[0004] According to one aspect of the present disclosure, a code protection method is provided, comprising: loading a target object; the target object is used to check the integrity of a target file, the target file comprising: a source code file, the source code file comprising: encrypted code and non-encrypted code; using the target object to check the integrity of the target file; in response to the target file passing the integrity check, running the target file.
[0005] According to another aspect of the present disclosure, a code protection device is provided, comprising: a loading module, used to load a target object; the target object is used to check the integrity of a target file, the target file comprising: a source code file, the source code file comprising: encrypted code and non-encrypted code; a checking module, used to use the target object to check the integrity of the target file; and a running module, used to run the target file in response to the target file passing the integrity check.
[0006] According to another aspect of the present disclosure, an electronic device is provided, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute any method as described in any of the above aspects.
[0007] According to another aspect of the present disclosure, a non-transitory computer-readable storage medium storing computer instructions is provided, wherein the computer instructions are used to enable the computer to execute any one of the methods according to any one of the above aspects.
[0008] According to another aspect of the present disclosure, a computer program product is provided, including a computer program, wherein when the computer program is executed by a processor, the computer program implements any one of the methods described in any one of the above aspects.
[0009] According to the embodiments of the present disclosure, the code protection effect can be improved.
[0010] It should be understood that the content described in this section is not intended to identify the key or important features of the embodiments of the present disclosure, nor is it intended to limit the scope of the present disclosure. Other features of the present disclosure will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0011] The accompanying drawings are used to better understand the present solution and do not constitute a limitation of the present disclosure.
[0012] Figure 1 is a schematic diagram according to a first embodiment of the present disclosure;
[0013] Figure 2 is a schematic diagram of an application scenario for implementing an embodiment of the present disclosure;
[0014] Figure 3 is a schematic diagram according to a second embodiment of the present disclosure;
[0015] Figure 4 is a schematic diagram of another application scenario for implementing the embodiments of the present disclosure;
[0016] Figure 5 is a schematic diagram according to a third embodiment of the present disclosure;
[0017] Figure 6 is a schematic diagram according to a fourth embodiment of the present disclosure;
[0018] Figure 7 It is a schematic diagram of an electronic device used to implement the code protection method of the embodiment of the present disclosure. DETAILED DESCRIPTION
[0019] The following is a description of exemplary embodiments of the present disclosure in conjunction with the accompanying drawings, including various details of the embodiments of the present disclosure to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present disclosure. Similarly, for the sake of clarity and conciseness, descriptions of well-known functions and structures are omitted in the following description.
[0020] In the related art, source code files are usually completely encrypted, which may cause compatibility issues and affect the code protection effect.
[0021] In order to improve the code protection effect, the present disclosure provides the following embodiments.
[0022] Figure 1 is a schematic diagram according to the first embodiment of the present disclosure. This embodiment provides a code protection method, such as Figure 1 As shown, the method includes:
[0023] 101. Load a target object; the target object is used to check the integrity of a target file, the target file includes a source code element, and the source code file includes encrypted code and non-encrypted code.
[0024] 102. Use the target object to check the integrity of the target file.
[0025] 103. In response to the target file passing the integrity check, execute the target file.
[0026] The target file refers to the file to be protected.
[0027] The target file includes a source code file. Taking Python language as an example, the source code file refers to a Python source code file, that is, a .py file.
[0028] The source code file usually contains core code. In order to protect the intellectual property rights of the core code, the core code can be encrypted. The encrypted core code is called encrypted code.
[0029] Encrypting the entire source code file may cause compatibility issues.
[0030] For this reason, in this embodiment, non-core codes may not be encrypted, so that the source code file includes: encrypted codes obtained by encrypting the core codes, and non-encrypted codes other than the core codes.
[0031] Although non-core codes do not need to be encrypted, in order to ensure data security, it is necessary to ensure that this part of the code has not been tampered with, that is, it is necessary to ensure that this part of the code is complete.
[0032] In this embodiment, the integrity of the target file is checked based on the target object.
[0033] Taking Python language as an example, the target object can be a custom importer integrated in the Python interpreter (custom importer); or, the target object can also be a custom shared library, represented by libpyenc.so.
[0034] The Python interpreter is used to parse Python code into machine language that the computer can understand and execute. The importer in the Python interpreter is used to import modules or packages. A module is a file containing Python code. The file name is the module name, and a package is a directory containing multiple modules.
[0035] The usual Python interpreter can be called the original Python interpreter. In order to check the integrity of the file, the original Python interpreter can be modified and a custom importer can be integrated into the original Python interpreter. The custom importer is used to check the integrity of the Python file. The modified Python interpreter, that is, the Python interpreter with the custom importer integrated can be called the target Python interpreter, denoted by pyenc.
[0036] In the scenario where a custom importer is used for file integrity checking, the target Python interpreter (pyenc) is used to load the custom importer, and the integrity of the target file is checked using the custom importer.
[0037] When using a shared library to check file integrity, there is no need to modify the Python interpreter. In this case, the original Python interpreter can be used to load the shared library. Specifically, the original Python interpreter can be used to load sitecustormize.py, and then sitecustormize.py can be used to load the shared library libpyenc.so, and the shared library can be used to check the integrity of the target file. sitecustomize.py is a special file in Python. It is a script that can be automatically executed when Python starts. It is used to customize Python's site configuration to initialize the Python environment.
[0038] When the target file passes the integrity check, that is, when the target file has not been tampered with, the target file is executed. Otherwise, when the target file fails the integrity check, the target file is not executed.
[0039] In this embodiment, the source code file includes encrypted code and non-encrypted code, which can avoid compatibility issues caused by full encryption; integrity checks can ensure that the target file has not been tampered with, ensuring data security. Since data security can be guaranteed and compatibility issues can be resolved, the code protection effect can be improved.
[0040] In order to better understand the present disclosure, the application scenarios involved in the present disclosure are described as follows:
[0041] Figure 2It is a schematic diagram of an application scenario for implementing the embodiment of the present disclosure.
[0042] like Figure 2 As shown, a target Python interpreter can be installed on an electronic device. The target Python interpreter is obtained by integrating a custom importer (custom importer) into the original Python interpreter. The custom importer is used to check the integrity of the target file. The electronic device can be a local terminal used by a user, such as a personal computer (PC), a laptop, a mobile device (such as a mobile phone), etc. The electronic device can also be a server accessed remotely by a user.
[0043] In this scenario, after the target Python interpreter is started, the custom importer integrated in it can be loaded, and the custom importer can be used to perform an integrity check on the target file. If the target file passes the integrity check, the target file is run, otherwise it is not run.
[0044] The target file includes a source code file, and the source code file includes encrypted code and non-encrypted code, that is, the core code can be encrypted as the encrypted code, and the non-core code is not encrypted and is used as the non-encrypted code.
[0045] In some cases, the core code may use library files in dependent libraries. Library files in dependent libraries are usually written in C / C++. Due to the different writing languages, they are not suitable for Python code encryption.
[0046] These library files do not need to be encrypted, but in order to ensure security, they need to be protected from tampering, that is, integrity checks need to be performed on these library files.
[0047] To this end, the target file may also include library files used by the core code (encrypted code), which are non-encrypted files and may be library files in a dependent library and / or a standard library.
[0048] The standard library is a library that comes with the Python language and can be used without additional installation. Dependent libraries are libraries created by third-party developers. These libraries are not part of the Python standard library and require additional installation steps to be used in the Python environment. They are usually used to extend the functionality of Python.
[0049] For library files in the standard library, you can use the custom importer to perform integrity checks after loading the custom importer, similar to source code files.
[0050] For library files in dependent libraries, you can use the custom importer to perform integrity checks after loading the custom importer, similar to source code files. In addition, some special packages, such as encoding-related packages, will be loaded in advance before loading the importer. Therefore, after loading the custom importer, you can check the integrity of the packages already in the system module (sys.modules) to avoid attacks.
[0051] In this embodiment, there is no need to encrypt the library files used by the core code, and the security of the overall program product is improved by checking the integrity of the library files.
[0052] For the integrity check of the target file, after the user writes or uses the target file, the user can also obtain a hash value of the target file, which can be called a standard hash value. The standard hash value is recorded in a benchmark file and delivered together with the target file as a product.
[0053] During the integrity check, the custom importer can calculate the hash value of the target file, which can be called the actual hash value, and compare the actual hash value with the standard hash value. If the two are the same, the integrity check passes, otherwise it fails.
[0054] The above hash value may specifically be a sha256 value. sha256 (Secure Hash Algorithm 256bit) is a cryptographic hash function that converts data of any length into a 256-bit (32-byte) hash value.
[0055] The benchmark file can be represented by pyenc.py, which records the hash value of the target file, and the target file can be one or more.
[0056] Furthermore, in order to improve security, the benchmark file may be encrypted to prevent the pyenc.py file from being modified by an attacker. The encrypted benchmark file is represented by pyenc.pyxes.
[0057] Taking the target file represented by mod.py as an example, the main directory includes the main program (main.py) and the encrypted benchmark file pyenc.pyxes. The main program main.py is used to import the target file mod.py.
[0058] Based on this, when the custom importer performs an integrity check, pyenc.pyxes can be decrypted to obtain the standard hash value of the target file in the decrypted file, and the actual hash value of the target file is calculated. When the two are the same, it is determined that the target file has passed the integrity check. After that, the main program is run, the main program imports the target file, and the target file runs normally. Otherwise, the main program is not run and the corresponding target file cannot run.
[0059] In combination with the above application scenarios, the present disclosure also provides the following embodiments.
[0060] Figure 3 is a schematic diagram according to the second embodiment of the present disclosure, and this embodiment provides a code protection method. Figure 2 Correspondingly, take the integrity check based on the custom importer (custom importer) as an example.
[0061] like Figure 3 As shown, the method includes:
[0062] 301. After the target Python interpreter is started, a benchmark file is loaded, in which the standard hash value of the target file is recorded.
[0063] The target file includes a source code file, and the source code file includes an encrypted code and a non-encrypted code. The encrypted code is obtained by encrypting the core code, which can avoid compatibility issues caused by full encryption.
[0064] In addition, the target file may also include library files used by the core code, and the library files are non-encrypted files.
[0065] 302. Determine whether the basic file passes the integrity check. If so, execute 303; otherwise, execute 306.
[0066] Among them, the base file is the file loaded before the target file. These files can be loaded into the system module (sys.modules). To this end, the base file can be obtained from the system module and the actual hash value of the base file can be calculated. In addition, the standard hash value of the base file can also be recorded in the benchmark file. When the actual hash value of the base file is the same as the standard hash value, it is determined that the base file passes the integrity check, otherwise it fails.
[0067] The above-mentioned basic files may include some files in the standard library, such as encoding-related files. By performing an integrity check on the basic files, tampering with the library files of the standard library can be avoided.
[0068] 303. Load the custom importer integrated in the target Python interpreter.
[0069] In this embodiment, by performing an integrity check on the basic file before loading the custom importer, the security of the basic file can be guaranteed, thereby improving the code protection effect.
[0070] According to the Python mechanism, when a Python program tries to import module A, the Python interpreter will try to import module A using the Importer in sys.meta_path in turn.
[0071] To this end, when the target Python interpreter starts, the custom importer can be generated and added to the system metadata path (sys.meta_path). In this way, when the code that imports A is executed, the custom importer will be loaded.
[0072] 304. Using the custom importer, determine whether the target file passes the integrity check based on the benchmark file. If so, execute 305; otherwise, execute 306.
[0073] Among them, the Python standard library provides an application programming interface (Application Programming Interface, API), which can find the file corresponding to the module by the module name. Therefore, when executing the import module operation, the corresponding file can be obtained based on the API.
[0074] When the file is a target file (such as the corresponding file identifier recorded in the benchmark file), the custom importer can calculate the actual hash value of the target file and compare it with the standard hash value in the benchmark file. If they are the same, it indicates that the integrity check has passed; if they are different, it indicates that the integrity check has failed.
[0075] In addition, the file identifier (such as the file name) and the standard hash value can be recorded in the benchmark file accordingly, so that the corresponding standard hash value can be found through the file identifier, and then the standard hash value and the actual hash value of the same file can be compared to see if they are the same.
[0076] In this embodiment, the integrity result is obtained by comparing the actual hash value with the standard hash value, so that the file integrity check can be performed simply and efficiently.
[0077] 305. Run the target file.
[0078] For example, if the custom importer determines that the target file is complete, the main program is run, and the target file is run through the main program.
[0079] 306. Termination.
[0080] If the base file or the target file is incomplete, the program execution process is exited.
[0081] In addition, the custom importer can also import other files besides the target file. For example, other files are represented by file A. At this time, if the relevant information of file A is not recorded in the benchmark file, then the relevant information of file A cannot be found in the benchmark file, such as the file name. This indicates that file A is not a file that needs to be protected, and the module corresponding to file A can be directly loaded to run file A.
[0082] In this embodiment, the integrity check of the target file is performed by the custom importer integrated in the target interpreter, which can improve the code security and solve the compatibility problem, so that the code protection effect can be improved. In addition, there is no need to encrypt the library files used by the core code, and the security of the overall program product is improved by checking the integrity of the library files.
[0083] The above takes checking the integrity of the target file for protection as an example, and other protection measures can also be implemented.
[0084] In some embodiments, the target object is a custom importer integrated into a target interpreter;
[0085] The method may further comprise one or more of the following:
[0086] In response to the target interpreter satisfying a preset condition, setting a target function of the target interpreter to a preset value to check the integrity of a bytecode file corresponding to the source code file;
[0087] In response to the actual environment variable value in the target interpreter being different from the preset environment variable value, terminating the execution process of the target interpreter;
[0088] In response to using an interpreter other than the target interpreter to run the target file, preventing the encrypted code from running;
[0089] In response to the reference file being deleted or replaced, the target interpreter is prevented from running; the reference file records the standard hash value of the target file, and the standard hash value is used for the integrity check.
[0090] Specifically, the target interpreter meets the preset condition, which can be that the version number of the target interpreter is higher than the preset version number, such as higher than version 3.8, and different versions of the target interpreter have different functions. When this condition is met, the target function (check_hash_based_pyc) can be set to the preset value (always), so that the file hash value check specified by PEP552 (an extension to Python's .pyc bytecode file format) can be enabled.
[0091] In this way, by setting the target function of the target interpreter to a preset value, the integrity of the bytecode file can be checked, tampering with the bytecode file can be avoided, and data security can be further improved.
[0092] One of the options of Python interpreter is environment variable (PYTHONPATH), and the value pointing to PYTHONPATH is recorded in the benchmark file. Based on this, when the target interpreter is running, the actual environment variable value can be obtained through this value, and the target interpreter also presets the environment variable value. If the two are different, the execution process of the target interpreter is terminated, that is, the program execution process is exited.
[0093] In this way, by comparing the actual environment variable value with the preset environment variable value, you can exit in time when the environment variable is tampered with to ensure data security.
[0094] When the target interpreter is not used, that is, when an interpreter other than the target interpreter is used to run the target file, the encrypted code can be prevented from running by means of preset codes and the like.
[0095] In this way, bypassing behavior that does not use the target interpreter can be avoided, improving data security.
[0096] The target interpreter can be prevented from running after the benchmark file is deleted or replaced. The benchmark file is the file used to record the standard hash value mentioned above.
[0097] In this way, the circumvention behavior of deleting or replacing the baseline file can be avoided, improving data security.
[0098] To this end, the above-mentioned means can be combined to achieve comprehensive code protection.
[0099] Figure 4 It is a schematic diagram of another application scenario for implementing the embodiment of the present disclosure.
[0100] like Figure 4 As shown, the original Python interpreter can be installed on the electronic device, and the original Python interpreter is a commonly used Python interpreter. The electronic device can be a local terminal used by the user, such as a personal computer (PC), a laptop, a mobile device (such as a mobile phone), etc. The electronic device can also be a server accessed remotely by the user.
[0101] In this scenario, after the original Python interpreter is started, sitecustormize.py can be loaded, and then sitecustormize.py can be used to load the custom shared library libpyenc.so. The custom shared library is used to perform an integrity check on the target file. If the target file passes the integrity check, the target file is run, otherwise it is not run.
[0102] The target file includes a source code file, and the source code file includes encrypted code and non-encrypted code, that is, the core code can be encrypted as the encrypted code, and the non-core code is not encrypted and is used as the non-encrypted code.
[0103] In some cases, the core code may use library files in dependent libraries. Library files are usually written in C / C++. Due to the different writing languages, they are not suitable for Python code encryption.
[0104] These library files do not need to be encrypted, but in order to ensure security, they need to be protected from tampering, that is, integrity checks need to be performed on these library files.
[0105] To this end, the target file can also include library files used by the core code. These library files can be library files in the dependent library or library files in the standard library. These library files are non-encrypted files. The standard library is a library that comes with the Python language and can be used without additional installation. Dependent libraries are libraries created by third-party developers. These libraries are not part of the Python standard library and require additional installation steps to be used in the Python environment. They are usually used to extend the functionality of Python.
[0106] For these library files, you can use shared libraries to perform integrity checks similar to source code files.
[0107] For the integrity check of the target file, after the user writes and obtains the target file, he can also obtain the hash value of the target file, such as the sha256 value. The hash value can be called the standard hash value, and the standard hash value is recorded in the benchmark file and delivered as a product together with the target file.
[0108] During the integrity check, the shared library can calculate the hash value of the target file, which can be called the actual hash value, and compare the actual hash value with the standard hash value. If the two are the same, the integrity check passes, otherwise it fails.
[0109] The above hash value may specifically be a sha256 value, where sha256 (Secure Hash Algorithm 256bit) is a cryptographic hash function that converts data of any length into a 256-bit (32-byte) hash value.
[0110] The benchmark file can be represented by pyenc.py, which records the hash value of the target file, and the target file can be one or more.
[0111] Furthermore, in order to improve security, the benchmark file can also be encrypted to prevent the _pyenc.py file from being modified by an attacker. The encrypted benchmark file is represented by pyenc.pyxes.
[0112] Taking the target file represented by mod.py as an example, the main directory includes a main program (main.py), and the main program main.py is used to import the target file mod.py.
[0113] pyenc.pyxes and the shared library can be placed in the same directory, and the value of the PYTHONPATH environment variable is set to this directory, where site_custormize.py, libpyenc.so, and pyenc.pyxes are stored.
[0114] After the target file passes the integrity check, the main program is run, the main program imports the target file, and the target file runs normally. Otherwise, the main program is not run and the corresponding target file cannot run either.
[0115] In combination with the above application scenarios, the present disclosure also provides the following embodiments.
[0116] Figure 5 is a schematic diagram according to the third embodiment of the present disclosure, and this embodiment provides a code protection method. Figure 4 Correspondingly, take the integrity check based on the custom shared library as an example.
[0117] like Figure 5 As shown, the method includes:
[0118] 501. After the original Python interpreter is started, the site configuration file (sitecustormize.py) is loaded.
[0119] 502. Load a custom shared library (libpyenc.so) through the site configuration file.
[0120] Among them, libpyenc.so is a library independent of the Python interpreter and is used to check the integrity of the target file.
[0121] 503. Use the shared library to determine whether the target file passes the integrity check. If so, execute 504; otherwise, execute 505.
[0122] Among them, the shared library can obtain the benchmark file in its own directory, and the benchmark file records the standard hash value of the target file. The shared library can also calculate the actual hash value of the target file and compare it with the standard hash value in the benchmark file. If they are the same, it indicates that the integrity check has passed. If they are different, it indicates that the integrity check has failed.
[0123] In addition, the file identifier (such as the file name) and the standard hash value can be recorded in the benchmark file accordingly, so that the corresponding standard hash value can be found through the file identifier, and then the standard hash value and the actual hash value of the same file can be compared to see if they are the same.
[0124] In this embodiment, the integrity result is obtained by comparing the actual hash value with the standard hash value, so that the file integrity check can be performed simply and efficiently.
[0125] 504. Run the target file.
[0126] 505. Termination.
[0127] Among them, after the shared library obtains the integrity result of the target file, it can feed back the result to the original Python interpreter. When the target file is complete, the original Python interpreter runs the main program and runs the target file through the main program, otherwise it exits the program execution process.
[0128] In addition, similar to the previous embodiment, for files not recorded in the reference file, these files can be directly executed.
[0129] In this embodiment, by performing integrity check on the target file through a shared library, the code security can be protected and the compatibility problem can be solved without modifying the interpreter, so the code protection effect can be improved. In addition, there is no need to encrypt the library files used by the core code, and the security of the overall program product can be improved by checking the integrity of the library files.
[0130] Figure 6 is a schematic diagram according to the fourth embodiment of the present disclosure, which provides a code protection device. Figure 6 As shown, the device 600 includes: a loading module 601, a checking module 602 and an operating module 603.
[0131] The loading module 601 is used to load the target object; the target object is used to check the integrity of the target file, and the target file includes: a source code file, and the source code file includes: encrypted code and non-encrypted code; the checking module 602 is used to use the target object to check the integrity of the target file; the running module 603 is used to run the target file in response to the target file passing the integrity check.
[0132] In this embodiment, the source code file includes encrypted code and non-encrypted code, which can avoid compatibility issues caused by full encryption; integrity checks can ensure that the target file has not been tampered with, ensuring data security. Since data security can be guaranteed and compatibility issues can be resolved, the code protection effect can be improved.
[0133] In some embodiments, the loading module 601 is further used to:
[0134] After the target interpreter is started, the custom importer integrated in the target interpreter is loaded as the target object; or,
[0135] After the original interpreter is started, the custom shared library is loaded as the target object.
[0136] In this embodiment, the integrity check of the target file is performed by a custom importer integrated in the target interpreter, which can improve code security and solve compatibility issues, thereby improving the code protection effect.
[0137] In this embodiment, by performing an integrity check on the target file through a shared library, the code security can be protected and compatibility issues can be resolved without modifying the interpreter, thereby improving the code protection effect.
[0138] In some embodiments, the loading module 601 is further used to:
[0139] After the target interpreter is started, an integrity check is performed on the base file loaded before the target file;
[0140] In response to the base file passing the integrity check, loading the custom importer.
[0141] In this embodiment, by performing an integrity check on the basic file before loading the custom importer, the security of the basic file can be guaranteed, thereby improving the code protection effect.
[0142] In some embodiments, the checking module 602 is further configured to:
[0143] Obtaining a preset standard hash value of the target object;
[0144] Calculating an actual hash value of the target file using the target object;
[0145] If the actual hash value is the same as the standard hash value, it is determined that the target file passes the integrity check.
[0146] In this embodiment, the integrity result is obtained by comparing the actual hash value with the standard hash value, so that the file integrity check can be performed simply and efficiently.
[0147] In some embodiments, the target file further includes:
[0148] The library file used by the encryption code, and the library file is a non-encrypted file.
[0149] In this embodiment, there is no need to encrypt the library files used by the core code, and the security of the overall program product is improved by checking the integrity of the library files.
[0150] In some embodiments, the target object is a custom importer integrated into a target interpreter;
[0151] The apparatus 600 further includes: a processing module, wherein the processing module is configured to perform at least one of the following items:
[0152] In response to the target interpreter satisfying a preset condition, setting a target function of the target interpreter to a preset value to check the integrity of a bytecode file corresponding to the source code file;
[0153] In response to the actual environment variable value in the target interpreter being different from the preset environment variable value, terminating the execution process of the target interpreter;
[0154] In response to using an interpreter other than the target interpreter to run the target file, preventing the encrypted code from running;
[0155] In response to the reference file being deleted or replaced, the target interpreter is prevented from running; the reference file records the standard hash value of the target file, and the standard hash value is used for the integrity check.
[0156] In this embodiment, the above-mentioned means are combined to achieve comprehensive code protection.
[0157] It can be understood that in the embodiments of the present disclosure, the same or similar contents in different embodiments can be referenced to each other.
[0158] It can be understood that the “first”, “second”, etc. in the embodiments of the present disclosure are only used for distinction and do not indicate the degree of importance, time sequence, etc.
[0159] It is understandable that unless there is any special limitation on the sequence of steps in the process, it means that the timing relationship between these steps is not limited.
[0160] In the technical solution of the present disclosure, the collection, storage, use, processing, transmission, provision and disclosure of user personal information involved are in compliance with the provisions of relevant laws and regulations and do not violate public order and good morals.
[0161] According to an embodiment of the present disclosure, the present disclosure also provides an electronic device, a readable storage medium and a computer program product.
[0162] Figure 7A schematic block diagram of an example electronic device 700 that can be used to implement an embodiment of the present disclosure is shown. The electronic device 700 is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital assistants, cellular phones, smart phones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the present disclosure described and / or required herein.
[0163] like Figure 7 As shown, the electronic device 700 includes a computing unit 701, which can perform various appropriate actions and processes according to a computer program stored in a read-only memory (ROM) 702 or a computer program loaded from a storage unit 708 into a random access memory (RAM) 703. In the RAM 703, various programs and data required for the operation of the electronic device 700 can also be stored. The computing unit 701, the ROM 702, and the RAM 703 are connected to each other via a bus 704. An input / output (I / O) interface 705 is also connected to the bus 704.
[0164] Multiple components in the electronic device 700 are connected to the I / O interface 705, including: an input unit 706, such as a keyboard, a mouse, etc.; an output unit 707, such as various types of displays, speakers, etc.; a storage unit 708, such as a disk, an optical disk, etc.; and a communication unit 709, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 709 allows the electronic device 700 to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks.
[0165] The computing unit 701 may be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the computing unit 701 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various dedicated artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, digital signal processors (DSPs), and any appropriate processors, controllers, microcontrollers, etc. The computing unit 701 performs the various methods and processes described above, such as the code protection method. For example, in some embodiments, the code protection method may be implemented as a computer software program, which is tangibly contained in a machine-readable medium, such as a storage unit 708. In some embodiments, part or all of the computer program may be loaded and / or installed on the electronic device 700 via ROM 702 and / or communication unit 709. When the computer program is loaded into RAM 703 and executed by the computing unit 701, one or more steps of the code protection method described above may be performed. Alternatively, in other embodiments, the computing unit 701 may be configured to perform the code protection method in any other appropriate manner (e.g., by means of firmware).
[0166] Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.
[0167] The program code for implementing the method of the present disclosure may be written in any combination of one or more programming languages. These program codes may be provided to a processor or controller of a general-purpose computer, a special-purpose computer, or other programmable task processing device, so that the program code, when executed by the processor or controller, implements the functions / operations specified in the flow chart and / or block diagram. The program code may be executed entirely on the machine, partially on the machine, partially on the machine and partially on a remote machine as a stand-alone software package, or entirely on a remote machine or server.
[0168] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0169] To provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user can provide input to the computer. Other types of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).
[0170] The systems and techniques described herein may be implemented in a computing system that includes back-end components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes front-end components (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such back-end components, middleware components, or front-end components. The components of the system may be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), and the Internet.
[0171] A computer system may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication network. The relationship between the client and the server is generated by computer programs running on the corresponding computers and having a client-server relationship with each other. The server may be a cloud server, also known as a cloud computing server or cloud host, which is a host product in the cloud computing service system to solve the defects of difficult management and weak business scalability in traditional physical hosts and VPS services ("Virtual Private Server", or "VPS" for short). The server may also be a server of a distributed system, or a server combined with a blockchain.
[0172] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps recorded in this disclosure can be executed in parallel, sequentially or in different orders, as long as the desired results of the technical solutions disclosed in this disclosure can be achieved, and this document does not limit this.
[0173] The above specific implementations do not constitute a limitation on the protection scope of the present disclosure. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present disclosure shall be included in the protection scope of the present disclosure.
Claims
1. A code protection method, comprising: Load the target object; The target object is used to check the integrity of the target file, the target file includes: a source code file, the source code file includes: encrypted code and non-encrypted code; Using the target object to check the integrity of the target file; In response to the target file passing the integrity check, the target file is executed.
2. The method according to claim 1, wherein: The loading target object includes: After the target interpreter is started, the custom importer integrated in the target interpreter is loaded as the target object; or, After the original interpreter is started, the custom shared library is loaded as the target object.
3. The method according to claim 2, wherein: After the target interpreter is started, loading the custom importer integrated in the target interpreter includes: After the target interpreter is started, an integrity check is performed on the base file loaded before the target file; In response to the base file passing the integrity check, loading the custom importer.
4. The method according to claim 1, wherein: The using the target object to check the integrity of the target file includes: Obtaining a preset standard hash value of the target object; Calculating an actual hash value of the target file using the target object; If the actual hash value is the same as the standard hash value, it is determined that the target file passes the integrity check.
5. The method according to claim 1, wherein: The target file also includes: The library file used by the encryption code, and the library file is a non-encrypted file.
6. The method according to claim 1, wherein: The target object is a custom importer integrated into the target interpreter; The method further comprises at least one of the following: In response to the target interpreter satisfying a preset condition, setting a target function of the target interpreter to a preset value to check the integrity of a bytecode file corresponding to the source code file; In response to the actual environment variable value in the target interpreter being different from the preset environment variable value, terminating the execution process of the target interpreter; In response to using an interpreter other than the target interpreter to run the target file, preventing the encrypted code from running; In response to the reference file being deleted or replaced, the target interpreter is prevented from running; the reference file records the standard hash value of the target file, and the standard hash value is used for the integrity check.
7. A code protection device, comprising: Loading module, used to load the target object; The target object is used to check the integrity of the target file, the target file includes: a source code file, the source code file includes: encrypted code and non-encrypted code; A checking module, used for checking the integrity of the target file using the target object; The running module is used for running the target file in response to the target file passing the integrity check.
8. The device according to claim 7, wherein: The loading module is further used for: After the target interpreter is started, the custom importer integrated in the target interpreter is loaded as the target object; or, After the original interpreter is started, the custom shared library is loaded as the target object.
9. The device according to claim 8, wherein: The loading module is further used for: After the target interpreter is started, an integrity check is performed on the base file loaded before the target file; In response to the base file passing the integrity check, loading the custom importer.
10. The device according to claim 7, wherein: The inspection module is further used for: Obtaining a preset standard hash value of the target object; Calculating an actual hash value of the target file using the target object; If the actual hash value is the same as the standard hash value, it is determined that the target file passes the integrity check.
11. The device according to claim 7, wherein: The target file also includes: The library file used by the encryption code, and the library file is a non-encrypted file.
12. The device according to claim 7, wherein: The target object is a custom importer integrated into the target interpreter; The apparatus further includes: a processing module, the processing module being configured to perform at least one of the following: In response to the target interpreter satisfying a preset condition, setting a target function of the target interpreter to a preset value to check the integrity of a bytecode file corresponding to the source code file; In response to the actual environment variable value in the target interpreter being different from the preset environment variable value, terminating the execution process of the target interpreter; In response to using an interpreter other than the target interpreter to run the target file, preventing the encrypted code from running; In response to the reference file being deleted or replaced, the target interpreter is prevented from running; the reference file records the standard hash value of the target file, and the standard hash value is used for the integrity check.
13. An electronic device comprising: at least one processor; as well as a memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the method according to any one of claims 1 to 6.
14. A non-transitory computer-readable storage medium storing computer instructions, wherein: The computer instructions are used to cause the computer to execute the method according to any one of claims 1-6.
15. A computer program product, comprising a computer program, which, when executed by a processor, implements the method according to any one of claims 1 to 6.