Ravindra BagaleCourses & study guides मराठी Track your progress

29. OWASP Top 10 Web Vulnerabilities

29.4 A03 Injection: Command Injection

Command injection tevha hoto jevha app user cha input thet operating-system command madhe vaparto.

// vulnerable: pinging a host the user typed
$ip = $_GET['ip'];
system("ping -c 1 " . $ip);

If the attacker sends 8.8.8.8; cat /etc/passwd, the server runs both commands. Fixes: avoid calling the shell; if you must, use safe APIs and strict allow-lists, and escape with escapeshellarg().

$ip = $_GET['ip'];
if (filter_var($ip, FILTER_VALIDATE_IP)) {        // allow only a valid IP
    system("ping -c 1 " . escapeshellarg($ip));
}

Ravindra Bagale's Tip

Avoid OS commands if you can – for most tasks there is a library in PHP/Python (files, network). If you really need a command, use an allow-list (only valid IPs/names) and escapeshellarg(). User input directly into system() – never.

Lab

In DVWA (Command Injection, low), append ; whoami to the IP field and see it run. On high, see it blocked. Write a safe version of the ping feature using FILTER_VALIDATE_IP and escapeshellarg().

Command injection deep dive

Never send user input through a shell. Use APIs and argument arrays.

Bad vs good

Python

Bad: os.system. Good: subprocess with a list of args and an allow-listed IP pattern.

subprocess.run(["ping","-c","1",ip])
Node.js

Bad: exec(string). Good: execFile(binary, [args]).

PHP

Bad: shell_exec with concat. Good: escapeshellarg + fixed binary, or no shell.

Java

Bad: Runtime.exec with sh -c. Good: ProcessBuilder list.

Why this lesson matters

Part 11 turns tool skills into professional defensive practice: web risks, cloud, SOC work, hardening, cryptography, Indian law and careers. Every example below stays inside systems you own or public teaching material.

Indian company (sample data)

Suppose we are a junior security engineer at an Infosys or TCS client project. You take this lesson's checklist, map it to three production services (for example a Spring Boot API, a Node.js portal and a Python job), and open tickets with evidence and owners.

Global practice

Follow public guidance from OWASP, CERT-In, CISA and cloud provider Well-Architected / security pillars. Cite them in reports.

Everyday analogy

A cricket team that only practises hitting but never fielding loses matches. Offence skills in a lab exist to improve fielding in real systems.

Apply across stacks

Wherever code appears, ask the same questions.

Java / Spring Boot

Parameterise queries, encode output, limit Actuator, patch dependencies.

.NET

EF Core parameters, HTTPS, Data Protection API, patch NuGet.

Node.js

helmet, parameterized SQL, npm audit, lockfiles.

Python

ORM, autoescape, pip-audit, Bandit.

Go / PHP / Ruby

Safe templates, prepared statements, govulncheck / composer audit / Brakeman.

nginx / Apache / Tomcat

Hide versions, TLS, least-open ports.

MySQL / PostgreSQL / MongoDB / Redis

No public ports, auth on, least-privilege users.

AWS / Azure / GCP

Roles not keys, private data stores, cloud logs on.

Defender worksheet

  1. Write a 5-line summary.
  2. Name one related public breach or advisory.
  3. Practise one safe command on a lab you own.
  4. Write the fix for a teammate.

Recap

  • Permission first.
  • Fix over spectacle.
  • Evidence in notes.

Defender's worksheet (do this today)

  1. Write the lesson title and date.
  2. List three owned systems where it applies.
  3. Run one safe lab check and paste evidence.
  4. Write a two-sentence fix for a teammate.
  5. Note one CERT-In or CISA advisory that relates (title + date).

Indian workplace (sample)

At a Wipro or LTIMindtree client SOC, turn this lesson into one playbook step with an owner and a due date.

Global practice

Compare your notes with OWASP, CIS Benchmarks or a cloud Well-Architected security question. Write the overlap in five lines.

Family test

Explain the risk and the fix without jargon. If a parent or sibling can repeat the warning, you are ready to teach juniors.

Common mistakes

  • Skipping permission.
  • Tool collecting without fixing.
  • Publishing lab screenshots that contain secrets.
  • Treating encoding as encryption.

Recap

  • Evidence, fix, owner, date.
  • Own lab only.

Key terms for this page

  • Control: a safeguard (patch, config, process).
  • Finding: a weakness with evidence.
  • Remediation: the fix and how you verified it.
  • Scope: systems you are allowed to touch.
  • Least privilege: only the access required for the task.
  • Defence in depth: several controls in layers.

Mini case study

Suppose we are a junior engineer at Zoho or Freshworks (sample data). A weekly review asks each person to present one control from their area. You pick a control from this lesson, show a before/after screenshot from a lab, and list the production ticket you would open. That habit builds both skill and credibility.