0

有一个netcatCTF 任务需要通过 RCE 获取标志(对我来说最简单和最明显的变体是exec()

Python 2.7.18 (default, Apr 28 2021, 17:39:59)
[GCC 10.2.1 20210110] on linux2

>>> print input()
pow(2, 3) # No problems with functions
8

>>> print input()
None # Can print None values
None

>>> print input()
eval('1 + 1')
2

>>> print input()
eval('1 + 1') # eval() works
2

>>> x = 1
>>> print input()
eval('x + 1') # eval() with local variables involved also works
2

>>> print input()
exec('') # Even empty exec() causes an error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    exec('') # Even empty exec() causes an error
       ^
SyntaxError: invalid syntax

>>> print input()
exec('import os') # exec() call causes an error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    exec('import os') # exec() call causes an error
       ^
SyntaxError: invalid syntax

是否可以在 Python 2.7中推送一个exec()with调用?(无法input()切换 Python 版本或更改可执行文件)print input()

UPD

我需要这样的东西:

>>> print input()
exec('import os\nprint os.name') # I need a similar RCE in this CTF task, so eval() is not suitable
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    exec('import os\nprint os.name') # I need a similar RCE in this CTF task, so eval() is not suitable
       ^
SyntaxError: invalid syntax
4

1 回答 1

0

代替

>>> print input()
exec('1 + 1') 

>>> print input()
eval('1 + 1') 

你会得到2你期望的输出。exec()期望可执行语句;eval()期望一个表达式,这就是1+1。尽管这更简单且等效:

>>> print input()
1 + 1 

我必须承认我不知道 CTF 任务是什么,或者为什么它需要执行 RTE 代码,不管是什么,但考虑到您的真正要求不是产生 的结果,1 + 1而是产生 的值os.name,它可能适合您的目的最好避免在过时input()函数的极端情况下搞砸,而只是简单地做,作为命令行,

python -c "import os;print(os.name)"

哪个(在我的系统上)产生输出

nt

并且适用于每个版本的 Python。

Python 2input()函数不能做你想做的事情的原因是在 Python 2

  • exec是一个语句,而不是一个函数。
  • input()大致相当于eval(raw_input()):它将接受表达式但不接受语句。
于 2021-07-07T19:49:44.613 回答