CN118916852A - Code encryption method, device, program product and electronic equipment - Google Patents
Code encryption method, device, program product and electronic equipment Download PDFInfo
- Publication number
- CN118916852A CN118916852A CN202410961915.2A CN202410961915A CN118916852A CN 118916852 A CN118916852 A CN 118916852A CN 202410961915 A CN202410961915 A CN 202410961915A CN 118916852 A CN118916852 A CN 118916852A
- Authority
- CN
- China
- Prior art keywords
- code
- encryption
- instruction
- keys
- key
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F21/00—Security arrangements for protecting computers, components thereof, programs or data against unauthorised activity
- G06F21/10—Protecting distributed programs or content, e.g. vending or licensing of copyrighted material ; Digital rights management [DRM]
- G06F21/12—Protecting executable software
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F21/00—Security arrangements for protecting computers, components thereof, programs or data against unauthorised activity
- G06F21/60—Protecting data
- G06F21/602—Providing cryptographic facilities or services
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Software Systems (AREA)
- Computer Hardware Design (AREA)
- Computer Security & Cryptography (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- General Health & Medical Sciences (AREA)
- Health & Medical Sciences (AREA)
- Bioethics (AREA)
- Multimedia (AREA)
- Technology Law (AREA)
- Storage Device Security (AREA)
Abstract
Description
技术领域Technical Field
本申请涉及软件安全技术领域,具体而言,涉及一种代码加密方法、装置、程序产品以及电子设备。The present application relates to the field of software security technology, and in particular to a code encryption method, device, program product, and electronic device.
背景技术Background Art
Python是目前软件开发技术领域流行的开发语言之一,Python是一种解释型语言,其代码在运行时才会被解释器执行,这使得Python代码难以像C、C++等编译型语言编写的代码那样能够通过编译器进行保护,并且,Python具有动态类型、动态绑定、动态加载等特性,这些Python特性使得Python代码在运行时能够被动态地修改、添加和删除,攻击者能够利用Python的这些特性来动态获取代码的运行逻辑,另外,Python的开源特点还能够让攻击者根据其源代码来对Python代码进行破解、修改和复制,例如,攻击者能够根据Python解释器的代码逻辑将Python解释器中的Python字节码反向恢复为Python源代码,从而造成现有技术中的Python代码的安全性低的技术问题。Python is one of the popular development languages in the field of software development technology. Python is an interpreted language, and its code will be executed by the interpreter only at runtime. This makes it difficult for Python code to be protected by the compiler like code written in compiled languages such as C and C++. In addition, Python has features such as dynamic typing, dynamic binding, and dynamic loading. These Python features enable Python code to be dynamically modified, added, and deleted at runtime. Attackers can use these features of Python to dynamically obtain the running logic of the code. In addition, Python's open source feature also allows attackers to crack, modify, and copy Python code based on its source code. For example, attackers can reversely restore the Python bytecode in the Python interpreter to Python source code based on the code logic of the Python interpreter, thereby causing the technical problem of low security of Python code in the prior art.
针对上述现有技术中的Python代码难以保护,安全性低的技术问题,目前尚未提出有效的解决方案。With respect to the technical problems in the above-mentioned prior art that Python code is difficult to protect and has low security, no effective solution has been proposed yet.
发明内容Summary of the invention
本申请提供了一种代码加密方法、装置、程序产品以及电子设备,以至少解决Python语言在运行过程中能够动态编辑和Python解释器的开源特性所导致的Python代码的安全性低的技术问题。The present application provides a code encryption method, device, program product and electronic device to at least solve the technical problem of low security of Python code caused by the ability of Python language to be dynamically edited during operation and the open source characteristics of the Python interpreter.
根据本申请的一个方面,提供了一种代码加密方法,包括:对目标代码进行编译,得到目标代码对应的代码对象,其中,目标代码为需要保护的Python代码;将代码对象加密为目标字节码,其中,目标字节码用于以字节码的形式表征加密后的目标代码,并且目标字节码中包括加密过程中动态生成的密钥信息。According to one aspect of the present application, a code encryption method is provided, comprising: compiling a target code to obtain a code object corresponding to the target code, wherein the target code is a Python code to be protected; encrypting the code object into a target bytecode, wherein the target bytecode is used to represent the encrypted target code in the form of a bytecode, and the target bytecode includes key information dynamically generated during the encryption process.
可选地,在将代码对象加密为目标字节码的过程中,代码加密方法还包括:对代码对象的第一属性进行扫描,得到L个常量,其中,L为正整数,第一属性用于表征代码对象的常量元组;在L个常量中的第i个常量为函数类型的情况下,对第i个常量所嵌入的代码对象进行递归加密;在第i个常量为字符串类型或数字类型的情况下,对第i个常量进行异或加密,其中,i为小于或等于L的正整数。Optionally, in the process of encrypting the code object into target bytecode, the code encryption method also includes: scanning the first attribute of the code object to obtain L constants, where L is a positive integer, and the first attribute is used to characterize the constant tuple of the code object; when the i-th constant among the L constants is a function type, recursively encrypting the code object embedded in the i-th constant; when the i-th constant is a string type or a numeric type, XOR-encrypting the i-th constant, where i is a positive integer less than or equal to L.
可选地,在第i个常量为字符串类型的情况下,对第i个常量进行异或加密,包括:将第i个常量划分为M个字节,其中,M为正整数;动态生成M个异或密钥,其中,M个异或密钥与M个字节一一对应,并且M个异或密钥中的第m+1个异或密钥是基于预设规则对第m个异或密钥进行更新得到的,其中,m为小于M的正整数;对M个异或密钥中的每个异或密钥和该异或密钥对应的字节进行异或处理,得到M个字节对应的所有异或结果;将M个字节对应的所有异或结果作为第i个常量的异或加密结果。Optionally, when the i-th constant is a string type, the i-th constant is XOR-encrypted, including: dividing the i-th constant into M bytes, where M is a positive integer; dynamically generating M XOR keys, where the M XOR keys correspond to the M bytes one-to-one, and the m+1-th XOR key among the M XOR keys is obtained by updating the m-th XOR key based on a preset rule, where m is a positive integer less than M; performing XOR processing on each XOR key in the M XOR keys and the byte corresponding to the XOR key to obtain all XOR results corresponding to the M bytes; and using all XOR results corresponding to the M bytes as the XOR encryption result of the i-th constant.
可选地,在将代码对象加密为目标字节码的过程中,代码加密方法还包括:对代码对象的第二属性进行扫描,得到N个字节码指令,其中,N为正整数,第二属性用于表征代码对象的字节码指令序列;将对代码对象的常量进行异或加密的过程中动态生成的最后一个异或密钥作为指令密钥的初始值,其中,指令密钥用于对字节码指令进行异或加密,字节码指令对应的冗余字节用于存储指令密钥的最新值;在N个字节码指令中的第j个字节码指令包括参数的情况下,依据指令密钥的最新值,对第j个字节码指令进行异或加密,并且禁止对指令密钥的最新值进行更新,其中,j为小于或等于N的正整数。Optionally, in the process of encrypting the code object into the target bytecode, the code encryption method also includes: scanning the second attribute of the code object to obtain N bytecode instructions, wherein N is a positive integer, and the second attribute is used to characterize the bytecode instruction sequence of the code object; using the last XOR key dynamically generated in the process of XOR encrypting the constant of the code object as the initial value of the instruction key, wherein the instruction key is used to XOR encrypt the bytecode instruction, and the redundant bytes corresponding to the bytecode instruction are used to store the latest value of the instruction key; when the jth bytecode instruction among the N bytecode instructions includes parameters, XOR encrypting the jth bytecode instruction according to the latest value of the instruction key, and prohibiting updating the latest value of the instruction key, wherein j is a positive integer less than or equal to N.
可选地,在将代码对象加密为目标字节码的过程中,代码加密方法还包括:在第j个字节码指令不包括参数的情况下,依据指令密钥的最新值对第j个字节码指令进行异或加密,并且将指令密钥的最新值更新为从预设范围中选取的随机数。Optionally, in the process of encrypting the code object into target bytecode, the code encryption method also includes: when the jth bytecode instruction does not include parameters, XOR encrypting the jth bytecode instruction based on the latest value of the instruction key, and updating the latest value of the instruction key to a random number selected from a preset range.
可选地,在将代码对象加密为目标字节码的过程中,代码加密方法还包括:判断密钥数量是否大于或等于第一预设阈值,其中,密钥数量为对字节码指令进行异或加密的过程中动态生成的指令密钥的总数量;在密钥数量大于或等于第一预设阈值的情况下,依据P个指令密钥生成目标密钥,其中,P个指令密钥为所有指令密钥中的生成时刻排名为前P个的指令密钥,P等于第一预设阈值;依据目标密钥对第一加密结果进行对称加密,其中,第一加密结果为通过第一集合中的指令密钥对代码对象的关键属性进行异或加密得到的结果,代码对象的关键属性至少包括第二属性,第一集合为所有指令密钥中除P个指令密钥之外的指令密钥组成的集合。Optionally, in the process of encrypting the code object into a target bytecode, the code encryption method also includes: determining whether the number of keys is greater than or equal to a first preset threshold, wherein the number of keys is the total number of instruction keys dynamically generated in the process of XOR encryption of the bytecode instructions; generating a target key based on P instruction keys when the number of keys is greater than or equal to the first preset threshold, wherein the P instruction keys are the instruction keys ranked as the top P in terms of generation time among all instruction keys, and P is equal to the first preset threshold; symmetrically encrypting a first encryption result based on the target key, wherein the first encryption result is a result obtained by XOR-encrypting a key attribute of the code object using an instruction key in a first set, the key attribute of the code object includes at least a second attribute, and the first set is a set of instruction keys other than the P instruction keys among all instruction keys.
可选地,在将代码对象加密为目标字节码的过程中,代码加密方法还包括:判断密钥数量是否大于或等于第二预设阈值,其中,第二预设阈值大于第一预设阈值;在密钥数量大于或等于第二预设阈值的情况下,依据Q个指令密钥生成目标向量,其中,Q为正整数,Q个指令密钥为所有指令密钥中的生成时刻排名为第P+1个至第R个的指令密钥,P等于第一预设阈值,R等于第二预设阈值;依据目标向量和目标密钥对第二加密结果进行对称加密,其中,第二加密结果为通过第二集合中的指令密钥对代码对象的关键属性进行异或加密得到的结果,第二集合为所有指令密钥中除P个指令密钥和Q个指令密钥之外的指令密钥组成的集合。Optionally, in the process of encrypting the code object into target bytecode, the code encryption method also includes: determining whether the number of keys is greater than or equal to a second preset threshold, wherein the second preset threshold is greater than the first preset threshold; when the number of keys is greater than or equal to the second preset threshold, generating a target vector based on Q instruction keys, wherein Q is a positive integer, and the Q instruction keys are instruction keys ranked from P+1th to Rth in generation time among all instruction keys, P is equal to the first preset threshold, and R is equal to the second preset threshold; symmetrically encrypting a second encryption result based on the target vector and the target key, wherein the second encryption result is a result obtained by XOR-encrypting a key attribute of the code object with instruction keys in a second set, and the second set is a set of instruction keys other than P instruction keys and Q instruction keys among all instruction keys.
可选地,在将代码对象加密为目标字节码之后,代码加密方法还包括:在将目标字节码加载至解释器之后,检测目标字节码中的需要调用的函数的加密标识,其中,函数的加密标识用于表征函数对应的代码对象是否被加密;在函数的加密标识为预设标识的情况下,通过调用函数对应的解密函数对函数对应的字节码进行解密,函数对应的解密函数为解密拓展库中存储的函数;检测函数的调用次数,其中,调用次数用于表征在函数被未完成运行的其他函数所调用的剩余次数;在调用次数等于预设次数的情况下,对函数对应的解密结果进行重新加密。Optionally, after the code object is encrypted into a target bytecode, the code encryption method further includes: after the target bytecode is loaded into an interpreter, detecting an encryption identifier of a function to be called in the target bytecode, wherein the encryption identifier of the function is used to indicate whether the code object corresponding to the function is encrypted; when the encryption identifier of the function is a preset identifier, decrypting the bytecode corresponding to the function by calling a decryption function corresponding to the function, wherein the decryption function corresponding to the function is a function stored in a decryption extension library; detecting the number of function calls, wherein the number of calls is used to indicate the remaining number of times the function is called by other functions that have not been completed; when the number of calls is equal to the preset number, re-encrypting the decryption result corresponding to the function.
根据本申请的另一个方面,还提供了一种代码加密装置,包括:编译单元,用于对目标代码进行编译,得到目标代码对应的代码对象,其中,目标代码为需要保护的Python代码;加密单元,用于将代码对象加密为目标字节码,其中,目标字节码用于以字节码的形式表征加密后的目标代码,并且目标字节码中包括加密过程中动态生成的密钥信息。According to another aspect of the present application, a code encryption device is also provided, including: a compilation unit, used to compile a target code to obtain a code object corresponding to the target code, wherein the target code is a Python code to be protected; an encryption unit, used to encrypt the code object into a target bytecode, wherein the target bytecode is used to represent the encrypted target code in the form of a bytecode, and the target bytecode includes key information dynamically generated during the encryption process.
根据本申请的另一个方面,还提供了一种计算机程序产品,该计算机程序产品中存储有计算机程序,其中,在计算机程序运行时控制计算机程序产品执行上述任意一项的代码加密方法。According to another aspect of the present application, a computer program product is provided, in which a computer program is stored, wherein when the computer program is running, the computer program product is controlled to execute any one of the above code encryption methods.
根据本申请的另一个方面,还提供了一种电子设备,其中,电子设备包括一个或多个处理器和存储器,存储器用于存储一个或多个程序,其中,当一个或多个程序被一个或多个处理器执行时,使得一个或多个处理器实现上述任意一项的代码加密方法。According to another aspect of the present application, an electronic device is also provided, wherein the electronic device includes one or more processors and a memory, the memory being used to store one or more programs, wherein when the one or more programs are executed by one or more processors, the one or more processors implement any one of the above-mentioned code encryption methods.
在本申请中,在需要对Python代码进行保护时,首先对目标代码进行编译,得到目标代码对应的代码对象,其中,目标代码为需要保护的Python代码,之后,本申请将代码对象加密为目标字节码,其中,目标字节码用于以字节码的形式表征加密后的目标代码,并且目标字节码中包括加密过程中动态生成的密钥信息。In the present application, when it is necessary to protect the Python code, the target code is first compiled to obtain a code object corresponding to the target code, wherein the target code is the Python code that needs to be protected. After that, the present application encrypts the code object into a target bytecode, wherein the target bytecode is used to represent the encrypted target code in the form of a bytecode, and the target bytecode includes key information dynamically generated during the encryption process.
由上述内容可知,本申请的技术方案通过对Python代码对应的字节码进行加密的方式,以实现对Python代码进行保护的目的。本申请首先将需要保护的Python代码编译为代码对象,之后,本申请将代码对象加密为目标字节码,从而得到Python代码对应的已经过加密的字节码文件(即目标字节码),从而实现了对Python代码的保护,同时,对比现有技术中未对Python代码进行保护,直接将Python代码转换为其对应的字节码文件的方式,本申请基于字节码的加密技术不会改变原来的Python代码的运行逻辑,即本申请的技术方案对Python代码进行加密的成本较低,不会造成额外的高成本的资源消耗。It can be seen from the above content that the technical solution of the present application realizes the purpose of protecting Python code by encrypting the bytecode corresponding to the Python code. The present application first compiles the Python code to be protected into a code object, and then encrypts the code object into a target bytecode, thereby obtaining an encrypted bytecode file (i.e., target bytecode) corresponding to the Python code, thereby realizing the protection of the Python code. At the same time, compared with the prior art in which the Python code is not protected and the Python code is directly converted into its corresponding bytecode file, the encryption technology based on bytecode of the present application will not change the running logic of the original Python code, that is, the technical solution of the present application has a low cost for encrypting the Python code, and will not cause additional high-cost resource consumption.
由此可见,本申请采用对Python代码对应的字节码进行加密的方式,实现了对Python代码进行保护的目的,即使攻击者在Python代码的运行过程中对Python代码对应的中间态文件进行了修改,由于攻击者无法获取密钥信息,经过攻击者修改后的Python代码也无法正常运行,从而实现了提高Python代码的安全性的技术效果,进而解决了Python语言在运行过程中能够动态编辑和Python解释器的开源特性所导致的Python代码的安全性低的技术问题。It can be seen that the present application adopts the method of encrypting the bytecode corresponding to the Python code to achieve the purpose of protecting the Python code. Even if the attacker modifies the intermediate file corresponding to the Python code during the running of the Python code, since the attacker cannot obtain the key information, the Python code modified by the attacker cannot run normally, thereby achieving the technical effect of improving the security of the Python code, and then solving the technical problem of low security of the Python code caused by the dynamic editing of the Python language during operation and the open source characteristics of the Python interpreter.
附图说明BRIEF DESCRIPTION OF THE DRAWINGS
此处所说明的附图用来提供对本申请的进一步理解,构成本申请的一部分,本申请的示意性实施例及其说明用于解释本申请,并不构成对本申请的不当限定。在附图中:The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
图1是根据本申请实施例的一种可选的代码加密方法的流程图;FIG1 is a flow chart of an optional code encryption method according to an embodiment of the present application;
图2是根据本申请实施例的一种可选的字节码指令格式的示意图;FIG2 is a schematic diagram of an optional bytecode instruction format according to an embodiment of the present application;
图3是根据本申请实施例的一种可选的字节码指令加密过程的示意图;FIG3 is a schematic diagram of an optional bytecode instruction encryption process according to an embodiment of the present application;
图4是根据本申请实施例的一种可选的代码加密方法的框架图;FIG4 is a framework diagram of an optional code encryption method according to an embodiment of the present application;
图5是根据本申请实施例的另一种可选的代码加密方法的流程图;FIG5 is a flowchart of another optional code encryption method according to an embodiment of the present application;
图6是根据本申请实施例的一种可选的代码加密装置的示意图;FIG6 is a schematic diagram of an optional code encryption device according to an embodiment of the present application;
图7是根据本申请实施例的一种可选的电子设备的示意图。FIG. 7 is a schematic diagram of an optional electronic device according to an embodiment of the present application.
具体实施方式DETAILED DESCRIPTION
为了使本技术领域的人员更好地理解本申请方案,下面将结合本申请实施例中的附图,对本申请实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例仅仅是本申请一部分的实施例,而不是全部的实施例。基于本申请中的实施例,本领域普通技术人员在没有做出创造性劳动前提下所获得的所有其他实施例,都应当属于本申请保护的范围。In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present application.
需要说明的是,本申请的说明书和权利要求书及上述附图中的术语“第一”、“第二”等是用于区别类似的对象,而不必用于描述特定的顺序或先后次序。应该理解这样使用的数据在适当情况下可以互换,以便这里描述的本申请的实施例能够以除了在这里图示或描述的那些以外的顺序实施。此外,术语“包括”和“具有”以及他们的任何变形,意图在于覆盖不排他的包含,例如,包含了一系列步骤或单元的过程、方法、系统、产品或设备不必限于清楚地列出的那些步骤或单元,而是可包括没有清楚地列出的或对于这些过程、方法、产品或设备固有的其它步骤或单元。It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
还需要说明的是,本申请所涉及的相关信息(包括已经生成的历史用例的信息)和数据(包括但不限于用于展示的数据以及分析的数据),均为经用户授权或者经过各方充分授权的信息和数据。例如,本系统和相关用户或机构间设置有接口,在获取相关信息之前,需要通过接口向前述的用户或机构发送获取请求,并在接收到前述的用户或机构反馈的同意信息后,获取相关信息。It should also be noted that the relevant information (including information on historical use cases that have been generated) and data (including but not limited to data for display and analysis) involved in this application are all information and data authorized by the user or fully authorized by all parties. For example, an interface is set up between this system and the relevant users or organizations. Before obtaining relevant information, it is necessary to send an acquisition request to the aforementioned user or organization through the interface, and obtain relevant information after receiving the consent information fed back by the aforementioned user or organization.
另外,本申请中所涉及的相关信息和相关数据的收集、存储、使用、加工、传输、提供、公开和应用等处理过程,均遵守相关地区的相关法律法规和标准,并且采取了必要保密措施,不违背公序良俗,另外,本申请提供有相应的操作入口,供用户选择同意授权或者拒绝授权,若用户选择拒绝授权,则进入到对应的专家决策流程。In addition, the collection, storage, use, processing, transmission, provision, disclosure and application of the relevant information and data involved in this application comply with the relevant laws, regulations and standards of the relevant regions, and necessary confidentiality measures have been taken and do not violate public order and good morals. In addition, this application provides corresponding operation entrances for users to choose to agree to authorization or refuse authorization. If the user chooses to refuse authorization, he/she will enter the corresponding expert decision-making process.
在第一种实施例中,提供了一种基于代码虚拟化的Python程序加密保护方法,具体内容包括:对需要保护的Python代码进行修改,在Python代码中加预先构建的载一个虚拟机,并将虚拟机的实现代码与需要保护的Python代码一同编译为字节码文件,之后,通过虚拟机对Python代码对应的字节码文件所包含的各个指令进行分发和执行,由于虚拟机的部分指令与Python的官方指令对应的操作不同,因此,攻击者无法基于Python官方字节码对应的反编译工具对Python代码对应的字节码文件进行破解,从而达到保护Python源代码的技术效果。In the first embodiment, a Python program encryption protection method based on code virtualization is provided, which specifically includes: modifying the Python code to be protected, adding a pre-built virtual machine to the Python code, and compiling the implementation code of the virtual machine and the Python code to be protected into a bytecode file, and then distributing and executing each instruction contained in the bytecode file corresponding to the Python code through the virtual machine. Since some instructions of the virtual machine are different from the operations corresponding to the official instructions of Python, the attacker cannot crack the bytecode file corresponding to the Python code based on the decompilation tool corresponding to the official bytecode of Python, thereby achieving the technical effect of protecting the Python source code.
上述第一种实施例所提供的技术方案的缺点包括:该实施例中的虚拟机的实现代码对应的字节码信息同样会保存在pyc格式的字节码文件中,虚拟机虽然能够保护Python代码的主体部分的字节码不被攻击者反编译,但无法对虚拟机自身的字节码进行保护,攻击者依然能够使用主流反编译工具对虚拟机的实现代码对应的字节码进行破解,从而得到Python代码的主体部分对应的字节码逻辑,另外,上述第一种实施例所提供的技术方案需要反复运行虚拟机,从而造成Python代码的整体运行效率低的问题。The disadvantages of the technical solution provided by the first embodiment mentioned above include: the bytecode information corresponding to the implementation code of the virtual machine in this embodiment will also be saved in the bytecode file in the pyc format. Although the virtual machine can protect the bytecode of the main part of the Python code from being decompiled by attackers, it cannot protect the bytecode of the virtual machine itself. Attackers can still use mainstream decompilation tools to crack the bytecode corresponding to the implementation code of the virtual machine, thereby obtaining the bytecode logic corresponding to the main part of the Python code. In addition, the technical solution provided by the first embodiment mentioned above requires repeatedly running the virtual machine, which causes the problem of low overall operating efficiency of the Python code.
与上述第一种实施例相比,本申请的技术方案通过对字节码进行加密,并且将加密后的字节码信息和加密过程中所使用的密钥信息隐藏到字节码文件中,从而加大了攻击者对密钥信息和字节码信息的破解难度,并且,本申请的技术方案不改变Python代码的运行逻辑,本申请也无需额外定制Python解释器,因此,本申请的技术方案对加密后的字节码执行对应的解密操作的资源开销也相对较小,从而保证了Python代码的整体运行效率。Compared with the first embodiment described above, the technical solution of the present application encrypts the bytecode and hides the encrypted bytecode information and the key information used in the encryption process in the bytecode file, thereby increasing the difficulty for attackers to crack the key information and the bytecode information. In addition, the technical solution of the present application does not change the running logic of the Python code, and the present application does not require additional customization of the Python interpreter. Therefore, the technical solution of the present application has relatively small resource overhead for performing corresponding decryption operations on the encrypted bytecode, thereby ensuring the overall running efficiency of the Python code.
在第二种实施例中,提供了一种基于pyc加密的Python源代码保护方法,具体内容包括:通过加密字节码文件来保护代码,对Python的官方解释器进行修改,得到定制的解释器,通过定制的解释器解密对已加密的字节码文件进行解释和执行。In the second embodiment, a Python source code protection method based on pyc encryption is provided, which specifically includes: protecting the code by encrypting the bytecode file, modifying the official Python interpreter to obtain a customized interpreter, and interpreting and executing the encrypted bytecode file by decrypting the customized interpreter.
上述第二种实施例所提供的技术方案的缺点包括:该实施例需要修改Python的官方解释器,不能使用本地原有的Python官方解释器运行加密后的字节码文件,从而提高了开发人员的使用学习成本和额外占用的空间大小,另外,攻击者能够通过对定制的Python解释器和原官方的Python解释器进行对比的方式,得到该实施例加密过程中使用的密钥信息。The disadvantages of the technical solution provided by the second embodiment include: this embodiment needs to modify the official Python interpreter, and cannot use the local original Python official interpreter to run the encrypted bytecode file, thereby increasing the learning cost of developers and the amount of additional space occupied. In addition, attackers can obtain the key information used in the encryption process of this embodiment by comparing the customized Python interpreter with the original official Python interpreter.
在第三种实施例中,提供了一种基于对称算法和专用加载模块的Python程序模块加密方法,具体内容包括:基于对称加密算法对字节码文件进行加密,并采用加载自制扩展模块的方式来执行自定义加密后的字节码文件,其中,自制扩展模块是一个能够被其它Python程序所共享的动态链接库,该实施例将核心的加密与解密过程编入动态链接库中以实现保护Python代码的目的,开发人员通过导入该动态链接库,并调用相应的函数接口就能够实现Python代码对应的加密操作、解密操作以及执行操作。In the third embodiment, a Python program module encryption method based on a symmetric algorithm and a dedicated loading module is provided, and the specific contents include: encrypting the bytecode file based on a symmetric encryption algorithm, and executing the custom encrypted bytecode file by loading a self-made extension module, wherein the self-made extension module is a dynamic link library that can be shared by other Python programs. This embodiment incorporates the core encryption and decryption processes into the dynamic link library to achieve the purpose of protecting the Python code. Developers can implement the encryption operations, decryption operations and execution operations corresponding to the Python code by importing the dynamic link library and calling the corresponding function interface.
上述第三种实施例所提供的技术方案的缺点包括:该实施例的自制拓展模块需要调用Python语言和/或C语言进行编写,虽然提高了攻击者对自制拓展模块进行破解的难度,但是,由于该方法中使用的核心的加密算法和相关密钥信息能够直接从二进制文件中找到,因此,攻击者仍能够轻松破解出Python源代码。The disadvantages of the technical solution provided by the third embodiment include: the homemade extension module of this embodiment needs to be written in Python language and/or C language. Although it increases the difficulty for attackers to crack the homemade extension module, since the core encryption algorithm and related key information used in this method can be found directly from the binary file, attackers can still easily crack the Python source code.
与上述第二种和第三种实施例相比,本申请中使用的密钥信息是不断动态更新的,从而提高了攻击者对加密后的字节码文件进行破解的难度,并且,本申请将加密过程中所使用的密钥信息隐藏至被保护代码对应的字节码文件中,并非写入拓展库中进行保存,从而提高了攻击者获取密钥信息的难度,提高了攻击者的攻击成本,进而实现了提高需要保护的Python代码的安全性的技术效果。Compared with the second and third embodiments described above, the key information used in the present application is continuously and dynamically updated, thereby increasing the difficulty for attackers to crack the encrypted bytecode file. In addition, the present application hides the key information used in the encryption process in the bytecode file corresponding to the protected code, rather than writing it into the extension library for storage, thereby increasing the difficulty for attackers to obtain the key information and increasing the attacker's attack cost, thereby achieving the technical effect of improving the security of the Python code that needs to be protected.
根据本申请实施例,提供了一种代码加密方法的实施例,需要说明的是,在附图的流程图示出的步骤可以在诸如一组计算机可执行指令的计算机系统中执行,并且,虽然在流程图中示出了逻辑顺序,但是在某些情况下,可以以不同于此处的顺序执行所示出或描述的步骤。According to an embodiment of the present application, an embodiment of a code encryption method is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
本申请提供了一种代码加密系统用于执行本申请中的代码加密方法,该代码加密系统可以采用软件或软硬件结合的方式实现。The present application provides a code encryption system for executing the code encryption method in the present application. The code encryption system can be implemented in software or a combination of software and hardware.
图1是根据本申请实施例的一种可选的代码加密方法的流程图,如图1所示,该方法包括如下步骤:FIG. 1 is a flow chart of an optional code encryption method according to an embodiment of the present application. As shown in FIG. 1 , the method includes the following steps:
步骤S101,对目标代码进行编译,得到目标代码对应的代码对象,其中,目标代码为需要保护的Python代码。Step S101, compile the target code to obtain a code object corresponding to the target code, wherein the target code is the Python code to be protected.
可选地,在执行步骤S101之前,代码加密系统首先获取需要保护的Python代码,之后,代码加密系统通过预设的正则表达式将需要保护的Python代码更新为目标代码,正则表达式能够为原Python代码中的非库函数的调用添加对应的解密函数,并且,代码加密系统将生成的解密函数存储至解密拓展库(或称为:解密扩展库)中,以便在后续通过Python官方解释器对加密后的字节码(即目标字节码)进行解释执行的过程中,本申请的技术方案能够实现通过解密拓展库对加密后的字节码文件进行解密的目的。Optionally, before executing step S101, the code encryption system first obtains the Python code to be protected, and then the code encryption system updates the Python code to be protected to the target code through a preset regular expression. The regular expression can add a corresponding decryption function for the call of the non-library function in the original Python code, and the code encryption system stores the generated decryption function in a decryption extension library (or called: decryption extension library), so that in the subsequent process of interpreting and executing the encrypted bytecode (i.e., the target bytecode) through the Python official interpreter, the technical solution of the present application can achieve the purpose of decrypting the encrypted bytecode file through the decryption extension library.
可选地,上述代码对象为PyCodeObject,PyCodeObject是一个C语言结构体,PyCodeObject用于表征需要保护的Python代码的编译结果,具体地,PyCodeObject包括以下一种或多种属性信息:Optionally, the code object is a PyCodeObject, which is a C language structure. PyCodeObject is used to represent the compilation result of the Python code to be protected. Specifically, PyCodeObject includes one or more of the following attribute information:
(1)基础信息:(1) Basic information:
co_argcount:函数参数的个数。co_argcount: The number of function parameters.
co_kwonlyargcount:关键字参数的个数。co_kwonlyargcount: The number of keyword arguments.
co_nlocals:函数内局部变量的个数。co_nlocals: The number of local variables in the function.
co_stacksize:函数运行时需要的栈空间。co_stacksize: The stack space required when the function is running.
co_flags:代码对象的标志位,例如是否为生成器等。co_flags: Flags of the code object, such as whether it is a generator.
co_lnotab:行号表,指示字节码指令和源代码行号的映射关系。co_lnotab: Line number table, indicating the mapping relationship between bytecode instructions and source code line numbers.
(2)指令序列信息:(2) Instruction sequence information:
co_code:字节码指令序列,即编译后的代码。co_code: bytecode instruction sequence, i.e. compiled code.
co_consts:常量元组,包含代码中使用的常量,例如数字、字符串以及自定义的函数体所对应的代码对象。co_consts: A constant tuple containing constants used in the code, such as numbers, strings, and code objects corresponding to custom function bodies.
co_names:名称表,包含全局变量、函数名等。co_names: Name table, including global variables, function names, etc.
co_varnames:变量名表,包含局部变量、参数名等。co_varnames: variable name table, including local variables, parameter names, etc.
(3)异常处理信息:(3) Exception handling information:
co_exceptions:异常表,用于异常处理的相关信息。co_exceptions: exception table, used for exception handling related information.
(4)其他信息:(4) Other information:
co_cellvars:闭包变量。co_cellvars: closure variables.
co_freevars:自由变量,即未在局部作用域定义的变量。co_freevars: Free variables, that is, variables not defined in the local scope.
在一些实施例中,代码加密系统所涉及PyCodeObject的属性至少包括:co_code属性、co_consts属性、co_flags属性、co_names属性以及co_varnames属性。In some embodiments, the attributes of the PyCodeObject involved in the code encryption system include at least: a co_code attribute, a co_consts attribute, a co_flags attribute, a co_names attribute, and a co_varnames attribute.
步骤S102,将代码对象加密为目标字节码,其中,目标字节码用于以字节码的形式表征加密后的目标代码,并且目标字节码中包括加密过程中动态生成的密钥信息。Step S102, encrypting the code object into a target bytecode, wherein the target bytecode is used to represent the encrypted target code in the form of a bytecode, and the target bytecode includes key information dynamically generated during the encryption process.
可选地,Python的字节码类似于汇编语言,Python的字节码是Python在运行过程中,从Python源代码转换而来并存储在内存中的一种中间语言,Python虚拟机逐行处理字节码的过程也类似于处理器逐行处理汇编语句的过程,为了节省相同Python程序运行时从Python源代码到字节码的转换时间,这些转换好的字节码会被存储到外存的Python字节码文件中,Python程序需要再次执行时会直接执行该Python字节码文件中的字节码。Optionally, Python's bytecode is similar to assembly language. Python's bytecode is an intermediate language converted from Python source code and stored in memory during Python's running. The process of Python virtual machine processing bytecode line by line is similar to the process of processor processing assembly statements line by line. In order to save the conversion time from Python source code to bytecode when the same Python program is running, these converted bytecodes will be stored in the Python bytecode file in the external memory. When the Python program needs to be executed again, the bytecode in the Python bytecode file will be directly executed.
可选地,代码加密系统能够通过预先设置的加密器执行对代码对象的加密操作,其中,加密器至少用于根据动态生成的密钥信息对代码对象的关键属性进行异或加密,并且,在生成的用于异或加密的异或密钥的总数量大于或等于预设阈值的情况下,加密器还会对部分代码对象的关键属性进行对称加密,从而实现了提高对python代码进行加密的复杂度以及生成的目标字节码的破解成本的技术效果。Optionally, the code encryption system can perform encryption operations on code objects through a pre-set encryptor, wherein the encryptor is at least used to XOR encrypt key attributes of the code object according to dynamically generated key information, and when the total number of XOR keys generated for XOR encryption is greater than or equal to a preset threshold, the encryptor will also symmetrically encrypt key attributes of some code objects, thereby achieving the technical effect of increasing the complexity of encrypting Python code and the cracking cost of the generated target bytecode.
这里,密钥信息可包括异或密钥,异或密钥能够用于对该异或密钥和代码对象的属性中与该异或密钥对应的字节进行异或处理。Here, the key information may include an XOR key, and the XOR key can be used to perform an XOR process on the XOR key and a byte corresponding to the XOR key in an attribute of the code object.
需要注意的是,在步骤S102中,代码加密系统并不是将加密过程中动态生成的密钥信息直接存储至目标代码对应的字节码文件中,而是对动态生成的密钥信息进行异或处理后隐藏存储至目标代码对应的字节码文件中,从而避免攻击者能够直接从字节码文件中直接获取代码加密系统所使用的密钥信息,进而提高了攻击者的攻击成本,提高了目标代码的安全性。It should be noted that in step S102, the code encryption system does not directly store the key information dynamically generated during the encryption process into the bytecode file corresponding to the target code, but performs XOR processing on the dynamically generated key information and then stores it hidden in the bytecode file corresponding to the target code, thereby preventing attackers from directly obtaining the key information used by the code encryption system from the bytecode file, thereby increasing the attack cost of attackers and improving the security of the target code.
由上述内容可知,本申请的技术方案通过对Python代码对应的字节码进行加密的方式,以实现对Python代码进行保护的目的。本申请首先将需要保护的Python代码编译为代码对象,之后,本申请将代码对象加密为目标字节码,从而得到Python代码对应的已经过加密的字节码文件(即目标字节码),从而实现了对Python代码的保护,同时,对比现有技术中未对Python代码进行保护,直接将Python代码转换为其对应的字节码文件的方式,本申请基于字节码的加密技术不会改变原来的Python代码的运行逻辑,即本申请的技术方案对Python代码进行加密的成本较低,不会造成额外的高成本的资源消耗。It can be seen from the above content that the technical solution of the present application realizes the purpose of protecting Python code by encrypting the bytecode corresponding to the Python code. The present application first compiles the Python code to be protected into a code object, and then encrypts the code object into a target bytecode, thereby obtaining an encrypted bytecode file (i.e., target bytecode) corresponding to the Python code, thereby realizing the protection of the Python code. At the same time, compared with the prior art in which the Python code is not protected and the Python code is directly converted into its corresponding bytecode file, the encryption technology based on bytecode of the present application will not change the running logic of the original Python code, that is, the technical solution of the present application has a low cost for encrypting the Python code, and will not cause additional high-cost resource consumption.
由此可见,本申请采用对Python代码对应的字节码进行加密的方式,实现了对Python代码进行保护的目的,即使攻击者在Python代码的运行过程中对Python代码对应的中间态文件进行了修改,由于攻击者无法获取密钥信息,经过攻击者修改后的Python代码也无法正常运行,从而实现了提高Python代码的安全性的技术效果,进而解决了Python语言在运行过程中能够动态编辑和Python解释器的开源特性所导致的Python代码的安全性低的技术问题。It can be seen that the present application adopts the method of encrypting the bytecode corresponding to the Python code to achieve the purpose of protecting the Python code. Even if the attacker modifies the intermediate file corresponding to the Python code during the running of the Python code, since the attacker cannot obtain the key information, the Python code modified by the attacker cannot run normally, thereby achieving the technical effect of improving the security of the Python code, and then solving the technical problem of low security of the Python code caused by the dynamic editing of the Python language during operation and the open source characteristics of the Python interpreter.
在一种可选的实施例中,在将代码对象加密为目标字节码的过程中,代码加密系统首先通过预设的加密器对代码对象的第一属性进行扫描,得到L个常量,其中,L为正整数,第一属性用于表征代码对象的常量元组,之后,在L个常量中的第i个常量为函数类型的情况下,通过加密器对第i个常量所嵌入的代码对象进行递归加密,其中,i为小于或等于L的正整数;在第i个常量为字符串类型或数字类型的情况下,通过加密器对第i个常量进行异或加密。In an optional embodiment, in the process of encrypting a code object into a target bytecode, the code encryption system first scans the first attribute of the code object through a preset encryptor to obtain L constants, where L is a positive integer, and the first attribute is used to characterize the constant tuple of the code object. Then, when the i-th constant among the L constants is a function type, the code object embedded in the i-th constant is recursively encrypted through the encryptor, where i is a positive integer less than or equal to L; when the i-th constant is a string type or a numeric type, the i-th constant is XOR-encrypted through the encryptor.
这里,为了区分目标代码对应的代码对象与第i个常量所嵌入的代码,可以将目标代码对应的代码对象记为第一代码对象,第i个常量所嵌入的代码对象记为第二代码对象,第一代码对象不同于第二代码对象。Here, in order to distinguish the code object corresponding to the target code from the code embedded in the i-th constant, the code object corresponding to the target code can be recorded as the first code object, and the code object embedded in the i-th constant can be recorded as the second code object. The first code object is different from the second code object.
可选地,上述代码对象的第一属性为代码对象的co_consts属性,co_consts属性中的常量能够用于表征数字、字符串、元组、列表以及字典等不可变对象,另外,函数对象的引用也能够作为常量出现在co_consts属性中,co_consts属性中的函数类型的常量实际上代表对函数对象的引用,而不是代表函数对象本身。Optionally, the first attribute of the above code object is the co_consts attribute of the code object. The constants in the co_consts attribute can be used to represent immutable objects such as numbers, strings, tuples, lists and dictionaries. In addition, references to function objects can also appear as constants in the co_consts attribute. The constants of the function type in the co_consts attribute actually represent references to function objects, rather than the function objects themselves.
可选地,在上述L个常量中的第i个常量为函数类型的情况下,第i个常量所嵌入的代码对象为第i个常量代表的函数所对应的Python代码编译得到的代码对象,此时,代码加密系统通过加密器对第i个常量代表的函数所对应的Python代码编译得到的代码对象进行加密。Optionally, when the i-th constant among the above L constants is a function type, the code object embedded in the i-th constant is a code object compiled from the Python code corresponding to the function represented by the i-th constant. At this time, the code encryption system encrypts the code object compiled from the Python code corresponding to the function represented by the i-th constant through an encryptor.
可选地,代码加密系统通过对代码对象的第一属性进行异或加密,实现了提高代码对象的第一属性的安全性的目的,即使在第一属性对应的数据被非法访问或泄露的情况下,加密后的第一属性数据也不易被攻击者解读,从而实现了提高代码对象的第一属性所对应的python源代码的安全性的目的。Optionally, the code encryption system achieves the purpose of improving the security of the first attribute of the code object by XOR encryption of the first attribute of the code object. Even if the data corresponding to the first attribute is illegally accessed or leaked, the encrypted first attribute data is not easily deciphered by attackers, thereby achieving the purpose of improving the security of the Python source code corresponding to the first attribute of the code object.
在一种可选的实施例中,在对第i个常量进行异或加密的过程中,在第i个常量为字符串类型的情况下,代码加密系统首先通过预设的加密器将第i个常量划分为M个字节,其中,M为正整数,之后,通过加密器动态生成M个异或密钥,其中,M个异或密钥与M个字节一一对应,并且M个异或密钥中的第m+1个异或密钥是基于预设规则对第m个异或密钥进行更新得到的,其中,m为小于M的正整数,然后,通过加密器对M个异或密钥中的每个异或密钥和该异或密钥对应的字节进行异或处理,得到M个字节对应的所有异或结果,最后,代码加密系统将M个字节对应的所有异或结果作为第i个常量的异或加密结果。In an optional embodiment, in the process of XOR encryption of the ith constant, when the ith constant is of a string type, the code encryption system first divides the ith constant into M bytes through a preset encryptor, where M is a positive integer. Then, M XOR keys are dynamically generated through the encryptor, where the M XOR keys correspond one-to-one to the M bytes, and the m+1th XOR key among the M XOR keys is obtained by updating the mth XOR key based on a preset rule, where m is a positive integer less than M. Then, each XOR key in the M XOR keys and the byte corresponding to the XOR key are XOR-processed by the encryptor to obtain all XOR results corresponding to the M bytes. Finally, the code encryption system uses all XOR results corresponding to the M bytes as the XOR encryption result of the ith constant.
在一种可选的实施例中,在对第i个常量进行异或加密的过程中,在第i个常量为数字类型的情况下,代码加密系统同样依据能够基于预设规则进行更新的异或密钥对第i个常量进行异或处理,并且,将异或处理得到的异或结果作为第i个常量的异或加密结果。In an optional embodiment, during the process of XOR encryption of the ith constant, when the ith constant is of a digital type, the code encryption system also performs XOR processing on the ith constant according to an XOR key that can be updated based on preset rules, and uses the XOR result obtained by the XOR processing as the XOR encryption result of the ith constant.
在一些实施例中,代码加密系统中所使用的异或密钥的大小处于[1,255]区间之内。In some embodiments, the size of the XOR key used in the code encryption system is within the interval [1, 255].
举例说明,假设代码加密系统在将第1个字符串类型的常量划分为M个字节之后,控制加密器从[1,255]区间内选取一个随机数作为第一个异或密钥,记为xorKey,并且将xorKey额外存储至代码对象的常量元组的末尾位置,之后,通过加密器对第一个异或密钥和第1个常量中的第一个字节进行异或处理,得到第一个字节对应的异或结果,并且,将第一个字节对应的异或结果存储至第一个字节在字节码文件中的原存储位置。For example, assume that after the code encryption system divides the first string type constant into M bytes, it controls the encryptor to select a random number from the interval [1,255] as the first XOR key, denoted as xorKey, and stores xorKey additionally at the end of the constant tuple of the code object. Afterwards, the encryptor performs XOR processing on the first XOR key and the first byte in the first constant to obtain the XOR result corresponding to the first byte, and stores the XOR result corresponding to the first byte to the original storage position of the first byte in the bytecode file.
可选地,在得到第一个字节对应的异或结果之后,控制加密器依据预设规则对所述第一个异或密钥进行更新,得到第二个异或密钥,之后,通过加密器对第二个异或密钥和第1个常量中的第二个字节进行异或处理,得到第二个字节对应的异或结果,并且,将第二个字节对应的异或结果存储至第二个字节在字节码文件中的原存储位置。Optionally, after obtaining the XOR result corresponding to the first byte, the encryptor is controlled to update the first XOR key according to a preset rule to obtain a second XOR key. Thereafter, the second XOR key and the second byte in the first constant are XOR-processed by the encryptor to obtain the XOR result corresponding to the second byte, and the XOR result corresponding to the second byte is stored in the original storage position of the second byte in the bytecode file.
可选地,代码加密系统依据加密器动态生成的密钥信息对代码对象的第一属性进行异或处理,从而实现对代码对象的第一属性进行加密的目的,异或处理操作是一种简单的位运算,异或处理操作只涉及到两个操作数和一个位操作,能够通过计算机的基础硬件实现,异或处理操作适合于资源受限或需要快速实现的应用场景,从而实现了不额外增加对代码进行加密的资源开销的技术效果。Optionally, the code encryption system performs XOR processing on the first attribute of the code object based on the key information dynamically generated by the encryptor, thereby achieving the purpose of encrypting the first attribute of the code object. The XOR processing operation is a simple bit operation. The XOR processing operation only involves two operands and a bit operation, which can be implemented through the basic hardware of the computer. The XOR processing operation is suitable for application scenarios with limited resources or that need to be implemented quickly, thereby achieving the technical effect of not adding additional resource overhead for encrypting the code.
在一种可选的实施例中,在将代码对象加密为目标字节码的过程中,代码加密系统首先通过预设的加密器对代码对象的第二属性进行扫描,得到N个字节码指令,其中,N为正整数,第二属性用于表征代码对象的字节码指令序列,之后,代码加密系统将对代码对象的常量进行异或加密的过程中动态生成的最后一个异或密钥作为指令密钥的初始值,其中,指令密钥用于对字节码指令进行异或加密,字节码指令对应的冗余字节用于存储指令密钥的最新值,然后,在N个字节码指令中的第j个字节码指令包括参数的情况下,加密器依据指令密钥的最新值对第j个字节码指令进行异或加密,并且禁止对指令密钥的最新值进行更新,其中,j为小于或等于N的正整数。In an optional embodiment, in the process of encrypting a code object into a target bytecode, the code encryption system first scans the second attribute of the code object through a preset encryptor to obtain N bytecode instructions, wherein N is a positive integer, and the second attribute is used to characterize the bytecode instruction sequence of the code object. Afterwards, the code encryption system uses the last XOR key dynamically generated in the process of XOR encrypting the constant of the code object as the initial value of the instruction key, wherein the instruction key is used to XOR encrypt the bytecode instruction, and the redundant bytes corresponding to the bytecode instruction are used to store the latest value of the instruction key. Then, when the jth bytecode instruction among the N bytecode instructions includes parameters, the encryptor XOR encrypts the jth bytecode instruction according to the latest value of the instruction key, and prohibits updating the latest value of the instruction key, wherein j is a positive integer less than or equal to N.
这里,指令密钥的最新值可以理解为指令密钥的当前值,例如,在指令密钥的值从未发生更新的情况下,该最新值即为初始值;在指令密钥的值发生过更新的情况下,该最新值即为指令密钥的最近一次更新后的值。Here, the latest value of the instruction key can be understood as the current value of the instruction key. For example, when the value of the instruction key has never been updated, the latest value is the initial value; when the value of the instruction key has been updated, the latest value is the value after the most recent update of the instruction key.
可选地,在N个字节码指令中的第j个字节码指令包括参数的情况下,在禁止加密器对指令密钥的最新值进行更新之后,加密器依据当前的指令密钥对第j+1个字节码指令进行异或加密。Optionally, when the j-th bytecode instruction among N bytecode instructions includes parameters, after prohibiting the encryptor from updating the latest value of the instruction key, the encryptor performs XOR encryption on the j+1-th bytecode instruction based on the current instruction key.
可选地,在第j个字节码指令不包括参数的情况下,加密器依据指令密钥的最新值对第j个字节码指令进行异或加密,并且将指令密钥的最新值更新为从预设范围中选取的随机数。Optionally, when the j-th bytecode instruction does not include parameters, the encryptor performs XOR encryption on the j-th bytecode instruction based on the latest value of the instruction key, and updates the latest value of the instruction key to a random number selected from a preset range.
示例性地,在加密器依据指令密钥的最新值对第j个字节码指令进行异或加密之前,加密器首先基于预设范围选取一个随机数,并且将选取的随机数存储到第j个字节码指令对应的冗余字节中。Exemplarily, before the encryptor XOR-encrypts the j-th bytecode instruction according to the latest value of the instruction key, the encryptor first selects a random number based on a preset range and stores the selected random number in the redundant bytes corresponding to the j-th bytecode instruction.
在一些实施例中,预设范围为[1,255],指令密钥的初始值为对代码对象的常量进行异或加密的过程中动态生成的最后一个异或密钥。In some embodiments, the preset range is [1, 255], and the initial value of the instruction key is the last XOR key dynamically generated during the XOR encryption of the constants of the code object.
可选地,图2是根据本申请实施例的一种可选的字节码指令格式的示意图,如图2所示,在Python 3.6及以后的版本中,co_code属性中的每一个字节码指令的长度均为2字节,其中,有参字节码指令由1字节长度的操作码和1字节长度的操作数所组成,无参字节码指令由1字节长度的操作码和1字节长度的“\x00”冗余字节所组成。Optionally, Figure 2 is a schematic diagram of an optional bytecode instruction format according to an embodiment of the present application. As shown in Figure 2, in Python 3.6 and later versions, the length of each bytecode instruction in the co_code attribute is 2 bytes, wherein the bytecode instruction with parameters consists of an opcode of 1 byte length and an operand of 1 byte length, and the bytecode instruction without parameters consists of an opcode of 1 byte length and a redundant byte of 1 byte length "\x00".
可选地,图3是根据本申请实施例的一种可选的字节码指令加密过程的示意图,如图3所示,字节码指令1、字节码指令2以及字节码指令4均为有参字节码指令,字节码指令3为无参字节码指令,即字节码指令1、字节码指令2以及字节码指令4中均包括参数,字节码指令3中不包括参数,假设指令密钥的初始值为K0,则加密器对字节码指令1和字节码指令2进行加密所使用的指令密钥均为K0,之后,在代码加密系统检测到字节码指令3为无参字节码指令时,代码加密系统控制加密器基于预设范围生成一个新的指令密钥K1,并且将新的指令密钥K1存储至字节码指令3的冗余字节中,之后,代码加密系统基于旧的指令密钥K0对整条字节码指令3(包括字节码指令3的冗余字节中存储的K1)进行异或加密,然后,代码加密系统通过新的指令密钥K1对字节码指令3之后的字节码指令(即字节码指令4)进行加密。Optionally, Figure 3 is a schematic diagram of an optional bytecode instruction encryption process according to an embodiment of the present application. As shown in Figure 3, bytecode instruction 1, bytecode instruction 2 and bytecode instruction 4 are all bytecode instructions with parameters, and bytecode instruction 3 is a bytecode instruction without parameters, that is, bytecode instruction 1, bytecode instruction 2 and bytecode instruction 4 all include parameters, and bytecode instruction 3 does not include parameters. Assuming that the initial value of the instruction key is K0, the instruction key used by the encryptor to encrypt bytecode instruction 1 and bytecode instruction 2 is K0. Thereafter, when the code encryption system detects that bytecode instruction 3 is a bytecode instruction without parameters, the code encryption system controls the encryptor to generate a new instruction key K1 based on a preset range, and stores the new instruction key K1 in the redundant bytes of bytecode instruction 3. Thereafter, the code encryption system performs XOR encryption on the entire bytecode instruction 3 (including K1 stored in the redundant bytes of bytecode instruction 3) based on the old instruction key K0, and then, the code encryption system encrypts the bytecode instruction after bytecode instruction 3 (that is, bytecode instruction 4) by the new instruction key K1.
可选地,相比较将新生成的指令密钥不进行异或加密,直接存储至字节码指令的冗余字节所在位置的方式,本申请在将新生成的指令密钥存储至字节码指令的冗余字节之后,基于旧的指令密钥对整条字节码指令(包括冗余字节)进行异或加密,从而提高了冗余字节中所存储的密钥信息的安全性,进而提高了攻击者获取密钥信息的难度。Optionally, compared with the method of not performing XOR encryption on the newly generated instruction key and directly storing it at the location of the redundant bytes of the bytecode instruction, the present application stores the newly generated instruction key in the redundant bytes of the bytecode instruction and then performs XOR encryption on the entire bytecode instruction (including the redundant bytes) based on the old instruction key, thereby improving the security of the key information stored in the redundant bytes and further increasing the difficulty for attackers to obtain the key information.
在一种可选的实施例中,在将代码对象加密为目标字节码的过程中,在加密器对代码对象的关键属性进行异或加密完毕之后,代码加密系统通过预设的加密器判断密钥数量是否大于或等于第一预设阈值,其中,密钥数量为对字节码指令进行异或加密的过程中动态生成的指令密钥的总数量,之后,在密钥数量大于或等于第一预设阈值的情况下,通过加密器依据P个指令密钥生成目标密钥,其中,P个指令密钥为所有指令密钥中的生成时刻排名为前P个的指令密钥,P等于第一预设阈值,然后,通过加密器依据目标密钥对第一加密结果进行对称加密,其中,第一加密结果为通过第一集合中的指令密钥对代码对象的关键属性进行异或加密得到的结果,代码对象的关键属性至少包括第二属性,第一集合为所有指令密钥中除P个指令密钥之外的指令密钥组成的集合。In an optional embodiment, in the process of encrypting a code object into a target bytecode, after the encryptor completes XOR encryption of the key attributes of the code object, the code encryption system determines through a preset encryptor whether the number of keys is greater than or equal to a first preset threshold, wherein the number of keys is the total number of instruction keys dynamically generated during the XOR encryption of the bytecode instructions. Then, when the number of keys is greater than or equal to the first preset threshold, the encryptor generates a target key based on P instruction keys, wherein the P instruction keys are the instruction keys ranked as the top P in terms of generation time among all instruction keys, and P is equal to the first preset threshold. Then, the encryptor symmetrically encrypts the first encryption result based on the target key, wherein the first encryption result is a result obtained by XOR encryption of the key attributes of the code object through the instruction keys in the first set, the key attributes of the code object include at least the second attribute, and the first set is a set of instruction keys other than the P instruction keys among all instruction keys.
在一些实施例中,第一预设阈值可以设置为16。In some embodiments, the first preset threshold may be set to 16.
可选地,代码加密系统能够将加密器生成的前16个指令密钥的组合作为目标密钥,并且依据AES(Advanced Encryption Standard,高级加密标准)对称加密算法的ECB(Electronic Codebook,电子密码本)模式通过目标密钥对使用除了前16个指令密钥之外的指令密钥加密得到的字节码信息进行二次加密。Optionally, the code encryption system can use the combination of the first 16 instruction keys generated by the encryptor as the target key, and use the target key to re-encrypt the bytecode information encrypted using instruction keys other than the first 16 instruction keys in accordance with the ECB (Electronic Codebook) mode of the AES (Advanced Encryption Standard) symmetric encryption algorithm.
可选地,在本申请的技术方案中,代码加密系统不仅能够控制加密器对代码对象的关键属性进行异或加密,还能够在异或加密的基础上,基于AES对称加密算法对异或加密得到的部分加密结果进行二次加密。异或加密方式虽然操作简单快捷,但其安全性相对较低,通过使用AES对称加密算法对异或操作的部分加密结果进行二次加密,从而实现了增强数据的保密性和安全性的技术效果。Optionally, in the technical solution of the present application, the code encryption system can not only control the encryptor to perform XOR encryption on the key attributes of the code object, but also perform secondary encryption on the partial encryption results obtained by the XOR encryption based on the AES symmetric encryption algorithm. Although the XOR encryption method is simple and fast to operate, its security is relatively low. By using the AES symmetric encryption algorithm to perform secondary encryption on the partial encryption results of the XOR operation, the technical effect of enhancing the confidentiality and security of the data is achieved.
在一种可选的实施例中,在将代码对象加密为目标字节码的过程中,代码加密系统通过预设的加密器判断密钥数量是否大于或等于第二预设阈值,其中,密钥数量为对字节码指令进行异或加密的过程中动态生成的指令密钥的总数量,第二预设阈值大于第一预设阈值,之后,在密钥数量大于或等于第二预设阈值的情况下,通过加密器依据Q个指令密钥生成目标向量,其中,Q为正整数,Q个指令密钥为所有指令密钥中的生成时刻排名为第P+1个至第R个的指令密钥,P等于第一预设阈值,R等于第二预设阈值,然后,通过加密器依据目标向量和目标密钥对第二加密结果进行对称加密,其中,第二加密结果为通过第二集合中的指令密钥对代码对象的关键属性进行异或加密得到的结果,代码对象的关键属性至少包括第二属性,第二集合为所有指令密钥中除P个指令密钥和Q个指令密钥之外的指令密钥组成的集合。In an optional embodiment, in the process of encrypting a code object into a target bytecode, the code encryption system determines whether the number of keys is greater than or equal to a second preset threshold through a preset encryptor, wherein the number of keys is the total number of instruction keys dynamically generated in the process of XOR encryption of bytecode instructions, and the second preset threshold is greater than the first preset threshold. Then, when the number of keys is greater than or equal to the second preset threshold, the encryptor generates a target vector based on Q instruction keys, wherein Q is a positive integer, and the Q instruction keys are instruction keys ranked from P+1 to R in generation time among all instruction keys, P is equal to the first preset threshold, and R is equal to the second preset threshold. Then, the encryptor symmetrically encrypts the second encryption result based on the target vector and the target key, wherein the second encryption result is a result obtained by XOR encryption of a key attribute of the code object by an instruction key in a second set, the key attribute of the code object includes at least the second attribute, and the second set is a set of instruction keys other than P instruction keys and Q instruction keys among all instruction keys.
在一些实施例中,第二预设阈值可以设置为32。In some embodiments, the second preset threshold may be set to 32.
可选地,ECB模式对每个明文块进行独立加密,这意味着对具有相同内容的明文块进行加密得到的加密结果是相同的,因此,ECB模式不适合对包括重复数据的长文本进行加密,因此,在密钥数量大于或等于第二预设阈值32的情况下,本申请中的代码加密系统除了依据AES对称加密算法的ECB模式通过目标密钥对使用除了前16个指令密钥之外的指令密钥加密得到的字节码信息进行二次加密之外,可选地,代码加密系统还将加密器生成的第17个至第32个指令密钥的组合作为目标向量,并且依据AES对称加密算法的CBC(CipherBlock Chaining,密码分组链接)模式通过目标密钥和目标向量对使用第32个指令密钥之后的指令密钥加密得到的字节码信息进行二次加密。Optionally, the ECB mode encrypts each plaintext block independently, which means that the encryption results obtained by encrypting plaintext blocks with the same content are the same. Therefore, the ECB mode is not suitable for encrypting long texts including repeated data. Therefore, when the number of keys is greater than or equal to the second preset threshold value 32, the code encryption system in the present application, in addition to performing secondary encryption on the bytecode information encrypted using instruction keys other than the first 16 instruction keys according to the ECB mode of the AES symmetric encryption algorithm through the target key, optionally, the code encryption system also uses the combination of the 17th to 32nd instruction keys generated by the encryptor as the target vector, and performs secondary encryption on the bytecode information encrypted using instruction keys after the 32nd instruction key according to the CBC (CipherBlock Chaining) mode of the AES symmetric encryption algorithm through the target key and the target vector.
可选地,相较于ECB模式,CBC模式的加密安全性更高,CBC模式对每个明文块的加密均依赖于对前一个明文块的加密结果,即CBC模式在每个加密块中引入了前一个密文块的影响,因此,即使对具有相同内容的两个明文块进行加密,得到两个密文块的内容也不同,这种链接式的加密方式能够提高加密结果的复杂度,从而降低信息泄露的风险,另外,CBC模式需要使用一个随机生成的初始化向量(即目标向量),初始化向量的随机性和独特性进一步提高了加密过程的复杂度,进而提高了代码加密系统所生成的目标字节码的安全性。Optionally, compared to the ECB mode, the CBC mode has higher encryption security. The encryption of each plaintext block in the CBC mode depends on the encryption result of the previous plaintext block, that is, the CBC mode introduces the influence of the previous ciphertext block in each encrypted block. Therefore, even if two plaintext blocks with the same content are encrypted, the contents of the two ciphertext blocks are different. This chained encryption method can increase the complexity of the encryption result, thereby reducing the risk of information leakage. In addition, the CBC mode requires the use of a randomly generated initialization vector (i.e., target vector). The randomness and uniqueness of the initialization vector further increase the complexity of the encryption process, thereby improving the security of the target bytecode generated by the code encryption system.
在一种可选的实施例中,在需要运行Python代码时,首先控制代码加密系统将目标字节码加载至解释器,在将目标字节码加载至解释器之后,代码加密系统检测目标字节码中的需要调用的函数的加密标识,其中,函数的加密标识用于表征函数对应的代码对象是否被加密,然后,在函数的加密标识为预设标识的情况下,通过调用函数对应的解密函数对函数对应的字节码进行解密,函数对应的解密函数为解密拓展库中存储的函数,之后,代码加密系统检测函数的调用次数,其中,调用次数用于表征函数被未完成运行的其他函数所调用的剩余次数,最后,在调用次数等于预设次数的情况下,代码加密系统控制加密器对函数对应的解密结果进行重新加密。In an optional embodiment, when it is necessary to run Python code, the code encryption system is first controlled to load the target bytecode into the interpreter. After the target bytecode is loaded into the interpreter, the code encryption system detects the encryption identifier of the function that needs to be called in the target bytecode, wherein the encryption identifier of the function is used to indicate whether the code object corresponding to the function is encrypted. Then, when the encryption identifier of the function is a preset identifier, the bytecode corresponding to the function is decrypted by calling the decryption function corresponding to the function, and the decryption function corresponding to the function is a function stored in the decryption extension library. Afterwards, the code encryption system detects the number of function calls, wherein the number of calls is used to indicate the remaining number of times the function is called by other functions that have not been completed. Finally, when the number of calls is equal to the preset number, the code encryption system controls the encryptor to re-encrypt the decryption result corresponding to the function.
在一些实施例中,加密标识即为代码对象的co_flags属性,预设标识用于表征函数的代码对象已被加密,预设次数可以设置为0。In some embodiments, the encryption flag is the co_flags attribute of the code object. The preset flag is used to indicate that the code object of the function has been encrypted. The preset number of times can be set to 0.
可选地,上述解密拓展库能够使用类装饰器实现,类装饰器用于装饰类而不是函数,类装饰器是一种函数或者可调用对象,类装饰器能够接受一个函数作为参数,并返回一个新的函数,从而实现在不修改原函数定义的情况下,对代码进行动态扩展或修改的目的。Optionally, the above-mentioned decryption extension library can be implemented using a class decorator, which is used to decorate a class instead of a function. A class decorator is a function or a callable object. A class decorator can accept a function as a parameter and return a new function, thereby achieving the purpose of dynamically expanding or modifying the code without modifying the original function definition.
可选地,在函数对应的调用次数等于0的情况下,即代表在目标代码的本次运行过程中,该函数不需要被再次调用,为了防止解密后的函数的字节码留存在内存中造成的Python源代码泄漏的问题,在该函数不再被使用时,通过加密器对该函数重新进行加密。Optionally, when the number of calls corresponding to the function is equal to 0, it means that the function does not need to be called again during the current execution of the target code. In order to prevent the problem of Python source code leakage caused by the decrypted function bytecode remaining in the memory, the function is re-encrypted by the encryptor when the function is no longer used.
由上述内容可知,本申请的技术方案通过对Python代码对应的字节码进行加密的方式,以实现对Python代码进行保护的目的。本申请首先将需要保护的Python代码编译为代码对象,之后,本申请将代码对象加密为目标字节码,从而得到Python代码对应的已经过加密的字节码文件(即目标字节码),从而实现了对Python代码的保护,同时,对比现有技术中未对Python代码进行保护,直接将Python代码转换为其对应的字节码文件的方式,本申请基于字节码的加密技术不会改变原来的Python代码的运行逻辑,即本申请的技术方案对Python代码进行加密的成本较低,不会造成额外的高成本的资源消耗。It can be seen from the above content that the technical solution of the present application realizes the purpose of protecting Python code by encrypting the bytecode corresponding to the Python code. The present application first compiles the Python code to be protected into a code object, and then encrypts the code object into a target bytecode, thereby obtaining an encrypted bytecode file (i.e., target bytecode) corresponding to the Python code, thereby realizing the protection of the Python code. At the same time, compared with the prior art in which the Python code is not protected and the Python code is directly converted into its corresponding bytecode file, the encryption technology based on bytecode of the present application will not change the running logic of the original Python code, that is, the technical solution of the present application has a low cost for encrypting the Python code, and will not cause additional high-cost resource consumption.
由此可见,本申请采用对Python代码对应的字节码进行加密的方式,实现了对Python代码进行保护的目的,即使攻击者在Python代码的运行过程中对Python代码对应的中间态文件进行了修改,由于攻击者无法获取密钥信息,经过攻击者修改后的Python代码也无法正常运行,从而实现了提高Python代码的安全性的技术效果,进而解决了Python语言在运行过程中能够动态编辑和Python解释器的开源特性所导致的Python代码的安全性低的技术问题。It can be seen that the present application adopts the method of encrypting the bytecode corresponding to the Python code to achieve the purpose of protecting the Python code. Even if the attacker modifies the intermediate file corresponding to the Python code during the running of the Python code, since the attacker cannot obtain the key information, the Python code modified by the attacker cannot run normally, thereby achieving the technical effect of improving the security of the Python code, and then solving the technical problem of low security of the Python code caused by the dynamic editing of the Python language during operation and the open source characteristics of the Python interpreter.
在一种可选的实施例中,图4是根据本申请实施例的一种可选的代码加密方法的框架图,如图4所示,本申请中的代码加密系统包括开发人员端使用的加密器和用户端使用的解密拓展库,其中,加密器能够将开发人员需要保护的Python源代码文件(即图4中的“Python代码”)转为加密的Python字节码文件(即图4中的“加密的Python代码”),并且加密器不会改变Python源代码的运行逻辑,之后,开发人员将加密的Python字节码文件及解密拓展库一同发布给用户,在用户需要运行Python代码时,通过用户的本地Python解释器即可正常执行该加密Python字节码文件,无需消耗额外的系统资源定制Python解释器,因此,本申请的技术方案不会因为需要对加密的Python字节码文件进行解密产生高额的系统开销。In an optional embodiment, Figure 4 is a framework diagram of an optional code encryption method according to an embodiment of the present application. As shown in Figure 4, the code encryption system in the present application includes an encryptor used by the developer side and a decryption extension library used by the user side, wherein the encryptor can convert the Python source code file that the developer needs to protect (i.e., the "Python code" in Figure 4) into an encrypted Python bytecode file (i.e., the "encrypted Python code" in Figure 4), and the encryptor will not change the running logic of the Python source code. Afterwards, the developer will publish the encrypted Python bytecode file and the decryption extension library to the user. When the user needs to run the Python code, the encrypted Python bytecode file can be executed normally through the user's local Python interpreter without consuming additional system resources to customize the Python interpreter. Therefore, the technical solution of the present application will not generate high system overhead due to the need to decrypt the encrypted Python bytecode file.
在一种可选的实施例中,图5是根据本申请实施例的另一种可选的代码加密方法的流程图,如图5所示,加密代码的步骤包括:In an optional embodiment, FIG. 5 is a flowchart of another optional code encryption method according to an embodiment of the present application. As shown in FIG. 5 , the steps of encrypting the code include:
步骤1:在读入Python源代码之后,通过预设的正则表达式对Python源代码进行匹配和修改。Step 1: After reading the Python source code, match and modify the Python source code using the preset regular expression.
如此,在实现在Python的代码运行过程中,在需要调用非库函数之前,预先调用解密拓展库中预设的解密函数对该非库函数对应的字节码信息进行解密的目的。In this way, during the execution of the Python code, before a non-library function needs to be called, the decryption function preset in the decryption extension library is called in advance to decrypt the bytecode information corresponding to the non-library function.
步骤2:将正则表达式修改后的代码编译为代码对象PyCodeObject。Step 2: Compile the code modified by the regular expression into a code object PyCodeObject.
步骤3:对PyCodeObject的co_consts进行扫描,在co_consts中的常量代表函数对象时,对该函数所嵌入的代码对象进行递归加密,在co_consts中的常量代表字符串或数字时,依据能够根据预设规则动态更新的异或密钥对该常量进行异或加密。Step 3: Scan the co_consts of PyCodeObject. When the constant in co_consts represents a function object, recursively encrypt the code object embedded in the function. When the constant in co_consts represents a string or a number, XOR encrypt the constant according to the XOR key that can be dynamically updated according to preset rules.
步骤4:对PyCodeObject的co_code进行扫描,使用指令密钥对字节码指令进行逐条异或加密(对应图5中的“扫描字节码并逐条异或”),并且在加密过程中,判断每条字节码指令是否为无参字节码指令(对应图5中的“指令为无参指令”),在该条字节码指令为无参字节码指令的情况下,将新的指令密钥隐藏在无参字节码指令的“\x00”冗余字节中(对应图5中的“使用新的异或密钥并隐藏”),并且控制加密器切换新的指令密钥对该条字节码指令之后的字节码指令进行异或加密。Step 4: Scan the co_code of PyCodeObject, use the instruction key to XOR encrypt the bytecode instructions one by one (corresponding to "Scan the bytecode and XOR one by one" in Figure 5), and during the encryption process, determine whether each bytecode instruction is a parameterless bytecode instruction (corresponding to "The instruction is a parameterless instruction" in Figure 5). If the bytecode instruction is a parameterless bytecode instruction, hide the new instruction key in the "\x00" redundant byte of the parameterless bytecode instruction (corresponding to "Use a new XOR key and hide" in Figure 5), and control the encryptor to switch the new instruction key to XOR encrypt the bytecode instructions after the bytecode instruction.
步骤5:控制加密器对字节码指令加密过程中所使用的每个指令密钥进行存储,并且统计加密器生成的指令密钥的总个数。在对PyCodeObject的co_code扫描完成的情况下,如果指令密钥的总个数大于或等于16,加密器要使用AES加密算法对部分加密结果进而二次加密;如果指令密钥的总个数小于16,加密器运行结束。Step 5: Control the encryptor to store each instruction key used in the bytecode instruction encryption process, and count the total number of instruction keys generated by the encryptor. When the co_code scan of the PyCodeObject is completed, if the total number of instruction keys is greater than or equal to 16, the encryptor will use the AES encryption algorithm to re-encrypt part of the encryption result; if the total number of instruction keys is less than 16, the encryptor ends.
在指令密钥的总个数大于或等于16并且小于32的情况下,则使用AES对称加密算法的ECB模式对加密器生成的除了前16个指令密钥之外的指令密钥加密得到的字节码信息进行二次加密,ECB模式的AES密钥为前16个指令密钥的组合;如果指令密钥的总个数大于或等于32,则使用AES对称加密算法的CBC模式对加密器生成的第32个指令密钥之后的指令密钥加密得到的字节码信息进行二次加密,CBC模式的AES密钥为前16个指令密钥的组合,CBC模式的初始化向量为第17个至第32个指令密钥的组合。需要注意的是,在依据AES对称加密算法进行加密的过程中,加密对象不涉及异或密钥隐藏的字节码部分。When the total number of instruction keys is greater than or equal to 16 and less than 32, the ECB mode of the AES symmetric encryption algorithm is used to encrypt the bytecode information generated by the encryptor with instruction keys other than the first 16 instruction keys, and the AES key in the ECB mode is the combination of the first 16 instruction keys; if the total number of instruction keys is greater than or equal to 32, the CBC mode of the AES symmetric encryption algorithm is used to encrypt the bytecode information generated by the encryptor with instruction keys after the 32nd instruction key, and the AES key in the CBC mode is the combination of the first 16 instruction keys, and the initialization vector in the CBC mode is the combination of the 17th to 32nd instruction keys. It should be noted that in the process of encryption according to the AES symmetric encryption algorithm, the encrypted object does not involve the bytecode part where the XOR key is hidden.
另外,图5中的解密拓展库使用类装饰器实现,在函数调用时解密,在函数不再被使用时对函数进行重新加密,进而防止解密后的函数字节码留存在内存中造成泄漏Python源代码的风险,该类装饰器在函数调用时会检测函数的co_flags,如co_flags中有加密标识则对该函数进行解密;在加密和解密过程中,本申请的技术方案对函数的引用计数表进行维护,当某个函数的引用计数为0时,代表该函数不再需要被使用,此时,控制加密器对该函数进行重新加密。In addition, the decryption extension library in Figure 5 is implemented using a class decorator, which decrypts when the function is called and re-encrypts the function when the function is no longer used, thereby preventing the decrypted function bytecode from remaining in the memory and causing the risk of leaking the Python source code. This class decorator will detect the co_flags of the function when the function is called. If there is an encryption flag in co_flags, the function will be decrypted; during the encryption and decryption process, the technical solution of the present application maintains a reference count table of the function. When the reference count of a function is 0, it means that the function no longer needs to be used. At this time, the control encryptor re-encrypts the function.
由上述内容可知,解密拓展库虽然也是二进制文件,存在被攻击者通过反编译方式进行破解的风险,但是,本申请中的异或加密和对称加密所使用的密钥信息均动态嵌入到了加密字节码中,本申请中的解密拓展库不涉及密钥信息,另外,解密拓展库需要使用Python语言和/或C语言对应的API(Application Programming Interface,应用程序接口)进行转换编译,相比于普通二进制文件,本申请中对解密拓展库进行的破解成本也得到了提高。From the above content, it can be seen that although the decryption extension library is also a binary file, there is a risk of being cracked by attackers through decompilation. However, the key information used for XOR encryption and symmetric encryption in this application is dynamically embedded in the encrypted bytecode, and the decryption extension library in this application does not involve key information. In addition, the decryption extension library needs to use the corresponding API (Application Programming Interface) of Python language and/or C language for conversion and compilation. Compared with ordinary binary files, the cost of cracking the decryption extension library in this application has also been increased.
可选地,一种关于本申请的技术方案的应用场景举例如下:开发人员编写的一款软件是由Python语言开发的,该软件是一款用于从数据库中随机选取成员数据的软件,能够应用于课堂随机点名、企业年会抽奖等实际场景。该开发人员希望将该款软件发布给用户自由使用,但是,一旦被攻击者破解该软件并获得其中的随机数算法或者篡改其随机数算法,那么该软件的抽取结果就有可能被人为预测或改变,从而丧失了其公平性和随机性。Optionally, an application scenario of the technical solution of the present application is exemplified as follows: a software written by a developer is developed in Python language, and the software is a software for randomly selecting member data from a database, which can be applied to actual scenarios such as random roll call in class and lottery at corporate annual meetings. The developer hopes to release the software to users for free use, but once the software is cracked by an attacker and the random number algorithm therein is obtained or tampered with, the extraction result of the software may be artificially predicted or changed, thereby losing its fairness and randomness.
因此,开发人员能够采用本申请的技术方案对该软件中所使用的随机数算法进行保护,上述软件开发人员不需要学习额外的代码加密技术,采取本方案能够简单快捷地实现对软件进行保护的目的,进而降低了开发人员对代码加密方法的学习成本,同时,增加了攻击者对软件的随机数算法进行破解的成本。另外,在使用本申请的技术方案对该软件进行加密之后,该软件不能通过常规的Python反编译工具和二进制逆向工具进行破解,从而提高了该软件所使用的随机数算法的安全性。Therefore, developers can use the technical solution of the present application to protect the random number algorithm used in the software. The above-mentioned software developers do not need to learn additional code encryption technology. The present solution can simply and quickly achieve the purpose of protecting the software, thereby reducing the learning cost of the developer for the code encryption method, and at the same time, increasing the cost for attackers to crack the random number algorithm of the software. In addition, after the software is encrypted using the technical solution of the present application, the software cannot be cracked by conventional Python decompilation tools and binary reverse tools, thereby improving the security of the random number algorithm used by the software.
由上述内容可知,本申请的技术方案通过字节码结构实现了对加密过程中所使用的动态密钥信息进行隐藏保护的目的,并且,本申请通过对字节码进行异或加密和对称加密的方式,提高了Python源代码的安全性。From the above content, it can be seen that the technical solution of the present application achieves the purpose of hiding and protecting the dynamic key information used in the encryption process through the bytecode structure, and the present application improves the security of the Python source code by performing XOR encryption and symmetric encryption on the bytecode.
可选地,本申请的技术方案能够保护软件的Python源代码能够实现确保该软件的核心逻辑不被攻击者盗用,从而保护该软件的知识产权的目的;同时,还能够防止软件被攻击者篡改源代码来达到破解、绕过检查的目的;并且,可保护软件中使用的商业机密,如重要数据结构、核心算法、机密数据等;可防止攻击者对软件的源代码进行安全审计,降低软件被安全性攻击的风险。Optionally, the technical solution of the present application can protect the Python source code of the software to ensure that the core logic of the software is not stolen by attackers, thereby protecting the intellectual property rights of the software; at the same time, it can also prevent the software from being tampered with by attackers to achieve the purpose of cracking and bypassing inspection; and it can protect the commercial secrets used in the software, such as important data structures, core algorithms, confidential data, etc.; it can prevent attackers from performing security audits on the source code of the software, thereby reducing the risk of the software being attacked by security attacks.
根据本申请实施例的另一方面,还提供了一种代码加密装置用于执行本申请中的代码加密方法。图6是根据本申请实施例的一种可选的代码加密装置的示意图,如图6所示,代码加密装置包括:编译单元601和加密单元602。According to another aspect of the embodiment of the present application, a code encryption device is also provided for executing the code encryption method in the present application. FIG6 is a schematic diagram of an optional code encryption device according to an embodiment of the present application. As shown in FIG6 , the code encryption device includes: a compilation unit 601 and an encryption unit 602.
可选地,编译单元,用于对目标代码进行编译,得到目标代码对应的代码对象,其中,目标代码为需要保护的Python代码;加密单元,用于将代码对象加密为目标字节码,其中,目标字节码用于以字节码的形式表征加密后的目标代码,并且目标字节码中包括加密过程中动态生成的密钥信息。Optionally, a compilation unit is used to compile the target code to obtain a code object corresponding to the target code, wherein the target code is the Python code to be protected; an encryption unit is used to encrypt the code object into a target bytecode, wherein the target bytecode is used to represent the encrypted target code in the form of a bytecode, and the target bytecode includes key information dynamically generated during the encryption process.
可选地,在通过编译单元对目标代码进行编译之前,代码加密装置首先获取需要保护的Python代码,之后,代码加密装置通过预设的正则表达式将需要保护的Python代码更新为目标代码,正则表达式能够为原Python代码中的非库函数的调用添加对应的解密函数,并且,代码加密装置将生成的解密函数存储至解密拓展库中,以便在后续通过Python官方解释器对加密后的字节码(即目标字节码)进行解释执行的过程中,本申请的技术方案能够实现通过解密拓展库对加密后的字节码文件进行解密的目的。Optionally, before compiling the target code through the compilation unit, the code encryption device first obtains the Python code to be protected, and then the code encryption device updates the Python code to be protected to the target code through a preset regular expression. The regular expression can add a corresponding decryption function for the call of the non-library function in the original Python code, and the code encryption device stores the generated decryption function in the decryption extension library, so that in the subsequent process of interpreting and executing the encrypted bytecode (i.e., the target bytecode) through the Python official interpreter, the technical solution of the present application can achieve the purpose of decrypting the encrypted bytecode file through the decryption extension library.
可选地,代码加密装置能够通过预先设置的加密器执行对代码对象的加密操作,其中,加密器至少用于根据动态生成的密钥信息对代码对象的关键属性进行异或加密,并且,在异或加密过程中生成的异或密钥的总数量大于或等于预设阈值的情况下,加密器还会对部分代码对象的关键属性进行对称加密,从而实现了提高对python代码进行加密的复杂度以及生成的目标字节码的破解成本的技术效果。Optionally, the code encryption device can perform encryption operations on the code object through a preset encryptor, wherein the encryptor is at least used to perform XOR encryption on key attributes of the code object based on dynamically generated key information, and when the total number of XOR keys generated during the XOR encryption process is greater than or equal to a preset threshold, the encryptor will also perform symmetrical encryption on key attributes of some code objects, thereby achieving the technical effect of increasing the complexity of encrypting the Python code and the cracking cost of the generated target bytecode.
可选地,在加密单元中,代码加密装置并不是将加密过程中动态生成的密钥信息直接存储至目标代码对应的字节码文件中,而是对动态生成的密钥信息进行异或处理后隐藏存储至目标代码对应的字节码文件中,从而避免攻击者能够直接从字节码文件中直接获取代码加密装置所使用的密钥信息,进而提高了攻击者的攻击成本,提高了目标代码的安全性。Optionally, in the encryption unit, the code encryption device does not directly store the key information dynamically generated during the encryption process into the bytecode file corresponding to the target code, but performs XOR processing on the dynamically generated key information and then hides and stores it in the bytecode file corresponding to the target code, thereby preventing attackers from directly obtaining the key information used by the code encryption device from the bytecode file, thereby increasing the attack cost of attackers and improving the security of the target code.
在一种可选的实施例中,加密单元包括:第一扫描子单元、第一加密子单元以及第二加密子单元。In an optional embodiment, the encryption unit includes: a first scanning subunit, a first encryption subunit and a second encryption subunit.
可选地,第一扫描子单元,用于对代码对象的第一属性进行扫描,得到L个常量,其中,L为正整数,第一属性用于表征代码对象的常量元组;第一加密子单元,用于在L个常量中的第i个常量为函数类型的情况下,对第i个常量所嵌入的代码对象进行递归加密,其中,i为小于或等于L的正整数;第二加密子单元,用于在第i个常量为字符串类型或数字类型的情况下,对第i个常量进行异或加密。Optionally, a first scanning subunit is used to scan a first attribute of a code object to obtain L constants, where L is a positive integer and the first attribute is used to characterize a constant tuple of the code object; a first encryption subunit is used to, when the i-th constant among the L constants is a function type, recursively encrypt the code object in which the i-th constant is embedded, where i is a positive integer less than or equal to L; and a second encryption subunit is used to, when the i-th constant is a string type or a numeric type, perform XOR encryption on the i-th constant.
可选地,代码加密装置通过对代码对象的第一属性进行异或加密,实现了提高代码对象的第一属性的安全性的目的,即使在第一属性对应的数据被非法访问或泄露的情况下,加密后的第一属性数据也不易被攻击者解读,从而实现了提高代码对象的第一属性所对应的python源代码的安全性的目的。Optionally, the code encryption device achieves the purpose of improving the security of the first attribute of the code object by XOR encryption of the first attribute of the code object. Even if the data corresponding to the first attribute is illegally accessed or leaked, the encrypted first attribute data is not easily deciphered by attackers, thereby achieving the purpose of improving the security of the Python source code corresponding to the first attribute of the code object.
在一种可选的实施例中,在第i个常量为字符串类型的情况下,第二加密子单元包括:字节划分模块、密钥生成模块、异或处理模块以及确定模块。In an optional embodiment, when the i-th constant is of a string type, the second encryption subunit includes: a byte division module, a key generation module, an XOR processing module and a determination module.
可选地,字节划分模块,用于将第i个常量划分为M个字节,其中,M为正整数;密钥生成模块,用于动态生成M个异或密钥,其中,M个异或密钥与M个字节一一对应,并且M个异或密钥中的第m+1个异或密钥是基于预设规则对第m个异或密钥进行更新得到的,其中,m为小于M的正整数;异或处理模块,用于对M个异或密钥中的每个异或密钥和该异或密钥对应的字节进行异或处理,得到M个字节对应的所有异或结果;确定模块,用于将M个字节对应的所有异或结果作为第i个常量的异或加密结果。Optionally, a byte division module is used to divide the i-th constant into M bytes, where M is a positive integer; a key generation module is used to dynamically generate M XOR keys, where the M XOR keys correspond one-to-one to the M bytes, and the m+1-th XOR key among the M XOR keys is obtained by updating the m-th XOR key based on a preset rule, where m is a positive integer less than M; an XOR processing module is used to perform XOR processing on each XOR key in the M XOR keys and the byte corresponding to the XOR key to obtain all XOR results corresponding to the M bytes; and a determination module is used to use all XOR results corresponding to the M bytes as the XOR encryption result of the i-th constant.
在一种可选的实施例中,在第i个常量为数字类型的情况下,第二加密子单元直接通过异或处理模块,依据预设的加密器动态生成的异或密钥对数字类型的第i个常量进行异或处理,之后,通过确定模块将异或处理得到的异或结果作为第i个常量的异或加密结果。In an optional embodiment, when the i-th constant is of digital type, the second encryption sub-unit directly performs XOR processing on the i-th constant of digital type through the XOR processing module according to the XOR key dynamically generated by the preset encryptor, and then uses the XOR result obtained by the XOR processing as the XOR encryption result of the i-th constant through the determination module.
可选地,代码加密装置依据加密器动态生成的密钥信息对代码对象的第一属性进行异或处理,从而实现对代码对象的第一属性进行加密的目的,异或处理操作是一种简单的位运算,异或处理操作只涉及到两个操作数和一个位操作,能够通过计算机的基础硬件实现,异或处理操作适合于资源受限或需要快速实现的应用场景,从而实现了不额外增加对代码进行加密的资源开销的技术效果。Optionally, the code encryption device performs XOR processing on the first attribute of the code object based on the key information dynamically generated by the encryptor, thereby achieving the purpose of encrypting the first attribute of the code object. The XOR processing operation is a simple bit operation. The XOR processing operation only involves two operands and a bit operation, which can be implemented through the basic hardware of the computer. The XOR processing operation is suitable for application scenarios with limited resources or that need to be implemented quickly, thereby achieving the technical effect of not adding additional resource overhead for encrypting the code.
在一种可选的实施例中,加密单元还包括:第二扫描子单元、指令密钥确定子单元、第三加密子单元以及第四加密子单元。In an optional embodiment, the encryption unit further includes: a second scanning subunit, an instruction key determination subunit, a third encryption subunit and a fourth encryption subunit.
可选地,第二扫描子单元,用于对代码对象的第二属性进行扫描,得到N个字节码指令,其中,N为正整数,第二属性用于表征代码对象的字节码指令序列;指令密钥确定子单元,用于将对代码对象的常量进行异或加密的过程中动态生成的最后一个异或密钥作为指令密钥的初始值,其中,指令密钥用于对字节码指令进行异或加密,字节码指令对应的冗余字节用于存储指令密钥的最新值;第三加密子单元,用于在N个字节码指令中的第j个字节码指令包括参数的情况下,依据指令密钥的最新值对第j个字节码指令进行异或加密,并且禁止对指令密钥的最新值进行更新,其中,j为小于或等于N的正整数;第四加密子单元,用于在第j个字节码指令不包括参数的情况下,依据指令密钥的最新值对第j个字节码指令进行异或加密,并且将指令密钥的最新值更新为从预设范围中选取的随机数。Optionally, a second scanning subunit is used to scan the second attribute of the code object to obtain N bytecode instructions, wherein N is a positive integer, and the second attribute is used to characterize the bytecode instruction sequence of the code object; an instruction key determination subunit is used to use the last XOR key dynamically generated in the process of XOR encryption of the constant of the code object as the initial value of the instruction key, wherein the instruction key is used to XOR encrypt the bytecode instruction, and the redundant bytes corresponding to the bytecode instruction are used to store the latest value of the instruction key; a third encryption subunit is used to XOR encrypt the jth bytecode instruction according to the latest value of the instruction key when the jth bytecode instruction among the N bytecode instructions includes parameters, and prohibit updating the latest value of the instruction key, wherein j is a positive integer less than or equal to N; a fourth encryption subunit is used to XOR encrypt the jth bytecode instruction according to the latest value of the instruction key when the jth bytecode instruction does not include parameters, and update the latest value of the instruction key to a random number selected from a preset range.
可选地,相比较将新生成的指令密钥不进行异或加密,直接存储至字节码指令的冗余字节所在位置的方式,代码加密装置在将新生成的指令密钥存储至字节码指令的冗余字节之后,基于旧的指令密钥对整条字节码指令(包括冗余字节)进行异或加密,从而提高了冗余字节中所存储的密钥信息的安全性,进而提高了攻击者获取原始密钥信息的难度。Optionally, compared with the method of not performing XOR encryption on the newly generated instruction key and directly storing it at the location of the redundant bytes of the bytecode instruction, the code encryption device stores the newly generated instruction key in the redundant bytes of the bytecode instruction, and then performs XOR encryption on the entire bytecode instruction (including the redundant bytes) based on the old instruction key, thereby improving the security of the key information stored in the redundant bytes, and further increasing the difficulty for attackers to obtain the original key information.
在一种可选的实施例中,加密单元还包括:第一判断子单元、目标密钥生成子单元以及第五加密子单元。In an optional embodiment, the encryption unit further includes: a first judgment subunit, a target key generation subunit and a fifth encryption subunit.
可选地,第一判断子单元,用于判断密钥数量是否大于或等于第一预设阈值,其中,密钥数量为对字节码指令进行异或加密的过程中动态生成的指令密钥的总数量;目标密钥生成子单元,用于在密钥数量大于或等于第一预设阈值的情况下,依据P个指令密钥生成目标密钥,其中,P个指令密钥为所有指令密钥中的生成时刻排名为前P个的指令密钥,P等于第一预设阈值;第五加密子单元,用于依据目标密钥对第一加密结果进行对称加密,其中,第一加密结果为通过第一集合中的指令密钥对代码对象的关键属性进行异或加密得到的结果,代码对象的关键属性至少包括第二属性,第一集合为所有指令密钥中除P个指令密钥之外的指令密钥组成的集合。Optionally, a first judgment subunit is used to judge whether the number of keys is greater than or equal to a first preset threshold, wherein the number of keys is the total number of instruction keys dynamically generated in the process of XOR encryption of bytecode instructions; a target key generation subunit is used to generate a target key based on P instruction keys when the number of keys is greater than or equal to the first preset threshold, wherein the P instruction keys are the instruction keys ranked as the top P in terms of generation time among all instruction keys, and P is equal to the first preset threshold; a fifth encryption subunit is used to symmetrically encrypt a first encryption result based on the target key, wherein the first encryption result is a result obtained by XOR-encrypting a key attribute of a code object using instruction keys in a first set, the key attribute of the code object includes at least a second attribute, and the first set is a set of instruction keys other than the P instruction keys among all instruction keys.
可选地,代码加密装置不仅能够控制加密器对代码对象的关键属性进行异或加密,还能够在异或加密的基础上,基于AES对称加密算法对异或加密得到的部分加密结果进行二次加密。异或加密方式虽然操作简单快捷,但其安全性相对较低,通过使用AES对称加密算法对异或操作的结果进行二次加密,从而实现了增强数据的保密性和安全性的技术效果。Optionally, the code encryption device can not only control the encryptor to perform XOR encryption on the key attributes of the code object, but also perform secondary encryption on the partial encryption results obtained by the XOR encryption based on the AES symmetric encryption algorithm. Although the XOR encryption method is simple and fast to operate, its security is relatively low. By using the AES symmetric encryption algorithm to perform secondary encryption on the result of the XOR operation, the technical effect of enhancing the confidentiality and security of the data is achieved.
在一种可选的实施例中,加密单元还包括:第二判断子单元、目标向量生成子单元以及第六加密子单元。In an optional embodiment, the encryption unit further includes: a second judgment subunit, a target vector generation subunit and a sixth encryption subunit.
可选地,第二判断子单元,用于判断密钥数量是否大于或等于第二预设阈值,其中,第二预设阈值大于第一预设阈值;目标向量生成子单元,用于在密钥数量大于或等于第二预设阈值的情况下,依据Q个指令密钥生成目标向量,其中,Q为正整数,Q个指令密钥为所有指令密钥中的生成时刻排名为第P+1个至第R个的指令密钥,P等于第一预设阈值,R等于第二预设阈值;第六加密子单元,用于依据目标向量和目标密钥对第二加密结果进行对称加密,其中,第二加密结果为通过第二集合中的指令密钥对代码对象的关键属性进行异或加密得到的结果,第二集合为所有指令密钥中除P个指令密钥和Q个指令密钥之外的指令密钥组成的集合。Optionally, a second judgment subunit is used to judge whether the number of keys is greater than or equal to a second preset threshold, wherein the second preset threshold is greater than the first preset threshold; a target vector generation subunit is used to generate a target vector based on Q instruction keys when the number of keys is greater than or equal to the second preset threshold, wherein Q is a positive integer, and the Q instruction keys are instruction keys ranked from P+1th to Rth in generation time among all instruction keys, P is equal to the first preset threshold, and R is equal to the second preset threshold; a sixth encryption subunit is used to symmetrically encrypt a second encryption result based on the target vector and the target key, wherein the second encryption result is a result obtained by XOR-encrypting a key attribute of the code object using the instruction keys in the second set, and the second set is a set of instruction keys other than P instruction keys and Q instruction keys among all instruction keys.
在一种可选的实施例中,代码加密装置还包括:第一检测单元、解密单元、第二检测单元以及重加密单元。In an optional embodiment, the code encryption device further includes: a first detection unit, a decryption unit, a second detection unit and a re-encryption unit.
可选地,第一检测单元,用于在将目标字节码加载至解释器之后,检测目标字节码中的需要调用的函数的加密标识,其中,函数的加密标识用于表征函数对应的代码对象是否被加密;解密单元,用于在函数的加密标识为预设标识的情况下,通过调用函数对应的解密函数对函数对应的字节码进行解密,函数对应的解密函数为解密拓展库中存储的预先通过正则表达式生成的函数;第二检测单元,用于检测函数的调用次数,其中,调用次数用于表征在目标代码运行结束之前函数还需要执行的总次数;重加密单元,用于在调用次数等于预设次数的情况下,对函数对应的解密结果进行重新加密。Optionally, a first detection unit is used to detect the encryption identifier of a function to be called in the target bytecode after the target bytecode is loaded into the interpreter, wherein the encryption identifier of the function is used to indicate whether the code object corresponding to the function is encrypted; a decryption unit is used to decrypt the bytecode corresponding to the function by calling the decryption function corresponding to the function when the encryption identifier of the function is a preset identifier, and the decryption function corresponding to the function is a function pre-generated by a regular expression and stored in a decryption extension library; a second detection unit is used to detect the number of function calls, wherein the number of calls is used to indicate the total number of times the function needs to be executed before the target code ends; a re-encryption unit is used to re-encrypt the decryption result corresponding to the function when the number of calls is equal to the preset number.
可选地,预设次数能够设置为0,在函数对应的调用次数等于0的情况下,即代表在目标代码的本次运行过程中,该函数不需要被再次调用,为了防止解密后的函数的字节码留存在内存中造成的Python源代码泄漏的问题,在该函数不再被使用时,通过加密器对该函数重新进行加密。Optionally, the preset number of times can be set to 0. When the number of calls corresponding to the function is equal to 0, it means that the function does not need to be called again during the current execution of the target code. In order to prevent the problem of Python source code leakage caused by the bytecode of the decrypted function remaining in the memory, the function is re-encrypted by the encryptor when it is no longer used.
由上述内容可知,代码加密装置通过对Python代码对应的字节码进行加密的方式,以实现对Python代码进行保护的目的。代码加密装置首先将需要保护的Python代码编译为代码对象,之后,代码加密装置将代码对象加密为目标字节码,从而得到Python代码对应的已经过加密的字节码文件(即目标字节码),从而实现了对Python代码的保护,同时,对比现有技术中未对Python代码进行保护,直接将Python代码转换为其对应的字节码文件的方式,代码加密装置所使用的基于字节码的加密技术不会改变原来的Python代码的运行逻辑,即通过代码加密装置对Python代码进行加密的成本较低,不会造成额外的高成本的资源消耗。As can be seen from the above content, the code encryption device achieves the purpose of protecting the Python code by encrypting the bytecode corresponding to the Python code. The code encryption device first compiles the Python code to be protected into a code object, and then encrypts the code object into a target bytecode, thereby obtaining an encrypted bytecode file corresponding to the Python code (i.e., the target bytecode), thereby achieving the protection of the Python code. At the same time, compared with the prior art in which the Python code is not protected and the Python code is directly converted into its corresponding bytecode file, the bytecode-based encryption technology used by the code encryption device will not change the original Python code's operating logic, that is, the cost of encrypting the Python code through the code encryption device is low, and will not cause additional high-cost resource consumption.
由此可见,代码加密装置采用对Python代码对应的字节码进行加密的方式,实现了对Python代码进行保护的目的,即使攻击者在Python代码的运行过程中对Python代码对应的中间态文件进行了修改,由于攻击者无法获取密钥信息,经过攻击者修改后的Python代码也无法正常运行,从而实现了提高Python代码的安全性的技术效果,进而解决了Python语言在运行过程中能够动态编辑和Python解释器的开源特性所导致的Python代码的安全性低的技术问题。It can be seen that the code encryption device adopts the method of encrypting the bytecode corresponding to the Python code to achieve the purpose of protecting the Python code. Even if the attacker modifies the intermediate file corresponding to the Python code during the running of the Python code, the attacker cannot obtain the key information, and the Python code modified by the attacker cannot run normally, thereby achieving the technical effect of improving the security of the Python code, and then solving the technical problem of low security of the Python code caused by the dynamic editing of the Python language during operation and the open source characteristics of the Python interpreter.
根据本申请实施例的另一方面,还提供了一种计算机可读存储介质,计算机可读存储介质包括存储的计算机程序,其中,在计算机程序运行时控制计算机可读存储介质所在设备执行上述任意一项的代码加密方法。According to another aspect of an embodiment of the present application, a computer-readable storage medium is also provided, the computer-readable storage medium including a stored computer program, wherein when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute any one of the above-mentioned code encryption methods.
根据本申请实施例的另一方面,还提供了一种电子设备,包括:处理器;以及存储器,用于存储处理器的可执行指令;其中,处理器配置为经由执行可执行指令来执行上述任意一项的代码加密方法。According to another aspect of an embodiment of the present application, an electronic device is also provided, including: a processor; and a memory for storing executable instructions of the processor; wherein the processor is configured to execute any one of the above-mentioned code encryption methods by executing the executable instructions.
图7是根据本申请实施例的一种可选的电子设备的示意图,如图7所示,本申请实施例提供了一种电子设备,电子设备包括处理器、存储器及存储在存储器上并可在处理器上运行的程序,处理器执行程序时实现上述任意一项的代码加密方法。Figure 7 is a schematic diagram of an optional electronic device according to an embodiment of the present application. As shown in Figure 7, an embodiment of the present application provides an electronic device, which includes a processor, a memory, and a program stored in the memory and executable on the processor. When the processor executes the program, any one of the above-mentioned code encryption methods is implemented.
本申请公开的上述实施方式或实施例并非穷举,仅为部分实施方式或实施例的示意,不作为对本申请公开保护范围的具体限制。在不矛盾的情况下,本申请中的某一个实施方式或实施例中的每个步骤均可以作为独立实施例来实施,且各步骤之间可以任意组合,例如,在某一个实施方式或实施例中去除部分步骤后的方案也可以作为独立实施例来实施,且在某一个实施方式或实施例中各步骤的顺序可以任意交换,另外,某一个实施方式或实施例中的可选方式或可选例可以任意组合;此外,各实施方式或者各个实施例之间可以任意组合,例如,不同实施方式或实施例的部分或全部步骤可以任意组合,某一个实施方式或实施例可以与其他实施方式或实施例的可选方式或可选例任意组合。The above-mentioned embodiments or examples disclosed in the present application are not exhaustive, but are only illustrative of some embodiments or examples, and are not intended to be specific limitations on the scope of protection disclosed in the present application. In the absence of contradiction, each step in a certain embodiment or example in the present application can be implemented as an independent example, and the steps can be combined arbitrarily. For example, the scheme after removing some steps in a certain embodiment or example can also be implemented as an independent example, and the order of the steps in a certain embodiment or example can be arbitrarily exchanged. In addition, the optional methods or optional examples in a certain embodiment or example can be combined arbitrarily; in addition, the various embodiments or examples can be combined arbitrarily, for example, some or all steps of different embodiments or examples can be combined arbitrarily, and a certain embodiment or example can be combined arbitrarily with the optional methods or optional examples of other embodiments or examples.
在本申请的上述实施例中,对各个实施例的描述都各有侧重,某个实施例中没有详述的部分,可以参见其他实施例的相关描述。In the above embodiments of the present application, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, please refer to the relevant description of other embodiments.
本申请是参照根据本申请实施例的方法、设备(系统)、和计算机程序产品的流程图和/或方框图来描述的。应理解可由计算机程序指令实现流程图和/或方框图中的每一流程和/或方框、以及流程图和/或方框图中的流程和/或方框的结合。可提供这些计算机程序指令到通用计算机、专用计算机、嵌入式处理机或其他可编程数据处理设备的处理器以产生一个机器,使得通过计算机或其他可编程数据处理设备的处理器执行的指令产生用于实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能的装置。The present application is described with reference to the flowchart and/or block diagram of the method, device (system) and computer program product according to the embodiment of the present application. It should be understood that each process and/or box in the flowchart and/or block diagram, and the combination of the process and/or box in the flowchart and/or block diagram can be realized by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device produce a device for realizing the function specified in one process or multiple processes in the flowchart and/or one box or multiple boxes in the block diagram.
这些计算机程序指令也可存储在能引导计算机或其他可编程数据处理设备以特定方式工作的计算机可读存储器中,使得存储在该计算机可读存储器中的指令产生包括指令装置的制造品,该指令装置实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能。These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer-readable memory produce a manufactured product including an instruction device that implements the functions specified in one or more processes in the flowchart and/or one or more boxes in the block diagram.
这些计算机程序指令也可装载到计算机或其他可编程数据处理设备上,使得在计算机或其他可编程设备上执行一系列操作步骤以产生计算机实现的处理,从而在计算机或其他可编程设备上执行的指令提供用于实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能的步骤。These computer program instructions may also be loaded onto a computer or other programmable data processing device so that a series of operational steps are executed on the computer or other programmable device to produce a computer-implemented process, whereby the instructions executed on the computer or other programmable device provide steps for implementing the functions specified in one or more processes in the flowchart and/or one or more boxes in the block diagram.
在一个典型的配置中,计算设备包括一个或多个处理器(CPU)、输入/输出接口、网络接口和内存。存储器可能包括计算机可读介质中的非永久性存储器,随机存取存储器(RAM)和/或非易失性内存等形式,如只读存储器(ROM)或闪存(flash RAM)。存储器是计算机可读介质的示例。In a typical configuration, a computing device includes one or more processors (CPU), input/output interfaces, network interfaces, and memory. The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and/or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.
计算机可读介质包括永久性和非永久性、可移动和非可移动媒体可以由任何方法或技术来实现信息存储。信息可以是计算机可读指令、数据结构、程序的模块或其他数据。计算机的存储介质的例子包括,但不限于相变内存(PRAM)、静态随机存取存储器(SRAM)、动态随机存取存储器(DRAM)、其他类型的随机存取存储器(RAM)、只读存储器(ROM)、电可擦除可编程只读存储器(EEPROM)、快闪记忆体或其他内存技术、只读光盘只读存储器(CD-ROM)、数字多功能光盘(DVD)或其他光学存储、磁盒式磁带,磁带式磁盘存储或其他磁性存储设备或任何其他非传输介质,可用于存储可以被计算设备访问的信息。按照本文中的界定,计算机可读介质不包括暂存电脑可读媒体(transitory media),如调制的数据信号和载波。Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.
还需要说明的是,术语“包括”、“包含”或者其任何其他变体意在涵盖非排他性的包含,从而使得包括一系列要素的过程、方法、商品或者设备不仅包括那些要素,而且还包括没有明确列出的其他要素,或者是还包括为这种过程、方法、商品或者设备所固有的要素。在没有更多限制的情况下,由语句“包括一个……”限定的要素,并不排除在包括要素的过程、方法、商品或者设备中还存在另外的相同要素。It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.
本领域技术人员应明白,本申请的实施例可提供为方法、系统或计算机程序产品。因此,本申请可采用完全硬件实施例、完全软件实施例或结合软件和硬件方面的实施例的形式。而且,本申请可采用在一个或多个其中包含有计算机可用程序代码的计算机可用存储介质(包括但不限于磁盘存储器、CD-ROM、光学存储器等)上实施的计算机程序产品的形式。Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.
以上仅为本申请的实施例而已,并不用于限制本申请。对于本领域技术人员来说,本申请可以有各种更改和变化。凡在本申请的精神和原理之内所作的任何修改、等同替换、改进等,均应包含在本申请的权利要求范围之内。The above are only embodiments of the present application and are not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included within the scope of the claims of the present application.
Claims (10)
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202410961915.2A CN118916852A (en) | 2024-07-17 | 2024-07-17 | Code encryption method, device, program product and electronic equipment |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202410961915.2A CN118916852A (en) | 2024-07-17 | 2024-07-17 | Code encryption method, device, program product and electronic equipment |
Publications (1)
Publication Number | Publication Date |
---|---|
CN118916852A true CN118916852A (en) | 2024-11-08 |
Family
ID=93305934
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202410961915.2A Pending CN118916852A (en) | 2024-07-17 | 2024-07-17 | Code encryption method, device, program product and electronic equipment |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN118916852A (en) |
-
2024
- 2024-07-17 CN CN202410961915.2A patent/CN118916852A/en active Pending
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US7549147B2 (en) | Security framework for protecting rights in computer software | |
Behera et al. | Different obfuscation techniques for code protection | |
EP1376310A2 (en) | Secure and opaque type library providing secure data protection of variables | |
CN110826031B (en) | Encryption method, device, computer equipment and storage medium | |
CN101751529A (en) | Method and apparatus for the secure processing of confidential content within a virtual machine of a processor | |
CN107273723B (en) | So file shell adding-based Android platform application software protection method | |
CN105022936A (en) | Class file encryption and decryption method and class file encryption and decryption device | |
US20120144208A1 (en) | Indexed table based code encrypting/decrypting device and method thereof | |
US7970133B2 (en) | System and method for secure and flexible key schedule generation | |
CN111814162B (en) | Kernel sensitive data protection method based on customized hardware security attribute | |
US10331896B2 (en) | Method of protecting secret data when used in a cryptographic algorithm | |
CN108134673A (en) | A kind of method and device for generating whitepack library file | |
GB2576755A (en) | System and method for providing protected data storage in a data memory | |
CN111475168B (en) | A code compilation method and device | |
CN106209346A (en) | Whitepack cryptographic technique is interlocked look-up table | |
CN108875321A (en) | A kind of generation method of instruction set, device and electronic equipment | |
Lee et al. | Classification and analysis of security techniques for the user terminal area in the Internet banking service | |
CN113626773B (en) | Code protection method based on intermediate language | |
CN117150515B (en) | Safety protection method for EDA secondary development source code, electronic equipment and storage medium | |
CN118916852A (en) | Code encryption method, device, program product and electronic equipment | |
KR101999209B1 (en) | A system and method for encryption of pointers to virtual function tables | |
CN104866740A (en) | Static analysis preventing method and device for files | |
EP3944106A1 (en) | Obfuscating method of protecting code | |
d’Amore et al. | Source Code Anti-Plagiarism: AC# Implementation Using the Routing Approach | |
CN113360859B (en) | Python interpreter-based encrypted file security control method and device |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |