# Database Lateral Pivot

> Database lateral movement and cross-database attacks. Use when you already hold one database account (PostgreSQL/MySQL/MSSQL) and need to pivot laterally to other databases or internal services. Covers PostgreSQL dblink cross-database connections, MSSQL Linked Server lateral movement, MySQL federated engine cross-database queries, and database→SSRF→internal network probing. When the target network is segmented but the database can still communicate, this is the key path for breaking out of the isolation (databases usually have looser network policies than application servers). This skill should be used for any scenario involving inter-database communication, lateral movement to internal databases, or cross-database queries

- Skill: `dangeroussong/database-lateral-pivot` (Agent Skill)
- Install (CLI): `npx get-skill add dangeroussong/database-lateral-pivot`
- Raw SKILL.md: https://api.skillmd.com/api/skills/dangeroussong/database-lateral-pivot/raw
- Safety review: pending (external: skill-scanner PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: DangerousSONG (https://skillmd.com/u/dangeroussong)
- Updated: 2026-11-19
- Page: https://skillmd.com/skills/dangeroussong/database-lateral-pivot

---


# 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](references/dblink-lateral.md)

---

## Phase 1: Assess Lateral Movement Conditions

Gather information in the current database to judge the possibility of lateral movement:

```sql
-- 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:

```sql
-- 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](references/dblink-lateral.md)

## Phase 4: Post-Lateral Exploitation

After successfully connecting to a new database:

1. **Extract data** — user tables, configuration tables, flags
2. **Achieve RCE** — COPY FROM PROGRAM / xp_cmdshell / UDF
3. **Continue laterally** — the new database may connect to even more internal resources
4. **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

