From dca425526bf909b2383b7338d4272acc0fb2af1d Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Fri, 25 Aug 2023 02:40:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E8=BF=90=E8=A1=8C=E7=9A=84?= =?UTF-8?q?=E9=A2=98=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crypto/Claude_Shannon/main_local.py | 57 +++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 crypto/Claude_Shannon/main_local.py diff --git a/crypto/Claude_Shannon/main_local.py b/crypto/Claude_Shannon/main_local.py new file mode 100644 index 0000000..8a2dcd9 --- /dev/null +++ b/crypto/Claude_Shannon/main_local.py @@ -0,0 +1,57 @@ +from random import choice as c +from random import randint, shuffle +from Crypto.Util.number import * # type: ignore + +flag = b"flag{this_is_a_test_flag}" + +# 白名单列表,包含允许在问题中使用的关键词和符号 +white_list = ['==', '(', ')', 'C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', '0', '1', 'and', 'or', 'not'] + +# 定义一个函数 calc,用于计算问题的结果 +def calc(ans, chests, expr): + try: + C0, C1, C2, C3, C4, C5, C6 = chests + r = eval(expr) + except Exception as e: + print("Shannon fails to understand your words.\n", e) + exit(0) + return ans(r) + +# 定义一个游戏回合函数 do_round +def do_round(): + # 定义两个函数 truth 和 lie,用于判断 Shannon 的回答是否为真或假 + truth = lambda r: not not r + lie = lambda r: not r + chests = [] + for i in range(7): + # 随机生成 7 个 True 或 False,表示每个箱子是宝藏还是怪物 + chests.append(c((True, False))) + print("Seven chests lie here, with mimics or treasure hidden inside.\nBut don't worry. Trusty Shannon knows what to do.") + # 随机选择 Shannon 的回答策略,包括多少次 lie + lie_count = c((1, 2)) + Shannon = [truth] * (15 - lie_count) + [lie] * lie_count + # 猜猜第几次是在说谎 :-) + shuffle(Shannon) + for i in range(15): + print("Ask Shannon:") + question = input().strip() # 输入问题 + for word in question.split(" "): + if word not in white_list: # 检查问题中是否包含白名单以外的关键词或符号 + print("({}) No treasure for dirty hacker!".format(word)) + exit(0) + res = str(calc(Shannon[i], chests, question)) # 计算问题的结果 + print('Shannon answers: {}!\n'.format(res)) + print("Now open the chests:") + # 输入0或1表示每个宝箱的真假,用空格隔开 + chests_string = input() + return chests == list(map(int, chests_string.strip().split(" "))) + +# 游戏开始 +print("The Unbreakable Shannon has returned, with some suspicious chests and a far more complicated strategy -- he MAY LIE ONCE OR TWICE! Can you still get all the treasure without losing your head?") + +for i in range(1): + if not do_round(): # 执行游戏回合 + print("A chest suddenly comes alive and BITE YOUR HEAD OFF.\n") + exit(0) +print("You've found all the treasure! {}\n".format(flag)) # 赢得游戏,获得flag +