What Is an Injection Attack? SQL Injection vs Command Injection vs Cross-Site Scripting

An injection attack happens when untrusted input is treated as code. That is the core risk. A search box, login form, URL parameter, API field, or file upload can become a doorway if the app sends that input to a database, operating system, or browser without safe handling. OWASP still lists injection as a major web application risk, which says a lot about how common and stubborn this problem is [source: OWASP].

TLDR: Injection attacks turn user input into commands the system runs. SQL injection targets databases, command injection targets the server’s operating system, and cross-site scripting targets other users’ browsers. For example, a fake login value like ‘ OR ‘1’=’1 may trick a weak SQL query into accepting a login. In a 10,000-user app, even a 1% account exposure means 100 real people may be affected.

What Makes Injection Attacks So Dangerous?

Injection attacks are dangerous because they often hide inside normal traffic. A request may look like a regular form submission. Behind that, the attacker is testing special characters, script tags, shell operators, or database logic.

The damage can be severe. Attackers may steal customer records, change prices, create admin users, read server files, or run commands on the host. Sometimes the first attack is small. Then it becomes a foothold for a larger breach. Honestly, it feels like many teams lose hours chasing “weird input bugs” that were really injection probes all along.

SQL Injection

SQL injection happens when user input is placed inside a database query without proper protection. SQL is the language used by many databases. If input is mixed into a query as raw text, an attacker can change what the query does.

A weak login query may look like this in plain terms:

  • User enters an email and password.
  • The app builds a database query using that text.
  • The attacker enters SQL syntax instead of normal text.
  • The database runs the altered query.

A classic payload is ‘ OR ‘1’=’1. The exact result depends on the code, but the intent is clear. The attacker wants the database to treat the condition as always true.

Common impact:

  • Reading private data.
  • Bypassing login checks.
  • Changing or deleting records.
  • Dumping entire tables.

Best defenses: use parameterized queries, stored procedures with safe parameters, strict input checks, and low-privilege database accounts. Do not build SQL by joining strings. That mistake is old, but it still shows up far too often.

Command Injection

Command injection is more severe in many cases because it targets the operating system. The attacker tries to make the server run system commands. This can happen when an app passes user input into tools like ping, curl, tar, image converters, backup scripts, or admin utilities.

For example, a network tool may let a user enter a host name to ping. If the app sends that value to the shell, an attacker may append another command. The server may run both commands if the input is not handled safely.

Common impact:

  • Reading server files.
  • Installing malware.
  • Opening reverse shells.
  • Moving deeper into internal systems.

Best defenses: avoid shell calls when possible. Use safe APIs instead. If a command must run, pass arguments as separate values, not one long shell string. Use allowlists for accepted values. Run services with limited permissions. A web app should not have broad system access “just in case.”

Cross-Site Scripting

Cross-site scripting, or XSS, injects code into a web page viewed by other users. In most cases, the injected code is JavaScript. The target is not the server database or operating system. The target is the browser.

XSS often appears in comments, profile pages, search results, support tickets, or admin dashboards. If a site stores unsafe input and shows it later, that is stored XSS. If the payload comes from a link or request and appears in the response, that is reflected XSS.

Common impact:

  • Stealing session tokens.
  • Changing what users see.
  • Sending actions as the victim.
  • Targeting admins with poisoned dashboard data.

Best defenses: encode output for the right context, such as HTML, JavaScript, CSS, or URLs. Sanitize rich text. Use safe template engines. Set secure cookie flags. Add a strong Content Security Policy, but do not treat it as a magic fix.

SQL Injection vs Command Injection vs XSS

Attack type Main target Typical payload Main risk
SQL injection Database SQL syntax Data theft or login bypass
Command injection Server operating system Shell commands Server takeover
XSS User browser JavaScript or HTML Session theft or user impersonation

The key difference is where the injected code runs. SQL injection runs in the database. Command injection runs on the server. XSS runs in the victim’s browser. That one detail shapes the whole risk model.

How to Reduce the Risk

Start with secure coding, not cleanup after the fact. It drives me crazy that teams sometimes turn on a web application firewall and call the job done. A WAF may block common payloads, but it cannot repair unsafe code.

  • Validate input with strict allowlists.
  • Encode output before showing data in a page.
  • Use prepared statements for all database queries.
  • Avoid shell execution unless there is no safer option.
  • Apply least privilege to databases, apps, and system users.
  • Log suspicious input and review it quickly.
  • Test with security tools during development, not only before release.

Injection attacks are not rare edge cases. They are practical, common, and well understood by attackers. Treat every input as untrusted. Then make the system prove it is safe before anything runs.