xiaohongshu-xs-reverse
⚠️ Disclaimer: This article is only used for security research and technical exchanges. Please do not use related technologies for illegal purposes. All consequences caused by malicious use of related technologies shall be borne by the users themselves.
Target site: https://www.xiaohongshu.com/explore
Overview
The Xiaohongshu web client carries theX-SRequest header for signature verification. Simply put, the server will verify the request headerX-SIs it legal? If not, the request will be rejected directly. This article will completely record the analysis process of this signature mechanism, from environment simulation to algorithm restoration, to using Python calls to generate signatures and initiate requests, taking you step by step to achieve automatic generation.X-S。
Reverse entrance positioning
To find the signature logic, the first step is to locate the encryption function in the code. Open the target site and follow these steps:
- Enter the Xiaohongshu exploration page, open the browser developer tools (F12), and switch to the Network panel.
- Filter
XHR/FetchRequest, just scroll the page to trigger some loading, find withX-SRequest header interface, such as home page feed interface/api/sns/web/v1/homefeed。 - In the Sources panel, search globally
X-sorX-SStrings are generally found in some request interceptors where they are assigned values. - A more direct method is to use
XHR/fetch Breakpoints: Add a filter condition in XHR/fetch Breakpoints on the right side of the Sources panel, such as/api/sns/web/v1/homefeed, so that it will be automatically disconnected before the request is sent. Then trace back all the way up the call stack to find the logic for signature generation.
💡 Tips: If you search
X-SThere are too many strings, you can try searchingXYS_The prefix, which is the fixed beginning of the final signature, has a high probability of directly locating the core encryption function.
After tracking and analysis, it will be found that the signature logic is roughly hidden in a huge Webpack packaging file, and the function names are usually obfuscated. All we need to do is strip out the key encryption logic and reproduce it in the Node.js environment.
Core signature process analysis
1. Signature generation steps
Through breakpoint debugging and code deobfuscation, sort outX-SThe complete generation process:
- Splicing string: Convert the API path (such as
/api/sns/web/v1/homefeed) concatenated with the JSON string of request parameters. - Calculate MD5: Get the MD5 digest of the string obtained in the previous step.
- Call mnsv2 algorithm: Pass the original concatenated string and MD5 value into a file named
mnsv2Custom function to generate core signature (signature)。 - Assemble signature object: Combine the version number, platform, operating system, signature obtained in the previous step, etc. into a fixed-structure object.
- Encoding output: Convert the signature object to a JSON string, encode it with UTF-8 and customize Base64 encoding, and finally add
XYS_prefix, get the finalX-Svalue.
2. Key data structures
The final object structure involved in coding is roughly as follows:
The entire signature process can be summarized in one sentence: "The parameters are put together to calculate MD5, then thrown to mnsv2 to generate the core signature, and finally packaged with custom Base64."
Key code implementation
1. Browser environment simulation
Xiaohongshu’s front-end code will verify the code running environment (such as checkingnavigatorproperties), if we want to implement pure algorithms in Node.js, we must complete these global objects in advance. Through means such as Proxy and class inheritance, the original code can be "cheated" so that it can still run normally in the Node environment.
NOTE:
mnsv2The specific environmental attributes used in the function need to be supplemented based on your reverse results, and are not fixed. The code above is just a sample framework.
2. Core signature function
With the patched environment in place, you can run the stripped encryption code. The following is a simplified main logic example. In actual use, you need to reverse themnsv2The function is completely moved over.
3. Auxiliary encoding function
The Base64 used by Xiaohongshu is a custom dictionary, not the standard Base64. The corresponding character table needs to be extracted during reverse engineering. The following is an example written according to a common pattern. Pay attention to thekeyStrReplace it with the specific string you reversed.
End-to-end call example
In order to facilitate actual use, we can encapsulate the Node.js script into a signature service, then use Python to call the script to obtain the signature, and then carry the signature to request the interface.
1. Node.js signature script (xiaohongshu_sign.js)
Complete the above environment code,mnsv2Functions and generated functions are integrated into one file, and the output is in JSON format at the end.X-s:
2. Python calling script
passsubprocessCall Node.js, get the return value and initiate the request. Don't forget to fill in your own cookies at the same time.
Key Notes
- Environment simulation must be accurate:
mnsv2Generally, the algorithm will accessnavigator.userAgent、screen.widthand other attributes, missing any one will lead to signature calculation errors. - JSON serialization order:
JSON.stringifyThe output results for key-value pairs in different orders may be different, so be sure to ensure that they are completely consistent with the browser. Usually Hook can be used when reversingJSON.stringifyto observe the original parameters. - Base64 custom dictionary: Do not use Node.js native
Buffer.toString('base64'), must be implemented using a reverse-engineered custom dictionary. - Version number and algorithm update:
x0Version,mnsv2Functions may change as the site is upgraded, and long-term maintenance needs to be prepared.
Summarize
Xiaohongshu Web versionX-SSignatures increase the threshold for constructing requests through a combination of "environmental verification + multi-layer encoding + custom algorithms". The key points of restoration are:
- Locate the encrypted entrance through browser breakpoints;
- Complete the browser environment required by Node.js;
- Cut out and rewrite
mnsv2Core functions; - Correctly handle parameter splicing order and custom Base64 encoding.
Once the above steps are completed, legal signatures can be stably generated locally, and automated data collection can be realized using languages such as Python. I hope this note can provide you with ideas in similar reverse analysis.

