> For the complete documentation index, see [llms.txt](https://heinosass.gitbook.io/leet-sheet/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://heinosass.gitbook.io/leet-sheet/web-app-hacking/server-attacks/loose-comparisons.md).

# Loose Comparisons

## MySQL

{% embed url="<https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html>" %}

MySQL's `=` operator does loose comparisons by default. These comparisons all evaluate to true:

* `SELECT '0' = 0;`
* `SELECT '0.0' = 0;`
* `SELECT '0      ' = '0';`

*Note: Postgresql doesn't do loose comparisons by default.*

## PHP

{% embed url="<https://owasp.org/www-pdf-archive/PHPMagicTricks-TypeJuggling.pdf>" %}

PHP has loose comparisons ("==") and strict comparisons ("==="). Loose comparisons have some weird conversion rules which can be used to trick the application into doing what you want.

*Note: Python and JS also have loose comparisons.*

Use cases:

* CSRF token bypass
* Authentication bypass
* Subverting application logic in general

**JSON is really useful** for exploiting this because if the application takes JSON input, you can specify the type of the variable you're sending. In other words, you can also send ints and booleans, not just strings.&#x20;

### Compare string to integer

Most strings are equal to the integer 0.

* `TRUE: "0000" == int(0)`&#x20;
* `TRUE: "1abc" == int(1)`&#x20;
* `TRUE: "0abc" == int(0)`&#x20;
* `TRUE: "abc" == int(0) // !!`

### Compare string to string

PHP does strange conversions between strings if they look like numbers.

* `TRUE: "0e12345" == "0e54321"`
* `TRUE: "0e12345" <= "1"`&#x20;
* `TRUE: "0e12345" == "0"`&#x20;
* `TRUE: "0xF" == "15"`

### Array comparison

Let's say you want to bypass the following if-condition:

```
if (strcmp($_POST['password'], 'thePassword') == 0) {
 // do authenticated things
}
```

If you submit an array as `$_POST['password']` like this: `password[]=` , then the `strcmp` operation will error out. The result will be NULL.

Thanks to type juggling, `NULL == 0` is true, and you bypass the check and do authenticated things.


---

# 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://heinosass.gitbook.io/leet-sheet/web-app-hacking/server-attacks/loose-comparisons.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.
