[RE] S0urc3 - POC CTF 2025

2025. 10. 13. 02:10·

Đề bài


Tóm tắt

Mục tiêu: Giải mã chuỗi encoded_flag trong mã Zig và thu được flag


Mô tả bài

Bài cho file S0urc3.zig (Zig). Chương trình có mảng byte encoded_flag và hàm decodeFlag thao tác từng byte theo chỉ số (i % 4). Hàm main chỉ cấp phát và giải mã nhưng không in giá trị ra màn hình


Nội dung tệp chính

const std = @import("std");

const encoded_flag = [_]u8{ 71,105,102,104,88,120,107,108,49,54,103,55,52,45,104,110,51,98,60,58,52,98,103,106,55,97,105,110,50,96,55,59,99,49,60,61,56,54,130 };

fn decodeFlag(allocator: std.mem.Allocator) ![]u8 {
    var decoded = try allocator.alloc(u8, encoded_flag.len);
    for (encoded_flag, 0..) |byte, i| {
        switch (i % 4) {
            0 => decoded[i] = byte ^ 0x01,
            1 => decoded[i] = byte + 3,
            2 => decoded[i] = byte - 5,
            3 => decoded[i] = byte ^ 0x0F,
            else => unreachable,
        }
    }
    return decoded;
}

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();
    const flag = try decodeFlag(allocator);
    defer allocator.free(flag);
}

Nhận xét nhanh:

  • encoded_flag.len = 39. Nếu flag dạng FlagY{<32-hex>}, độ dài 6 + 32 + 1 = 39 → hợp lý
  • Mỗi byte được biến đổi theo chu kỳ 4 phần tử bằng các phép XOR, +, −

Phân tích xử lý theo chỉ số

Với phần tử có chỉ số i:

  • i % 4 == 0 → decoded[i] = byte ^ 0x01
  • i % 4 == 1 → decoded[i] = byte + 3
  • i % 4 == 2 → decoded[i] = byte - 5
  • i % 4 == 3 → decoded[i] = byte ^ 0x0F

Kiểm tra vài phần tử đầu (đối chiếu chữ cái “FlagY

  • i=0: 71 ^ 1 = 70 → 'F'
  • i=1: 105 + 3 = 108 → 'l'
  • i=2: 102 − 5 = 97 → 'a'
  • i=3: 104 ^ 0x0F = 103 → 'g'
  • i=4: 88 ^ 1 = 89 → 'Y'
  • i=5: 120 + 3 = 123 → '{'

Dấu hiệu đúng: bắt đầu bằng FlagY{


Script

encoded_flag = [71,105,102,104,88,120,107,108,49,54,103,55,52,45,104,110,51,98,60,58,52,98,103,106,55,97,105,110,50,96,55,59,99,49,60,61,56,54,130]
decoded = []
for i, byte in enumerate(encoded_flag):
    r = i % 4
    if r == 0:
        decoded.append(byte ^ 0x01)
    elif r == 1:
        decoded.append((byte + 3) & 0xFF)
    elif r == 2:
        decoded.append((byte - 5) & 0xFF)
    elif r == 3:
        decoded.append(byte ^ 0x0F)

print(bytes(decoded).decode())

Zig

Nếu muốn “giải đúng chuẩn Zig”, ta có thể in flag:

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    const flag = try decodeFlag(allocator);
    defer allocator.free(flag);

    // In ra
    const stdout = std.io.getStdOut().writer();
    try stdout.print("{s}\n", .{flag});
}

Biên dịch:

zig build-exe S0urc3.zig
./S0urc3

Flag

Flag: FlagY{fc09b850ca2e755ebe6dda3c24b47299}

'WriteUp > RE' 카테고리의 다른 글

Square Cipher - BuckeyeCTF 2025  (0) 2025.11.09
Python 0bf  (0) 2025.11.02
[RE] Clockwork - EnigmaXplore 3.0  (0) 2025.10.20
[RE] Dsp - POC CTF 2025  (0) 2025.10.13
[RE] Mystery Zone  (0) 2025.09.01
'WriteUp/RE' Other posts in category
  • Python 0bf
  • [RE] Clockwork - EnigmaXplore 3.0
  • [RE] Dsp - POC CTF 2025
  • [RE] Mystery Zone
longhd
longhd
Longhd's Blog
  • longhd
    Ha Duy Long - InfosecPTIT
    longhd
  • Total
    Today
    Yesterday
  • About me

    • Hello I'm Duy Long 👋🏻
    • View all categories (117) N
      • Certificates (4)
      • CTF (3)
      • WriteUp (94) N
        • Forensics (44) N
        • Steganography (5)
        • RE (9) N
        • OSINT (8)
        • Web (17)
        • MISC (6)
        • Crypto (3)
        • Pwn (2)
      • Love Story (0)
      • Labs (15)
        • Information Gathering (10)
        • Vulnerability Scanning (2)
        • Introduction to Web Applica.. (1)
        • Common Web Application Atta.. (1)
        • SQL Injection Attacks (1)
  • Blog Menu

    • Home
    • Tag
    • GuestBook
  • Popular Posts

  • Tags

    Re
    V1tCTF2025
    Forensics
    Dreamhack
    SunshineCTF2025
    CTF
    htb
    writeup
    BuckeyeCTF2025
    misc
    EnigmaXplore3.0
    picoCTF
    POCCTF2025
    Steganography
    THM
    Web
    CSCV2025
    CHH
    PTITCTF2025
    OSINT
  • Recent Comments

  • Recent Posts

  • hELLO· Designed ByLong.v4.10.4
longhd
[RE] S0urc3 - POC CTF 2025
Go to Top

티스토리툴바