Course contentsShow
← Security
Lesson 585 of 3,05213. Web Security - Injection AttacksFree lesson

File System and OS Command Execution

Leveraging database functions to read/write files or execute operating system commands through SQLi.

File System and OS Command Execution

What you'll learn: How attackers can abuse SQL injection vulnerabilities to read/write files on the server and execute operating system commands.


Beyond Data Theft

You've learned how SQL injection lets attackers steal or manipulate database contents. But some database systems offer built-in functions that interact with the underlying operating system—and if you can inject SQL, you might control those too.

Database Functions That Cross Boundaries

Modern databases like MySQL, PostgreSQL, and MSSQL include powerful functions for administrative tasks:

  • File operations: LOAD_FILE() in MySQL reads files from disk; INTO OUTFILE writes query results to files
  • Command execution: xp_cmdshell in MSSQL runs Windows shell commands; COPY PROGRAM in PostgreSQL can invoke system utilities

Think of it like this: If SQL injection is picking the lock to enter a building (the database), these functions are finding that the building has doors leading directly outside to the operating system—and those doors might be unlocked.

The Exploitation Path

An attacker who has already achieved SQL injection can attempt queries like:

  • Reading sensitive server files (configuration files, application source code, SSH keys)
  • Writing web shells or malicious scripts to web-accessible directories
  • Executing system commands to enumerate users, escalate privileges, or establish persistence

Prerequisites and Limitations

Not all SQLi leads to OS access. It requires:

  1. The vulnerable database account having sufficient privileges
  2. The dangerous functions being enabled (many are disabled by default)
  3. File system permissions allowing read/write operations
  4. Knowledge of correct file paths on the target system

Key Takeaway:
SQL injection vulnerabilities can escalate from database compromise to full operating system access when attackers leverage database functions that interact with the file system or execute commands—turning a data breach into complete server takeover.