[MISC] Suspicious File Writeup

2025 HKCERT Misc Writeup

HKCert25 Misc/Suspicious File Writeup

Attachment

108K SuspiciousFile

0x01 Indentify The Suspicious File

The attachment is encoded by Base58, restore it and use file to recognize it’s header.

1
2
$ file ./download
./download: ISO Media, AVIF Image Sequence

0x02 Identify Stego Image

Initially, we can see this picture.

alt text

Actually, the flag was divided into to two different parts and hide in to two different channel, which is:

  • Pixel LSBs: common stego method, and AVIF preserves YUV channels.
  • Timing (stts): MP4 time-to-sample table can encode bits via duration variance.

0x03 Extract First Part from Pixel LSBs

3.1 Decode first frame as YUV444

Force yuv444p to keep all channels at full resolution.

3.2 Handle plane padding

VideoPlane may include padding per row (line_size). Slice to width to remove it.

3.3 Read bits in the correct order

Per pixel: V_lsb, Y_lsb, U_lsb

1
2
3
bits[0::3] = v & 1
bits[1::3] = y & 1
bits[2::3] = u & 1

3.4 Pack bits and decode payload

First 4 bytes are a big-endian length prefix.

1
2
3
data = pack_bits(bits, bitorder="big")
L = int.from_bytes(data[:4], "big")
first = data[4:4+L]

So that, we can get the first part of the flag.

b'first part:hkcert25{AVIF_Will_Be_The_'

0x04 Extract Second Part from Timing

4.1 Try packet durations (PyAV)

If PyAV reports only a single duration value, it’s normalized and unusable.

4.2 Fallback: parse MP4 stts

The stts box stores (count, delta) pairs. Expand them into full per-frame durations.

4.3 Map two duration levels → bits

  • small → 0
  • large → 1
1
2
bits = [1 if d == large else 0 for d in durs]
last = pack_bits(bits, bitorder="big").rstrip(b"\x00")

0x05Combine to Form the Flag

  • first usually starts with b'first part:hkcert25{AVIF_Will_Be_The_'
  • last usually starts with b'last part: Future_0f_Im4ge_F0rm4t}'

Strip prefixes and concatenate.

flag: hkcert25{AVIF_Will_Be_The_Future_0f_Im4ge_F0rm4t}

0x06 All-In-One Script

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import argparse
import sys
from typing import List, Optional, Tuple

import av
import numpy as np


