> For the complete documentation index, see [llms.txt](https://ctf.zeyu2001.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ctf.zeyu2001.com/2021/dawgctf-2021/back-to-the-lab-3.md).

# Back to the Lab 3

## Problem

I think you just have to unzip it and open up \_main.vi. ... Oh god, what... what is this?

back\_to\_the\_lab\_3.7z: <https://drive.google.com/file/d/184nf-brey1pmk7uvW0VaZ234hrpB3Mgi/view?usp=sharing>

Author: nb

## Solution

First things first, it's pretty annoying that you only get one try, and we need to debug this multiple times later on. We can patch the program and make sure that the checker always allows the rest of the program to run by replacing it with 1.

![](/files/-M_EM9t64fzd57OUJmQO)

### First 14 Chars

![](/files/-M_EMFfiEFMq-AVZAUMr)

The bytearray with each number rotated left by 3 bits and 4 bits respectively is compared with two constant bytearrays. But note that the bytes are treated as 8-bit unsigned integers, so our implementation needs to consider that.

### Second 14 Chars

![](/files/-M_EMNDlHYRe-aBfQR4d)

The bytearray is represented as RGB values.

* R = byte value
* G = 5 (constant)
* B = array index

We can inspect the RGB values:

![](/files/-M_EMSDfLHcUEJf9hrMS)

## Third 14 Chars

![](/files/-M_EMW7BvakYZrDQ5tOu)

The input is split into the first 7 and last 7 bytes, then interleaved.

## Fourth 14 Chars

![](/files/-M_EM_A_xsSgPLwgucuH)

Every 2 characters in the array are concatenated and used as the "group name" to read the TDMS file. The Y-value is compared to the array index. Use the probe watch window to debug the program.

Y-value for "ru" is 0

![](/files/-M_EMfOYxZ_m-ATe4hv5)

Y-value for "ct" is 1

![](/files/-M_EMjPkUL-jNesTDWzC)

"3\_" is 3

"ur" is 2

"th" is 4

"r1" is 6

"e\_" is 5

Then, rearrange so that we go from 0 to 6.

## Fifth 14 Chars

There are multiple parts to this.

First, we know it starts with 'd', since other options give us False immediately.

![](/files/-M_EMzUKSy0Lfsqd5QbK)

One of the parts is password protected. If we drag the comment out we can see the hidden password.

![](/files/-M_EN1OcVNgdfrJSlaJf)

This unlocks the block diagram.

![](/files/-M_EN3vu0ci3yVI247KF)

The input is first converted to their ASCII values, then multiplied with 7927.

![](/files/-M_ENCGRUsrImON4gxaj)

Then, 0x49 is subtracted.

![](/files/-M_ENAHSqROwlNlKHjbd)

Then it goes through some redundant enqueuing, and is compared to some constants.

![](/files/-M_ENHnCtYX_wM8iDkiu)

But we get some weird results. 990802 = 1187410?

![](/files/-M_ENOAYF0m1NNPU7jID)

Then I realised that the last 16 bits are equal. We are probably comparing 16 bit numbers!

![](/files/-M_ENWGeNchtxMhISp8r)

## Final Script

Finally!

![](/files/-M_ENcKeFB9Gh5_ohuYl)

![](/files/-M_ENe4LhvG_Ac2W6JnG)

```python
import string

INT_BITS = 8

def leftRotate(n, d):
    result = bin(n << d)
    if len(result) - 2 > 8:
        result = result[-8:-1 * (len(result) - 2 - 8)] + result[2:-8]

    return int(result, 2)

rotated_left_3 = [34, 11, 187, 59, 26, 162, 0, 0, 0, 0, 147, 153, 250, 179]
rotated_left_4 = [68, 22, 119, 118, 52, 69, 100, 183, 119, 19, 0, 0, 0, 0]

flag = ''

for i in range(14):
    for char in string.ascii_letters + string.digits + '-_{}':
        if leftRotate(ord(char), 3) == rotated_left_3[i] or leftRotate(ord(char), 4) == rotated_left_4[i]:
            flag += char

    i += 1

print(flag)
assert len(flag) == 14

r_values = [49, 95, 99, 108, 117, 53, 116, 51, 114, 95, 52, 114, 114, 97]
flag += ''.join([chr(x) for x in r_values])

print(flag)
assert len(flag) == 28

flag += 'y_l00p_ca53_5t'
print(flag)
assert len(flag) == 42

flag += 'ructur3_the_r1'
print(flag)
assert len(flag) == 56

fragment = ''
results = [1187410, 616666, 989235, 1068505, 600812, 949600, 1100213, 600812, 1131921, 600812, 1068505, 949600, 997162]
for result in results:
    for char in string.ascii_letters + string.digits + '-_{}':
        output = ord(char) * 7927 - 0x49
        if int(bin(output)[-16:], 2) == int(bin(result)[-16:], 2):
            fragment += char
            break

flag += 'd'
flag += fragment[::-1]
print(flag)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ctf.zeyu2001.com/2021/dawgctf-2021/back-to-the-lab-3.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
