-1

I'm asking a quick question, I've being trying to solve this challenge for a while (for my own benefit).

How do I set the name of a field after a selection of all columns in a table.

See below for code snippet

SELECT * 
FROM `random$table` 
LEFT JOIN users AS u AND u.username AS title ON u.rank > 4 
WHERE u.username = 'exploit' 
GROUP BY `id` LIMIT 1

As you see can from the above snippet, I'm trying to assign a given name to a field after LEFT JOINING it.

AND u.username AS title

I couldn't find any other question related to this issue, please mark as duplicate if there is.

Side Note: Query is vulnerable for a reason

Thanks kind regards

1 Answers1

0

You cannot do that after the query. Instead set the aliases inside of your SELECT expression. Perhaps you need some sort of mapping so based on your $table value you will have and $array of field=>alias matching, which you will put with join(',', $array) into the SELECT expression.

Something like that:

    function mapFields($table) {
        $mapping = [];


        switch($table) {
            case 'abc': {
                $mapping = [
                    $table.'.id' => 'id',
                    $table.'.rank' => 'rank',
                    $table.'.title' => 'title'
                ];
            }
            case 'xyz': {
                $mapping = [
                    $table.'.id' => 'userId',
                    $table.'.username' => 'username',
                    $table.'.email' => 'userEmail'
                ];
            }
        }

        return array_map(
            function($k, $v){return "$k as $v";}, 
            array_keys($mapping), 
            $mapping
        );
    }

    $mapping = mapFields('random'.$table);

    // do propper checks if the mapping array is empty or so


    $select = join(',', $mapping);



    SELECT $select
    FROM `random$table`
Anton
  • 417
  • 3
  • 9
  • Could you elaborate on that, perhaps add a code snippet, I couldn't quite understand your answer. See below for additional information. The payload ```1` LEFT JOIN users AS u AND u.username AS title ON u.rank > 4 WHERE u.username = 'exploit' GROUP BY `id``` If this helps – Derek Brazil Sep 24 '19 at 03:00
  • I can only control $table, SELECT * must be there no matter what – Derek Brazil Sep 24 '19 at 03:20
  • perhaps [this](https://stackoverflow.com/questions/4165195/mysql-query-to-get-column-names) can give you some ideas – Anton Sep 24 '19 at 03:40