Write up CyberSpace CTF 2024
# **Begineer**
## **encryptor(rev/mobile)**

tải file có tên encryptor.apk về ta bỏ vào https://www.decompiler.com/ để decompiler apk
tại sources/com/example/encryptor/MainActivity.java
ta biết được flag được mã hóa blowfish
key dưới dạng base64 là **ZW5jcnlwdG9yZW5jcnlwdG9y**

chạy file apk ta được flag bị mã hóa
**OIkZTMehxXAvICdQSusoDP6Hn56nDiwfGxt7w/Oia4oxWJE3NVByYnOMbqTuhXKcgg50DmVpudg=**
giải mã:

flag: ~~CSCTF{3ncrypt0r_15nt_s4Fe_w1th_4n_h4Rdc0d3D_k3y!}~~
## **cipher block clock(crypto)**

source code:
```python
f = b"CSCTF{placeholder}"
k = Crypto.Random.get_random_bytes(16)
def enc(pt):
iv = Crypto.Random.get_random_bytes(32)
ct = Crypto.Cipher.AES.new(iv, Crypto.Cipher.AES.MODE_CBC, k)
return iv.hex(), ct.encrypt(Crypto.Util.Padding.pad(pt, 32)).hex()
iv0, ct0 = enc(f)
iv1, ct1 = enc(b"Lookie here, someone thinks that this message is unsafe, well I'm sorry to be the bearer of bad news but tough luck; the ciphertext is encrypted with MILITARY grade encryption. You're done kiddo.")
print(f"iv0: {iv0}\nct0: {ct0}\n")
print(f"iv1: {iv1}\nct1: {ct1}")
#iv0: b800f4bfd38030ff3ed82560a11e9ef67e9c3529ab52938c9458c7d8602d7a51
#ct0: 5abb8490d872f101dcd89af421958c54204642e7d0f96a6393759f45c9630e9b6b16e87e9a96d00044fed28e295163c5fc6ed2a59839c4be433f74f8614fce54
#iv1: 667a704a5b4730f1954692ea0d924a7f9ea8fe478415fa2aad8ae59604f7950e
#ct1: 6570f5f9b8c43a6622b1b4abc037fa09cd82be1570ec1f262538eff17374161673276cb82304676f7a37b658ae9c997e6c5b17987928f2dd292cc7ec2fcc6b9ed22994616848ee716bcf6142e0689b3aa1c5abbcd3c316a3329f8f51f378cf6e10cbeeefe54a3611dc878d23c606e78e114da6816fa384605f75f26299a1d9dca83ded23b1f7cdd1e8acced6fcb199fda34a3bc26d2e88bc3ce01466a74e44744e5a0e65cab25745c64f178cd7680e2b3c993286e236cd3c55e22cdd71aac279c749ddcf4f8137867b682c29f10d754da4f6b22603531a4885a5144f012c8f23
```
ở đây, nếu để ý, ta sẽ nhận ra key và iv đã bị đổi chỗ
```python=
ct = Crypto.Cipher.AES.new(iv, Crypto.Cipher.AES.MODE_CBC, k)
```
vậy ta cần sửa đoạn code để đúng với thứ tự
```python=
f = b"CSCTF{placeholder}"
iv = Crypto.Random.get_random_bytes(16)
def enc(pt):
k = Crypto.Random.get_random_bytes(32)
ct = Crypto.Cipher.AES.new(k, Crypto.Cipher.AES.MODE_CBC, iv)
return k.hex(), ct.encrypt(Crypto.Util.Padding.pad(pt, 32)).hex()
k0, ct0 = enc(f)
k1, ct1 = enc(b"Lookie here, someone thinks that this message is unsafe, well I'm sorry to be the bearer of bad news but tough luck; the ciphertext is encrypted with MILITARY grade encryption. You're done kiddo.")
print(f"k0: {k0}\nct0: {ct0}\n")
print(f"k1: {k1}\nct1: {ct1}")
#k0: b800f4bfd38030ff3ed82560a11e9ef67e9c3529ab52938c9458c7d8602d7a51
#ct0: 5abb8490d872f101dcd89af421958c54204642e7d0f96a6393759f45c9630e9b6b16e87e9a96d00044fed28e295163c5fc6ed2a59839c4be433f74f8614fce54
#k1: 667a704a5b4730f1954692ea0d924a7f9ea8fe478415fa2aad8ae59604f7950e
#ct1: 6570f5f9b8c43a6622b1b4abc037fa09cd82be1570ec1f262538eff17374161673276cb82304676f7a37b658ae9c997e6c5b17987928f2dd292cc7ec2fcc6b9ed22994616848ee716bcf6142e0689b3aa1c5abbcd3c316a3329f8f51f378cf6e10cbeeefe54a3611dc878d23c606e78e114da6816fa384605f75f26299a1d9dca83ded23b1f7cdd1e8acced6fcb199fda34a3bc26d2e88bc3ce01466a74e44744e5a0e65cab25745c64f178cd7680e2b3c993286e236cd3c55e22cdd71aac279c749ddcf4f8137867b682c29f10d754da4f6b22603531a4885a5144f012c8f23
```
có thể tham khảo thêm về AES ở https://nguyenquanicd.blogspot.com/2019/10/aes-bai-6-cac-che-o-ma-hoa-va-giai-ma.html
có 2 plaintext bị mã hóa là **~~FLAG~~** và **b"Lookie here, someone thinks that this message is unsafe, well I'm sorry to be the bearer of bad news but tough luck; the ciphertext is encrypted with MILITARY grade encryption. You're done kiddo."**
hướng giải quyết:
decrypt ct1 bằng k1 và iv = 0000000000000000
ta nhận thấy 16 bytes đầu khác với plaintext
cho xor 16 bytes đầu của plaintext với 16 bytes đầu của thứ ta nhận được sau khi decrypt ct1
->ta được iv
do xài chung iv nên ta xor iv với 16 bytes đầu thứ ta nhận được sau khi decrypt ct0
->flag hoàn chỉnh
## **Modulus RSA(crypto)**

