# SMB

## <mark style="color:yellow;">About</mark>

<mark style="color:red;">**Server Message Block (SMB)**</mark> is a client-server <mark style="color:purple;">**protocol**</mark> that regulates access to files and entire directories and other network resources such as printers, routers, or interfaces released for the network. The **SMB** protocol enables the client to communicate with other participants in the same network to access files or services shared with it on the network. By default is <mark style="color:yellow;">**using ports 137, 138, 139, and 445**</mark>

## <mark style="color:yellow;">TOOLS</mark>

There are a lot different tools for SMB interacton:

### <mark style="color:blue;">CrackMapExec</mark>

<mark style="color:yellow;">**Null-session**</mark> login, or **Pass-The-Hash** Attack

```bash
crackmapexec smb 13.13.13.13 -u Administrator -d . -H 12379N1D2YV31U20931C031 -x whoami
OR
crackmapexec smb {DOMAIN} -u '' -p '' --shares
OR
crackmapexec smb {DOMAIN} -u 'blibbityblabbity' -p '' --shares
```

Check if there are **users from list** available

```bash
crackmapexec smb {DOMAIN} -u users.txt -p '' -d . --continue-on-success
# Or we could do user=password type of attack with using:
crackmapexec smb {DOMAIN} -u users.txt -d . --no-bruteforce --continue-on-success
```

| Command                                                       | Explanation                               |
| ------------------------------------------------------------- | ----------------------------------------- |
| <mark style="color:green;">**`--shares`**</mark>              | Look for SMB shares                       |
| <mark style="color:green;">**`--continue-on-success`**</mark> | Continue after finding right creds        |
| <mark style="color:green;">**`--no-bruteforce`**</mark>       | just using worlist without any variations |
| <mark style="color:green;">**`--rid-brute`**</mark>           | Bruteforce RID                            |
| <mark style="color:green;">**`--local-auth`**</mark>          | Local auth, without domain creds          |
| <mark style="color:green;">**`-M`**</mark>                    | Use module                                |
| <mark style="color:green;">**`--sam`**</mark>                 | Extract SAM hashes                        |
| <mark style="color:green;">**`--exec-method`**</mark>         | Method used for executing commands        |
| <mark style="color:green;">**`--loggedon-users`**</mark>      | Enumerate logged in users                 |
| <mark style="color:green;">`-M spider_plus`</mark>            | Spider module                             |
| <mark style="color:green;">`-x 'type C:\root.txt'`</mark>     | Execute command                           |

### <mark style="color:blue;">Samba</mark>

<mark style="color:red;">**Samba**</mark> is an alternative variant to the **SMB** server, developed for Unix-based operating system. Samba implements the <mark style="color:red;">**Common Internet File System (CIFS)**</mark> network protocol. **CIFS** is a "dialect" of **SMB**. In other words, **CIFS** is a very specific implementation of the **SMB** protocol, which in turn was created by Microsoft. Therefore, it usually is referred to as **SMB / CIFS**. However, **CIFS** is the extension of the **SMB** protocol.

* Samba server over TCP ports <mark style="color:yellow;">**137**</mark>, <mark style="color:yellow;">**138**</mark>, <mark style="color:yellow;">**139**</mark>, but CIFS uses TCP port <mark style="color:yellow;">**445**</mark> only. In a network,using Samba, each host participates in the same <mark style="color:red;">**workgroup**</mark>. A workgroup is a group name that identifies an arbitrary collection of computers and their resources on an SMB network.
* <mark style="color:green;">**`smbstatus`**</mark> command to see who, from which host, and which share the client is connected.
* Config location:

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

### <mark style="color:blue;">Smbclient</mark>

#### Recursively download all files

```
smbclient //13.13.13.13/share -N -c 'prompt OFF; recurse ON; mget *'
```

#### **Connecting to the Share with&#x20;**<mark style="color:green;">**`smbclient`**</mark>

