BUUCTF 每日打卡 2021-7-22
引言
病了,还要练车,鸽了一天,抱歉
[NCTF2019]Sore
加密代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 from string import ascii_lettersfrom flag import flagctoi = lambda x: ascii_letters.index(x) itoc = lambda x: ascii_letters[x] key = flag.strip('NCTF{}' ) len_key = len (key) plaintext = open ('plaintext.txt' , 'r' ).read() plain = '' .join(p for p in plaintext if p in ascii_letters) cipher = '' .join(itoc((ctoi(p) + ctoi(key[i % len_key])) % 52 ) for i, p in enumerate (plain)) open ('ciphertext.txt' , 'w' ).write(cipher)
通过key在ascii_letters("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")中的位置,以key的长度循环加密 可以看出来,有点像维吉尼亚密码(然而我没看出来( ),但是正统的维吉尼亚密码是不区分大小写的 从出题者的角度,看官方wp 啊这 就找了个网站 爆破
最后要区分大小写,把x改成X就是flag(。。。草) 至于为什么只把'x'改成大写,当然可以以ascii_letters为字母表另外写一个爆破程序,但是大可不必 或者通过解密,使得明文是一段有意义的文本 但是,其实可以耍小聪明: 设x为原文中的某个字符,y为key中对应于x的字符,z为cipher中对应于x的字符 若y为大写字母,则26<=ctoi(y)<=51,那么ctoi(x)+ctoi(y)大概率(至少大于50%的概率)会大于52,则ctoi(z)会小于ctoi(y),故ctoi(z)-ctoi(y)小于0的次数较多的y极有可能是key中的大写字母 代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 from string import ascii_lettersctoi = lambda x: ascii_letters.index(x) cipher = 'nsfAIHFrMuLynuCApeEstxJOzniQuyBVfAChDEznppfAiEIDcyNFBsCjsLvGlDtqztuaHvHbCmuyGNsIMhGlDtbotCoDzDjhyBzHGfHGfoHsuhlssIMvwlixBHHGfDRjoCKrapNIwqNyuxIBACQhtMwCmMCfEBpsrzEuiLGBoMipTkxrznoHfAkqwzvxuzCzDbLyApCGvjpqxkuwpgsLrqsVfCRwzlFmtlyrhuyEiivruFRpCR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jCcxMthlNBPsQitPgSwDFXEhwyqdHfrNBPsQbCBukEvxtytCtxDDciHpBoMeHFgGpFCXyivoJJyulypuFQpJQgvdzntlqzetvwmeLBOBCjIgoolFBepBplAzoprwruzKuwCykJsAlFssiJosfrMuyGzusMyxzFCetxqiwwCpAHoyoSvEJqyvAwdzqshEMDfXBrHHGfrytBzMBbwxIaHOpeeHqcKzurFgnswAdzfGoKIobrxnLCTosrjoCwFbCjDnBTlcsOGzwUfDPusIGCepwzitNzoxBrLwCfpLfDswBlylIhuxlsvIHMsKxpQrvlQrkrBpsiHzliarNGHonMwBPQnpTyLaIKwbCCAAwSwtPAtlRIvlssfKIyzEFyNvDlxBuupHCqCDxnwOzhvuozCQuwCiywvAfylpntNzxeMBFroiDCdolFmFHfHsEMEpjusLoHeHFgnqsuizkutxzrphxnGvNsHCdEEamfosIsqTloCNuCBFpGBqkyQCetsvTxzEimHtQwSizGfCtKrcEmtyMvyuxItLoAuwCiywvAfylNoKClwiNBFsSuwConzACXyiCoJNlzeHLNvHovghDswHclqAovAEiiSsuzKpuDdEEACpmfsivTzvwuLuBXuwGpqEGCeprlhuIEiiLvxsVbHMxoCKqbrtIovAsfvBBLGDbCBekxwxspwIoCzjpyLvxsNorCvyzLqfDyxmuNsfwuvxbNJAJlEDLFXEhwyqdEimHxczKkJQvGlLleTxpitdrbzyuyFRpBCoyCwxcsjGdEPriLyEyUDuHMooxGAbEnrkuODTlyGICJuwCfyFyqlqkBeYHypxGnxoSzDScxJExopxweIvteMEAIKgvGPAEALqbEmxnwNrprHnMHzsIyiktFcepsplBJqbwOqxsMtwCikGwvblpxsIosfrMuyvDmsFgBsswazzIaDyDbmxVNGQbxLkxraCpDyprJDyhxIEuwMJzLqGeznkHmptICpemxvNrznCLgkCCriwjsmuNsfvynwvDexLoInGjqAtrkuOlohNBIyNvIKABpvqryyxnwClueHqJISjIMpwJznXoIDnJTzvAuANwSJHyknjGDZlsLeqMtueQuCzDPzyARFJAvFuIhEPrirIJsCTstE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mIPAqsMrxHNhDRqxpPCclqAavOpsMArNCTuDDjoCwkrENsoDOqfiFyCyDjIgtolDuvotCtjCluwNHztCptQpDtFCbCjHtCzEpsGHwvQjvFvxzOJizyDfFzzqpyrMDDdxynvJLqfDtCeFNJdlInHoKzHRiEJLqbJmpvuCpsiEryDRbHIkxrEnfqNBgEDyheJCFMLzHCnpHznkTlDbqxvusMpBCNmCCzDdwyqprqeHdEtwOpBoRuJNknBMnpEnDnyIxzsJvHwNoXKgkyzxtotNoKFypAQuuHXpJPgqzAwdEtsoJDwmCIHxCHuiFgkyKFbCnHyEPoprNVNvHozgcwmMCezBsoYFypAcFQszsxRukDLDmtiFuuNEjsHQVwRoIyullvjpEmtruNEpjNuyAAvIFgupwyplxziDBxfeFBNCErJCuDtGwpEtDHuyCpzyBPsQmpQvclLDoofNwyOsulCFYBFmxQjlltnqsfIsyIEimMAyKOjrRwBpznpHwxtyIrTlyJuGOstRvIlxobnytdrPEwiLLACNeAMqutFpxyDLaOJyfxCzyKGfCQjoHwwqEtIhuGlemyFLCNnLyADswqbwqsoMItoxBrIHGfGUkxrVKXDptdCzHieNVNvNvvFvkmGDqlqAtxDDtxOszWIvHRhsyABepiIeBGtokSBOoApJRKntvwqvsDwMCluxBrBsKmIMukJaovzzLaDOEpoHBQHGfIPwDsamlyyznERHieNVNvHozydyFLrqTrHoHMJJxIyxGNnpLAzpGyipfqoKOtuEvBOHzmAgmxzOrpTxDrJJqnmMFyJDsNzqnJaClwipbEPEFzyAIzCTIPcnwsCbCfCdQxvmiSsIFHoHRcxnwRqsnCkYzGfrGvMGSipRiyovjjXfJryxpJxMsOBMzSMpDpNnoEjAlqIJcsxLuBXuwGpqTxHlFiDyEPDueLGGwRtxLioGwAvmtsy' key = 'vlbeunuozbpycklsjxlfpaq' c = {} count = {} for i in range (len (key)): c[key[i]] = 0 count[key[i]] = 0 for i in range (len (cipher)): c[key[i % len (key)]] += 1 if (ctoi(cipher[i]) - ctoi(key[i % len (key)])) < 0 : count[key[i % len (key)]] += 1 print (c)print (count)
结果为: 容易看出只有'x'符合条件 从解题人的角度老老实实地解的话,可以参考另一位师傅的wp ,这里不再赘述 但仅仅解出这道题是不够的 如这位师傅在wp中分析的,维吉尼亚密码爆破需要经历三个过程 1、Kasiski 实验,通过查找相同的子串(3个字符以上)来猜测可能的key的长度 2、重合指数攻击,通过计算重合指数,进一步确定key的长度 3、字母频率分析,根据英文文档中各个字母的出现频率来推算key的内容
这里提供一个用python写的爆破脚本示例 ,具体原理也可参照wiki ,这里不再赘述 关键是,如本题所示,如果加密用的字母表(本题为ascii_letters即包含大小写共52个字母),不是经典维吉尼亚密码的26个顺序字母,是否能用类似方法破解? 我们假设加密的内容都是有意义的英文文本,这里讨论三种情况:
1、乱序小写字母表
Kasiski实验至于密文有关,通过查找相同的子串(3个字符以上)来猜测可能的key的长度,因此没有影响 由于字母还是那26个字母,重合指数与各个字母的出现频率都是不变的,所以重合指数攻击和字母频率分析依然可以进行 所以上述爆破方法仍适用
2、缺失的字母表
同样,Kasiski实验至于密文有关,没有影响 但是,由于字母的缺失,重合指数与各个字母的出现频率与正常的英文文本不相同,所以无法进行 因此上述爆破方法并不适用 key做到避开某些字母是容易的,但是一段有意义的英文文本完全避开一些字面是困难的 但如果仅仅避开一些出现频率不高的字母(如'x','z',频率只有0.001),上述爆破方法还是适用的
3、混入大写字母的小写字母表(或是混入小写字母的大写字母表)
这又会有两种情况 一种是原文的这些混入字母也是大写的(这样做可能是为了和key契合,或者混淆视听),那么只要把全部字母换成小写爆破就行 另一种就是原文特意避开这些字母,和第2种情况不同的是,key中可能含有这些大写字母,那就只要把key和字母表里的大写字母都换成小写,如果仅仅原文避开了一些出现频率不高的字母,仍然可以爆破
结语
原本还想写下一题[SUCTF2019]MT的,但是MT算法似乎比较麻烦,就单独分一期吧 希望继续坚持