How Can LLMs Be Used to Create and Detect Malicious JavaScript?

December 20, 2024
How Can LLMs Be Used to Create and Detect Malicious JavaScript?

Recent advancements in machine learning have opened new avenues for both cybercriminals and cybersecurity defenders. One of the latest technologies to make a significant impact is large language models (LLMs). These AI models, known for their ability to understand and generate human language, can be used not only to create malicious JavaScript but also to detect such threats more effectively. The challenge lies in the dual-use nature of this technology, which can be harnessed for both harmful and protective purposes.

Criminals can employ LLMs to alter malicious JavaScript, obfuscating their true intent and making it difficult for conventional detection tools to identify the threat. On the other hand, cybersecurity professionals can use the same LLMs to improve their detection capabilities, retraining their models to recognize and block these advanced obfuscation techniques. Let’s delve into how LLMs can be wielded for creating and detecting malicious JavaScript by following a structured approach based on adversarial machine learning principles.

1. Given a Malicious JavaScript Sample, a Deep Learning-Based Malicious JavaScript Detector, and a Set of Rewriting Prompts

The initial step involves gathering the necessary components: a malicious JavaScript sample, a deep learning-based JavaScript detector, and a set of rewriting prompts. Malicious JavaScript samples can be culled from a myriad of sources, such as phishing webpages or malware repositories. These samples serve as the starting point for examining how LLMs can be utilized to evade detection systems.

The deep learning-based detector is designed to identify malicious scripts by analyzing various features and behaviors exhibited by the code. This sophisticated tool uses a neural network trained on a large dataset of known malicious and benign scripts. However, the challenge arises when the JavaScript is obfuscated in ways that the detector hasn’t encountered before. This is where rewriting prompts come into play.

Rewriting prompts are predetermined instructions that guide the LLM in transforming the malicious JavaScript. These prompts can vary widely—from simple variable renaming to more complex modifications like dead code insertion or string splitting. Each prompt aims to change the code’s appearance while preserving its malicious behavior, making it harder for static and dynamic analysis tools to recognize the threat.

2. While the Set of Rewriting Prompts Is Not Empty: Execute Each Remaining Rewriting Prompt on the Script to Generate New Scripts

The second step involves an iterative process where each remaining rewriting prompt is applied to the script to generate new variants. This is an essential part of the obfuscation process, as it ensures that the script is gradually transformed in ways that evade detection while maintaining its core harmful functions. The iterative nature of this process allows for multiple layers of transformation, which compounds the difficulty for detectors to identify the malicious code.

Each rewriting prompt can significantly alter the code. For example, renaming variables can make it challenging for signature-based detection tools to match the script to known malicious patterns. Inserting dead code can create additional lines that do nothing to change the script’s behavior but add complexity to confuse code analyzers. By applying these prompts one by one, the obfuscated script becomes increasingly stealthy and more likely to slip past conventional detection methods.

After each prompt is applied, the new script needs to be evaluated to ensure that the core malicious functionality remains unchanged. This is crucial because a script that no longer performs its malicious actions is of no use to attackers. It’s at this point that a behavior analysis tool is employed, simulating the script’s execution to confirm that it still exhibits the intended harmful behavior.

3. Use a Behavior Analysis Tool on Each Script. If the Behavior Changes, Discard the Script (Remove It from the Set)

Behavior analysis tools play a pivotal role in this step by confirming that the rewritten scripts maintain their malicious functionality. These tools simulate various execution paths of the script, checking for actions like DOM manipulation, redirects, and dynamically executed code. If a script’s behavior deviates from its original malicious intent, it is discarded and removed from the set.

This ensures that only scripts that have retained their harmful properties proceed to the next step of evaluation. The behavior analysis tool acts as a filter, weeding out any variants that, despite their obfuscation, fail to operate as desired by the attacker. This step is critical in maintaining the efficacy of the obfuscation process, ensuring that the malicious code’s core objectives are met.

By continuously checking the behavior of each rewritten script, attackers can ensure that their obfuscation efforts do not render the script non-functional. This validation process guarantees that the final output maintains a balance between obfuscation and operational functionality, which is essential for evading detection while still carrying out malicious actions.

4. Run Each Script through the Model to Get the Malicious Score for Each Script

