> For the complete documentation index, see [llms.txt](https://pikachuuu1436.gitbook.io/re_fun/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pikachuuu1436.gitbook.io/re_fun/ctf-challenges/other-ctf-challenges/sekai-bank.md).

# Sekai Bank

### Challenge Description

{% code overflow="wrap" %}

```
Hi I heard my friend has registered herself for a new bank account! I wonder if the bank system will be secure(I managed to get the program and encrypted account details, but something seems to be missing...)

After decrypting the json file, simply login with the following details: 
Username: Mizuki Password: sadge
```

{% endcode %}

### Solution

The following 2 files were provided  for the challenge:

<div align="left"><figure><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FngHDTmRhunBhxibutBmY%2Fimage.png?alt=media&amp;token=2ae09f08-ab18-4e64-93ad-b02d09f9dfce" alt=""><figcaption></figcaption></figure></div>

bank.json is just full of junk stuff which implies it might be encrypted:

<figure><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FPm42IZ3sY3A8i1T7o5VH%2Fimage.png?alt=media&amp;token=6d35acda-fd19-4e05-8de8-6109eba30226" alt=""><figcaption></figcaption></figure>

While banky is just an X64 ELF file:

<figure><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FQxo1MaiAgNPh94NahiFK%2Fimage.png?alt=media&amp;token=304394e2-6623-4e41-aa2a-b7b019397656" alt=""><figcaption></figcaption></figure>

When the executable is first ran, the following error is shown:

<div align="left"><figure><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FhatLdY95x108mqVFLyj2%2Fimage.png?alt=media&amp;token=02378aee-c201-46e6-ac4f-5f1d33e1e976" alt=""><figcaption></figcaption></figure></div>

As there's nothing more to look around, its time to disassemble the file!

#### Disassembling bank

Here I will be using cutter, a free reverse engineering tool that is built upon radare! Feel free to use any other debugging tools/disassemblers such as IDA to disassemble the executable! Here, I used the aaaa analysis option.

First the entry point is disassembled which shows that libc\_start\_main is being called, with the main function is being passed into the first argument:

<div align="left"><figure><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FXUsxDFlS8GoBf2hMDQks%2Fimage.png?alt=media&amp;token=85aebad6-08c2-4066-afbc-d6990f33eb65" alt=""><figcaption></figcaption></figure></div>

From here we can access main, which is at 0x6aa0!

<figure><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FMlx44NjqdXsQUgeESvdN%2Fimage.png?alt=media&amp;token=f4e7ac4f-5a5e-4265-ac24-5c469f5a33a4" alt=""><figcaption></figcaption></figure>

Here the auto analysis auto name a function to sub.bank.json before printint out what looks like the main menu, maybe it does something to the json file before showing the main menu?

#### JSON File Decryption:

The following is the disassembly for the start of sub.bank.json\_5ad0:

<div data-full-width="true"><figure><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FmlR5I7LmLT5EA1qkFhQp%2Fimage.png?alt=media&amp;token=80aea553-4f79-4c5b-a107-4dfcccbc5338" alt=""><figcaption></figcaption></figure></div>

The program attempts to open the file with fopen, exiting if it fails

If successful, the program will get the file size of the file and save to file\_size, where a buffer will be created with&#x20;

```c
calloc(1,file_size)
```

<figure><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FuvFn5gsRMgSzky0Bfwtm%2Fimage.png?alt=media&amp;token=cd921a1a-a3a3-4b45-8db4-42fd723a00df" alt=""><figcaption></figcaption></figure>

There it will jump to another subroutine sub.fread\_5c0b, as identified on cutter!

When decompiled, it shows that the fread is being used to read from the file into the buffer created:<br>

<figure><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FyaHd1Qy33Zl89xAGHLcy%2Fimage.png?alt=media&amp;token=b3bc87c6-cef9-4229-923b-2a37322bc4e9" alt=""><figcaption></figcaption></figure>

After the file is read, a suspicious subroutine secret\_key\_35750 is being called with a reference being passed into it. This reference is then passed into EVP\_CIPHER\_CTX\_new\_35a50, where EVP is a popular interface provided by the OpenSSL library  to perform cryptographic operation. The buffer and file size is also being passed into this subroutine, which makes it plausible that decryption might take place here! A quick look into the function confirms this:

<figure><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FxkXUEUsLxqUNq6RtvthN%2Fimage.png?alt=media&amp;token=c6affc53-a54c-4102-a692-1ee2350bdf14" alt=""><figcaption></figcaption></figure>

Here it inits the Cipher Context, and uses AES CBC with a 256 bit key passed from param\_1 with the IV stored in 0x3dc80. The cipher will then be used to decrypt the buffer in param4! This means that the bank.json file is being decrypted from here! Hence the key is being retrieved in secret\_key\_35750!

#### Getting the key:

The following is the decompilation of secret\_key\_35750:

<figure><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FYAs80sR8CVsFP4mqT3KP%2Fimage.png?alt=media&amp;token=b49af1b9-8cfe-4028-8229-0d28a776216a" alt=""><figcaption></figcaption></figure>

Here the program tries to open secret.key, which will stop the program if unsuccessful which is why our program crashed!

For secret.key, it will read 2 bytes from secret.key and run them through the first byte through a function 16 times and the 2nd byte through another function 16 times(which will be shown below) and the results will be saved to a 32 bytes buffer which will be stored in param1 which will be used as the key for decryption.

<div align="left"><figure><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2F7vXAUW5SMcDf45DE6LM6%2Fimage.png?alt=media&amp;token=032429bd-5915-4388-a21d-7e8331e3a49b" alt=""><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2F85AyzWykOuySBrc5lxqk%2Fimage.png?alt=media&amp;token=3ba9c6e1-f508-4883-b8c4-93b518d4436a" alt=""><figcaption></figcaption></figure></div>

Here, these two functions take in an integer which will be run through an algorithm known as a hashing algorithm. This means that the key is generated from 2 bytes which makes it brute forceable!

Here is the bruteforce script:

```python
import subprocess


for i in range(65536):
    key = i.to_bytes(2,byteorder="little",signed=False)
    with open("secret.key","wb") as f:
        f.write(key)
    process = subprocess.Popen("./banky", text=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    while True:
        line = process.stdout.readline()
        if not line:
            break
        print(line)
    while True:
        try:
            line = process.stderr.readline()
            if not line:
                break
            print(line)
        except UnicodeDecodeError:
            break
    if process.returncode == 0:
        break
```

Here it runs through 0 - 65535, which is the larges integer 2 bytes can hold and saving it to secret.key and running the program. Once the key is correct, ideally the main menu should pop out instead of "An error occured"

<figure><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FJljAVudLj2PWfdEqwBbD%2Fimage.png?alt=media&amp;token=33c79b5f-f041-401d-bd2c-54f721407ad6" alt=""><figcaption></figcaption></figure>

Yayyy, the key is obtained:

<figure><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FdTNsUpWNCTyK3Gm9h984%2Fimage.png?alt=media&amp;token=6ac61b3d-fe5d-4682-b63e-bf3db9591337" alt=""><figcaption></figcaption></figure>

With the credentials provided in the description, the flag can be obtained:

<figure><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FrTkN2HKbX8I9fEn3o6Lp%2Fimage.png?alt=media&amp;token=8ccb7854-27b2-4ad0-97e4-7d6ab336dc69" alt=""><figcaption></figcaption></figure>

Flag:

```
Malaysia{kUch1Ng}
```