source code:
```python=
from Crypto.Util.number import bytes_to_long
from sympy import randprime
m = bytes_to_long(b'CSCTF{fake_flag}')
p, q, r = sorted([randprime(0, 1<<128) for _ in range(3)])
n = p * q * r
e = 65537
c = pow(m, e, n)
w, x, y = q % p, r % p, r % q
print(f'{w = }')
print(f'{x = }')
print(f'{y = }')
print(f'{c = }')
"""
w = 115017953136750842312826274882950615840
x = 16700949197226085826583888467555942943
y = 20681722155136911131278141581010571320
c = 2246028367836066762231325616808997113924108877001369440213213182152044731534905739635043920048066680458409222434813
"""
```
Phân tích:
q=w+p\*a
r=x+p\*b
r=y+q\*c
do p, q, r gần nhau (dựa vào w,x,y là các số dư) và được sort bé đến lớn nên ta dễ dàng nhận thấy a, b, c chỉ nằm trong khoảng 0 đến 10?
bruteforce a, b, c là ta nhận được p, q, r (phải thỏa mãn tất cả các điều kiện trên)
flag: ~~CSCTF{i_l1k3_m0du1us_v3ry_MUCH!!\^\_^}~~
# **Crypto**
## **ezcoppersmith (crypto)**

source code:
```python=
import os
from Crypto.Util.number import bytes_to_long, getPrime
from random import getrandbits
from sympy import nextprime
flag = os.urandom(70)+b"CSCTF{fake_flag}"+os.urandom(70)
flag = bytes_to_long(flag)
e = 0x10001
p = getPrime(1024)
q = nextprime(p+getrandbits(512))
n = p*q
ct = pow(flag,e,n)
print(f"{n = }")
print(f"{ct = }")
"""
n = 18644771606497209714095542646224677588981048892455227811334258151262006531336794833359381822210403450387218291341636672728427659163488688386134401896278003165147721355406911673373424263190196921309228396204979060454870860816745503197616145647490864293442635906688253552867657780735555566444060335096583505652012496707636862307239874297179019995999007369981828074059533709513179171859521707075639202212109180625048226522596633441264313917276824985895380863669296036099693167611788521865367087640318827068580965890143181999375133385843774794990578010917043490614806222432751894223475655601237073207615381387441958773717
ct = 814602066169451977605898206043894866509050772237095352345693280423339237890197181768582210420699418615050495985283410604981870683596059562903004295804358339676736292824636301426917335460641348021235478618173522948941541432284037580201234570619769478956374067742134884689871240482950578532380612988605675957629342412670503628580284821612200740753343166428553552463950037371300722459849775674636165297063660872395712545246380895584677099483139705934844856029861773030472761407204967590283582345034506802227442338228782131928742229041926847011673393223237610854842559028007551817527116991453411203276872464110797091619
"""
```
tác giả muốn ta giải bằng coppersmith, tuy nhiên ta có thể dùng cách đơn giản hơn để giải
bỏ n vào https://www.alpertron.com.ar/ECM.HTM

