Whispers of the Ancients - Writeup by AI

张开发
2026/4/20 9:13:03 15 分钟阅读

分享文章

Whispers of the Ancients - Writeup by AI
Whispers of the Ancients - Writeup by AI一、题目描述题目来源: Bugku Crypto题目类别: Classical Cryptography - Vigenère Cipher题目提供了一个维吉尼亚密码加密服务包含以下功能加密任意明文选项 1解密功能被禁用选项 2获取加密的 flag选项 3退出选项 4目标破解加密的 flag获取原始明文。二、考点分析考点权重说明维吉尼亚密码原理40%理解多表代换密码的加密机制Oracle 攻击40%利用已知明文攻击推导密钥数学推导能力20%发现 E(‘a’, k) k 的特性网络交互能力附加使用 pwntools 等工具进行远程交互知识点:维吉尼亚密码 (Vigenère Cipher)模运算 (Modular Arithmetic)选择明文攻击 (Chosen Plaintext Attack)Oracle 攻击三、解题思路3.1 加密算法分析defencrypt(plaintext,key):keykey.lower()ciphertextkey_i0foriinrange(len(plaintext)):charplaintext[i]ifchar.isalpha():ifchar.islower():lowTrueelse:lowFalsecharchar.lower()enc_charchr((((ord(char)-97)(ord(key[key_i%len(key)])-97))%26)97)ifnotlow:enc_charenc_char.upper()key_i1else:enc_charchar ciphertextenc_charreturnciphertext这是标准的维吉尼亚密码实现加密公式为C [ i ] ( P [ i ] K [ i m o d l e n ( K ) ] ) m o d 26 C[i] (P[i] K[i \mod len(K)]) \mod 26C[i](P[i]K[imodlen(K)])mod26其中P [ i ] P[i]P[i]是明文字母a0, b1, …, z25K [ i ] K[i]K[i]是密钥字母C [ i ] C[i]C[i]是密文字母3.2 关键发现当明文P [ i ] ′ a ′ P[i] aP[i]′a′时即数值为 0C [ i ] ( 0 K [ i ] ) m o d 26 K [ i ] C[i] (0 K[i]) \mod 26 K[i]C[i](0K[i])mod26K[i]结论: 加密全 ‘a’ 字符串会直接得到密钥本身3.3 攻击策略利用选择明文攻击Oracle 攻击的一种连接远程服务使用选项 1 加密全 ‘a’ 字符串从输出中提取密钥使用选项 3 获取加密的 flag用脚本解密 flag四、详细步骤4.1 连接远程服务使用 pwntools 连接到靶场frompwnimport*ioremote(49.232.142.230,15317)4.2 获取密钥发送命令1选择加密功能然后发送全 ‘a’ 字符串 1 What do you want to encrypt? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa接收输出finditeasyyyyyyyyyfinditeasyyyyyyyyyfinditeasyyyyy分析:输入 50 个 ‘a’输出finditeasyyyyyyyyy重复多次密钥长度: 18密钥:finditeasyyyyyyyyy4.3 获取加密的 Flag发送命令3 3 Did you really tought I would easily give you the flag, but since you asked for it here is the encrypted flag: The encrypted flag is: xprotfetwq{t1e3L3p3_c4$w_ApdBc0}4.4 解密 Flag使用 Python 脚本进行维吉尼亚解密defdecrypt_vigenere(ciphertext,key):keykey.lower()plaintextkey_i0forcharinciphertext:ifchar.isalpha():is_upperchar.isupper()char_lowerchar.lower()# P (C - K) mod 26p_val(ord(char_lower)-ord(key[key_i%len(key)]))%26p_charchr(p_val97)ifis_upper:p_charp_char.upper()plaintextp_char key_i1else:plaintextcharreturnplaintext encrypted_flagxprotfetwq{t1e3L3p3_c4$w_ApdBc0}keyfinditeasyyyyyyyyyflagdecrypt_vigenere(encrypted_flag,key)print(flag)# shellmates{v1g3N3r3_e4$y_CryTp0}4.5 验证重新加密验证fromverify_flagimportencrypt_vigenere decryptedshellmates{v1g3N3r3_e4$y_CryTp0}re_encryptedencrypt_vigenere(decrypted,finditeasyyyyyyyyy)assertre_encryptedxprotfetwq{t1e3L3p3_c4$w_ApdBc0}# ✓ 验证通过五、完整代码#!/usr/bin/env python3frompwnimport*defdecrypt_vigenere(ciphertext,key):维吉尼亚解密keykey.lower()plaintextkey_i0forcharinciphertext:ifchar.isalpha():is_upperchar.isupper()char_lowerchar.lower()p_val(ord(char_lower)-ord(key[key_i%len(key)]))%26p_charchr(p_val97)ifis_upper:p_charp_char.upper()plaintextp_char key_i1else:plaintextcharreturnplaintext# 连接远程服务ioremote(49.232.142.230,15317)# 加密全 a 获取密钥io.sendline(b1)io.recvuntil(b )io.sendline(ba*50)resultio.recvuntil(b ).decode()# 提取密钥finditeasyyyyyyyyy# 获取加密 flagio.sendline(b3)flag_outputio.recvuntil(b ).decode()# 提取xprotfetwq{t1e3L3p3_c4$w_ApdBc0}# 解密flagdecrypt_vigenere(xprotfetwq{t1e3L3p3_c4$w_ApdBc0},finditeasyyyyyyyyy)print(flag)# shellmates{v1g3N3r3_e4$y_CryTp0}使用方法python solve_remote.py六、运行结果执行攻击脚本的输出 Whispers of the Ancients - 远程靶场攻击 [步骤 1] 连接到 49.232.142.230:15317... ✓ 连接成功 [步骤 2] 发送全 a 明文以推导密钥... 发送明文aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 接收密文finditeasyyyyyyyyyfinditeasyyyyyyyyyfinditeasyyyyy 提取的加密结果finditeasyyyyyyyyyfinditeasyyyyyyyyyfinditeasyyyyy 分析密钥模式finditeasyyyyyyyyyfinditeasyyyyyyyyyfinditeasyyyyy 找到重复模式长度18 推导的密钥finditeasyyyyyyyyy [步骤 3] 获取加密的 flag... Flag 输出: The encrypted flag is: xprotfetwq{t1e3L3p3_c4$w_ApdBc0} [步骤 4] 使用密钥 finditeasyyyyyyyyy 解密... 解密的 flag: shellmates{v1g3N3r3_e4$y_CryTp0} 最终答案shellmates{v1g3N3r3_e4$y_CryTp0}七、总结与防御技术要点维吉尼亚密码的本质多重凯撒密码的叠加密钥循环使用属于古典密码已被完全破解Oracle 攻击的威力即使不知道密钥也能通过加密服务推导选择明文攻击是最强大的攻击模型之一现代密码学必须抵抗此类攻击数学特性利用E ( ′ a ′ , k ) k E(a, k) kE(′a′,k)k是关键突破口特殊值测试在密码分析中非常有效网络交互技巧使用 pwntools 进行高效的网络通信精确定位提示符和响应内容自动化整个攻击流程防御建议问题建议优先级使用古典密码使用 AES、RSA 等现代算法 高提供任意加密接口实施访问控制和认证 高密钥硬编码使用密钥管理系统 中无盐加密添加随机 salt 或 IV 中最佳实践:✅ 绝不使用古典密码保护敏感数据✅ 避免提供未授权的加密/解密 Oracle✅ 使用经过验证的加密库如 PyCryptodome✅ 实施完整的认证和授权机制✅ 定期更新和轮换密钥

更多文章