Database Lateral Movement Methodology
- 攻击大类:C31 远程服务横向移动
Databases are natural pivots inside an internal network — they usually have looser network access policies than application servers (they need to connect to multiple services), and PostgreSQL's dblink, MSSQL's Linked Server, and MySQL's FEDERATED engine all provide native cross-host query capability. When application-layer network segmentation is strict, lateral movement through the database is often the only path.
⛔ Further Reading (Must Read)
- Full commands and scenarios for dblink connections, Linked Server exploitation, and SSRF probing → references/dblink-lateral.md
Phase 1: Assess Lateral Movement Conditions
Gather information in the current database to judge the possibility of lateral movement:
-- PostgreSQL: check available extensions
SELECT * FROM pg_available_extensions WHERE name IN ('dblink','postgres_fdw');
-- PostgreSQL: existing foreign server configurations
SELECT * FROM pg_foreign_server;
SELECT * FROM pg_user_mapping;
-- MSSQL: check Linked Servers
EXEC sp_linkedservers;
SELECT * FROM sys.servers WHERE is_linked = 1;
-- MySQL: check the FEDERATED engine
SHOW ENGINES;
Lateral movement condition checklist:
- Is the current user superuser/sa/root? (non-privileged users usually cannot create dblink)
- Are there connection details for other hosts in the database configuration?
- Are there credentials for other databases in environment variables/configuration files?
Phase 2: Internal Network Information Gathering (Through the Database)
The database itself is an information goldmine:
-- Search for connection strings
SELECT * FROM pg_settings WHERE name LIKE '%connection%';
-- Search for tables/columns containing passwords
SELECT table_name, column_name FROM information_schema.columns
WHERE column_name LIKE '%pass%' OR column_name LIKE '%secret%' OR column_name LIKE '%token%';
-- Find other database hosts (common in configuration tables)
SELECT * FROM config WHERE key LIKE '%host%' OR key LIKE '%db%' OR value LIKE '%.%.%.%';
Phase 3: Lateral Attack Decision Tree
Have you compromised a database?
├─ PostgreSQL →
│ ├─ dblink (most flexible) → connect to any PostgreSQL/supported database
│ ├─ postgres_fdw (foreign data wrapper) → transparent cross-database queries
│ └─ COPY TO PROGRAM + curl → SSRF probing of the internal network
├─ MSSQL →
│ ├─ Linked Server → connect to other MSSQL/any OLE DB data source
│ ├─ OPENROWSET → ad-hoc cross-database queries
│ └─ xp_cmdshell + curl → internal network probing
├─ MySQL →
│ ├─ FEDERATED engine → query across MySQL instances
│ └─ LOAD DATA LOCAL → read local files
└─ Generic →
├─ Port scanning / service discovery via command execution
└─ Use credentials stored in the database to connect to other services
→ Full commands for lateral movement across each database → references/dblink-lateral.md
Phase 4: Post-Lateral Exploitation
After successfully connecting to a new database:
- Extract data — user tables, configuration tables, flags
- Achieve RCE — COPY FROM PROGRAM / xp_cmdshell / UDF
- Continue laterally — the new database may connect to even more internal resources
- Persistence — create backdoor accounts, triggers
Notes
- Traffic generated by dblink/Linked Server queries originates from the database server IP, not your attacker machine IP
- Exploiting this characteristic lets you bypass IP-based network ACLs
- Inter-database communication is usually plaintext — mind credential security
- Large numbers of cross-database queries generate logs — keep operations restrained