【CTF-CRYPTO-教学-XOR】第二节:只有密文没有任何线索,暴力破解

题目

https://ctf2.dasctf.com/dashboard/practice/b9bbb32f-f186-458f-b90b-12440c0f6aea?tab=challenges&challenge=cf34eee0-4739-410f-b554-5b91f920b157

bash 复制代码
from secret import *

ciphertext = []

for f in flag:
    ciphertext.append(f ^ key)

print(bytes(ciphertext).hex())
# e9e3eee8f4f7bffdd0bebad0fcf6e2e2bcfbfdf6d0eee1ebd0eabbf5f6aeaeaeaeaeaef2

解题思路

题目分析

核心:每个 flag 的字节,和同一个固定 key(单字节!)异或 ,也就是:

c i = f i ⊕ k c_i = f_i \oplus k ci=fi⊕k

变形:

f i = c i ⊕ k f_i = c_i \oplus k fi=ci⊕k

k k k 是一个 0~255 的字节(0x00 ~ 0xff)。

这是【单字节全局异或】,属于非常经典的简单 Crypto。

解题思路

密文 hex:

e9e3eee8f4f7bffdd0bebad0fcf6e2e2bcfbfdf6d0eee1ebd0eabbf5f6aeaeaeaeaeaef2

异或性质:

  • 如果 c = f ^ k,那么 k = c ^ f
  • flag 一般有固定开头,CTF 里最常见:flag{
    我们拿密文前 5 个字节和 "flag{" 试算 key。

"flag{" 的 ASCII:

  • f → 0x66
  • l → 0x6c
  • a → 0x61
  • g → 0x67
  • { → 0x7b

密文前 5 字节:

  • 0xe9
  • 0xe3
  • 0xee
  • 0xe8
  • 0xf4

计算第一个候选 key: k = 0 x e 9 ⊕ 0 x 66 k = 0xe9 \oplus 0x66 k=0xe9⊕0x66

直接写 Python 一次性爆破,下面是完整解题脚本:

python 复制代码
cipher_hex = "e9e3eee8f4f7bffdd0bebad0fcf6e2e2bcfbfdf6d0eee1ebd0eabbf5f6aeaeaeaeaeaef2"
cipher = bytes.fromhex(cipher_hex)

# key 是单字节:0 ~ 255
for k in range(256):
    plain = bytes([b ^ k for b in cipher])
    # 判断是否是可见字符串,包含 flag{
    try:
        s = plain.decode("ascii")
        if "flag{" in s:
            print(f"found key = {k:#04x} ({k})")
            print("flag:", s)
    except:
        continue

运行结果:

复制代码
found key = 0x8f (143)
flag: flag{x0r_15_symm3try_and_e4zy!!!!!!}

✅ Key:0x8f(十进制 143)

✅ Flag:flag{x0r_15_symm3try_and_e4zy!!!!!!}


CyberChef快速出答案

作业:GKCTF 2021XOR

https://ctf2.dasctf.com/dashboard/practice/b9bbb32f-f186-458f-b90b-12440c0f6aea?tab=challenges&challenge=1c44a440-72cb-4091-b59f-d73d67ba1215

xor.py

python 复制代码
from os import urandom
from secret import flag
key = urandom(1)

def xor(plaintext, key):
    ret = []
    for i in range(len(plaintext)):
        ret.append(plaintext[i] ^ key[0])
    return bytes(ret)

ciphertext = xor(flag, key)

print(ciphertext.hex())

output.txt

bash 复制代码
9b919c9a8685cd8fa294c8a28c88cc89cea2ce9c878480

解题思路

答案:flag{x0r_i5_qu1t3_3azy}

相关推荐
kingx32 天前
Bugku 真题 Vigenère XOR(UMDCTF 2022)WriteUp:汉明距离 + 逐列频率破重复密钥 XOR
ctf·bugku·汉明距离·维吉尼亚
lone wolf0072 天前
bugku web题文件包含2
ctf
hengdonghui5 天前
Writeup 4 Bugku - USB 流量截取
wireshark·ctf·流量分析·misc(杂项)
hengdonghui5 天前
Writeup 4 GFCTF 2021 Baby_Web
web·ctf·变量覆盖·路径穿越·php 代码审计
kingx35 天前
Bugku 逆向 MatrixHunter:黑盒 oracle + GF(2) 消元出唯一解
网络安全·矩阵·逆向·ctf
kingx36 天前
Bugku 3122 谷歌小恐龙题解:纯离线逆向 Vault 的 AES-GCM 加密资源(附 Python 解密器)
java·逆向·ctf·bugku·aes-gcm
kingx37 天前
Bugku 326 Baby-heap1 题解:glibc 2.23 堆溢出到 FSOP 劫持 _IO_list_all(附完整 exp)
pwn·ctf·glibc·堆溢出·fsop
hengdonghui7 天前
Writeup 4 0xGame 2021 · command (WEB)
web·ctf·rce(远程代码执行)
0xBADCODE8 天前
2026 强网杯 Web 题 cwp · 全链路复盘
前端·安全·web安全·网络安全·系统安全·ctf