```bash
smbclient -N -L //13.13.13.13
```

**OR**

```bash
smbclient -U username \\\\13.13.13.13\\share
```

> <mark style="color:green;">**`-N`**</mark> is for <mark style="color:yellow;">**null session**</mark>, which is anonymous access without the input user/pass. And <mark style="color:green;">**`-L`**</mark> is to list all shares.

> For basic interaction with share, you would need only these commands:

* <mark style="color:green;">**`help`**</mark> to have list of available commands
* <mark style="color:green;">**`ls`**</mark> list
* <mark style="color:green;">**`get`**</mark> download
* <mark style="color:green;">**`!command`**</mark> user prefix ! before command to execute it locally

### <mark style="color:blue;">SMBMap</mark>

#### Regular SMB enum

```bash
smbmap -H 13.13.13.13
```

#### Recursive SMB enum

```bash
smbmap -H 13.13.13.13 -r share
```

#### Download to SMB server:

```bash
smbmap -H 13.13.13.13 --download "share\example.txt"
```

#### Upload to SMB server

```bash
smbmap -H 13.13.13.13 --upload example.txt "share\example.txt"
```

### <mark style="color:blue;">Impacket-smbclient</mark>

```bash
impacket-smbclient 'sreed@domain.local' -no-pass
sudo impacket-smbserver share ./ -smb2support # Creation of SMB Server
```

### <mark style="color:blue;">Impacket-psexec</mark>

```bash
impacket-psexec administrator:'amoguskek'@13.13.13.13
# Same goes for impacket-smbexec and impacket-atexec
```

### <mark style="color:blue;">RPCclient</mark>

<mark style="color:red;">**Remote Procedure Call (RPC)**</mark> is a <mark style="color:purple;">**concept**</mark> and, therefore, also a central <mark style="color:purple;">**tool**</mark> to realize operational and work-sharing structures in networks and client-server architectures. Other words, **RPC** is a mechanism for interaction between processes that are executed on different nodes in the networ&#x6B;**.** The communication process via **RPC** includes passing parameters and the return of a function value. Also **RPC** have <mark style="color:red;">**Remote Invocation Descriptor**</mark> or <mark style="color:red;">**RID**</mark>,which is a unique <mark style="color:purple;">**identifier**</mark> assigned to each remote procedure call. It is used to keep track of and manage the execution of remote procedures. Also there is a nice <mark style="color:yellow;">**cheatsheet**</mark> from SANS Institute [**\[LINK\]**](https://www.willhackforsushi.com/sec504/SMB-Access-from-Linux.pdf)

#### Connect to SMB server

```bash
rpcclient -U "" 13.13.13.13
rpcclient $> {function}
```

With connected **RPC** client we could use many different functions as:

| Function                                                        | Description                                                       |
| --------------------------------------------------------------- | ----------------------------------------------------------------- |
| <mark style="color:green;">**`srvinfo`**</mark>                 | Server information                                                |
| <mark style="color:green;">**`enumdomains`**</mark>             | Enumerate all domains that are deployed in the network            |
| <mark style="color:green;">**`querydominfo`**</mark>            | Provides domain, server, and user information of deployed domains |
| <mark style="color:green;">**`netshareenumall`**</mark>         | Enumerates all available shares                                   |
| <mark style="color:green;">**`netsharegetinfo <share>`**</mark> | Provides information about a specific share                       |
| <mark style="color:green;">**`enumdomusers`**</mark>            | Enumerates all domain users                                       |
| <mark style="color:green;">**`queryuser <RID>`**</mark>         | Provides information about a specific user                        |
| <mark style="color:green;">**`querygroup <RID>`**</mark>        | Provides information about a specific group                       |

### <mark style="color:yellow;">WINDOWS NET</mark>

```powershell
net use \\DC01\ipc$ "" /u:""
```

## <mark style="color:yellow;">Dangerous Settings</mark>

