SMB

Pentesting SMB - TCP Ports 445,139

Theory

The Server Message Block Protocol (SMB Protocol) is a client-server communication protocol used for sharing access to files, printers, serial ports, and data on a network. It can also carry transaction protocols for authenticated inter-process communication.

SMB protocol operate on different ports depending on the type of communication:

  • Port 445 (TCP): This port is used for direct SMB communication over TCP/IP, including file and printer sharing, remote administration, and inter-process communication.

  • Port 139 (TCP): This port is used for SMB over NetBIOS, which is an underlying protocol that SMB relies on for name resolution and session establishment.

Practice

Authentication

Null session refers to an unauthenticated session established with an SMB server where the client does not provide any credentials.

#SmbClient
smbclient -U '' -N -L '\\<IP>\'

#NetExec
netexec smb <IP> -u '' -p '' --shares

Enumerate

Using nmap, we can enumerate sessions/shares/users/domains/groups at one time using the following command :

nmap --script="smb-enum*" -p 445 <IP>

Version & Configuration

Tools like NetExec can be used to enumerate supported protocols, dialects and signing configuration of SMB.

#Enum host with SMB signing not required
netexec smb 192.168.1.0/24 --gen-relay-list relaylistOutputFilename.txt

#Simply fingerprint SMB versipn
netexec smb <TARGET>

Users

NetExec can be used to enumerate users over SMB.

# Enumerate domain users over \pipe\samr 
netexec smb <TARGET> -u <USER> -p <PASSWORD> --users

# Enumerate local users over \pipe\samr 
netexec smb <TARGET> -u <USER> -p <PASSWORD> --local-users

#Brute force RID using querydispinfo over \pipe\samr 
netexec smb <TARGET> -u <USER> -p <PASSWORD> --rid-brute 5000

Groups

NetExec can be used to enumerate groups over SMB.

# Enumerate domain groups over \pipe\samr 
netexec smb <TARGET> -u <USER> -p <PASSWORD> --groups

#Enum local groups over \pipe\samr
netexec smb $IP -u $USER -p $PASS --local-group

#Brute force RID using querydispinfo over \pipe\samr 
netexec smb <TARGET> -u <USER> -p <PASSWORD> --rid-brute 5000

Shares

SMBClient is a native tool that allow us to interact with SMB shares. We can use it to list shares as follow

smbclient -U <USER> -L '\\<IP>\'

ACLs of Share's File/Folder

The smbcacls program allow us to get ACLs on an NT file or directory on a SMB file shares.

#File/Folder permission with anonymous/guest login (remove -N for password prompt)
smbcacls -U <USER> -N '\\<IP>\<SHARE>' <FILE/FOLDER Name>

If you see a lot off files and folders, the following commands will make a recursive permissions check on each item

#Mount the Share locally
sudo mount -t cifs -o username='USER',password='PASSWORD' '\\<IP>\<SHARE>' /mnt/Share

#Get all items
find /mnt/Share|sed 's|/mnt/Share/||g' > smb_items.txt

#Get all ACLs
for i in $(cat smb_items.txt); do echo $i; smbcacls -N '\\10.10.10.103\Department Shares' $i; echo ; done > smb_acls.txt

Sessions

NetExec can be used to enumerate active sessions and logged in users over SMB.

#Enumerate active sessions
netexec smb <TARGET> -u <USER> -p <PASSWORD> --sessions

#Enumerate logged-on in users
netexec smb <TARGET> -u <USER> -p <PASSWORD> --loggedon-users

Password Policy

NetExec can be used to enumerate various objects over SMB like the domain password policy.

#Enumerate the password policy
netexec smb <TARGET> -u <USER> -p <PASSWORD> --pass-pol

Execute Remote Commands

SMB-based

Vulnerabilities

You may use nmap to scan target for SMB vulnerabilities

sudo nmap -p 445 --script="smb-vuln-*" <IP>

EternalBlue - MS17-010

Eternalblue is a flaw that allows remote attackers to execute arbitrary code on a target system by sending specially crafted messages to the SMBv1 server.

Windows Vista, Windows 7, Windows 8.1, Windows 10, Windows Server 2008, Windows Server 2012 et Windows Server 2016 versions using SMBv1 are likely vulnerable if not patched.

Tools like nmap can be used to detect the presence of the EternalBlue vulnerability.

sudo nmap -p 445 --script="smb-vuln-ms17-010" <IP>

NetExec (Python) can be used to check if the target is vulnerable to MS17-010.

netexec smb <IP> -u <USER> -p <PASSWORD> -M ms17-010

MS08-067

The MS08-067 vulnerability is a buffer overflow vulnerability in the Windows Server service.The vulnerability could allow remote code execution if an affected system received a specially crafted RPC request. On Microsoft Windows 2000, Windows XP, and Windows Server 2003 systems, an attacker could exploit this vulnerability without authentication to run arbitrary code.

Tools like nmap can be used to to detect the presence of the MS08-067 vulnerability.

sudo nmap -p 445 --script="smb-vuln-ms08-067" <IP>

Exfiltration

Over SMB

Resources

Last updated