> For the complete documentation index, see [llms.txt](https://sarpers-organization.gitbook.io/ctftricks/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sarpers-organization.gitbook.io/ctftricks/_chapter-intro-3/obfuscation/trick-0016.md).

# Decode Brainfuck Credentials

***

Identify data encoded using the Brainfuck esoteric programming language by recognizing its characteristic eight command characters. The language itself only uses eight characters which are used to manipulate a pointer and a 30,000-cell, 1-dimensional byte (8-bit) array to which the pointer is pointing. As each cell is a single byte, it can contain any number from 0–255 inclusive; over- and underflows are possible (e.g., 0–1 = 255 and 255+1=0). The Brainfuck code itself is read from left to right, executing each command once, unless there is a loop.

The eight characters used within the language are as follows:

* `>` Increment the data pointer by 1, moving the pointer to the next cell on the right.
* `<` Decrement the data pointer by 1, moving the pointer to the previous cell on the left.
* `+` Increment the byte value at the current data pointer's location by 1.
* `-` Decrement the byte value at the current data pointer's location by 1.
* `.` Output the byte value (interpreted as an ASCII character) at the current data pointer's location.
* `,` Accept one byte of input and store its value at the current data pointer's location.
* `[` If the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching `]` command.
* `]` If the byte at the data pointer is non-zero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching `[` command.

An example of Brainfuck code looks like this:

```brainfuck
++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>++++++++++++++++.++++.>>+++++++++++++++++.----.<++++++++++.-----------.>-----------.++++.<<+.>-.--------.++++++++++++++++++++.<------------.>>---------.<<++++++.++++++.
```

To decode Brainfuck, you will need a Brainfuck interpreter or an online decoder. For instance, an interpreter can be compiled and run from source code. One such example is `unsafebf`, an unsafe Brainfuck interpreter that uses the stack as memory directly. If you have its source code (e.g., in a file named `main.c`), you might compile and run it with specific Brainfuck code as follows:

```bash
gcc main.c -o unsafebf
./unsafebf "Some bf code"
```

Alternatively, you can use online decoders; many websites offer this functionality, allowing you to paste the code and see the output. Web apps designed for encryption, encoding, compression, and data analysis, like CyberChef, often include modules or "recipes" to decode Brainfuck as well.

Copy the sequence of Brainfuck commands and paste it into the input field of the decoder or feed it to the interpreter. Running the Brainfuck code will execute the commands, manipulating a data pointer and memory cells as described. The output of the execution (usually generated by the `.` command) will reveal the decoded string. This string often contains sensitive information like credentials or flags in CTFs.

For manual analysis or to better understand the execution flow, the Brainfuck code can be organized. For example, breaking it into different lines, perhaps where each line or segment leads to an output using the `.` character, can aid comprehension:

```brainfuck
++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>++++++++++++++++.
++++.
>>+++++++++++++++++.
----.
<++++++++++.
-----------.
>-----------.
++++.
<<+.
>-.
--------.
++++++++++++++++++++.
<------------.
>>---------.
<<++++++.
++++++.
```

This formatting helps in tracking how the values within the memory array are increased or decreased by the Brainfuck code, which then correspond to ASCII characters when outputted by the `.` command.
