Anti-replay method based on WebAssbly coding without caching middleware
By using WebAssembly-encoded nonce random code generation algorithm on the client and server, user keys are generated and updated, and the vulnerabilities in time synchronization and cache management in the existing anti-replay method are solved, and efficient and secure anti-replay is achieved. Attack defense.
Patent Information
- Application Number
- CN202310614599.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-05-26
- Publication Date
- 2024-11-26
AI Technical Summary
The existing anti-replay attack methods have vulnerabilities in client and server time synchronization and cache management, and the client algorithm is easy to be cracked and cannot effectively resist replay attacks.
Using the nonce random code generation algorithm based on WebAssembly encoding, the user key is generated by using the same seed on the client and the server. The client uses the user key to encrypt the data every time the client requests, and the server decrypts and verifys it. If the key matches Generate a new key and update the dual-ended key to avoid cache dependencies and timestamp synchronization.
It realizes deployment that does not rely on cache middleware such as Redis, reduces server resource requirements, improves security and execution efficiency, avoids timestamp synchronization requirements, and enhances defense capabilities against replay attacks.
Smart Images

Figure CN119030729A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of network security, and in particular relates to an anti-replay method based on WebAssmbly encoding without requiring a cache middleware. Background Art
[0002] In wireless networks, replay attacks are the most common and harmful type of attack. Among the authentication protocols that can be attacked, more than 90% are caused by replay attacks. A replay attack is when an attacker sends a packet that has already been received by the destination host. On the one hand, this attack damages the availability of the system by occupying the resources of the receiving system. On the other hand, the attacker steals the authentication credentials through network monitoring or other means, and then resends it to the authentication server to achieve the purpose of deceiving the system. At present, the more commonly used anti-replay attack solutions are as follows:
[0003] A: Timestamp verification: A timestamp parameter is added to each HTTP request, and then the timestamp and other parameters are digitally signed together. Assuming that a normal HTTP request will not exceed a fixed time from the time it is sent to the server, such as 60 seconds, the server compares the timestamp parameter in the HTTP request with the current time to see if it has exceeded 60 seconds. If it has exceeded, it is considered an illegal request. Generally speaking, it takes hackers much longer than 60 seconds from packet capture to replaying the request. If the hacker modifies the timestamp parameter, the corresponding digital signature will become invalid because the hacker does not know the token value and cannot generate a new digital signature. This method is simple and easy to use, but the loopholes are also obvious, that is, it cannot resist replay attacks that occur within the defined timeout period, and all solutions that apply timestamp verification require that the client and server time are accurately synchronized, the same below.
[0004] B: Nonce solution: Nonce is a random string that is valid only once. It is required that the parameter be different for each request. The server stores the nonce parameter of each request in the cache (generally implemented by middleware, such as Redis or memcached). Each time an HTTP request is processed, it is first determined whether the nonce parameter of the request exists in the cache. If it does, it is considered an illegal request. This method also has a big problem, that is, the cache for storing nonce will become larger and larger, and it will take longer and longer to verify whether nonce exists, so the cache needs to be cleaned up regularly. Once the cache is cleaned up, the replay defense is cleared. Assuming that the cache is cleaned up once a day, the hacker captures a legitimate request. Although it cannot replay the attack at that time, it can replay the attack one day later; and the cache used to store nonce is also a considerable expense.
[0005] C: Timestamp + nonce solution: Each request carries a timestamp and a nonce random code. The server records all nonce random codes, but sets a timeout. A replay attack that times out will be judged invalid by the timestamp parameter, while a replay attack that does not time out will be judged invalid because the nonce random code exists in the cache. The nonce random code stored in the cache will be automatically cleared due to timeout, avoiding unlimited growth of cache space. This solution relies on the timeout invalidation mechanism of high-speed cache middleware such as Redis.
[0006] In addition, the above three methods are all because the client programming language Javascript is executed in the browser as source code and is visible to everyone. All algorithms (nonce, signature, etc.) applied by the client are theoretically likely to be cracked. Therefore, this application provides an anti-replay method based on WebAssmbly encoding without the need for a cache middleware. Summary of the invention
[0007] The purpose of the present invention is to provide an anti-replay method based on WebAssmbly encoding without caching middleware. By selecting a nonce random code generation algorithm with low re-coding rate and guaranteed execution determinism, for each user, the same seed is used to generate the same nonce random code sequence on the client and the server respectively. The latest nonce random code in the sequence is the user key. The client uses the user key to execute the data encryption algorithm to encrypt the data to obtain the anti-replay parameter each time it requests. The server uses the user key to decrypt the anti-replay parameter. If the client user key is the same as the server user key, valid data containing the verification magic number will be obtained after decryption. If the decryption fails or the verification magic number does not match after decryption, it is regarded as an invalid request. After the user key is used, the client and the server use the nonce random code generation algorithm to synchronously update their respective user keys respectively, solving the existing problems.
[0008] To solve the above technical problems, the present invention is achieved through the following technical solutions:
[0009] The present invention is an anti-replay method based on WebAssmbly encoding without caching middleware, which is mainly composed of the following four parts: encryptor, interface, algorithm and constant. The present invention needs to implement the same nonce random code generation algorithm and key encryption / decryption algorithm on the client and server. The server can be implemented using various Web programming languages; to avoid source code leakage, the client algorithm is implemented using WebAssembly encoding; this encoding implementation is called an encryptor in the present invention, and the encryptor has three forms, encryptor template, encryptor instance, and encryption module; specifically includes the following steps:
[0010] Compilation: A placeholder magic number is defined in the code through the encryptor template. The length of the magic number is the same as the length of the protection key, which is the key used to encrypt / decrypt the user key. The encryptor template is the implementation of multiple algorithms in this solution. It is prepared before the server is released and can be implemented in various programming languages that support WebAssembly and compiled into WebAssembly code.
[0011] Instantiation: When a user initiates a session request, the user key and encryptor template are instantiated to dynamically generate an encryptor instance; the encryptor instance is sent to the client;
[0012] Loading: After receiving the encryptor instance, the client browser loads and generates a Javascript closure object in the browser; the closure object is used to encrypt the request and dynamically update the client's user key. The closure object exports the encryption function, which calls the data encryption method of the encryptor WebAssembly instance to encrypt the data and obtain the anti-replay parameters;
[0013] Interface: There are two interfaces: session application interface and communication interface. Normally, the session application interface is only called once at the beginning, and then only the communication interface is used to interact with the server;
[0014] Algorithm: This solution uses: nonce random code algorithm, data encryption algorithm, key encryption algorithm, protection key construction algorithm, and user seed generation algorithm; the implementation of each algorithm can be flexibly selected according to actual scenarios and specific needs, and algorithms of the same type are interchangeable with each other, thereby enhancing the flexibility and scalability of the overall solution.
[0015] Furthermore, the method for constructing the encryptor instance is:
[0016] S1: Process user request: verify user identity, obtain user ID, and return the current server timestamp;
[0017] S2: Get the user key: Find the user key according to the user ID and update the user key using the nonce random code algorithm; the nonce random code algorithm is used to calculate the nonce random code sequence. Each random code is iteratively generated by the previous random code;
[0018] S3: Build protection key: Use the protection key building algorithm to calculate the user ID and server timestamp to generate the protection key; Protection key building algorithm: Any hash algorithm, such as MD5, is used to generate the protection key;
[0019] S4: Encrypt user key: Use key encryption algorithm and protection key to encrypt user key; key encryption algorithm: a symmetric encryption algorithm that uses protection key as key to encrypt / decrypt user key. Function: prevent user key from being displayed in plain text; protection key construction algorithm: any hash algorithm, such as MD5, is used to generate protection key;
[0020] S5: Get the encryptor template: The encryptor template is a static resource on the server side. You can consider dynamically and randomly selecting one from multiple encryptor templates.
[0021] S6: Build a copy of the encryptor: copy the encryptor template, and replace the magic number in the copied encryptor copy with the protection key;
[0022] S7: Construct an encryptor instance: define the data structure and construct binary data, and convert it into a string using BASE64 encoding.
[0023] Furthermore, the method for generating the closure object is:
[0024] SS1: Parsing the encryptor example: The binary data obtained by BASE64 decoding is parsed to obtain the encryptor user key ciphertext and encryptor copy according to the format of the defined data structure;
[0025] SS2: Build a WebAssembly instance: Use the WebAssembly system object of Javascript to compile the binary data of the encryptor copy into a WebAssembly instance that can be called by the Javascript source code;
[0026] SS3: Build the encryptor module: Generate a closure object, which contains two closure variables: one is the user key ciphertext variable, and the other is the encryptor WebAssembly instance variable.
[0027] Furthermore, the method for the encryption function to process data is:
[0028] S011: Calling the encryption function: passing the request parameters in plain text to the encryption function through the business function;
[0029] S012: Get user key ciphertext: the user key ciphertext is a closure variable of the encryptor module;
[0030] S013: Call the data encryption method of the encryptor WebAssembly instance, and pass in the user key ciphertext and request parameter plaintext;
[0031] S014: Decrypt user key: call the key encryption / decryption method in the encryptor WebAssembly instance and decrypt the user key with the protection key;
[0032] S015: Generate anti-replay parameters: Use the user key to execute the data encryption algorithm to encrypt the request parameter plain text and the verification magic number to obtain the anti-replay parameters; Data encryption algorithm: A symmetric encryption algorithm that uses the user key as the key to encrypt / decrypt business data, which is used to construct the anti-replay parameters;
[0033] S016: Update user key: Use nonce random code algorithm to update user key; nonce random code algorithm: used to calculate nonce random code sequence, each random code is iteratively generated by the previous random code;
[0034] S017: Encrypt new user key: call the key encryption / decryption method in the encryptor WebAssembly instance and encrypt with the protection key;
[0035] S018: Return the result to the data encryption function: the returned result includes: anti-replay parameters and new user key ciphertext;
[0036] S019: Update the user key ciphertext in the encryptor module;
[0037] S0110: Return anti-replay parameters to the business function.
[0038] Furthermore, the client interacts with the server through a communication interface, and the timing of the communication interface is:
[0039] T1: Construct anti-replay parameters: The client calls the encryptor module to encrypt business data and generate anti-replay parameters;
[0040] T2: The client adds signature and authentication information to the request and initiates a request to the server.
[0041] T3: User identity verification: If the verification is successful, the user information is obtained; if the verification is not successful, an error message is returned and the processing is terminated;
[0042] T4: Anti-replay verification: The server obtains the user key based on the user information, executes the data encryption algorithm (data encryption algorithm: a symmetric encryption algorithm that uses the user key as the key to encrypt / decrypt business data. It is used to construct anti-replay parameters), decrypts the plain text of the business data, and checks whether it contains the magic number for verification. If so, it means the verification is successful. If not, an error message is returned and the processing is terminated;
[0043] T5: Update user key: The server executes the nonce random code algorithm (nonce random code algorithm: used to calculate the nonce random code sequence. Each random code is iteratively generated by the previous random code) to obtain a new user key and update it;
[0044] T6: Processing request: executing specific business processing and obtaining processing results;
[0045] T7: Return the processing result to the client.
[0046] Furthermore, the session between the client and the server is initialized by applying for a session interface to construct an encryptor instance. The timing of applying for the session interface is:
[0047] C1: Initialize user keys: When the server starts the service and adds a new user, it executes the user seed generation algorithm (user seed generation algorithm: used to generate the seed of each user key, any hash algorithm), initializes the user key for each user, and stores the initialized user key in the database or session cache;
[0048] C2: The client initiates a session request to the server: the request contains user identity authentication information;
[0049] C3: User identity verification: The specific implementation of the user identity verification protocol is irrelevant to this solution and will not be described here;
[0050] C4: Update user key: The server uses the user information in the request to obtain the user key of the user, executes the nonce random code algorithm (nonce random code algorithm: used to calculate the nonce random code sequence. Each random code is iteratively generated by the previous random code), obtains a new user key and updates it;
[0051] C5: Build an encryptor instance;
[0052] C6: The client receives and loads the encryptor instance, generates a Javascript closure object, and the client and server synchronize the user key through the encryptor.
[0053] Furthermore, the server uses the user key to decrypt the anti-replay parameter;
[0054] If the client user key is the same as the server user key, valid data including the verification magic number is obtained after decryption;
[0055] If the decryption fails or the magic number does not match after decryption, it is considered an invalid request;
[0056] After the user key is used, the client and server use the nonce random code generation algorithm to synchronously update their respective user keys.
[0057] Furthermore, the nonce random code algorithm is used to calculate a nonce random code sequence, each random code is iteratively generated by a previous random code, and the nonce random code algorithm is a XorShift algorithm or a linear congruential method or a Mersenne prime rotation method.
[0058] Furthermore, the protection key construction algorithm is an arbitrary hash algorithm for generating a protection key.
[0059] Furthermore, the user seed generation algorithm is an arbitrary hash algorithm, which is used to generate the seed of each user key.
[0060] The present invention has the following beneficial effects:
[0061] 1. It does not rely on third-party cache middleware such as Redis, which makes deployment easier and requires lower server resources. It is a whitelist solution: record the valid user key for the next request, use this key to encrypt the data, and pass if it matches, otherwise deny service. Because only one code needs to be recorded for each user on the server, the storage requirements are extremely low, and there is no need for a timeout mechanism.
[0062] 2. Improved security: It is reflected in three aspects: 2.1. The client algorithm is implemented in binary WebAssembly encoding, which is not readable. Unlike conventional Javascript encoding, it is executed in source code mode; 2.2. During the communication process, the user key is not transmitted. The user key is stored on the server and client respectively, and the algorithm is used to maintain rolling synchronization between the two ends; 2.3. The user key is stored in ciphertext form on the client, and the protection key that protects the user key is part of the WebAssembly encoding and is in binary format.
[0063] 3. Improved execution efficiency: The execution efficiency of WebAssembly coding is close to that of native coding and is higher than that of Javascript source code.
[0064] 4. Because timestamps are not used, there is no need for accurate synchronization of the client and server time.
[0065] Of course, any product implementing the present invention does not necessarily need to achieve all of the advantages described above at the same time. BRIEF DESCRIPTION OF THE DRAWINGS
[0066] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings required for describing the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying creative work.
[0067] Figure 1 A diagram showing the relationship between the encryptor template, encryptor instance, and encryption module of the present invention;
[0068] Figure 2 Construct an encryptor instance (instantiation) flow chart for the present invention;
[0069] Figure 3 A flowchart generated for the closure object of the present invention;
[0070] Figure 4 A flowchart of the encryption function of the present invention for processing data;
[0071] Figure 5 Apply for a session interface timing diagram for the present invention;
[0072] Figure 6 It is a timing diagram of the communication interface of the present invention. DETAILED DESCRIPTION
[0073] In the following description, specific details such as specific system structures, technologies, etc. are provided for the purpose of illustration rather than limitation, so as to provide a thorough understanding of the embodiments of the present application. However, it should be clear to those skilled in the art that the present application may also be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to prevent unnecessary details from obstructing the description of the present application.
[0074] It should be understood that when used in the present specification and the appended claims, the term "comprising" indicates the presence of described features, wholes, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components and / or combinations thereof.
[0075] It should also be understood that the term “and / or” used in the specification and appended claims refers to any and all possible combinations of one or more of the associated listed items, and includes these combinations.
[0076] As used in the specification and appended claims of this application, the term "if" can be interpreted as "when" or "uponce" or "in response to determining" or "in response to detecting", depending on the context. Similarly, the phrase "if it is determined" or "if [described condition or event] is detected" can be interpreted as meaning "uponce it is determined" or "in response to determining" or "uponce [described condition or event] is detected" or "in response to detecting [described condition or event]", depending on the context.
[0077] In addition, in the description of the present application specification and the appended claims, the terms "first", "second", "third", etc. are only used to distinguish the descriptions and cannot be understood as indicating or implying relative importance.
[0078] References to "one embodiment" or "some embodiments" etc. described in the specification of this application mean that one or more embodiments of the present application include specific features, structures or characteristics described in conjunction with the embodiment. Therefore, the statements "in one embodiment", "in some embodiments", "in some other embodiments", "in some other embodiments", etc. that appear in different places in this specification do not necessarily refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized in other ways. The terms "including", "comprising", "having" and their variations all mean "including but not limited to", unless otherwise specifically emphasized in other ways.
[0079] The present invention is an anti-replay method based on WebAssmbly encoding without caching middleware. A nonce random code generation algorithm is selected, which requires a low re-coding rate and ensures execution certainty (the same input must obtain the same output), such as the XorShift algorithm, the linear congruential method, the Mersenne prime rotation method, etc. For each user, the same seed is used to generate the same nonce random code sequence on the client and the server, respectively. The latest nonce random code in the sequence is the user key. The client uses the user key to execute the data encryption algorithm to encrypt the data to obtain the anti-replay parameter each time it requests. The server uses the user key to decrypt the anti-replay parameter. If the client user key is the same as the server user key, valid data containing the verification magic number will be obtained after decryption. If the decryption fails or the verification magic number does not match after decryption, it is regarded as an invalid request. After the user key is used, the client and the server use the nonce random code generation algorithm to synchronously update their respective user keys.
[0080] As an embodiment provided by the present invention, preferably, the anti-replay method based on WebAssmbly encoding without caching middleware is mainly composed of the following four parts: encryptor, interface, algorithm and constant. The present invention needs to implement the same nonce random code generation algorithm and key encryption / decryption algorithm on the client and server. The server can be implemented using various Web programming languages; to avoid source code leakage, the client algorithm is implemented using WebAssembly encoding; this encoding implementation is called an encryptor in the present invention, please refer to Figure 1 As shown, there are three forms of encryptors: encryptor template, encryptor instance, and encryption module.
[0081] As an embodiment provided by the present invention, preferably, the same nonce random code generation algorithm and key encryption / decryption algorithm need to be implemented on the client and the server. The server can be implemented using various Web programming languages. To avoid source code leakage, the client algorithm is implemented using WebAssembly encoding. This encoding implementation is called an encryptor in this embodiment. The encryptor has three forms: an encryptor template, an encryptor instance, and an encryption module. The relationship between the encryptor template, the encryptor instance, and the encryption module is as follows: Figure 1 shown.
[0082] As an embodiment provided by the present invention, preferably, an anti-replay method based on WebAssmbly encoding without cache middleware specifically includes the following steps:
[0083] Compilation: The encryptor template is the implementation of multiple algorithms in this solution (nonce random code algorithm, data encryption algorithm, key encryption algorithm, protection key construction algorithm, user seed generation algorithm), which is prepared before the server is released, and can be implemented in various programming languages that support WebAssembly and compiled into WebAssembly code. A placeholder magic number is defined in the code through the encryptor template. The length of the magic number is the same as the length of the protection key. The protection key is the key used to encrypt / decrypt the user key; the encryptor template is the implementation of multiple algorithms in this solution. As an embodiment provided by the present invention, preferably, the encryptor template mainly includes two methods, as shown in Table 1:
[0084] Table 1
[0085]
[0086] Instantiation: When a user initiates a session request, the encryptor instance is dynamically generated by instantiating the user key and encryptor template. The encryptor instance will be sent to the client. The process of constructing the encryptor instance (instantiation) is as follows: Figure 2 As shown;
[0087] Loading: After receiving the encryptor instance, the client browser loads and generates a Javascript closure object in the browser; the closure object is used to encrypt the request and dynamically update the client's user key. The closure object exports the encryption function, which calls the data encryption method of the encryptor WebAssembly instance to encrypt the data and obtain the anti-replay parameters; the generated Javascript closure object, i.e. the encryptor module, is loaded in the browser. The closure object is used to encrypt the request and dynamically update the client's user key. The process of generating the closure object is as follows: Figure 3 As shown;
[0088] Interface: There are two interfaces: session application interface and communication interface. Normally, the session application interface is only called once at the beginning, and then only the communication interface is used to interact with the server;
[0089] Algorithm: This solution uses: nonce random code algorithm, data encryption algorithm, key encryption algorithm, protection key construction algorithm, and user seed generation algorithm; the implementation of each algorithm can be flexibly selected according to actual scenarios and specific needs, and algorithms of the same type are interchangeable with each other, thereby enhancing the flexibility and scalability of the overall solution.
[0090] As an embodiment provided by the present invention, preferably, Figure 2 As shown, the method for constructing an encryptor instance (instantiation) is:
[0091] S1: Process user request: verify user identity, obtain user ID, and return the current server timestamp;
[0092] S2: Get the user key: Find the user key according to the user ID and update the user key using the nonce random code algorithm; the nonce random code algorithm is used to calculate the nonce random code sequence. Each random code is iteratively generated by the previous random code;
[0093] S3: Build protection key: Use the protection key building algorithm to calculate the user ID and server timestamp to generate the protection key; Protection key building algorithm: Any hash algorithm, such as MD5, is used to generate the protection key;
[0094] S4: Encrypt user key: Use key encryption algorithm and protection key to encrypt user key; key encryption algorithm: a symmetric encryption algorithm that uses protection key as key to encrypt / decrypt user key. Function: prevent user key from being displayed in plain text; protection key construction algorithm: any hash algorithm, such as MD5, is used to generate protection key;
[0095] S5: Get the encryptor template: The encryptor template is a static resource on the server side. You can consider dynamically and randomly selecting one from multiple encryptor templates.
[0096] S6: Build a copy of the encryptor: copy the encryptor template, and replace the magic number in the copied encryptor copy with the protection key;
[0097] S7: Construct an encryptor instance: define the data structure and construct binary data, and convert it into a string using BASE64 encoding. The data structure is defined as shown in Table 2:
[0098] Table 2
[0099]
[0100] As an embodiment provided by the present invention, preferably, the constant is:
[0101] Magic placeholder number: used to identify the placeholder data in the encryptor template, and its length is the same as the length of the protection key;
[0102] Encryptor magic number: used to identify the encryptor instance. It is recommended that it should not exceed 4 bytes.
[0103] Verification magic number: A constant added when constructing anti-replay parameters to verify the legitimacy of the data.
[0104] As an embodiment provided by the present invention, preferably, Figure 3 As shown, the method for generating the closure object is:
[0105] SS1: Parsing the encryptor example: The binary data obtained by BASE64 decoding is parsed according to the format of the defined data structure (Table 2) to obtain the encryptor user key ciphertext and encryptor copy;
[0106] SS2: Build a WebAssembly instance: Use the Javascript WebAssembly system object to compile the binary data of the encryptor copy into an instance that can be called by the Javascript source code, that is, the WebAssembly instance of the encryptor;
[0107] SS3: Build the encryptor module: Generate a closure object, which contains two closure variables, one is the user key ciphertext variable, and the other is the encryptor WebAssembly instance variable. The closure exports the encryption function, which calls the data encryption method of the encryptor WebAssembly instance to encrypt the data.
[0108] As an embodiment provided by the present invention, preferably, Figure 4 As shown, the method for the encryption function to process data is:
[0109] S011: Calling the encryption function: passing the request parameters in plain text to the encryption function through the business function;
[0110] S012: Get user key ciphertext: the user key ciphertext is a closure variable of the encryptor module;
[0111] S013: Call the data encryption method of the encryptor WebAssembly instance, and pass in the user key ciphertext and request parameter plaintext;
[0112] S014: Decrypt user key: call the key encryption / decryption method in the encryptor WebAssembly instance and decrypt the user key with the protection key;
[0113] S015: Generate anti-replay parameters: Use the user key to execute the data encryption algorithm to encrypt the request parameter plain text and the verification magic number to obtain the anti-replay parameters; Data encryption algorithm: A symmetric encryption algorithm that uses the user key as the key to encrypt / decrypt business data, which is used to construct the anti-replay parameters;
[0114] S016: Update user key: Use nonce random code algorithm to update user key; nonce random code algorithm: used to calculate nonce random code sequence, each random code is iteratively generated by the previous random code;
[0115] S017: Encrypt new user key: call the key encryption / decryption method in the encryptor WebAssembly instance and encrypt with the protection key;
[0116] S018: Return the result to the data encryption function: the returned result includes: anti-replay parameters and new user key ciphertext;
[0117] S019: Update the user key ciphertext in the encryptor module;
[0118] S0110: Return anti-replay parameters to the business function.
[0119] As an embodiment provided by the present invention, preferably, Figure 6 As shown, the client interacts with the server through a communication interface, and the timing of the communication interface is:
[0120] T1: Construct anti-replay parameters: The client calls the encryptor module to encrypt business data to generate anti-replay parameters. For details, see the process of encryption function processing data;
[0121] T2: The client adds signature and authentication information to the request and initiates a request to the server.
[0122] T3: User identity verification: If the verification is successful, the user information is obtained; if the verification is not successful, an error message is returned and the processing is terminated;
[0123] T4: Anti-replay verification: The server obtains the user key based on the user information, executes the data encryption algorithm (data encryption algorithm: a symmetric encryption algorithm that uses the user key as the key to encrypt / decrypt business data. It is used to construct anti-replay parameters), decrypts the plain text of the business data, and checks whether it contains the magic number for verification. If so, it means the verification is successful. If not, an error message is returned and the processing is terminated;
[0124] Note: If the anti-replay verification fails, the client needs to re-initiate the application session interface to synchronize the user key;
[0125] T5: Update user key: The server executes the nonce random code algorithm (nonce random code algorithm: used to calculate the nonce random code sequence. Each random code is iteratively generated by the previous random code) to obtain a new user key and update it;
[0126] T6: Processing request: executing specific business processing and obtaining processing results;
[0127] T7: Return the processing result to the client.
[0128] As an embodiment provided by the present invention, preferably, Figure 5 As shown, applying for a session interface: initializing a session between the client and the server, and constructing an encryptor instance; specifically, the session between the client and the server is initialized by applying for a session interface, and an encryptor instance is constructed. The timing of applying for a session interface is:
[0129] C1: Initialize user keys: When the server starts the service and adds a new user, it executes the user seed generation algorithm (user seed generation algorithm: used to generate the seed of each user key, any hash algorithm), initializes the user key for each user, and stores the initialized user key in the database or session cache;
[0130] C2: The client initiates a session request to the server: the request contains user identity authentication information;
[0131] C3: User identity verification: The specific implementation of the user identity verification protocol is irrelevant to this solution and will not be described here;
[0132] C4: Update user key: The server uses the user information in the request to obtain the user key of the user, executes the nonce random code algorithm (nonce random code algorithm: used to calculate the nonce random code sequence; each random code is iteratively generated by the previous random code), obtains a new user key and updates it;
[0133] C5: Build an encryptor instance;
[0134] C6: The client receives and loads the encryptor instance, generates a Javascript closure object, and the client and server synchronize the user key through the encryptor.
[0135] Furthermore, the server uses the user key to decrypt the anti-replay parameter;
[0136] If the client user key is the same as the server user key, valid data including the verification magic number is obtained after decryption;
[0137] If the decryption fails or the magic number does not match after decryption, it is considered an invalid request;
[0138] After the user key is used, the client and server use the nonce random code generation algorithm to synchronously update their respective user keys.
[0139] As an embodiment provided by the present invention, preferably, the present embodiment uses the following algorithms. The implementation of each algorithm can be flexibly selected according to the actual scenario and specific needs. The same algorithms are interchangeable with each other, thereby enhancing the flexibility and scalability of the overall solution. The algorithms are:
[0140] Algorithm 1, nonce random code algorithm: used to calculate the nonce random code sequence. Each random code is iteratively generated by the previous random code;
[0141] Algorithm 2, data encryption algorithm: a symmetric encryption algorithm that uses the user key as the key to encrypt / decrypt business data; used to construct anti-replay parameters;
[0142] Algorithm 3, key encryption algorithm: a symmetric encryption algorithm that uses the protection key as the key to encrypt / decrypt the user key; function: to prevent the user key from being displayed in plain text. For ease of implementation, this algorithm can be the same as Algorithm 2;
[0143] Algorithm 4, protection key construction algorithm: any hash algorithm, such as MD5, is used to generate the protection key;
[0144] Algorithm 5, user seed generation algorithm: used to generate the seed of each user's key, any hash algorithm. For ease of implementation, the algorithm implementation can be the same as Algorithm 4, but the method of constructing parameters should be different.
[0145] As an embodiment provided by the present invention, preferably, the nonce random code algorithm is used to calculate the nonce random code sequence, each random code is iteratively generated by the previous random code, and the nonce random code algorithm is a XorShift algorithm or a linear congruential method or a Mersenne prime rotation method.
[0146] The anti-replay method based on WebAssmbly encoding without cache middleware, the WebAssembly encoding used in the present invention is the development trend of the front end in the future, which represents higher execution efficiency and better security, does not rely on third-party cache middleware such as Redis, is more convenient to deploy, and has lower requirements for server resources; it is a whitelist solution: record the valid user key for the next request, use this key to encrypt data, pass if matched, otherwise refuse service; because only one code needs to be recorded for each user on the server side, the storage requirement is extremely low, and no timeout failure mechanism is required. Improved security: reflected in three aspects, 1. The client algorithm is implemented by binary WebAssembly encoding, which is not readable, unlike conventional Javascript encoding, which is executed in source code; 2. During the communication process, the user key is not transmitted, and the user key is stored in the server and client respectively, and the algorithm is used to keep the rolling synchronization of both ends; 3. The user key is stored in ciphertext form on the client, and the protection key that protects the user key is part of the WebAssembly encoding and is in binary format. Improved execution efficiency: The execution efficiency of WebAssembly encoding is close to that of native encoding, and is higher than that of Javascript source code. Because timestamps are not used, there is no need for precise synchronization of the client and server time.
[0147] In the description of this specification, the description with reference to the terms "one embodiment", "example", "specific example", etc. means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present invention. In this specification, the schematic representation of the above terms does not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described can be combined in any one or more embodiments or examples in a suitable manner.
[0148] The preferred embodiments of the present invention disclosed above are only used to help illustrate the present invention. The preferred embodiments do not describe all the details in detail, nor do they limit the invention to the specific implementation methods described. Obviously, many modifications and changes can be made according to the content of this specification. This specification selects and specifically describes these embodiments in order to better explain the principles and practical applications of the present invention, so that those skilled in the art can understand and use the present invention well. The present invention is limited only by the claims and their full scope and equivalents.
Claims
1. An anti-replay method based on WebAssmbly encoding without cache middleware, characterized in that: The following steps are involved: A placeholder magic number is defined in the code through the encryptor template, wherein the length of the magic number is the same as the length of the protection key, and the protection key is a key used to encrypt / decrypt the user key; When a user initiates a session request, the user key and encryptor template are instantiated to dynamically generate an encryptor instance; After receiving the encryptor instance, the client browser loads and generates a Javascript closure object in the browser; the closure object exports an encryption function, and the encryption function calls the data encryption method of the encryptor WebAssembly instance to complete the encryption of the data and obtain the anti-replay parameters.
2. The anti-replay method based on WebAssmbly encoding without cache middleware according to claim 1 is characterized in that: The method to construct the encryptor instance is: S1: Process user request: verify user identity, obtain user ID, and return the current server timestamp; S2: Get the user key: Find the user key based on the user ID and update the user key using the nonce random code algorithm; S3: Build protection key: Use the protection key building algorithm to calculate the user ID and server timestamp to generate the protection key; S4: Encrypt user key: Encrypt the user key using the key encryption algorithm and the protection key; S5: Obtain an encryptor template: dynamically and randomly select from multiple encryptor templates; S6: Build a copy of the encryptor: copy the encryptor template, and replace the magic number in the copied encryptor copy with the protection key; S7: Construct an encryptor instance: define the data structure and construct binary data, and convert it into a string using BASE64 encoding.
3. The anti-replay method based on WebAssmbly encoding without cache middleware according to claim 2 is characterized in that: The method for generating the closure object is: SS1: Parsing the encryptor example: The binary data obtained by BASE64 decoding is parsed to obtain the encryptor user key ciphertext and encryptor copy according to the format of the defined data structure; SS2: Build a WebAssembly instance: Use the WebAssembly system object of Javascript to compile the binary data of the encryptor copy into a WebAssembly instance that can be called by the Javascript source code; SS3: Build the encryptor module: Generate a closure object, which contains two closure variables: one is the user key ciphertext variable, and the other is the encryptor WebAssembly instance variable.
4. The anti-replay method based on WebAssmbly encoding without cache middleware according to claim 3 is characterized in that: The method of processing data by the encryption function is: S011: Calling the encryption function: passing the request parameters in plain text to the encryption function through the business function; S012: Get user key ciphertext: the user key ciphertext is a closure variable of the encryptor module; S013: Call the data encryption method of the encryptor WebAssembly instance, and pass in the user key ciphertext and request parameter plaintext; S014: Decrypt user key: call the key encryption / decryption method in the encryptor WebAssembly instance and decrypt the user key with the protection key; S015: Generate anti-replay parameters: Use the user key to execute the data encryption algorithm to encrypt the request parameter plain text and the verification magic number to obtain the anti-replay parameters; S016: Update user key: Update user key using nonce random code algorithm; S017: Encrypt new user key: call the key encryption / decryption method in the encryptor WebAssembly instance and encrypt with the protection key; S018: Return the result to the data encryption function: the returned result includes: anti-replay parameters and new user key ciphertext; S019: Update the user key ciphertext in the encryptor module; S0110: Return anti-replay parameters to the business function.
5. The anti-replay method based on WebAssmbly encoding without cache middleware according to claim 4 is characterized in that: The client interacts with the server through a communication interface, and the timing of the communication interface is: T1: Construct anti-replay parameters: The client calls the encryptor module to encrypt business data and generate anti-replay parameters; T2: The client adds signature and authentication information to the request and initiates a request to the server. T3: User identity verification: If the verification is successful, the user information is obtained; if the verification fails, an error message is returned and the processing is terminated; T4: Anti-replay verification: The server obtains the user key based on the user information, executes the data encryption algorithm, decrypts the plain text of the business data, and checks whether the magic number is included. If so, the verification is successful. If not, an error message is returned and the processing is terminated. T5: Update user key: The server executes the nonce random code algorithm to obtain a new user key and update it; T6: Processing request: executing specific business processing and obtaining processing results; T7: Return the processing result to the client.
6. The anti-replay method based on WebAssmbly encoding without cache middleware according to claim 5 is characterized in that: The session between the client and the server is initialized by applying for a session interface to construct an encryptor instance. The timing of applying for a session interface is: C1: Initialize user key: When the server starts the service or adds a new user, it executes the user seed generation algorithm to initialize the user key for each user. The initialized user key is stored in the database or session cache; C2: The client initiates a session request to the server: the request contains user identity authentication information; C3: User identity verification; C4: Update user key: The server uses the user information in the request to obtain the user key of the user, executes the nonce random code algorithm, obtains the new user key and updates it; C5: Build an encryptor instance; C6: The client receives and loads the encryptor instance, generates a Javascript closure object, and the client and server synchronize the user key through the encryptor.
7. The anti-replay method based on WebAssmbly encoding without cache middleware according to claim 1 is characterized in that: The server decrypts the anti-replay parameters using the user key; If the client user key is the same as the server user key, valid data including the verification magic number is obtained after decryption; If the decryption fails or the magic number does not match after decryption, it is considered an invalid request; After the user key is used, the client and server use the nonce random code generation algorithm to synchronously update their respective user keys.
8. The anti-replay method based on WebAssmbly encoding without cache middleware according to claim 6 is characterized in that: The nonce random code algorithm is used to calculate a nonce random code sequence, each random code is iteratively generated by a previous random code, and the nonce random code algorithm is a XorShift algorithm or a linear congruential method or a Mersenne prime rotation method.
9. The anti-replay method based on WebAssmbly encoding without cache middleware according to claim 6 is characterized in that: The protection key construction algorithm is an arbitrary hash algorithm, which is used to generate a protection key.
10. The anti-replay method based on WebAssmbly encoding without cache middleware according to claim 6, characterized in that: The user seed generation algorithm is an arbitrary hash algorithm, which is used to generate the seed of each user key.