* <mark style="color:green;">**`browseable = yes`**</mark> - Allow listing available shares in the current share?
* <mark style="color:green;">**`read only = no`**</mark> - Forbid the creation and modification of files?
* <mark style="color:green;">**`writable = yes`**</mark> - Allow users to create and modify files?
* <mark style="color:green;">**`guest ok = yes`**</mark> - Allow connecting to the service without using a password?
* <mark style="color:green;">**`enable privileges = yes`**</mark> - Honor privileges assigned to specific SID?
* <mark style="color:green;">**`create mask = 0777`**</mark> - What permissions must be assigned to the newly created files?
* <mark style="color:green;">**`directory mask = 0777`**</mark> - What permissions must be assigned to the newly created directories?
* <mark style="color:green;">**`logon script = script.sh`**</mark> - What script needs to be executed on the user's login?
* <mark style="color:green;">**`magic script = script.sh`**</mark> - Which script should be executed when the script gets closed?
* <mark style="color:green;">**`magic output = script.out`**</mark> - Where the output of the magic script needs to be stored?

## <mark style="color:yellow;">Making a Share</mark>

> As example we will be using Windows 10.
>
> For a beginning we need to make a folder.
>
> Then we are going to use the **Advanced Sharing** option to configure share.

<figure><img src="https://1512601210-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxX3XegaS6tqwW9d8bpam%2Fuploads%2Ff0vOVlRczGyiG0KXAgrK%2Fimage.png?alt=media&#x26;token=a02aa584-e150-4417-9dff-af1bc6457677" alt=""><figcaption><p><strong>Making the Folder a Share</strong></p></figcaption></figure>

> Keep in mind that with shared resources, both the SMB and NTFS permissions lists apply to every resource that gets shared in Windows.

<figure><img src="https://1512601210-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxX3XegaS6tqwW9d8bpam%2Fuploads%2FMABJ41oZ8s3dwzSTFQfs%2Fimage.png?alt=media&#x26;token=9585a397-3cbe-4589-b747-87cf197161c5" alt=""><figcaption><p><strong>Share Permissions ACL</strong></p></figcaption></figure>

> Here we could do more precise NTFS permissions control. If the mark is grey, then permissions are inherited from parent directory.

<figure><img src="https://1512601210-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxX3XegaS6tqwW9d8bpam%2Fuploads%2FZmf1y0mu1yMIsSY77t3y%2Fimage.png?alt=media&#x26;token=5bb797c5-3944-4c23-8d88-4f45950f2f9f" alt=""><figcaption><p><strong>NTFS Permissions ACL (Security Tab)</strong></p></figcaption></figure>

## <mark style="color:yellow;">Mount a Share</mark>

```bash
sudo mount -t cifs -o username=venator17,password=ababagalabaga //13.13.13.13/"Sharename" /home/user/Desktop/
```

## <mark style="color:yellow;">Tips2Hack</mark>

1. To bruteforce RID using rpcclient to do active enum of users and groups you could use this command:

```bash
for i in $(seq 500 1100);do rpcclient -N -U "" 13.13.13.13 -c "queryuser 0x$(printf '%x\n' $i)" | grep "User Name\|user_rid\|group_rid" && echo "";done
```

3. Or for the same cause you could try yo use [**samrdump.py**](https://github.com/SecureAuthCorp/impacket/blob/master/examples/samrdump.py) script
4. You could use [**enum4linux-ng**](https://github.com/cddmp/enum4linux-ng) or <mark style="color:green;">**`enum4linux`**</mark>

```bash
./enum4linux-ng.py 13.13.13.13 -A
```

5. Also you could monitor shares from <mark style="color:yellow;">**Computer Management**</mark> tool, or view Share access logs in <mark style="color:yellow;">**Event Viewer**</mark>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://venator17.gitbook.io/bibliotheque/pentesting/protocols/smb.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
