How can an attacker execute malware through a script​

March 30, 2025
- Markus Fletcher

Attackers can execute malware through scripts by leveraging popular scripting languages like JavaScript, PowerShell, and VBScript to deliver and run malicious code on target systems. These script-based attacks are often difficult to detect due to their ability to operate in memory and blend in with legitimate system operations.

How can an attacker execute malware through a script​

Script-based malware attacks leverage common scripting languages to deliver and execute malicious code on target systems. Attackers often use VBScript, PHP, and batch scripts embedded in seemingly benign files or web pages to exploit vulnerabilities. These scripts can automate the process of downloading and installing malware, making them particularly dangerous.

Common delivery methods include phishing emails with malicious attachments containing embedded scripts, compromised websites injecting malicious JavaScript into visitors’ browsers, infected software downloads that execute scripts during installation, exploitation of vulnerabilities in document readers like Adobe Acrobat, etc.

Once executed, these scripts can operate in memory, making them difficult to detect with traditional antivirus solutions. To protect against script-based attacks, users should be cautious when downloading attachments or software from untrusted sources, keep systems and applications updated, and use comprehensive security solutions that can detect and block malicious scripts.

PowerShell Exploits in Phishing 

PowerShell has become a popular tool for attackers in phishing campaigns due to its versatility and widespread availability on Windows systems. Threat actors often use social engineering tactics to trick users into executing malicious PowerShell commands, bypassing traditional security measures. These attacks typically involve:

  • Sending phishing emails with attachments containing embedded code that launches PowerShell payloads
  • Creating fake IT support sites that instruct users to copy and paste malicious PowerShell scripts as supposed “fixes” for common Windows errors
  • Disguising PowerShell commands as part of fake CAPTCHA verification processes on malicious websites
  • Masquerading as legitimate officials to build rapport before sending spear-phishing emails with instructions to run PowerShell as an administrator

These techniques allow attackers to deliver information-stealing malware like Vidar, DarkGate, and Lumma Stealer, often evading detection by operating in memory and leveraging trusted Windows processes.

JavaScript Injection via Websites 

JavaScript injection attacks exploit vulnerabilities in websites to insert malicious code into client-side scripts, potentially compromising user data and system security. These attacks often target legitimate websites, with over 150,000 sites recently affected by a campaign promoting gambling platforms. Attackers use various techniques to bypass detection, including:

  • Obfuscation of malicious code to hide external URLs and payload
  • Appending malicious scripts to larger benign files
  • Multistep injections that perform a series of code insertions before loading the final payload
  • Exploiting input validation vulnerabilities to inject code into forms or URL parameters

Once injected, the malicious JavaScript can redirect users to fraudulent sites, steal sensitive information like cookies or payment data, or force unintended actions on legitimate web applications. To mitigate these risks, website owners should implement robust input validation, use Content Security Policies, and regularly scan for vulnerabilities.

HTA Scripts in Malware Attacks 

HTA (HTML Application) scripts have become a popular tool for malware attacks due to their ability to execute code with local user privileges through the Windows mshta.exe process. Attackers often distribute malicious HTA files via phishing emails or compromised websites, exploiting the fact that mshta.exe is a trusted Windows utility. These scripts can bypass security measures by running directly in memory, making them difficult to detect. Common tactics include:

  • Embedding malicious code within seemingly harmless HTA files, such as fake browser updates or corporate documents
  • Using obfuscated scripts to download and execute additional payloads, like PowerShell commands or ransomware
  • Exploiting vulnerabilities like CVE-2017-0199 to deliver HTA files through malicious Word documents
  • Leveraging social engineering to trick users into opening HTA attachments, which are then executed by mshta.exe

To mitigate these threats, organizations should consider implementing Windows Defender Application Control policies to block HTA execution and educate users about the risks of opening suspicious attachments or clicking on untrusted links.

Mitigating Script-Based Threats

Script-based malware attacks have become increasingly prevalent due to their versatility and effectiveness in evading traditional security measures. Attackers leverage scripting languages like PowerShell, JavaScript, and VBScript to create sophisticated malware with multiple features and obfuscations. These scripts can operate in memory, making them harder to detect and analyze.

To protect against script-based attacks, organizations should implement tailored security controls beyond default configurations. This includes configuring execution policies for PowerShell, using Windows Defender Exploit Guard features, and regularly updating systems and applications. Additionally, user education on identifying suspicious attachments and links, combined with comprehensive security solutions that can detect and block malicious scripts, is crucial in mitigating the risks posed by script-based malware.