{"id":89,"date":"2026-04-08T21:42:02","date_gmt":"2026-04-08T13:42:02","guid":{"rendered":"https:\/\/blog.lmoon.cn\/?p=89"},"modified":"2026-04-08T21:46:14","modified_gmt":"2026-04-08T13:46:14","slug":"aes-cbc%e8%a7%a3%e5%af%86","status":"publish","type":"post","link":"https:\/\/blog.lmoon.cn\/index.php\/2026\/04\/08\/aes-cbc%e8%a7%a3%e5%af%86\/","title":{"rendered":"AES-CBC\u57fa\u7840\u7ec3\u4e60"},"content":{"rendered":"\n<p>CBC\u7684\u8fd0\u7b97\u8fc7\u7a0b\u90fd\u662f\u5728\u5b57\u8282\u7684\u524d\u63d0\u4e0b\u8fd0\u7b97\u7684<\/p>\n\n\n\n<p>CBC\u52a0\u5bc6\u8fc7\u7a0b\u4e3a\u968f\u673a\u9009\u53d6\u4e00\u4e2a16\u4f4d\u7684\u5b57\u8282vi\uff0c\u548c\u4e00\u4e2akey\u503c\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e00\u8f6e<\/p>\n\n\n\n<p>\u5229\u7528\u5f02\u6216\u5c06vi\u548c\u660e\u6587\u7684\u524d16\u4f4d\u5b57\u8282\u8fdb\u884c\u5f02\u6216<\/p>\n\n\n\n<p>\u7136\u540e\u5229\u7528key\u5bf9\u5f02\u6216\u7684\u7ed3\u679c\u8fdb\u884c\u52a0\u5bc6<\/p>\n\n\n\n<p>\u7b2c\u4e8c\u8f6e<\/p>\n\n\n\n<p>\u5c06\u7b2c\u4e00\u8f6e\u7684\u7ed3\u679c\u4e0e\u660e\u658716-32(\u4e5f\u5c31\u662f\u7b2c\u4e8c\u7ec416\u4f4d\u5b57\u8282)\u8fdb\u884c\u5f02\u6216<\/p>\n\n\n\n<p>\u5728\u5229\u7528key\u5bf9\u5f02\u6216\u7684\u7ed3\u679c\u8fdb\u884c\u52a0\u5bc6<\/p>\n\n\n\n<p>\u4f9d\u6b21\u7c7b\u63a8<\/p>\n\n\n\n<p>\u6700\u7ec8\u5c06\u5168\u90e8\u7684\u52a0\u5bc6\u7ed3\u679c\u8fdb\u884c\u62fc\u63a5<\/p>\n\n\n\n<p>\u901a\u8fc7\u4ed6\u7684\u52a0\u5bc6\u8fc7\u7a0b\u51b3\u5b9a\u4e86\uff0c\u4e0d\u8bba\u662f\u660e\u6587\uff0ckey\uff0c\u8fd8\u662fvi\u90fd\u5fc5\u987b\u662f\u6574\u966416.<\/p>\n\n\n\n<p>\u660e\u6587\u4e0d\u8db316\u7684\u500d\u6570\u8981\u8fdb\u884c\u586b\u5145\uff0ckey\u548cvi\u4e0d\u662f16\u7684\u500d\u6570\u76f4\u63a5\u62a5\u9519<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u9898\u76ee<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">import os<br>from Crypto.Util.number import *<br>from Crypto.Cipher import AES<br>from secret import flag, key<br>from Crypto.Util.Padding import pad<br>assert(len(flag) == 38)<br>assert flag[:5] == b'flag{' and flag[-1:] == b'}'<br>assert(len(key) == 16)<br>def padding(msg):<br> &nbsp;  tmp = 16 - len(msg) % 16<br> &nbsp;  pad = format(tmp, '02x')<br> &nbsp;  return bytes.fromhex(pad * tmp) + msg<br>message = padding(flag)<br>hint = bytes_to_long(key) ^ bytes_to_long(message[:16])<br>message = pad(message, 16, 'pkcs7')<br>IV = os.urandom(16)<br>\u200b<br>encryption = AES.new(key, AES.MODE_CBC, iv=IV)<br>enc = encryption.encrypt(message)<br>print('enc =', enc.hex())<br>print('hint =', hex(hint)[2:])<br># enc = 1ce1df3812668ce0bccd86c146cc56989681e128edd0676f5d26e01abdee90c860e22a5a491f94ac5ca3ab02242740fb8c35a3b60ea737ca0d2662fba2b0e299<br># hint = 32393f4e3c3c4f3e323a512a5356437d<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u9898\u76ee\u5206\u6790<\/h3>\n\n\n\n<p>\u5bf9\u4e8e\u9898\u76ee\u53d1\u73b0flag==38\u662f16\u76842\u500d\u4f596\uff0c\u4e5f\u5c31\u662f\u5dee10\u4e2a\u5b57\u8282\u3002\u6240\u4ee5\u5b83\u5229\u7528padding\u51fd\u6570\u5bf9\u660e\u6587\u8fdb\u884c\u4e86\u586b\u5145\uff0c\u4f7f\u5176\u6ee1\u8db316\u7684\u500d\u6570\u3002<\/p>\n\n\n\n<p>hint\u662fkey\u4e0emessages[:16]\u7684\u5f02\u6216\u7ed3\u679c\u3002\u7531padding\u77e5\u9053\u5b83\u8865\u5145\u4e8610\u4e2a\u5b57\u8282\u7684 \\x0a(02x\u768416\u8fdb\u5236\u7684\u5b57\u8282)\u3002\u5176\u6b21\u6211\u4eec\u77e5\u9053\u4e86flag\u524d5\u4e2a\u5b57\u8282\u4e3aflag{\u3002\u4e5f\u5c31\u662f\u8bf4\u73b0\u5728\u53ea\u5269\u7b2c16\u4e2a\u5b57\u8282\u4e0d\u77e5\u9053\u4e86\u3002<\/p>\n\n\n\n<p>\u540e\u9762\u5c31\u662f\u968f\u673a\u83b7\u53d6IV(\u4e0d\u4fe1\u7684\u8bdd\u53ef\u4ee5\u81ea\u5df1\u8fd0\u884c\u4e00\u4e0b)\uff0c\u7136\u540e\u5229\u7528key, AES.MODE_CBC, iv=IV\u521b\u5efa\u4e86\u4e00\u4e2aencryption\u7684AES-CBC\u5de5\u5177\uff0c\u7136\u540e\u8c03\u7528encryption.encrypt\u5bf9messages\u8fdb\u884c\u52a0\u5bc6<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u89e3\u9898\u601d\u8def<\/h3>\n\n\n\n<p>\u5bf9\u4e8eAES-CBC\u52a0\u5bc6\uff0c\u6211\u4eec\u5982\u679c\u77e5\u9053\u4e86\u4ed6\u7684key\uff0cvi\u90a3\u4e48\u8fd9\u4e2a\u9898\u76ee\u662f\u4e0d\u662f\u5c31\u53ef\u4ee5\u76f4\u63a5\u63a5\u51fa\u6765\u4e86\u3002\u5e94\u4e3a\u6211\u4eec\u5982\u679c\u77e5\u9053\u4e86\u8fd9\u4e24\u4e2a\u503c\u5c31\u53ef\u4ee5\u5efa\u9020\u4e00\u4e2a\u4e0e\u52a0\u5bc6\u4e00\u6a21\u4e00\u6837\u7684AES-CBC\u5de5\u5177\uff0cencryption\uff0c\u7136\u540e\u6211\u4eec\u5229\u7528\u8fd9\u4e2a\u51fd\u6570\u76f4\u63a5\u8c03\u7528encryption.decrypt\u8fdb\u884c\u89e3\u5bc6\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\u6709\u4e86\u8fd9\u4e2a\u60f3\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0hint\u662f\u4e0d\u662f\u77e5\u9053\u4e86\uff0c\u660e\u6587\u7684\u524d16\u4e2a\u5b57\u8282\u662f\u4e0d\u662f\u4e5f\u77e5\u9053\u524d15\u4f4d\u4e86\u3002\u90a3\u4e48\u6211\u4eec\u63a5\u4e0b\u6765\u662f\u4e0d\u662f\u5c31\u662f\u53bb\u6c42\u89e3vi\uff0c\u548ckey\u4e86.<\/p>\n\n\n\n<p>\u5bf9\u4e8ekey\u7684\u6c42\u6cd5\u5c06hint\u4e0emessages[:16]\u8fdb\u884c\u5f02\u6216\uff0c\u8fd9\u4e2a\u65f6\u5019\u4f1a\u53d1\u73b0mssages\u6700\u540e\u4e00\u4e2a\u4e0d\u77e5\u9053\uff0c\u6211\u4eec\u53ef\u4ee5\u968f\u4fbf\u7ed9\u4ed6\u4e00\u4e2a\u5b571\uff0c2\uff0c3\u90fd\u53ef\u4ee5\u3002\u8fd9\u4e2a\u9898\u76ee\u7684\u653b\u51fb\u70b9\u5c31\u662f\u7206\u7834\u8fd9\u4e2a\u5b57\u8282&#8212;&#8212;&#8211;\u6ce8\u610f\u8981\u662f\u5b57\u8282\u5f62\u5f0f\u7684\u3002\u5176\u6b21\u8981\u5bf9enc\u548chint\u7684\u683c\u5f0f\u8fdb\u884c\u8f6c\u5316\u3002&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;\u5f02\u6216\u7684\u683c\u5f0f\u8981\u6c42\u5e94\u8be5\u77e5\u9053\u5427\u3002<\/p>\n\n\n\n<p>\u5728\u5f97\u5230key\u7684\u524d\u63d0\u4e0b\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u5c31\u8981\u53bb\u5f97\u5230vi\u3002\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u8981\u77e5\u9053<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u4e00\u628a\u9501\u6765\u8bf4\uff0c\u4ed6\u7684\u94a5\u5319\u662f\u4e0d\u662f\u53ea\u6709\u4e00\u628a\uff0c<\/p>\n\n\n\n<p>\u90a3\u4e48\u5bf9\u4e8e\u52a0\u5bc6\u51fd\u6570\u6765\u8bf4\u5b83\u7684\u52a0\u5bc6\u548c\u89e3\u5bc6\u7684vi\u662f\u4e0d\u662f\u5bf9\u5e94\u7684\u4e5f\u662f\u552f\u4e00\u7684\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\u6784\u9020\u4e00\u4e2aAES-CBC\u5de5\u5177\u6765\u8fdb\u884c\u53cd\u63a8vi\u3002\u5f88\u5408\u7406\u5427\uff0c\u6211\u6709\u9501\u4e86\u90a3\u4e48\u6211\u5c31\u53ef\u4ee5\u53bb\u627e\u94a5\u5319\u4e86\uff0c<\/p>\n\n\n\n<p>\u4f46\u662f\u6ce8\u610fvi\u7684\u4f5c\u7528\u8303\u56f4\u53ea\u9650\u5236\u4e8e\u7b2c\u4e00\u5757\u52a0\u5bc6<\/p>\n\n\n\n<p>\u56e0\u6b64\u6211\u4eec\u8981\u52a0\u5bc6\u7684\u5fc5\u987b\u662f\u53ea\u670916\u5b57\u8282\uff0c\u5e76\u4e14\u5df2\u77e5\u5bc6\u6587\u7684\u3002&#8212;&#8212;enc\u7684\u524d16\u5b57\u8282<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">encryption = AES.new(key, AES.MODE_CBC, iv=bytes(16))<br> &nbsp; &nbsp; &nbsp;  di = encryption.decrypt(enc_list[0])<br> &nbsp; &nbsp; &nbsp;  vi = bytes([pad[i] ^ di[i] for i in range(16)])<\/pre>\n\n\n\n<p>\u5f97\u5230vi\u540e\u6211\u4eec\u76f4\u63a5\u6784\u9020\u65b0\u7684AES-CBC\u5de5\u5177\u53bb\u76f4\u63a5\u89e3\u5bc6\u5b8c\u6574\u7684enc.\u7136\u540e\u5f97\u5230flag\u3002<\/p>\n\n\n\n<p>\u4f60\u80af\u4f1a\u5bf9\u8fd9\u4e24\u4e2a\u6784\u9020\u4e0d\u662f\u5f88\u7406\u89e3<\/p>\n\n\n\n<p>\u4f60\u8981\u8bb0\u4f4f\u4f60\u8981\u5148\u627e\u5230\u94a5\u5319\uff0c\u5728\u53bb\u5f00\u9501&#8212;-\u94a5\u5319\u90fd\u6ca1\u6709\u662f\u5f00\u4e0d\u4e86\u9501\u7684<\/p>\n\n\n\n<p>\u5f53\u7136\u4ee5\u4e0a\u7684\u57fa\u7840\u662f\u5728\u7b2c16\u4f4d\u660e\u6587\u6709\u5b57\u8282\u7684\u60c5\u51b5\u4e0b\uff0c\u63a5\u4e0b\u6765\u4f60\u8981\u505a\u7684\u5c31\u662f\u53bb\u7206\u7834\u90a3\u4e00\u4e2a\u5b57\u8282 \u4e00\u4e2a\u5b57\u8282\u53608\u4e2abit\u4f4d\u4e5f\u5c31\u662f 256<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">plaintext = unpad(plaintext_padded, 16)[10:]<\/pre>\n\n\n\n<p>\u5bf9\u4e8e\u8fd9\u4e2a\u4ee3\u7801\u4ed6\u7684\u4f5c\u7528\u662f\u76f4\u63a5\u5229\u7528unpad\u53bb\u67e5\u770b\u4f60\u7684\u540e10\u4f4d\u662f\u4e0d\u662f\u53ef\u8bfb\uff08\u5e94\u4e3a\u9898\u76ee\u7684\u586b\u5145\u662f\u586b\u5728\u524d\u9762\u7684\u4f60\u53ef\u4ee5\u8fd0\u884c\u9a8c\u8bc1\u4e00\u4e0b\uff09\u3002\u4e0d\u662f\u7684\u8bdd\u4ed6\u5c31\u4f1a\u62a5\u9519\u7136\u540etry\u7ed3\u675f\u6267\u884cexcept\uff0cexcept\u6267\u884ccontinue\u8df3\u8fc7\u672c\u6b21\u5faa\u73af\uff0c\u6267\u884c\u4e0b\u4e00\u6b21\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u200b<br>from Crypto.Util.Padding import unpad<br>from Crypto.Util.number import *<br>from Crypto.Cipher import AES<br>hint='32393f4e3c3c4f3e323a512a5356437d'<br>hint=int(hint,16)<br>enc='1ce1df3812668ce0bccd86c146cc56989681e128edd0676f5d26e01abdee90c860e22a5a491f94ac5ca3ab02242740fb8c35a3b60ea737ca0d2662fba2b0e299'<br>enc=bytes.fromhex(enc)<br>enc_list = [enc[i:i + 16] for i in range(0, len(enc), 16)]<br>for a in range(256):<br> &nbsp;  #pad = b'\\x0a' * 10 + b'flag{' + bytes(a)<br> &nbsp;  pad = b'\\x0a' * 10 + b'flag{' + bytes([a])<br> &nbsp;  key = hint ^ bytes_to_long(pad)<br> &nbsp;  key = long_to_bytes(key, 16)<br> &nbsp;  try:<br> &nbsp; &nbsp; &nbsp;  encryption = AES.new(key, AES.MODE_CBC, iv=bytes(16))<br> &nbsp; &nbsp; &nbsp;  di = encryption.decrypt(enc_list[0])<br> &nbsp; &nbsp; &nbsp;  vi = bytes([pad[i] ^ di[i] for i in range(16)])<br> &nbsp; &nbsp; &nbsp;  # \u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u66f4\u7b80\u6d01<br> &nbsp; &nbsp; &nbsp;  aes_cbc_real = AES.new(key, AES.MODE_CBC, iv=vi)<br> &nbsp; &nbsp; &nbsp;  plaintext_padded = aes_cbc_real.decrypt(enc)<br> &nbsp; &nbsp; &nbsp;  plaintext = unpad(plaintext_padded, 16)[10:]<br> &nbsp; &nbsp; &nbsp;  if plaintext[:5] == b'flag{' and plaintext[-1:] == b'}':<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  print(plaintext)<br> &nbsp;  except Exception as e:<br> &nbsp; &nbsp; &nbsp;  continue<br>\u200b<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5efa\u8bae<\/h3>\n\n\n\n<p>\u6b64\u5916\u5bf9\u4e8e\u4e00\u4e9bfor\u5faa\u73af\u7684\u7f16\u5199\u5982\u679c\u4f60\u662f\u5199\u4e00\u884c\u76f4\u63a5\u5199\u5b8c\u90a3\u6ca1\u6709\u95ee\u9898\u5e94\u4e3a\u5b83\u7684\u57fa\u7840\u5c31\u662f\u6570\u7ec4\uff0c\u4e0d\u7528\u5728append\u4f46\u662f\u5982\u679c\u4f60\u662f\u7ed9\u4ed6\u5c55\u5f00\u5199\u7684\u4f60\u8981\u7279\u522b\u6ce8\u610f\u5b83\u7684append\u662f\u4e0d\u662f\u771f\u7684\u662f\u4e00\u4e2a\u4e00\u4e2a\u52a0\u5165\u7684\u3002\u5176\u6b21\u5c31\u8981\u662f\u8981\u6ce8\u610f\u5b83\u6240\u9700\u8981\u7684\u7c7b\u578b\u662f\u4ec0\u4e48\u6837\u7684\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u4ee3\u7801\u5148\u5bf9\u7740\u8fd9\u4e2a\u601d\u8def\u81ea\u5df1\u6572\u4e00\u904d\uff0c\u4e0d\u4f1a\u7684\u4e0d\u8981\u76f4\u63a5\u95eeai\u8981\u811a\u672c\uff0c\u6211\u5df2\u7ecf\u628a\u77e5\u8bc6\u70b9\u62c6\u89e3\u7684\u8fd8\u7b97\u6bd4\u8f83\u7ec6\u4e86\uff0c\u76f4\u63a5\u95ee\u5b83\u8fd9\u4e2a\u77e5\u8bc6\u70b9\u5e94\u8be5\u5982\u4f55\u7f16\u5199\uff0c\u5f53\u7136\u5f53\u4f60\u5757\u6ca1\u6709\u8010\u5fc3\u7684\u65f6\u5019\u53ef\u4ee5\u53ebai\u7ed9\u4f60\u4ee3\u7801\u505a\u4e00\u4e2a\u5168\u65b9\u9762\u68c0\u67e5\u3002\u8fd8\u662f\u4e00\u6837\u81ea\u5df1\u6572\u4e00\u904d\u7387\u4e00\u904d\u611f\u89c9\u8fd9\u4e2a\u6ca1\u5fc5\u8981\u5728\u590d\u5236<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CBC\u7684\u8fd0\u7b97\u8fc7\u7a0b\u90fd\u662f\u5728\u5b57\u8282\u7684\u524d\u63d0\u4e0b\u8fd0\u7b97\u7684 CBC\u52a0\u5bc6\u8fc7\u7a0b\u4e3a\u968f\u673a\u9009\u53d6\u4e00\u4e2a16\u4f4d\u7684\u5b57\u8282vi\uff0c\u548c\u4e00\u4e2akey\u503c\u3002 \u7b2c\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":92,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-89","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-learn"],"_links":{"self":[{"href":"https:\/\/blog.lmoon.cn\/index.php\/wp-json\/wp\/v2\/posts\/89","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.lmoon.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.lmoon.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.lmoon.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.lmoon.cn\/index.php\/wp-json\/wp\/v2\/comments?post=89"}],"version-history":[{"count":2,"href":"https:\/\/blog.lmoon.cn\/index.php\/wp-json\/wp\/v2\/posts\/89\/revisions"}],"predecessor-version":[{"id":91,"href":"https:\/\/blog.lmoon.cn\/index.php\/wp-json\/wp\/v2\/posts\/89\/revisions\/91"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.lmoon.cn\/index.php\/wp-json\/wp\/v2\/media\/92"}],"wp:attachment":[{"href":"https:\/\/blog.lmoon.cn\/index.php\/wp-json\/wp\/v2\/media?parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.lmoon.cn\/index.php\/wp-json\/wp\/v2\/categories?post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.lmoon.cn\/index.php\/wp-json\/wp\/v2\/tags?post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}