ta nhận được p và q mà không mất công sức
flag: ~~CSCTF{c0pp3rsm1th_1s_a_m4th_g0d_n0_d0ub7}~~
## **trendy windy trigonity(crypto)**

source code:
```python=
from Crypto.Util.number import bytes_to_long
flag = REDACTED
print(len(flag))
R = RealField(1000)
a,b = bytes_to_long(flag[:len(flag)//2]),bytes_to_long(flag[len(flag)//2:])
x = R(0.75872961153339387563860550178464795474547887323678173252494265684893323654606628651427151866818730100357590296863274236719073684620030717141521941211167282170567424114270941542016135979438271439047194028943997508126389603529160316379547558098144713802870753946485296790294770557302303874143106908193100)
enc = a*cos(x)+b*sin(x)
#38
#2.78332652222000091147933689155414792020338527644698903976732528036823470890155538913578083110732846416012108159157421703264608723649277363079905992717518852564589901390988865009495918051490722972227485851595410047572144567706501150041757189923387228097603575500648300998275877439215112961273516978501e45
```
bài này có thể sẽ khá khó với những người chưa biết dùng lattice
tham khảo: https://github.com/nguyen-xuan-quoc/Cryptography/blob/Crypto_Blog/lattice.md
việc của ta là tìm a,b
a, b là các phần của flag
ta nhận thấy bài này có thể giải dễ dàng bằng lattice
trước tiên ta cần tính sin(x), cos(x) trước
bỏ dấu phẩy của sin(x), cos(x), enc để đưa dạng số thực về số nguyên và để độ lớn các vector trong cơ sở lưới đủ lớn
->dựng lưới và rút gọn bằng thuật toán LLL()
solve script:
```python=
enc=278332652222000091147933689155414792020338527644698903976732528036823470890155538913578083110732846416012108159157421703264608723649277363079905992717518852564589901390988865009495918051490722972227485851595410047572144567706501150041757189923387228097603575500648300998275877439215112961273516978501
si=688000066129451582287455775590271790852522834500913497990313379977640996221564070751793932445783809066091998551308753734844878806989472950186030059858750702044192410491496871167109016610094099724870771056714462857829757818135300013935546022506733140773473400409304166692974232215367769438828974926774
co=725710623462182927460386107818518991861766695402262109720861525090606727280956023359670394988149345108055120445571438017603316025002641689071890899592673681187182904644945219637632169105859718998421403432869211844698260215966666342442905719787859079964593837807783215898392247074753171887995720511822
H= [co,si]
c = enc
I = identity_matrix(len(H))
I = I.insert_row(len(H), [0 for x in range(len(H))])
L_helper = [[x] for x in H]
L_helper.append([-c])
L = I.augment(matrix(L_helper))
M=L.LLL()
M
```
chạy script ta nhận được cơ sở lưới rút gọn
```
[ 1501403158973585406817603354497647816859742771 2461834501240441634675537458806974655348946301 78371099108360323685167396678355016917701736]
[ 284306321062793967364586263548065265576567078391542842324578280322977193318353687112887237261878893717566170914082880933242028 -354164789654068623129439107858084763008860360011113560181374364226857637279896195241739080745364053538114505071038983777870273 5678581706756207877555905357624067293155569528568166549417611578588443482976433425452270619840647754458137188520074586856178251]
[14539523260241899955214921673238876284970468640752573963444933572405569367124923362066472721484576327009970099582168286145813007 -8891513998790673847027059845126146145407690426022147848988416069701329486909271995857649832878770362873723616505165195476166434 762395554839900614693733156723298683781574166336981000028301268614413894653651603961175251064374125987777586783472398148360954]
```
ta nhận thấy vector M[0] là vector ta cần tìm:
a=1501403158973585406817603354497647816859742771
b=2461834501240441634675537458806974655348946301

flag: ~~CSCTF{Trigo_453_Tr3ndy_FuN_Th35e_D4Y5}~~
----
# **Forensics**
## **Geometry Dash 2.1(forensics)**

ta nhận được file .dat geometry dash
bỏ file vào https://gdcolon.com/gdsave/

ta nhận được file xml
có khá nhiều thứ linh tinh nhưng ta chỉ lấy tên author
~~CSCTFa52de5~~
kế đến ta vào GDBrowser
https://gdbrowser.com/u/CSCTFa52de5
flag bị giấu trong phần bình luận

flag: ~~CSCTF{geometry_dash_d0895c120d671b}~~
--END--