# Nmap Scanning

### Stealth Scan (Faster) <a href="#scan-for-alive-hosts" id="scan-for-alive-hosts"></a>

```
sudo nmap -sS 192.168.1.2
```

### Nmap Connect Scan

```
nmap -sT 192.168.1.2
```

### UDP Scan

```
sudo nmap -sU 192.168.1.2
```

### Merge TCP + UDP Scan

```
sudo nmap -sU -sS 192.168.1.2
```

**Quickly Generate list of IPs:**

```
for ip in $(seq 1 254); do echo 192.168.1.$ip; done > ips
```

### Scan for alive hosts <a href="#scan-for-alive-hosts" id="scan-for-alive-hosts"></a>

```
$ nmap -sn $ip/24

$ nmap -vvv -sn $ip/24
```

If you want little faster

```
$ nmap -sn -n $ip/24 > ip-range.txt
```

### Scan specific IP range <a href="#scan-specific-ip-range" id="scan-specific-ip-range"></a>

```
$ nmap -sP 10.0.0.0-100
```

### Auto Recon <a href="#auto-recon" id="auto-recon"></a>

```
autorecon 10.10.10.3
```

### Initial Scan TCP <a href="#initial-scan-tcp" id="initial-scan-tcp"></a>

```
nmap -sC -sV -O -oA initial 10.10.10.3Full Scan TCP 
```

### Full Scan TCP <a href="#full-scan-tcp" id="full-scan-tcp"></a>

Comprehensive nmap scans in the background to make sure we cover all bases.

```
nmap -sC -sV -O -p- -oA nmap/full 10.10.10.3
```

### Full Scan UDP <a href="#full-scan-udp" id="full-scan-udp"></a>

```
nmap -sU -O -p- -oA nmap/udp 10.10.10.3
```

### Normal Scan <a href="#normal-scan" id="normal-scan"></a>

```
nmap -A $ip
```

### Scan specific machine <a href="#scan-specific-machine" id="scan-specific-machine"></a>

#### Scan common port <a href="#scan-common-port" id="scan-common-port"></a>

```
$ nmap -A -oA filename $ip/24
```

The command:

* Scan 1024 most common ports
* Run OS detection
* Run default nmap scripts
* Save the result into `.nmap`, `.gnmap` and `.xml`
* Faster

### Fast scanning <a href="#fast-scanning" id="fast-scanning"></a>

Scan 100 most common ports

```
nmap -F $ip
```

### Quick TCP Scan <a href="#quick-tcp-scan" id="quick-tcp-scan"></a>

```
nmap -sC -sV -vv -oA quick $ip
```

### Quick UDP Scan <a href="#quick-udp-scan" id="quick-udp-scan"></a>

```
nmap -sU -sV -vv -oA quick_udp $ip
```

### Full TCP Scan <a href="#full-tcp-scan" id="full-tcp-scan"></a>

```
nmap -sC -sV -p- -vv -oA full 10.10.10.10
```

### Port knock <a href="#port-knock" id="port-knock"></a>

```
for x in 7000 8000 9000; do nmap -Pn --host_timeout 201 --max-retries 0 -p $x $ip; done
```

### Scan deeply <a href="#scan-deeply" id="scan-deeply"></a>

Scanning more deeply:

```
$ nmap -v -p- -sT $ip

Example:
$ nmap -v -p- -sT 10.0.1.0/24
```

This command:

* Scan all 65535 ports with full connect scan
* Take very long time
* Print out straigt away instead of having to wait until end of the scan

Tips:

Scanning this takes a long time, suggest to leave the scan running overnight, when you're sleep or move on to different box in the meantime.

### **Maximum scan delay** <a href="#maximum-scan-delay" id="maximum-scan-delay"></a>

The –max-scan-delay is used to specify the maximum amount of time Nmap should wait between probes.

```
nmap -sC -sV $ip -oN initial -v --max-scan-delay=10
```