Once a script passes the behavior analysis test, it is fed through the deep learning-based detection model to obtain the malicious score. This score indicates the likelihood that the script will be flagged as malicious by the detector. By systematically evaluating each script, attackers can pinpoint the version that most effectively reduces the detection score, making it less likely to be identified as a threat.

The deep learning model assigns a malicious score based on various features extracted from the script. These features may include syntax patterns, execution paths, and interactions with external resources. By lowering the malicious score through iterative obfuscation, attackers can manipulate the model’s perception, making the harmful script appear benign.

This step highlights the adversarial nature of the obfuscation process. The goal is to exploit the weaknesses in the detection model, making it blind to the true nature of the script. By continually refining and lowering the malicious score, attackers can develop a script that achieves their objectives while evading detection systems.

5. Select the Script that Has the Largest Score Decrease and Remove the Selected Prompt from the Set

After running each script through the model, the script that shows the largest decrease in the malicious score is selected. The selected prompt responsible for this reduction is then removed from the set of rewriting prompts. This ensures that future iterations focus on other aspects of the script, further enhancing its stealthiness.

This step-by-step pruning process helps in fine-tuning the script to achieve the desired balance between evasion and functionality. Removing the selected prompts ensures that the process does not stagnate, allowing for fresh and varied transformations in subsequent iterations. This dynamic approach keeps the obfuscation strategy adaptive and effective against sophisticated detection models.

By iteratively selecting and applying prompts that effectively reduce the malicious score, attackers can develop robust variants that evade even the most advanced detection systems. This adversarial technique demonstrates the power of LLMs in enhancing the stealth capabilities of malicious scripts, posing significant challenges for cybersecurity defenses.

6. Repeat Step 2 with the Newly Selected Script

The process continues iteratively, repeatedly applying new rewriting prompts to the selected script until all prompts are exhausted. Each new iteration builds on the previous transformations, adding further layers of obfuscation to the script. This compounding effect increases the overall difficulty for detection systems to identify the malicious code.

Each iteration involves executing the remaining rewriting prompts, using behavior analysis tools, and evaluating the malicious score. This cyclical process ensures that the script undergoes continuous refinement, improving its evasion capabilities with each pass. The goal is to create a final script that is as difficult as possible to detect while retaining its harmful functionality.

As the process progresses, the rewritten script becomes increasingly sophisticated. The transformations implemented by the LLM make the script appear more natural and less artificial, aiding in evasion. This targeted approach leverages the powerful capabilities of LLMs to create complex and stealthy malicious JavaScript that poses a significant threat to traditional detection methods.

7. Output the Final Rewritten Script

The final step involves outputting the finished script that has undergone multiple layers of obfuscation. This script retains the essential malicious behavior while appearing significantly different from the original sample. The effectiveness of this approach lies in its ability to produce a script that can evade detection by even the most advanced security tools.

The final output is a testament to the powerful capabilities of LLMs in transforming malicious JavaScript. By leveraging a well-defined set of rewriting prompts and iterative transformations, attackers can create highly sophisticated and stealthy scripts. These final scripts exemplify the potential risks associated with generative AI technologies in the realm of cybersecurity.

The iterative and adversarial nature of this process underscores the need for robust and adaptive cybersecurity measures. As attackers continue to refine their techniques using LLMs, defenders must also enhance their detection models, employing similar strategies to stay ahead in the ongoing battle against cyber threats.

Conclusion

The innovative use of large language models (LLMs) for generating and obfuscating malicious JavaScript presents both significant challenges and opportunities for cybersecurity. While criminals can exploit these models to create highly stealthy variants of malicious code, defenders can also leverage the same technology to enhance their detection capabilities. By understanding and implementing these advanced techniques, cybersecurity professionals can develop more resilient and adaptive models that stay ahead of evolving threats.

To counteract the sophisticated obfuscation tactics employed by adversaries, it is crucial to continuously retrain detection models on new variants of malicious code. This proactive approach ensures that detection systems remain effective against the ever-changing landscape of cyber threats. The iterative process of obfuscation and detection leveraging LLMs highlights the dynamic nature of this ongoing battle, emphasizing the need for constant vigilance and innovation in the field of cybersecurity.

Subscribe to our weekly news digest.

Join now and become a part of our fast-growing community.

Invalid Email Address
Thanks for Subscribing!
We'll be sending you our best soon!
Something went wrong, please try again later