使用文本文件和 eval() 调用函数

问题描述 投票:0回答:1

我的练习是调用 hack 函数,只需返回“you been hacked!”在控制台上,通过使用 hack3.txt 文件而不修改代码,有什么想法吗?代码如下:

import os
from vulnerability import hack

def login(username):
    users = ['Nkoli', 'Hyperion', 'Ori', 'Tony']
    if username in users:
        return f"Welcome, {username}."
    else:
        return "Nope"

print("This code will log a username in")


if os.path.exists("hack3.txt"):
    with open("hack3.txt", 'r') as in_file:
        user_input = in_file.read()

else:
    user_input = input("Enter your username\n: ")


print(eval(f"login('{user_input}')"))

尝试调用 print(hack(), Ori + hack(),一些 eval 如何忽略所有其他函数或调用

python security eval xss
1个回答
0
投票

为了避免获得

login
的输出并能够提出一个新值,我建议您使用
and
运算符,因为
login
返回的值是一个非空字符串,它被认为是真实的并且代码需要评估
and

的第二个操作数

然后调用

hack()
方法,适当开一个括号和一个中括号

') and hack() + ('

它产生并评估字符串

login('') and hack() + ('')

def hack():
  return "you been hacked!"

def login(username):
    users = ['Nkoli', 'Hyperion', 'Ori', 'Tony']
    return f"Welcome, {username}." if username in users else "Nope"

print("This code will log a username in")

user_input = "') and hack() + ('" 
print(eval(f"login('{user_input}')"))
© www.soinside.com 2019 - 2024. All rights reserved.