### **Maximum Retries** <a href="#maximum-retries" id="maximum-retries"></a>

–max-retries specifies the number of times a packet is to be resent on a port to check if it is open or closed. If –max-retries is set to 0, the packets will be sent only once on a port and no retries will be done.

```
nmap -p21-25$ip --max-retries 0
```

### Scan for specific port <a href="#scan-for-specific-port" id="scan-for-specific-port"></a>

```
$ nmap -p T:80,443,8080 $ip/24
```

Use `-T`: specifies TCP ports. Use `-U`: for UDP ports.

### Scan for unused IP addresses and store in text file <a href="#scan-for-unused-ip-addresses-and-store-in-text-file" id="scan-for-unused-ip-addresses-and-store-in-text-file"></a>

```
$ nmap -v -sn $ip/24 | grep down | awk '{print $5}' > filename.txt
```

### Other option <a href="#other-option" id="other-option"></a>

```
nmap -sV -sC -v -oA output $ip
```

### UDP scan <a href="#udp-scan" id="udp-scan"></a>

Scanning this might slow and unreliadble

```
$ nmap $ip -sU

Example:
$ nmap 10.11.1.X -sU
```

### Top ports <a href="#top-ports" id="top-ports"></a>

To save time and network resources, we can also scan multiple IPs, probing for a short list of a an common ports. For example, let’s conduct a TCP connect scan for the top twenty TCP ports with kw Ma the `--top-ports` option and enable OS version detection, script scanning, and traceroute with -A:

```
nmap -sT -A --top-ports=20 10.11.1.1-254 -oG top-port-sweep.txt
```

### Scan targets from a text file <a href="#scan-targets-from-a-text-file" id="scan-targets-from-a-text-file"></a>

Create a text file contains of our targets machine (like in method Scan for unused IP addresses and store in text file):

```
192.168.1.144
192.168.1.179
192.168.1.182
```

Run this nmap command with `-iL`

```
nmap -iL list-of-ips.txt
```

### Onetwopunch.sh <a href="#onetwopunch.sh" id="onetwopunch.sh"></a>

Grab the latest bash script

```
git clone https://github.com/superkojiman/onetwopunch.git
cd onetwopunch
```

Create a text file contains of our targets machine (like in method Scan for unused IP addresses and store in text file):

```
192.168.1.144
192.168.1.179
192.168.1.182
```

Then, run the script and tell it to read our txt file and perform TCP scan against each target.

```
./onetwopunch.sh -t ip-range.txt -p tcp
```

So, the idea behind the script to generate a scan of 65,535 ports on the targets. The script use unicornscan to scan all ports, and make a list of those ports that are open. The script then take the open ports and pass them to nmap for service detection.

### Grepable Nmap output:

```
nmap -v -sn 192.168.50.1-253 -oG ping-sweep.txt
grep Up ping-sweep.txt | cut -d " " -f 2
```

<figure><img src="https://62622891-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTpMsK2Wqj3HDjKCIKxDH%2Fuploads%2F18O0uABEOo7RGKVA6ioH%2Fimage.png?alt=media&#x26;token=99355799-83c9-4dd8-a069-87e5692a1f7c" alt=""><figcaption></figcaption></figure>

### Guess OS of machine:

```
sudo nmap -O 192.168.50.14 --osscan-guess
```

-A is for getting service details:

```
nmap -sT -A 192.168.50.14
```

### Get HTTP headers

```
nmap --script http-headers 192.168.50.6
```

### Nmap Services Mapping

```
cat /usr/share/nmap/nmap-services
```

### Powershell One-Liner:

```
Test-NetConnection -Port 445 192.168.50.151
```

One-liner for Powershall scanning of 1024 TCP Ports:

```
1..1024 | % {echo ((New-Object Net.Sockets.TcpClient).Connect("192.168.50.151", $_)) "TCP port $_ is open"} 2>$null
```

## References

<https://www.stationx.net/nmap-cheat-sheet/>&#x20;
