2

这是我在这里的第一篇文章,希望有人能给我一些帮助!

我目前正在为一个大学项目开发​​ Capture The Flag (CTF),作为其中的一部分,我想参与一个 Python 测验;验证用户的答案是否正确。

尽管我编写了一个相当简单的脚本来完成工作,但我想到的一个潜在问题是用户可以简单地在 gedit 或 nano 中打开 Python 脚本并轻松找到答案。我尝试了一些解决方案,但我想不出任何可靠的方法。

正如我所说,代码本身相当简单,但我想我会把它包括在内,这样你就可以了解我目前所掌握的内容:

import sys

answer = ("Blessed be our saviour, a warrior...")
userInput = input("What does the decrypted text read? ")

if userInput == answer:
    print("Correct, the way is open")
else:
    print("Incorrect! The way is barred until you find the solution!")
    sys.exit(0)

我真的不知道这是否可能,但我想我不妨问问。

感谢您提供的任何建议!

4

3 回答 3

1

如上所述,如果用户可以打开并查看您的脚本,是什么阻止他们将您的检查更改if True or ...为绕过它?

那说:

使用答案的 sha256 哈希。

然后,对用户回答的内容进行哈希处理,看看是否匹配。

sha256 是一种单向哈希,您无法阅读脚本并知道生成它的内容。

简单编码器:

import hashlib

def encode(str):
  return hashlib.sha256(str).hexdigest()

print("Encoded string: %s" %(encode("Test")))

输出是:532eaabd9574880dbf76b9b8cc00832c20a6ec113d682299550d7a6e0f345e25

所以,你的脚本:

import sys

answer = '532eaabd9574880dbf76b9b8cc00832c20a6ec113d682299550d7a6e0f345e25' # encode("Test") -> Change "Test" to whatever your answer is
userInput = input("What does the decrypted text read? ")
hashedUserInput = encode(userInput)

if hashedUserInput == answer:
    print("Correct, the way is open")
else:
    print("Incorrect! The way is barred until you find the solution!")
    sys.exit(0)

在脚本中使用该值。

于 2019-12-12T16:14:30.083 回答
1

要么以某种包含所有答案的二进制数据类型将其存储在磁盘上,要么使用一些简单的加密技术,例如 fi Vigenere cipher。

以下是使用 Vigenere 密码进行加密的函数的简短示例:

def encode_string(key, string):
    """Encode a string using the Vigenere cipher."""
    if isinstance(key, (int, float)):
        key = str(key)
    if isinstance(string, (int, float)):
        string = str(string)
    encoded_chars = []
    for i in range(len(string)):
        key_c = key[i % len(key)]
        encoded_c = chr(ord(string[i]) + ord(key_c) % 256)
        encoded_chars.append(encoded_c)
    encoded_string = "".join(encoded_chars)
    return encoded_string

使用此功能,您可以对答案进行编码,例如:

encoded_answer = encode_string('this is some key', 'This is the answer')

编码的答案是encoded_answer='ÈÐÒæ@Òæ@ç×Ò\x85\x81ÙØðÙÚ'. 您可以将编码的答案硬编码到您的测验中。

要检查正确的答案,只需使用相同的密钥对输入进行编码并将其与硬编码的答案进行比较:

if encode_string('this is some key', userInput) == encoded_answer:
    print("Correct, the way is open")
else:
    print("Incorrect! The way is barred until you find the solution!")
    sys.exit(0)
于 2019-12-12T16:16:26.447 回答
1

.yml您可以将答案存储在用户无权访问的单独文件中,例如:data.yml

您的.yml文件将如下所示:

answer: Blessed be our saviour, a warrior...

您的代码将如下所示:

import sys
import yaml
data = yaml.load(open('data.yml'))

answer = data['answer']
userInput = input("What does the decrypted text read? ")

if userInput == answer:
    print("Correct, the way is open")
else:
    print("Incorrect! The way is barred until you find the solution!")
    sys.exit(0)

然后将您的文件设置为只读模式,这样用户将无法编辑和打印答案。您可以通过以下方式做到这一点:

chmod 0444 filename.py

PS:将.yml文件保存在最终用户无法访问的路径中。如果没有,您可以hashlib通过编码来隐藏文件的路径。如果您使用的是 unix,则可以data.yml通过执行锁定文件,chmod -rwx data.yml并且最终用户无法打开该文件。

希望这可以帮助!

于 2019-12-12T16:35:38.850 回答