A proof-of-concept project that demonstrates how to embed JavaScript code within a BMP image file, similar to techniques used in polyglot malvertising attacks. This project creates a hybrid file that can be both displayed as an image and contain executable JavaScript code.
This project demonstrates a technique used in real-world malvertising attacks where attackers embed malicious JavaScript code within image files. The project takes a BMP image and JavaScript code as input, and produces a modified BMP file that contains both the image data and the JavaScript code. The resulting file can be used as a regular image while also containing executable JavaScript code.
This project demonstrates a technique that has been observed in real-world malvertising attacks, where attackers:
- Hide malicious JavaScript payloads within seemingly innocent image files
- Use polyglot files that can be interpreted as both images and JavaScript
- Obfuscate the malicious code to evade detection
- Exploit browser behavior to execute the hidden code
Note: This is an educational demonstration of a technique that has been used in actual attacks. The implementation is for research and educational purposes only.
- Converts JavaScript code into an image-embedded format
- Maintains image display capabilities
- Simple Express.js server for demonstration
- Proof-of-concept implementation of polyglot file creation
- Educational demonstration of malvertising techniques
- Node.js (v12 or higher)
- npm (Node Package Manager)
- Clone this repository:
git clone https://github.com/x052/js2img
cd js2img
- Install dependencies:
npm install
- Start the server:
npm start
-
The server will run on
http://localhost:8080
-
Access the demo page and view the generated image with embedded JavaScript
server.js
- Express server implementationPOC.js
- Core JavaScript to Image conversion logicinput.bmp
- Sample input imageindex.html
- Demo pagepackage.json
- Project configuration and dependencies
The project demonstrates how polyglot files work by:
- Taking a BMP image as input
- Appending JavaScript code to the image data
- Creating a modified BMP file that contains both the image and code
- Serving the hybrid file through an Express server
- Exploiting browser behavior to execute the hidden JavaScript
This project demonstrates several important security concepts:
- How attackers can hide malicious code in seemingly innocent files
- The importance of proper file validation and sanitization
- The risks of polyglot files in web applications
- Browser security considerations when handling mixed content
When implementing file handling in production environments:
- Always validate file types using multiple methods
- Implement proper content-type checking
- Use secure file handling practices
- Consider implementing file sanitization
- Monitor for unusual file behavior
ISC License