Unsafe OS Command Execution Without File Upload (PHP)

๐Ÿ“… Published 2026-04-26 ยทweb-securityattack-techniquespentestingbug-bountywindows

Written by Aryan Giri


๐Ÿ”ฅ Overview

When people think about getting code execution on a server, they usually think about file upload vulnerabilities (uploading a webshell, etc.).

But thereโ€™s a quieter and often more dangerous path:

๐Ÿ‘‰ Unsafe OS command execution via existing functionality

This is NOT PHP code injection โ€” itโ€™s the server passing user input into a system shell.

No file upload. No shell file. Just abusing whatโ€™s already there.


โš ๏ธ The Core Vulnerability

This attack happens when user input is passed directly into system-level functions like:

system()
exec()
shell_exec()
passthru()

Vulnerable Code Example

<?php
$name = $_GET['name'];
system("ping " . $name);
?>

๐Ÿ‘‰ Important:

This leads to command/argument injection, not PHP code execution.


๐Ÿ’ฃ The Payload

';system("ipconfig");//

Example Exploit URL

http://AttackTest.com?file=';system("ipconfig");//

๐Ÿ‘‰ If the backend uses the file parameter inside a shell call, this will execute ipconfig and return the output directly in the web response.

Breakdown

Component What It Does Why It Matters
' Breaks out of the original string Escapes developer-controlled context so you can inject your own code
; Terminates the current command Lets you chain a new command after the original one
system("ipconfig") Executes OS-level command Gives you direct command execution on the server
// Comments out the rest of the line Prevents syntax errors and ignores remaining backend code

๐Ÿง  How the Exploit Works

Backend builds a shell command like:

system("ping " . $_GET['name']);

If input is not sanitized, it becomes:

ping example.com; dir

Shell interprets:

  1. Run ping example.com
  2. Then run dir

๐Ÿ‘‰ The shell executes both because of command chaining (;)

โš ๏ธ Note: This is shell-level injection, not execution of injected PHP code.

Boom โ€” command execution achieved.


๐ŸŽฏ Why This Is Powerful


๐Ÿงช Real Demo (Intentionally Vulnerable Site)

You can safely test this technique on the following intentionally vulnerable lab:

http://php.testinvicti.com/hello.php?name=%27;system(%22dir%22);//

Also works without URL encoding

http://php.testinvicti.com/hello.php?name=';system("dir");//

๐Ÿ‘‰ Same payload, just not URL-encoded. Depending on the server and filters, this may work directly in the browser.

Whatโ€™s happening here?

About the dir command

๐Ÿ‘‰ So instead of just seeing โ€œHello userโ€, youโ€™ll see server directory contents in the page.


โš ๏ธ Responsible Testing Note


๐Ÿšซ About DoS (Reality Check)

Youโ€™ll see a lot of forums hyping DoS as โ€œhigh bountyโ€.

Reality:

๐Ÿ‘‰ Instead of wasting time on DoS, focus on:

These are the bugs that actually pay and matter.


๐ŸŒ Real-World Scenarios

Youโ€™ll commonly find this in:


๐Ÿ›ก๏ธ Mitigation

โŒ Donโ€™t do this

system("ping " . $_GET['name']);

โœ… Do this instead

system("ping " . escapeshellarg($_GET['name']));

๐Ÿ” Detection Tips

Look for:


โšก Advanced Notes

If output is not visible:


๐Ÿงจ When & Why This Vulnerability Happens

When it appears

Why it happens

๐Ÿ‘‰ The backend cannot distinguish between intended command and injected command


๐Ÿ›ก๏ธ Mitigation (Deep Dive)

1. Avoid Shell Calls Completely

Instead of:

system("ping " . $_GET['name']);

Use native functions or APIs wherever possible.


2. Escape User Input Properly

system("ping " . escapeshellarg($_GET['name']));

This ensures input is treated as a single argument, not executable code.


3. Strict Input Validation (Allowlist)

Allow only:

Reject everything else.


4. Drop Privileges

Even if exploited โ†’ limited impact


5. Disable Dangerous Functions (if possible)

In php.ini:

disable_functions = system, exec, shell_exec, passthru

6. Use Sandboxing / Containers


7. Logging & Monitoring


๐Ÿงฉ Key Takeaway

If user input reaches a shell โ†’ assume command injection is possible.

This technique proves you donโ€™t need file upload to get RCE โ€” sometimes the system hands it to you.