Challenge Description
This app is pretty handy
Wrap the solved flag in SEKAI{}
Mowwwww
Got an apk
Threw it in jadx gui
i found some weird ambient_trace logic in b7w and b7a basically the app tracks every "feature" you use (eg officetext pdfmetadata) by flipping bits in a hidden mask
then i saw that r7/m0java was reading that mask to draw dots on the home screen wtf it was a qr code split into five layers! each layer only appears if you've used a specific feature if you use all five you get the full picture
Each layer is an obfuscated byte array XOR'd with some murmur-style hash bullshit
The Moww Plan
I had to write a script to undo the XOR and rebuild the grid The math was just a bunch of magic numbers designed to look scary I also had to reproduce Java's 32-bit signed integer overflows using these constants:
- Initial seed stuff:
4618459071831565813-2048144789 - Mix constants:
2146121005-2073254261
Here is the logic used to decrypt the arrays and build the layers:
Python
from PIL import Image, ImageDraw
import cv2, numpy as np
PRIVATE=189; HDMV_DTS=130; VIDEO_MASK=240; AC4=172; SPLICE=134; AUDIO=192
arrs=[
[118,210,104,186,26,106,72,223,100,211,224,18,33,45,219,16,53,83,49,41,165,231,4,33,155,214,247,253,1,47,248,56,239,57,46,229,246,153,230,118,190,219,78,243,133,249,224,106,106,63,204,75,106,22,100,40],
[16,224,137,160,57,77,181,214,AC4,SPLICE,102,46,187,215,208,141,193,133,133,205,7,105,141,96,207,77,242,195,156,184,228,96,10,123,11,246,147,150,120,160,184,59,120,141,216,45,AUDIO,83,216,186,250,243,55,7,168,6],
[83,218,104,143,137,155,45,196,201,119,110,200,66,213,50,45,220,55,72,89,30,250,150,158,183,104,VIDEO_MASK,156,71,86,195,113,156,127,50,241,196,37,39,6,125,89,213,187,38,249,48,120,52,63,229,183,155,28,126,123],
[204,215,0,44,85,0,45,59,245,88,238,159,162,22,23,227,154,196,111,42,209,201,153,55,204,194,147,171,174,145,194,202,178,1,237,211,47,105,15,222,252,57,237,119,16,204,247,64,37,67,49,222,160,247,171,3],
[37,106,34,213,37,70,91,150,61,85,183,79,88,191,30,218,169,124,231,124,106,108,163,55,213,83,164,22,88,120,121,206,185,79,51,215,214,236,99,165,8,94,118,87,PRIVATE,HDMV_DTS,236,45,208,216,217,33,232,155,33,48]
]
def int32(x):
x &= 0xffffffff
return x-0x100000000 if x&0x80000000 else x
def ushr(x,n): return (x & 0xffffffff) >> n
base = [[0]*21 for _ in range(21)]
for i15, arr in enumerate(arrs):
for i16 in range(56):
i17 = int32((((i15+1)*461845907) ^ 1831565813) ^ ((i16+17)*(-2048144789)))
i18 = int32((i17 ^ ushr(i17,16)) * 2146121005)
i19 = int32((i18 ^ ushr(i18,15)) * (-2073254261))
mask = ushr(i19 ^ ushr(i19,16), 24) & 255
val = arr[i16] ^ mask
for b in range(8):
if val & (1<<b):
pos = i16*8 + b
if pos < 441:
base[pos//21][pos%21] = 1
The decrypted data produced this raw grid before adding the QR function patterns:
The app only stored the data modules There were no finder patterns timing lines or format information I had to manually glue the QR function patterns back on and brute-force the mask and ECL using OpenCV Ngl that part was a pain in the ass
I calculated the format information bits using the QR specification polynomial 0x537 and constant 0x5412
Here is the script that finished the job:
Python
def add_finder(g, x0, y0):
for y in range(7):
for x in range(7):
if x in [0,6] or y in [0,6] or (2<=x<=4 and 2<=y<=4): g[y0+y][x0+x] = 1
else: g[y0+y][x0+x] = 0
def format_bits(ecl, mask):
data = (ecl << 3) | mask
v = data << 10
poly = 0x537
for i in range(14, 9, -1):
if (v >> i) & 1: v ^= poly << (i-10)
res = ((data << 10) | v) ^ 0x5412
return [(res >> i) & 1 for i in range(14, -1, -1)]
def build_final_qr(ecl, mask):
g = [row[:] for row in base]
add_finder(g, 0, 0); add_finder(g, 14, 0); add_finder(g, 0, 14)
for i in range(8, 13):
g[6][i] = 1 if i % 2 == 0 else 0
g[i][6] = 1 if i % 2 == 0 else 0
g[13][8] = 1 # dark module
bits = format_bits(ecl, mask)
pos = [(8,0),(8,1),(8,2),(8,3),(8,4),(8,5),(8,7),(8,8),(7,8),(5,8),(4,8),(3,8),(2,8),(1,8),(0,8)]
for b, (x, y) in zip(bits, pos): g[y][x] = b
return g
det = cv2.QRCodeDetector()
for ecl in range(4):
for mask in range(8):
g = build_final_qr(ecl, mask)
img = np.array([[255 if not c else 0 for c in row] for row in g], dtype=np.uint8)
val, _, _ = det.detectAndDecode(img)
if val:
print(f"FOUND: {val}")
And TRAAAA! We got a valid scannable QR code:
OpenCV finally made sense to me and printed clankedormiscgod Ez pz
Flag
SEKAI{clankedormiscgod}