def pack_bits(bits01: np.ndarray, bitorder: str = "big") -> bytes:
    bits01 = bits01.astype(np.uint8)
    n = (bits01.size // 8) * 8
    bits01 = bits01[:n]
    return np.packbits(bits01, bitorder=bitorder).tobytes()


def _plane_to_1d(plane: av.video.plane.VideoPlane, width: int, height: int) -> np.ndarray:
    buf = np.frombuffer(plane, dtype=np.uint8)
    if plane.line_size == width:
        return buf[: width * height]
    rows = buf.reshape(height, plane.line_size)
    return rows[:, :width].reshape(-1)


def extract_first_part_pixels(path: str) -> bytes:
    container = av.open(path)
    try:
        frame = next(container.decode(video=0))
        frame = frame.reformat(format="yuv444p")
        w, h = frame.width, frame.height
        n = w * h
        y = _plane_to_1d(frame.planes[0], w, h)[:n]
        u = _plane_to_1d(frame.planes[1], w, h)[:n]
        v = _plane_to_1d(frame.planes[2], w, h)[:n]

        bits = np.empty(n * 3, dtype=np.uint8)
        bits[0::3] = v & 1
        bits[1::3] = y & 1
        bits[2::3] = u & 1

        data = pack_bits(bits, bitorder="big")

        if len(data) < 4:
            raise ValueError("LSB stream too short")

        L = int.from_bytes(data[0:4], "big")
        payload = data[4 : 4 + L]

        return payload
    finally:
        container.close()


def _durations_from_packets(path: str) -> List[int]:
    container = av.open(path)
    try:
        vstream = next(s for s in container.streams if s.type == "video")

        durs: List[Optional[int]] = []
        pts_list: List[Optional[int]] = []

        for pkt in container.demux(vstream):
            if pkt.dts is None and pkt.pts is None:
                continue
            pts_list.append(pkt.pts)
            durs.append(pkt.duration)

        if all(d is not None for d in durs) and len(durs) > 0:
            vals = [int(d) for d in durs]  # type: ignore
            if len(set(vals)) >= 2:
                return vals

        pts_clean = [p for p in pts_list if p is not None]
        if len(pts_clean) < 2:
            return []

        inferred = []
        for i in range(len(pts_clean) - 1):
            inferred.append(int(pts_clean[i + 1] - pts_clean[i]))
        inferred.append(inferred[-1])
        return inferred
    finally:
        container.close()


def _iter_boxes(data: bytes, start: int, end: int) -> List[Tuple[int, int, int, bytes]]:
    boxes = []
    off = start
    while off + 8 <= end:
        size = int.from_bytes(data[off : off + 4], "big")
        btype = data[off + 4 : off + 8]
        header = 8
        if size == 1:
            if off + 16 > end:
                break
            size = int.from_bytes(data[off + 8 : off + 16], "big")
            header = 16
        elif size == 0:
            size = end - off
        if size < header or off + size > end:
            break
        boxes.append((off, size, header, btype))
        off += size
    return boxes


def _find_trak_boxes(data: bytes) -> List[Tuple[int, int, int]]:
    traks: List[Tuple[int, int, int]] = []
    top = _iter_boxes(data, 0, len(data))
    for off, size, header, btype in top:
        if btype == b"moov":
            for off2, size2, header2, btype2 in _iter_boxes(data, off + header, off + size):
                if btype2 == b"trak":
                    traks.append((off2, size2, header2))
    return traks


def _get_handler_type(data: bytes, trak_off: int, trak_size: int) -> Optional[bytes]:
    for off, size, header, btype in _iter_boxes(data, trak_off + 8, trak_off + trak_size):
        if btype != b"mdia":
            continue
        for off2, size2, header2, btype2 in _iter_boxes(data, off + header, off + size):
            if btype2 != b"hdlr":
                continue
            base = off2 + header2
            if base + 12 > off2 + size2:
                return None
            return data[base + 8 : base + 12]
    return None


def _get_stts_entries(data: bytes, trak_off: int, trak_size: int) -> Optional[List[int]]:
    for off, size, header, btype in _iter_boxes(data, trak_off + 8, trak_off + trak_size):
        if btype != b"mdia":
            continue
        for off2, size2, header2, btype2 in _iter_boxes(data, off + header, off + size):
            if btype2 != b"minf":
                continue
            for off3, size3, header3, btype3 in _iter_boxes(data, off2 + header2, off2 + size2):
                if btype3 != b"stbl":
                    continue
                for off4, size4, header4, btype4 in _iter_boxes(data, off3 + header3, off3 + size3):
                    if btype4 != b"stts":
                        continue
                    base = off4 + header4
                    if base + 8 > off4 + size4:
                        return None
                    entry_count = int.from_bytes(data[base + 4 : base + 8], "big")
                    pos = base + 8
                    durations: List[int] = []
                    for _ in range(entry_count):
                        if pos + 8 > off4 + size4:
                            break
                        count = int.from_bytes(data[pos : pos + 4], "big")
                        delta = int.from_bytes(data[pos + 4 : pos + 8], "big")
                        if count > 0:
                            durations.extend([delta] * count)
                        pos += 8
                    return durations if durations else None
    return None


def _durations_from_stts(path: str) -> List[int]:
    data = open(path, "rb").read()
    for trak_off, trak_size, _ in _find_trak_boxes(data):
        htype = _get_handler_type(data, trak_off, trak_size)
        if htype != b"vide":
            continue
        durs = _get_stts_entries(data, trak_off, trak_size)
        if durs:
            return durs
    for trak_off, trak_size, _ in _find_trak_boxes(data):
        durs = _get_stts_entries(data, trak_off, trak_size)
        if durs:
            return durs
    raise ValueError("No stts durations found")


def extract_last_part_timing(path: str) -> bytes:
    durs = _durations_from_packets(path)
    if len(set(durs)) < 2:
        durs = _durations_from_stts(path)
    uniq = sorted(set(durs))
    if len(uniq) < 2:
        raise ValueError(f"Need 2 distinct duration values, got {uniq}")

    if len(uniq) > 2:
        uniq = uniq[:2]

    small, large = uniq[0], uniq[1]
    bits = np.array([1 if d == large else 0 for d in durs], dtype=np.uint8)

    data = pack_bits(bits, bitorder="big")
    return data.rstrip(b"\x00")


def combine_flag(first_payload: bytes, last_payload: bytes) -> str:
    fp = first_payload.decode("utf-8", errors="replace")
    lp = last_payload.decode("utf-8", errors="replace")

    if "hkcert25{" in fp:
        fp = fp[fp.index("hkcert25{") :]
    fp = fp.replace("first part:", "")
    if "last part:" in lp:
        lp = lp.split("last part:", 1)[1].lstrip()

    return fp + lp


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("input", help="Path to .AVIF")
    args = ap.parse_args()

    first = extract_first_part_pixels(args.input)
    last = extract_last_part_timing(args.input)

    print("[+] first payload :", first)
    print("[+] last  payload :", last)

    flag = combine_flag(first, last)
    print("\n[+] FLAG =", flag)


if __name__ == "__main__":
    main()

Run result:

1
2
3
4
5
$ python .\solve.py .\download.AVIF
[+] first payload : b'first part:hkcert25{AVIF_Will_Be_The_'
[+] last  payload : b'last part: Future_0f_Im4ge_F0rm4t}'

[+] FLAG = hkcert25{AVIF_Will_Be_The_Future_0f_Im4ge_F0rm4t}
Built with Hugo
Theme Stack designed by Jimmy