> For the complete documentation index, see [llms.txt](https://sansong.gitbook.io/cyber/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sansong.gitbook.io/cyber/rev/python/decompiler-un-.pyc.md).

# Décompiler un .pyc

Comment récupérer le code source à partir d'un fichier .pyc

## Identifier la version d'un .pyc

Le magic number permet d'identifier la version de python utilisée.

Il s'agit des 2 premiers octets du fichier. Il faut les inverser, convertir le nombre résultant en décimal et le comparer à la liste ci-dessous.

{% embed url="<https://github.com/python/cpython/blob/main/Include/internal/pycore_magic_number.h>" %}

{% hint style="success" %}
Exemple:

```
$ xxd fichier.pyc
00000000: 550d 0d0a 0000 0000 0000 0000 0000 0000  U...............
[...]
```

1. Inversion des 2 premiers octets: 0x0d55
2. Conversion en décimal: 3413
3. Comparaison avec la liste: Python 3.8
   {% endhint %}

En fonction de la version le code source peut être récupéré avec uncompyle ou pycdc.

## **uncompyle6**

{% hint style="danger" %}
Pour des .pyc compilés avec des versions de Python allant de 1.0 à 3.8. Pour les versions supérieures, [`pycdc`](#pycdc) fonctionne bien.
{% endhint %}

**Installation avec pip**

```
pip install uncompyle6
```

**Installation depuis le code source**

```
git clone https://github.com/rocky/python-uncompyle6.git
cd python-uncompyle6
pip install -e .
```

**Utilisation**

```
uncompyle6 fichier.pyc
```

{% embed url="<https://github.com/rocky/python-uncompyle6>" %}

## pycdc

**Installation**

```
git clone https://github.com/zrax/pycdc.git
cd pycdc
cmake .
make
make check
```

**Utilisation**

```
# récupérer le code source
./pycdc fichier.pyc

# récupérer le bytecode (instructions)
./pycdas fichier.pyc
```

{% embed url="<https://github.com/zrax/pycdc>" %}
