JavaScript reverse engineering and protection technology analysis
1. Introduction
When you are browsing for hidden prices, grabbing reservation codes, and using web-based widgets every day, have you ever been curious - why is it useless to copy the API request package captured by others? Why can't I see the complete and readable code when I open the Sources panel of the browser? Behind this are two lines of defense: the "black boxing" of front-end interaction logic and the "access lock" of back-end API calls. This article will quickly explain these down-to-earth technical routines from two aspects: Front-end JS protection actual combat and Simple reverse disassembly ideas.
2. Lightweight data protection solution for website front-end + back-end
Let’s first look at a complete “anti-malicious call” combo: the backend locks API parameters, and the frontend hides the logic for generating parameters.
2.1 URL/API parameter encryption/verification
Core function: Prevent unauthorized crawlers or individuals from directly copying API request package calls Common implementation methods:
- Front-end and back-end** jointly maintain algorithm rules/keys**
- Commonly used tool chains: Base64/Hex (simple escape), MD5/SHA256 (irreversible tamper-proof), AES/DES (reversible encryption of sensitive content), RSA (for key exchange)
- Typical product scenarios:
- Ticket grabbing platform: Each request will bring updates
sign(sign),ts(timestamp),nonce(random number) - E-commerce hidden price: the encrypted data returned by the API is decrypted before display
2.2 JavaScript front-end code protection technology
The front end is the place closest to the user. The logic of generating parameters and the steps of decrypting data will be exposed here, so "black box reinforcement" is needed. There are three commonly used ones: code compression, code obfuscation, and WebAssembly.
2.2.1 Code compression
Entry-level protection: The main purpose is not to protect against people, but to optimize the loading speed + blur the variable short names by the way, and it can be read after it is fully formatted. Principle: Remove all redundancies (spaces, newlines, comments), and change long variable/function names into abbreviations of 1-2 characters Commonly used tools: Terser (the default compression tool for Webpack/Vite/Rollup) Example comparison:
2.2.2 Code obfuscation
Advanced level protection: Specially used for "disgusting contrarians". Even if it is formatted, it is like reading a bible. The difficulty of restoration depends on the level of obfuscation. Comparison of core obfuscation methods:
Most commonly used free obfuscation tools:javascript-obfuscator(There is an online version and a CLI/Node.js version)
2.2.3 WebAssembly
Hard-core protection: Directly move the core logic of the front-end (such as the algorithm for generating complex signs and the steps to decrypt sensitive data) from JS to C/C++/Rust, and compile it into a binary file.wasm. JS is only responsible for calling. **Why is it difficult to reverse? **
- It is not a readable JS code, but a binary that the machine can run directly.
- Reverse requires knowledge of assembly language and the threshold is high
- It is faster and smaller than JS, and developers are willing to use it
3. JavaScript obfuscation in practice (usejavascript-obfuscator)
The Node.js version is used here for demonstration, which is suitable for batch obfuscation of project code.
3.1 Basic obfuscation configuration
First add the basic "code compression + variable modification + string hiding":
3.2 Advanced obfuscation configuration (optional)
If you have sufficient budget/high security requirements, you can add these options (note: the higher the obfuscation level, the slower the code will run):
4. WebAssembly simple example (using Emscripten)
Suppose we want to move the "core function for calculating dynamic sign" to WASM:
4.1 Writing C code
4.2 Compile to WASM with Emscripten
First install Emscripten (the official website has a one-click installation script), and then run:
Two files will be generated:add_sign.js(JS calls WASM bridge),add_sign.wasm(core binary logic)
4.3 Call in web page
5. Simple ideas for reverse analysis (for security self-examination)
If the website is already online, you can use these simple methods to check whether the protection is effective:
5.1 Browser Developer Tools Self-Check
- Network Panel: Search globally for "api", "get", "sign" and "token" to see if the API request has encrypted parameters and whether sensitive data is returned in plain text.
- Sources Panel: Find a JS file and click "Format Code" (the one in the lower left corner
{}button) to see if you can find the key logic - Console panel: Press
F12Open it and see if it keeps playing.debugger, or report an error directly
5.2 Hook simple API self-examination
If you still cannot find the logic for generating parameters after formatting, you can use Hook key function to capture the parameters:
6. Practical protection suggestions (performance and security balance)
Don't pursue "100% irreversible" (there is no absolutely safe code). The key point is to increase the time cost of reverse engineering and make malicious callers give up:
- Layered protection, special processing of core logic:
- Ordinary page interaction logic: only use code compression
- Logic for generating simple parameters: obfuscation with basics
- Logic for generating core sign and decrypting sensitive data: using WebAssembly
- Update the obfuscation strategy/key regularly: For example, change it once a week
javascript-obfuscatorRandom seed or configuration of - Control obfuscation level: Advanced obfuscation will slow down the code running speed by 10%-30%. Do not add too advanced obfuscation to core interactions (such as button clicks and drop-down loading)
- Don’t just rely on front-end protection: The back-end must add authentication (token, IP current limit, sign verification), the front-end is just the first buffer.
7. Summary
The protection of modern web applications is a combination of "front-end + back-end":
- Backend: Lock API parameters (sign, ts, nonce), IP current limit, and authentication
- Front-end: compress common logic, confuse important logic, and move core logic to WebAssembly
Understanding these technologies can not only help developers build more secure websites, but also help security researchers quickly locate vulnerabilities.
Sample code repository: https://github.com/Python3WebSpider/JavaScriptObfuscate

