~$ 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++.
#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
jandj+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:
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 ifcalculation = 123, reversed_calc will be321] - If
reversed_calccan be divided by4without reminder thenanswer = 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:
x = 1answer = 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 :
4 = 0^2 + 2^28 = 2^2 + 2^216 = 0^2 + 4^2----------------------------5002 = 39^2 + 59^2 => 49^2 + 51^2 => 51^2 + 49^2 => 59^2 + 39^2And 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:
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:
6 ** 2 + 158 ** 250 ** 2 + 150 ** 290 ** 2 + 130 ** 2130 ** 2 + 90 ** 2150 ** 2 + 50 ** 2We can remove the duplicates:
6 ** 2 + 158 ** 250 ** 2 + 150 ** 290 ** 2 + 130 ** 2The third result is 90, 130 so the flag is KCTF{90,130}.