Sumar
Am extras backup-ul si APK-ul, am identificat parametrii de securitate din security.xml, apoi am facut
reverse pe codul smali si pe libraria nativa pentru a reconstrui exact schema de criptare.
Dupa ce am recuperat pepper-ul si am validat PIN-ul corect, am parsat direct WAL-ul si am decriptat
payload-urile pana am gasit flagul.
Dovada rezolvarii
- Verificam ce contine arhiva:

- In
security.xmlavem niste parametri KDF: un pin de 8 cifre, iteratii / salt / dkLen:

- Sa ne uitam in
notes.db. Ce este important pentru decriptare este payload-ul, iar nonce-ul se poate calcula dincreated_at:

- Acuma in JadX avem o functie
pepper:

- Daca deschidem in IDA
lib/x86_64/libnative.soputem gasi functiapepper():

Avem un string obfuscat, prin urmare putem rula deobfuscarea:

pepper = v2::rocsc::pepper::9f3a
- Avem nevoie de pepper deoarece in functia
Security.deriveKey()parola este"pin:pepper":

- In functia
CrashInsertActivityse regaseste aceasta parte din cod care insereaza 12 note dummy in DB/WAL. Prin urmare daca noi vom face bruteforce la parola (pin), ne vom da seama de parola corecta cand plaintext-ul va aveaDUMMYin el:

- Am avut noroc si am ghicit pinul dupa ce am incercat mai multe precum
00000000,11111111,22222222,… am ajuns si la12345678si era corect! - Tot ce mai ramane este sa decriptam AES-GCM pentru fiecare rand (calculam nonce-ul din timestamp):
import struct, hashlib, base64
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
WAL = "databases/notes.db-wal"
pin = "12345678"
pepper = "v2::rocsc::pepper::9f3a"
salt = base64.b64decode("8uOT9OHUHNLoqjpOelb2Gw==")
iters = 150000
key = hashlib.pbkdf2_hmac("sha256", f"{pin}:{pepper}".encode(), salt, iters, dklen=32)
aes = AESGCM(key)
def read_varint(buf, off):
x = 0
for i in range(9):
if off + i >= len(buf):
return None, 0
b = buf[off + i]
if i == 8:
x = (x << 8) | b
return x, 9
x = (x << 7) | (b & 0x7F)
if not (b & 0x80):
return x, i + 1
return None, 0
def int_be(data):
if not data:
return 0
v = int.from_bytes(data, "big", signed=False)
if data[0] & 0x80:
v -= 1 << (8 * len(data))
return v
def decode_serial(payload, pos, s):
if s == 0:
return None, 0
if s == 1:
return int_be(payload[pos:pos + 1]), 1
if s == 2:
return int_be(payload[pos:pos + 2]), 2
if s == 3:
return int_be(payload[pos:pos + 3]), 3
if s == 4:
return int_be(payload[pos:pos + 4]), 4
if s == 5:
return int_be(payload[pos:pos + 6]), 6
if s == 6:
return int_be(payload[pos:pos + 8]), 8
if s == 7:
return struct.unpack(">d", payload[pos:pos + 8])[0], 8
if s == 8:
return 0, 0
if s == 9:
return 1, 0
if s >= 12 and s % 2 == 0:
n = (s - 12) // 2
return payload[pos:pos + n], n
if s >= 13 and s % 2 == 1:
n = (s - 13) // 2
return payload[pos:pos + n].decode("utf-8", "replace"), n
return None, 0
def parse_leaf(page, pgno):
out = []
base = 100 if pgno == 1 else 0
if len(page) < base + 8:
return out
if page[base] != 0x0D:
return out
cnt = int.from_bytes(page[base + 3:base + 5], "big")
ptr = base + 8
for i in range(cnt):
cptr = ptr + 2 * i
if cptr + 2 > len(page):
break
off = int.from_bytes(page[cptr:cptr + 2], "big")
if off <= 0 or off >= len(page):
continue
p = off
psz, n1 = read_varint(page, p); p += n1
rid, n2 = read_varint(page, p); p += n2
if not n1 or not n2 or psz is None or p + psz > len(page):
continue
rec = page[p:p + psz]
hsz, hn = read_varint(rec, 0)
if not hn or hsz is None or hsz > len(rec):
continue
j = hn
serials = []
while j < hsz:
s, sn = read_varint(rec, j)
if not sn:
break
serials.append(s); j += sn
if j != hsz:
continue
vals = []
d = hsz
ok = True
for s in serials:
v, sz = decode_serial(rec, d, s)
if d + sz > len(rec):
ok = False; break
vals.append(v); d += sz
if ok:
out.append((rid, vals))
return out
wal = open(WAL, "rb").read()
pgsz = struct.unpack(">I", wal[8:12])[0]
if pgsz == 1:
pgsz = 65536
frame_sz = 24 + pgsz
frames = (len(wal) - 32) // frame_sz
seen = set()
for fi in range(frames):
off = 32 + fi * frame_sz
hdr = wal[off:off + 24]
if len(hdr) < 24:
break
pgno = struct.unpack(">I", hdr[:4])[0]
page = wal[off + 24:off + 24 + pgsz]
for rid, vals in parse_leaf(page, pgno):
if len(vals) != 3:
continue
created, blob = vals[1], vals[2]
if not isinstance(created, int):
continue
if not isinstance(blob, (bytes, bytearray)):
continue
if not (20 <= len(blob) <= 300):
continue
tup = (rid, created, bytes(blob))
if tup in seen:
continue
seen.add(tup)
nonce = hashlib.sha256(b"no" + struct.pack("<Q", created) + b"\x00" * 8).digest()[:12]
try:
pt = aes.decrypt(nonce, bytes(blob), None)
except Exception:
continue
k = pt.find(b"body")
body = None
if k != -1 and k + 4 < len(pt):
i = k + 4
t = pt[i]
if 0xA0 <= t <= 0xBF:
n = t - 0xA0
body = pt[i + 1:i + 1 + n].decode("utf-8", "replace")
elif t == 0xD9 and i + 1 < len(pt):
n = pt[i + 1]
body = pt[i + 2:i + 2 + n].decode("utf-8", "replace")
if body and not body.startswith("DUMMY_"):
print("FLAG:", body)
raise SystemExit
print("Nu am gasit flagul in WAL")
Rezultatul decripteaza nota reala si scoate flagul:
