---
description: 'Skills: Programming, cryptography, GitHub, steganography, forensics'
---

# Knowledge Repository

Participation page: [https://sunshinectf.org/](https://sunshinectf.org/)

Event weight on ctftime: 34

START: 2023-10-07 14:00 UTC

FINISH: 2023-10-09 14:00 UTC

**Flag Format:** `sun{stuffs}`

**Category: Miscellaneous**

**Points system: Dynamic reducing based on number of solves**

**Points collected: 396**&#x20;

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2F3LSJBauV5t3GwemavWT0%2Fimage.png?alt=media&#x26;token=0f89554e-d20a-4508-a804-0f4e439ca0ef" alt=""><figcaption></figcaption></figure>

The challenge gives us a zip file. I unzipped it and saw an EML file in there. Since I had never encountered an EML in a CTF file before, I had to read up about it. [https://www.adobe.com/uk/acrobat/resources/document-files/text-files/eml.html](https://www.adobe.com/uk/acrobat/resources/document-files/text-files/eml.html)

It is just a plain text file where an E-mail conversation is stored. The contents of the E-mail were base64 encoded.

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FlK7UHGX0Uc7oU8vL8L9k%2Fimage.png?alt=media&#x26;token=1f68ccce-3053-4c26-90c1-9e6a2f61b4b9" alt=""><figcaption></figcaption></figure>

Upon inspecting the file, I saw that there was an attachment too.

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FwHSY2jnJQMkqt3IvwJXs%2Fimage.png?alt=media&#x26;token=75a6ce3c-5c26-4cef-b36e-5154cf7aaefa" alt=""><figcaption></figcaption></figure>

After decoding the message contents, I saw this message:

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FPTDo3CjAdViHjjkp8tWX%2Fimage.png?alt=media&#x26;token=9cd9b1d5-d6d3-4614-9b8a-e4ebcd4cf0c6" alt=""><figcaption></figcaption></figure>

I used an online EML viewer to directly download the attachment. This was a Git bundle.

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2Fn7kACDhaS9JrEGtcjwV9%2Fimage.png?alt=media&#x26;token=9b4bf1c6-3f76-4d14-8d70-c4958d974e0d" alt=""><figcaption></figcaption></figure>

I then created a new git repo and verified the bundle

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FY5kf6P61YmTTYagGLjXB%2Fimage.png?alt=media&#x26;token=f4441a33-344d-4a15-a3f2-ccdb1c3d0543" alt=""><figcaption></figcaption></figure>

I cloned the repository then.

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FfrGp3GO89dYJjfF1PISf%2Fimage.png?alt=media&#x26;token=eb862135-0906-4440-9422-b0b876361002" alt=""><figcaption></figcaption></figure>

Upon visiting the recently cloned repository, I saw an audio file.

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FdkX7sxaLeqAdAnrOv4wX%2Fimage.png?alt=media&#x26;token=6382032e-87f6-4c92-b432-e65555c05da6" alt=""><figcaption></figcaption></figure>

Upon listening to this audio file, it felt like a simple frequency varying beeps file. The beeps were quickly changing and had only 2 distinguishable tones. This indicated it was probably a morse code.



{% file src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FMQEortN3XNwfFqExZz7k%2Fdata.wav?alt=media&token=1715e908-feed-471a-890e-85bf38fc01d9" %}

Upon deciphering it on an online tool [https://morsecode.world/international/decoder/audio-decoder-adaptive.html](https://morsecode.world/international/decoder/audio-decoder-adaptive.html), I noticed some text come out. This initially didn't make sense.&#x20;

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FFejYQsizGYSlTJzOZn2a%2Fimage.png?alt=media&#x26;token=bbab2c0f-29f8-4020-aac0-7fc9778de02b" alt=""><figcaption></figcaption></figure>

Upon analyzing this in dcode.fr, I observed that it is NATO encoded text.

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FFuZz5CcgxdoJgT7wk0T9%2Fimage.png?alt=media&#x26;token=eaa11db7-7f51-4348-9600-f4a9fc892522" alt=""><figcaption></figcaption></figure>

Further, upon deciphering it, I noticed that it was english for "="

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FmH56rovBxwEiEuWzEzxZ%2Fimage.png?alt=media&#x26;token=1ad70e64-38e4-4bd2-bd6f-b748c2bc1595" alt=""><figcaption></figcaption></figure>

Since, this is not at all a flag, there has to be more to it. Further, it can be speculated that "=" is the  end of a base64 or base32 encoded string.

I examined the github repo a bit more and found out that a total of 3000+ commits were there.

`git rev-list --count --all`

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2F1qCI0OiPrdJdru5ChVSv%2Fimage.png?alt=media&#x26;token=8bccaf48-efc6-48d8-9c01-1d9f2aae16ae" alt=""><figcaption></figcaption></figure>

Upon inspecting git log and a few of the commits (reverting repo to the commit), I observed that in each commit, data file was of different size. So, the data file must have different character/word which would be later combined together to give us the next clue.

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FHC9rXMdjgBGHr5fB6ZXu%2Fimage.png?alt=media&#x26;token=07b38df8-acad-4329-a304-49a487bec294" alt=""><figcaption></figcaption></figure>

Now upon checking different commits, I see different data files. I inspect a few data files and turns out each one of then had a single character (Alfa, Bravo, Delta etc.)

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FT7QVGxRZ7fmfIW0FCSaq%2Fimage.png?alt=media&#x26;token=2ecc8d5f-cd41-4d46-afa5-af54ceb18329" alt=""><figcaption></figcaption></figure>

All of these files are encoded in NATO phonetic encoding and it goes like A,B,C for Alfa, Bravo, Charlie etc.

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FbpABzwjvxF2StImXkyN0%2Fimage.png?alt=media&#x26;token=daf7a890-4f7f-4fba-b379-1cedd185d92c" alt=""><figcaption></figcaption></figure>

To extract the message, we might need to combine all of these together. Now, doing this for 3016 commits is cumbersome. So, a programmatic approach was essential to save some time.

I found a script online that would extract all the files in a repository here: [https://gist.github.com/magnetikonline/5faab765cf0775ea70cd2aa38bd70432](https://gist.github.com/magnetikonline/5faab765cf0775ea70cd2aa38bd70432)

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FvQ5pVvzvlplz7vlKMy3j%2Fimage.png?alt=media&#x26;token=bc0ec3b7-02b5-436f-8056-72408645bbc8" alt=""><figcaption></figcaption></figure>

But since the script was only producing output related to the commits name, I had to modify the script so that it is numbered (in order to view from first to last commit)

Script used was:

```
#!/bin/bash -e

function getRepoSHA1List {
        pushd "$1" >/dev/null
        git rev-list --all
        popd >/dev/null
}

function exportCommits {
        local commitSHA1
        local count=1

        local IFS=$'\n'
        for commitSHA1 in $(getRepoSHA1List "$1"); do
                # build export directory for commit and create
                local exportDir="${2%%/}/$(printf "%04d" $count)"
                echo "Export $commitSHA1 -> $exportDir"
                mkdir --parents "$exportDir"

                # create archive from commit then unpack to export directory
                git \
                        --git-dir "$1/.git" \
                        archive \
                        --format tar \
                        "$commitSHA1" | \
                                tar \
                                        --directory "$exportDir" \
                                        --extract
                count=$((count + 1))
        done
}


# verify arguments
if [[ (! -d $1) || (! -d $2) ]]; then
        echo "Usage: $(basename "$0") GIT_DIR OUTPUT_DIR"
        exit 1
fi

if [[ ! -d "$1/.git" ]]; then
        echo "Error: it seems [$1] is not a Git repository?" >&2
        exit 1
fi

exportCommits "$1" "$2"

```

Upon running the script we see the data was being renamed as per the numbers

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FYuxsG979oESWLBVzdzsd%2Fimage.png?alt=media&#x26;token=8a402f6b-e26c-4760-a532-6a6bfdf134eb" alt=""><figcaption></figcaption></figure>

Now, upon inspecting the data files in these folders, I see some of them had the same sizees. For example, files with size 4444 corresponded to the alphabet "6"

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FcKSh8JQzCDjzFXssIcOp%2Fimage.png?alt=media&#x26;token=29877422-8a54-43e2-8ae9-e5c120afffbc" alt=""><figcaption></figcaption></figure>

Now, an algorithm is needed to rename these files based on the size to alphanumeric conversion. Since there are 26 english alphabets and 10 numerals, I need to classify 36 files of different sizes and put them in a dictionary for conversion. I wrote a simple Python script to rename size 4444 to 6 first and tested it out. Before that, I ran simple CLI commands to ensure how many files of size 4444 are there.

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FLO0luLIkyfHOcgHr58Fx%2Fimage.png?alt=media&#x26;token=229637f3-2e92-41ec-9912-aaf8273c0197" alt=""><figcaption></figcaption></figure>

Putting this through wc would give me how many files have 4444 size.

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FE9oWK0MX9Lf8R65ArNcA%2Fimage.png?alt=media&#x26;token=12d81259-fef0-4deb-a4d3-8793b4e3d6eb" alt=""><figcaption></figcaption></figure>

I wrote this small script  to replace files with size 4444 with the digit 6. The dictionary "size\_to\_code" can be extended for other files as well. But for testing I just tried to replace all the files with 4444 sizee first.

```
import os

size_to_code = {
    4444: "6",
}

def rename_files_in_directory(directory):
    count = 0
    for foldername, subfolders, filenames in os.walk(directory):
        for filename in filenames:
            filepath = os.path.join(foldername, filename)
            filesize = os.path.getsize(filepath)

            if filesize in size_to_code:
                new_filename = size_to_code[filesize] + os.path.splitext(filename)[1]
                new_filepath = os.path.join(foldername, new_filename)
                os.rename(filepath, new_filepath)
                count += 1
                print(f'{count}. Renamed {filename} to {new_filename}')

output_directory = '/home/kali/ctf/sunshine/knowledge_repo/_the_ai_repository_of_knowledge/output'
rename_files_in_directory(output_directory)
```

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FM7PbnJOvS2VWMhoPNevb%2Fimage.png?alt=media&#x26;token=92de8a20-aed8-40e7-8ae4-c1e940231fb5" alt=""><figcaption></figcaption></figure>

I saw that a total 94 files were renamed. I confirmed this earlier in the CLI too. I then grepped and saw that all the files were renamed properly.

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2Fm1asCWORNfPHLGrMQJxQ%2Fimage.png?alt=media&#x26;token=8458b951-f328-4b71-8374-068997a81c3b" alt=""><figcaption></figcaption></figure>

So the algorithm to solve and extract the message is as follows:

1. Classify 36 files (decode audio morse) manually and based on their sizes, add them to the dictionary "size\_to\_code" in Python script. To obtain the directory names of these 36 different files, write a simple bash script.
2. Rename the files from NATO phonetic to their respective plaintext conversion.
3. Open folders one by one using Python and read the file name. Keep appending the filename in a text file.
4. Reverse the obtained string.
5. Decode base64 to plaintext and save in a file.
6. Extract the message.

I wrote a small script to find out the folders with data files of similar sizes:

```
import os
from collections import defaultdict

def find_files_with_same_size(directory):
    size_to_directories = defaultdict(list)
    for foldername, subfolders, filenames in os.walk(directory):
        for filename in filenames:
            if filename != "data":
                continue
            filepath = os.path.join(foldername, filename)
            filesize = os.path.getsize(filepath)
            folder = os.path.basename(foldername)
            size_to_directories[filesize].append(folder)

    with open('sizes_and_files.txt', 'w') as f:
        for size, folders in size_to_directories.items():
            if len(folders) > 1:
                f.write(f"Size: {size}\nFiles: {', '.join(folders)}\n\n")

output_directory = '/home/kali/ctf/sunshine/knowledge_repo/_the_ai_repository_of_knowledge/output'
find_files_with_same_size(output_directory)
```

The script classifies a size then tells which folders have "data" file of the same sizes.

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FD5d3f94VVkQqh5EZ8WYR%2Fimage.png?alt=media&#x26;token=09f6e5bd-0ccd-4d3d-951c-220d24b93dd1" alt=""><figcaption></figcaption></figure>

This shortened my looking up time in audio files. Now I can manually go ahead and decode them, then add the decoded value in my solution script's dictionary.

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FatzGrM0cSK7nWL5Qt100%2Fimage.png?alt=media&#x26;token=b2db87fa-f39f-450e-8d4e-712402cb2824" alt=""><figcaption></figcaption></figure>

Also, since the audio files showed no case sensitivity, it is safe to assume the message could also be base32 where all letters are capital. The final dictionary becomes:

```
size_to_code = {
    4444: "6",
    11549: "E",
    35098: "F",
    14364: "2",
    18804: "A",
    22464: "N",
    10004: "3",
    32442: "Q",
    24124: "D",
    16614: "V",
    17094: "L",
    12439: "G",
    66378: "Y",
    76892: "I",
    50701: "Z",
    4986: "7",
    394044: "K",
    19998: "X",
    26684: "P",
    98164: "H",
    31644: "W",
    29616: "C",
    73508: "O",
    120294: "J",
    10404: "4",
    6036: "5",
    14576: "T",
    12830: "S",
    13006: "M",
    17643: "R",
    34298: "U",
    278894: "=",
}
```

Upon running the modified solution script, we can see that whole 3016 files were renamed to their respective mappings.

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FFugqlFLefy4NhbTeIPHn%2Fimage.png?alt=media&#x26;token=0d5c1b11-7a6b-4c9c-aabc-d3d44d12b5e3" alt=""><figcaption></figcaption></figure>

Finally, a script was created to read the audio file names in respective folder, append those filenames in a file called output.txt

```
import os

def read_filenames_and_append_to_file(directory, output_file):
    characters = []
    for folder in sorted(os.listdir(directory)):
        folder_path = os.path.join(directory, folder)
        if not os.path.isdir(folder_path):
            continue
        files = os.listdir(folder_path)
        if files:
            characters.append(files[0])

    with open(output_file, 'w') as f:
        f.write(''.join(characters))

output_directory = '/home/kali/ctf/sunshine/knowledge_repo/_the_ai_repository_of_knowledge/output'
output_txt = '/home/kali/ctf/sunshine/knowledge_repo/_the_ai_repository_of_knowledge/output.txt'

read_filenames_and_append_to_file(output_directory, output_txt)
```

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FpHtn9TmZu2QrB3WnV5mj%2Fimage.png?alt=media&#x26;token=4d7d0ca3-0437-40de-bb73-f224720e0f5b" alt=""><figcaption></figcaption></figure>

The script upon running gave an output.txt file. Since it starts with === sign, this gives me an idea that the base32 string is reversed.

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FdbAO7F26zN3HXK6yLG88%2Fimage.png?alt=media&#x26;token=557106ae-d63e-49f7-b850-ade18fc17003" alt=""><figcaption></figcaption></figure>

So, to make it a proper base32 string, I then finally reversed this string and saved the output in a file called final.&#x20;

```
rev output.txt | base32 -d > final
file final
```

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2FDvnQdHX9fTgPmsCS1B3f%2Fimage.png?alt=media&#x26;token=d39f3b23-4ce6-46d4-b8e0-977f0a45e941" alt=""><figcaption></figcaption></figure>

This turned out to be a gz file. Then finally, I used gunzip to extract a text file out of this archive and obtained the flag!

<figure><img src="https://62284611-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSvRnuhl_P5WCd1fZEn%2Fuploads%2Fl7OuKJaWYEYwrctBUqz5%2Fimage.png?alt=media&#x26;token=dacbca9c-7bc3-470e-aac4-73b701179e41" alt=""><figcaption></figcaption></figure>

Alternate: One can also use python based decoders like morse2ascii and wav2ascii to do this. I tried it using that but a lot of the small length audio files were not being decoded correctly. Also, the same algorithm I devised can be done using md5 checksums of the files.

