香农的题目修改分类为misc
This commit is contained in:
@@ -1,57 +1,57 @@
|
|||||||
from random import choice as c
|
from random import choice as c
|
||||||
from random import randint, shuffle
|
from random import randint, shuffle
|
||||||
from Crypto.Util.number import * # type: ignore
|
from Crypto.Util.number import * # type: ignore
|
||||||
|
|
||||||
flag = b"flag{this_is_a_test_flag}"
|
flag = b"flag{this_is_a_test_flag}"
|
||||||
|
|
||||||
# 白名单列表,包含允许在问题中使用的关键词和符号
|
# 白名单列表,包含允许在问题中使用的关键词和符号
|
||||||
white_list = ['==', '(', ')', 'C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', '0', '1', 'and', 'or', 'not']
|
white_list = ['==', '(', ')', 'C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', '0', '1', 'and', 'or', 'not']
|
||||||
|
|
||||||
# 定义一个函数 calc,用于计算问题的结果
|
# 定义一个函数 calc,用于计算问题的结果
|
||||||
def calc(ans, chests, expr):
|
def calc(ans, chests, expr):
|
||||||
try:
|
try:
|
||||||
C0, C1, C2, C3, C4, C5, C6 = chests
|
C0, C1, C2, C3, C4, C5, C6 = chests
|
||||||
r = eval(expr)
|
r = eval(expr)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Shannon fails to understand your words.\n", e)
|
print("Shannon fails to understand your words.\n", e)
|
||||||
exit(0)
|
exit(0)
|
||||||
return ans(r)
|
return ans(r)
|
||||||
|
|
||||||
# 定义一个游戏回合函数 do_round
|
# 定义一个游戏回合函数 do_round
|
||||||
def do_round():
|
def do_round():
|
||||||
# 定义两个函数 truth 和 lie,用于判断 Shannon 的回答是否为真或假
|
# 定义两个函数 truth 和 lie,用于判断 Shannon 的回答是否为真或假
|
||||||
truth = lambda r: not not r
|
truth = lambda r: not not r
|
||||||
lie = lambda r: not r
|
lie = lambda r: not r
|
||||||
chests = []
|
chests = []
|
||||||
for i in range(7):
|
for i in range(7):
|
||||||
# 随机生成 7 个 True 或 False,表示每个箱子是宝藏还是怪物
|
# 随机生成 7 个 True 或 False,表示每个箱子是宝藏还是怪物
|
||||||
chests.append(c((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.")
|
print("Seven chests lie here, with mimics or treasure hidden inside.\nBut don't worry. Trusty Shannon knows what to do.")
|
||||||
# 随机选择 Shannon 的回答策略,包括多少次 lie
|
# 随机选择 Shannon 的回答策略,包括多少次 lie
|
||||||
lie_count = c((1, 2))
|
lie_count = c((1, 2))
|
||||||
Shannon = [truth] * (15 - lie_count) + [lie] * lie_count
|
Shannon = [truth] * (15 - lie_count) + [lie] * lie_count
|
||||||
# 猜猜第几次是在说谎 :-)
|
# 猜猜第几次是在说谎 :-)
|
||||||
shuffle(Shannon)
|
shuffle(Shannon)
|
||||||
for i in range(15):
|
for i in range(15):
|
||||||
print("Ask Shannon:")
|
print("Ask Shannon:")
|
||||||
question = input().strip() # 输入问题
|
question = input().strip() # 输入问题
|
||||||
for word in question.split(" "):
|
for word in question.split(" "):
|
||||||
if word not in white_list: # 检查问题中是否包含白名单以外的关键词或符号
|
if word not in white_list: # 检查问题中是否包含白名单以外的关键词或符号
|
||||||
print("({}) No treasure for dirty hacker!".format(word))
|
print("({}) No treasure for dirty hacker!".format(word))
|
||||||
exit(0)
|
exit(0)
|
||||||
res = str(calc(Shannon[i], chests, question)) # 计算问题的结果
|
res = str(calc(Shannon[i], chests, question)) # 计算问题的结果
|
||||||
print('Shannon answers: {}!\n'.format(res))
|
print('Shannon answers: {}!\n'.format(res))
|
||||||
print("Now open the chests:")
|
print("Now open the chests:")
|
||||||
# 输入0或1表示每个宝箱的真假,用空格隔开
|
# 输入0或1表示每个宝箱的真假,用空格隔开
|
||||||
chests_string = input()
|
chests_string = input()
|
||||||
return chests == list(map(int, chests_string.strip().split(" ")))
|
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?")
|
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):
|
for i in range(1):
|
||||||
if not do_round(): # 执行游戏回合
|
if not do_round(): # 执行游戏回合
|
||||||
print("A chest suddenly comes alive and BITE YOUR HEAD OFF.\n")
|
print("A chest suddenly comes alive and BITE YOUR HEAD OFF.\n")
|
||||||
exit(0)
|
exit(0)
|
||||||
print("You've found all the treasure! {}\n".format(flag)) # 赢得游戏,获得flag
|
print("You've found all the treasure! {}\n".format(flag)) # 赢得游戏,获得flag
|
||||||
|
|
||||||
@@ -1,100 +1,100 @@
|
|||||||
from random import choice as c
|
from random import choice as c
|
||||||
from random import randint, shuffle
|
from random import randint, shuffle
|
||||||
import socketserver
|
import socketserver
|
||||||
import signal
|
import signal
|
||||||
import string
|
import string
|
||||||
import random
|
import random
|
||||||
import os
|
import os
|
||||||
|
|
||||||
class Task(socketserver.BaseRequestHandler):
|
class Task(socketserver.BaseRequestHandler):
|
||||||
def _recvall(self):
|
def _recvall(self):
|
||||||
BUFF_SIZE = 2048
|
BUFF_SIZE = 2048
|
||||||
data = b''
|
data = b''
|
||||||
while True:
|
while True:
|
||||||
part = self.request.recv(BUFF_SIZE)
|
part = self.request.recv(BUFF_SIZE)
|
||||||
data += part
|
data += part
|
||||||
if len(part) < BUFF_SIZE:
|
if len(part) < BUFF_SIZE:
|
||||||
break
|
break
|
||||||
return data.strip()
|
return data.strip()
|
||||||
|
|
||||||
def send(self, msg, newline=True):
|
def send(self, msg, newline=True):
|
||||||
try:
|
try:
|
||||||
if newline:
|
if newline:
|
||||||
msg += b'\n'
|
msg += b'\n'
|
||||||
self.request.sendall(msg)
|
self.request.sendall(msg)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def recv(self, prompt=b'[-] '):
|
def recv(self, prompt=b'[-] '):
|
||||||
self.send(prompt, newline=False)
|
self.send(prompt, newline=False)
|
||||||
return self._recvall()
|
return self._recvall()
|
||||||
|
|
||||||
# 定义一个函数 calc,用于计算问题的结果
|
# 定义一个函数 calc,用于计算问题的结果
|
||||||
def calc(self, ans, chests, expr):
|
def calc(self, ans, chests, expr):
|
||||||
try:
|
try:
|
||||||
C0, C1, C2, C3, C4, C5, C6 = chests
|
C0, C1, C2, C3, C4, C5, C6 = chests
|
||||||
r = eval(expr)
|
r = eval(expr)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.send(b"Shannon fails to understand your words.\n")
|
self.send(b"Shannon fails to understand your words.\n")
|
||||||
self.send(e)
|
self.send(e)
|
||||||
exit(0)
|
exit(0)
|
||||||
return ans(r)
|
return ans(r)
|
||||||
|
|
||||||
# 定义一个游戏回合函数 do_round
|
# 定义一个游戏回合函数 do_round
|
||||||
def do_round(self):
|
def do_round(self):
|
||||||
# 定义两个函数 truth 和 lie,用于判断 Shannon 的回答是否为真或假
|
# 定义两个函数 truth 和 lie,用于判断 Shannon 的回答是否为真或假
|
||||||
truth = lambda r: not not r
|
truth = lambda r: not not r
|
||||||
lie = lambda r: not r
|
lie = lambda r: not r
|
||||||
chests = []
|
chests = []
|
||||||
for i in range(7):
|
for i in range(7):
|
||||||
# 随机生成 7 个 True 或 False,表示每个箱子是宝藏还是怪物
|
# 随机生成 7 个 True 或 False,表示每个箱子是宝藏还是怪物
|
||||||
chests.append(c((True, False)))
|
chests.append(c((True, False)))
|
||||||
self.send(b"Seven chests lie here, with mimics or treasure hidden inside.\nBut don't worry. Trusty Shannon knows what to do.")
|
self.send(b"Seven chests lie here, with mimics or treasure hidden inside.\nBut don't worry. Trusty Shannon knows what to do.")
|
||||||
# 随机选择 Shannon 的回答策略,包括多少次 lie
|
# 随机选择 Shannon 的回答策略,包括多少次 lie
|
||||||
lie_count = c((1, 2))
|
lie_count = c((1, 2))
|
||||||
Shannon = [truth] * (15 - lie_count) + [lie] * lie_count
|
Shannon = [truth] * (15 - lie_count) + [lie] * lie_count
|
||||||
# 猜猜第几次是在说谎 :-)
|
# 猜猜第几次是在说谎 :-)
|
||||||
shuffle(Shannon)
|
shuffle(Shannon)
|
||||||
for i in range(15):
|
for i in range(15):
|
||||||
self.send(b"Ask Shannon:")
|
self.send(b"Ask Shannon:")
|
||||||
question = self.recv().decode().strip() # 输入问题
|
question = self.recv().decode().strip() # 输入问题
|
||||||
for word in question.split(" "):
|
for word in question.split(" "):
|
||||||
if word not in white_list: # 检查问题中是否包含白名单以外的关键词或符号
|
if word not in white_list: # 检查问题中是否包含白名单以外的关键词或符号
|
||||||
self.send("({}) No treasure for dirty hacker!".format(word).encode())
|
self.send("({}) No treasure for dirty hacker!".format(word).encode())
|
||||||
exit(0)
|
exit(0)
|
||||||
res = str(self.calc(Shannon[i], chests, question)) # 计算问题的结果
|
res = str(self.calc(Shannon[i], chests, question)) # 计算问题的结果
|
||||||
self.send('Shannon answers: {}!\n'.format(res).encode())
|
self.send('Shannon answers: {}!\n'.format(res).encode())
|
||||||
self.send(b"Now open the chests:")
|
self.send(b"Now open the chests:")
|
||||||
# 输入0或1表示每个宝箱的真假,用空格隔开
|
# 输入0或1表示每个宝箱的真假,用空格隔开
|
||||||
chests_string = self.recv().decode()
|
chests_string = self.recv().decode()
|
||||||
return chests == list(map(int, chests_string.strip().split(" ")))
|
return chests == list(map(int, chests_string.strip().split(" ")))
|
||||||
|
|
||||||
def handle(self):
|
def handle(self):
|
||||||
signal.alarm(60)
|
signal.alarm(60)
|
||||||
|
|
||||||
# 游戏开始
|
# 游戏开始
|
||||||
self.send(b"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?")
|
self.send(b"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):
|
for i in range(1):
|
||||||
if not self.do_round(): # 执行游戏回合
|
if not self.do_round(): # 执行游戏回合
|
||||||
self.send(b"A chest suddenly comes alive and BITE YOUR HEAD OFF.\n")
|
self.send(b"A chest suddenly comes alive and BITE YOUR HEAD OFF.\n")
|
||||||
return
|
return
|
||||||
self.send("You've found all the treasure! {}\n".format(flag).encode()) # 赢得游戏,获得flag
|
self.send("You've found all the treasure! {}\n".format(flag).encode()) # 赢得游戏,获得flag
|
||||||
|
|
||||||
|
|
||||||
class ThreadedServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
class ThreadedServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ForkedServer(socketserver.ForkingMixIn, socketserver.TCPServer):
|
class ForkedServer(socketserver.ForkingMixIn, socketserver.TCPServer):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
flag = bytes(os.getenv("FLAG"),"utf-8") # type: ignore
|
flag = bytes(os.getenv("FLAG"),"utf-8") # type: ignore
|
||||||
# 白名单列表,包含允许在问题中使用的关键词和符号
|
# 白名单列表,包含允许在问题中使用的关键词和符号
|
||||||
white_list = ['==', '(', ')', 'C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', '0', '1', 'and', 'or', 'not']
|
white_list = ['==', '(', ')', 'C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', '0', '1', 'and', 'or', 'not']
|
||||||
HOST, PORT = '0.0.0.0', 10011
|
HOST, PORT = '0.0.0.0', 10011
|
||||||
server = ForkedServer((HOST, PORT), Task)
|
server = ForkedServer((HOST, PORT), Task)
|
||||||
server.allow_reuse_address = True
|
server.allow_reuse_address = True
|
||||||
print(HOST, PORT)
|
print(HOST, PORT)
|
||||||
server.serve_forever()
|
server.serve_forever()
|
||||||
Reference in New Issue
Block a user