~$ Dissecting the KnightCTF Programming challenges!

Posted on Jan. 30th, 2022. | Est. reading time: 5 minutes

Tags:CTFProgrammingWrite-Up


Write-ups

Loop in a Loop (100 points)

This challenge was a simple code reversal challenge, written in C++.

```py #include <iostream> using namespace std; int main() { string flag; cout << "Enter the flag: "; cin >> flag; for (int i=0; i < flag.length(); i++) { for (int j=i; j < flag.length() - 1; j++) { char x = flag[j]; flag[j] = flag[j+1]; flag[j+1] = x; } } if (flag == "CFb5cp0rm1gK{1r4nT_m4}6") cout << "Congrats. That's the flag!" << endl; else cout << "Wrong flag. Bye" << endl; return 0; } ```

I rarely do C++ and never when I could use something else, so the solution will be in Python.

But first, analysis:

  • The code takes a flag as input.
  • It then loops on all indices, and then initiates a second loop on all remaining indices.
  • The code inverts the characters at positions j and j+1.

To reverse this we do the exact same loop with reversed parameters (i: length -> 0, j: length -> i), and then print the original flag:

```py flag = list("CFb5cp0rm1gK{1r4nT_m4}6") for i in range(len(flag) - 1, -1, -1): for j in range(len(flag) - 1, i, -1): flag[j], flag[j-1] = flag[j-1], flag[j] print("".join(flag)) ```

The flag is KCTF{b451c_pr06r4mm1ng}


Reverse the Answer (50 points)

This is a coding challenge aimed at the resolution of a mathematical problem.

The statement of the problem is the following:

  • Let x = 1
  • Let calculation = (x*(x+1)) + (2 *(x + 1))
  • Let reversed_calc = reversed number of calculation [for example if calculation = 123, reversed_calc will be 321]
  • If reversed_calc can be divided by 4without reminder then answer = answer + reversed_calc
  • Repeat all the calculations until you have x = 543
  • The final answer will be the flag when x = 543
  • Flag Format: KCTF{answer_here} Example Flag : KCTF{123}

We perform the loop for x going from 1 to 543.

To facilitate the calculation, we write the following program, in Python:

```py x = 1 answer = 0 while x <= 543: calculation = (x + 2) * (x + 1) rev = int(str(calculation)[::-1]) if rev % 4 == 0: answer += rev x += 1 print(answer) ```

This produces 12252696, so the flag is KCTF{12252696}.


Square Sum (50 points)

Have you ever heard the term "The sum of two squares"?

It's like the following :

```txt 4 = 0^2 + 2^2 8 = 2^2 + 2^2 16 = 0^2 + 4^2 ---------------------------- 5002 = 39^2 + 59^2 => 49^2 + 51^2 => 51^2 + 49^2 => 59^2 + 39^2 ```

And so on. In the example of 16, if we add the square of 0 & 4 we get 16. So here we are getting two values 0 & 4. So that's the answer.

So write a program & find out the two values of 25000. Conditions are the following:

  • Remove the duplicates
  • Pick the third one

Flag Format: KCTF{0,1}


We can produce an algorithm that loops between both bounds (or more accurately the square root of both bounds). This can quite easily be written in Python:

```py from math import sqrt for i in range(round(sqrt(25000))): for j in range(round(sqrt(25000)), -1, -1): if i ** 2 + j ** 2 == 25000: print(f"{i} ** 2 + {j} ** 2") ```

This produces:

```txt 6 ** 2 + 158 ** 2 50 ** 2 + 150 ** 2 90 ** 2 + 130 ** 2 130 ** 2 + 90 ** 2 150 ** 2 + 50 ** 2 ```

We can remove the duplicates:

```txt 6 ** 2 + 158 ** 2 50 ** 2 + 150 ** 2 90 ** 2 + 130 ** 2 ```

The third result is 90, 130 so the flag is KCTF{90,130}.