# T1046 - Network Service Discovery

## Description from ATT&CK

> Adversaries may attempt to get a listing of services running on remote hosts and local network infrastructure devices, including those that may be vulnerable to remote software exploitation. Common methods to acquire this information include port, vulnerability, and/or wordlist scans using tools that are brought onto a system.(Citation: CISA AR21-126A FIVEHANDS May 2021)
> 
> Within cloud environments, adversaries may attempt to discover services running on other cloud hosts. Additionally, if the cloud environment is connected to a on-premises environment, adversaries may be able to identify services running on non-cloud systems as well.
> 
> Within macOS environments, adversaries may use the native Bonjour application to discover services running on other macOS hosts within a network. The Bonjour mDNSResponder daemon automatically registers and advertises a host’s registered services on the network. For example, adversaries can use a mDNS query (such as <code>dns-sd -B _ssh._tcp .</code>) to find other systems broadcasting the ssh service.(Citation: apple doco bonjour description)(Citation: macOS APT Activity Bradley)

[Source](https://attack.mitre.org/techniques/T1046)

## Atomic Tests

- [Atomic Test #1: Port Scan](#atomic-test-1-port-scan)
- [Atomic Test #2: Port Scan Nmap](#atomic-test-2-port-scan-nmap)
- [Atomic Test #3: Port Scan NMap for Windows](#atomic-test-3-port-scan-nmap-for-windows)
- [Atomic Test #4: Port Scan using python](#atomic-test-4-port-scan-using-python)
- [Atomic Test #5: WinPwn - spoolvulnscan](#atomic-test-5-winpwn---spoolvulnscan)
- [Atomic Test #6: WinPwn - MS17-10](#atomic-test-6-winpwn---ms17-10)
- [Atomic Test #7: WinPwn - bluekeep](#atomic-test-7-winpwn---bluekeep)
- [Atomic Test #8: WinPwn - fruit](#atomic-test-8-winpwn---fruit)
- [Atomic Test #9: Network Service Discovery for Containers](#atomic-test-9-network-service-discovery-for-containers)
- [Atomic Test #10: Port-Scanning /24 Subnet with PowerShell](#atomic-test-10-port-scanning-24-subnet-with-powershell)
- [Atomic Test #11: Remote Desktop Services Discovery via PowerShell](#atomic-test-11-remote-desktop-services-discovery-via-powershell)
- [Atomic Test #12: Port Scan using nmap (Port range)](#atomic-test-12-port-scan-using-nmap-port-range)

### Atomic Test #1: Port Scan

Scan ports to check for listening ports.

Upon successful execution, sh will perform a network connection against a single host (192.168.1.1) and determine what ports are open in the range of 1-65535. Results will be via stdout.

**Supported Platforms:** Linux, macOS

**auto_generated_guid:** `68e907da-2539-48f6-9fc9-257a78c05540`

#### Inputs

| Name | Description | Type | Default Value |
|------|-------------|------|---------------|
| host | Host to scan. | string | 192.168.1.1|

#### Attack Commands: Run with `bash`!

```bash
for port in {1..65535}; do (2>/dev/null echo >/dev/tcp/#{host}/$port) && echo port $port is open ; done
```

### Atomic Test #2: Port Scan Nmap

Scan ports to check for listening ports with Nmap.
Upon successful execution, sh will utilize nmap, telnet, and nc to contact a single or range of addresses on port 80 to determine if listening. Results will be via stdout.

**Supported Platforms:** Linux, macOS

**auto_generated_guid:** `515942b0-a09f-4163-a7bb-22fefb6f185f`

#### Inputs

| Name | Description | Type | Default Value |
|------|-------------|------|---------------|
| host | Host to scan. | string | 192.168.1.1|
| port | Ports to scan. | string | 80|
| network_range | Network Range to Scan. | string | 192.168.1.0/24|

#### Attack Commands: Run with `sh`! Elevation Required (e.g. root or admin)

```sh
sudo nmap -sS #{network_range} -p #{port}
telnet #{host} #{port}
nc -nv #{host} #{port}
```


#### Dependencies: Run with `sh`!

##### Description: Check if nmap command exists on the machine

###### Check Prereq Commands

```sh
if [ -x "$(command -v nmap)" ]; then exit 0; else exit 1; fi;
```

###### Get Prereq Commands

```sh
(which yum && yum -y install epel-release nmap)||(which apt-get && DEBIAN_FRONTEND=noninteractive apt-get install -y nmap)||(which pkg && pkg install -y nmap)
```

##### Description: Check if nc command exists on the machine

###### Check Prereq Commands

```sh
if [ -x "$(command -v nc)" ]; then exit 0; else exit 1; fi;
```

###### Get Prereq Commands

```sh
(which yum && yum -y install epel-release nc)||(which apt-get && DEBIAN_FRONTEND=noninteractive apt-get install -y netcat)||(which pkg && pkg install -y netcat)
```

##### Description: Check if telnet command exists on the machine

###### Check Prereq Commands

```sh
if [ -x "$(command -v telnet)" ]; then exit 0; else exit 1; fi;
```

###### Get Prereq Commands

```sh
(which yum && yum -y install epel-release telnet)||(which apt-get && DEBIAN_FRONTEND=noninteractive apt-get install -y telnet)
```

### Atomic Test #3: Port Scan NMap for Windows

Scan ports to check for listening ports for the local host 127.0.0.1

**Supported Platforms:** Windows

**auto_generated_guid:** `d696a3cb-d7a8-4976-8eb5-5af4abf2e3df`

#### Inputs

| Name | Description | Type | Default Value |
|------|-------------|------|---------------|
| nmap_url | NMap installer download URL | url | https://nmap.org/dist/nmap-7.80-setup.exe|
| host_to_scan | The host to scan with NMap | string | 127.0.0.1|

#### Attack Commands: Run with `powershell`! Elevation Required (e.g. root or admin)

```powershell
nmap #{host_to_scan}
```


#### Dependencies: Run with `powershell`!

##### Description: NMap must be installed

###### Check Prereq Commands

```powershell
if (cmd /c "nmap 2>nul") {exit 0} else {exit 1}
```

###### Get Prereq Commands

```powershell
New-Item -Type Directory "PathToAtomicsFolder\..\ExternalPayloads\" -ErrorAction Ignore -Force | Out-Null
Invoke-WebRequest -OutFile "PathToAtomicsFolder\..\ExternalPayloads\nmap-7.80-setup.exe" #{nmap_url}
Start-Process "PathToAtomicsFolder\..\ExternalPayloads\nmap-7.80-setup.exe" /S
```

### Atomic Test #4: Port Scan using python

Scan ports to check for listening ports with python

**Supported Platforms:** Windows

**auto_generated_guid:** `6ca45b04-9f15-4424-b9d3-84a217285a5c`

#### Inputs

| Name | Description | Type | Default Value |
|------|-------------|------|---------------|
| host_ip | Host to scan. | string | 127.0.0.1|
| filename | Location of the project file | path | PathToAtomicsFolder&#92;T1046&#92;src&#92;T1046.py|

#### Attack Commands: Run with `powershell`!

```powershell
python "#{filename}" -i #{host_ip}
```


#### Dependencies: Run with `powershell`!

##### Description: Check if python exists on the machine

###### Check Prereq Commands

```powershell
if (Get-Command py -errorAction SilentlyContinue) { exit 0 } else { exit 1 }
```

###### Get Prereq Commands

```powershell
New-Item -Type Directory "PathToAtomicsFolder\..\ExternalPayloads\" -ErrorAction ignore -Force | Out-Null
invoke-webrequest "https://www.py