# Nmap

### Intro

Nmap is an industry-standard tool for mapping networks, identifying live hosts and discovering the services. As it is one of the most used network scanner tools, a security analyst should identify the network patterns created with it. This section will cover identifying the most common Nmap scan types.

* TCP connect scans
* SYN scans
* UDP scans

It is essential to know how Nmap scans work to spot scan activity on the network. However, it is impossible to understand the scan details without using the correct filters. Below are the base filters to probe Nmap scan behavior on the network.&#x20;

### TCP flags in a nutshell.

| Notes                                                                                                       | Wireshark Filters                                                                         |
| ----------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
| Global search.                                                                                              | <ul><li>tcp</li><li>udp</li></ul>                                                         |
| <ul><li>Only SYN flag.</li><li>SYN flag is set. The rest of the bits are not important.</li></ul>           | <ul><li>tcp.flags == 2</li><li>tcp.flags.syn == 1</li></ul>                               |
| <ul><li>Only ACK flag.</li><li>ACK flag is set. The rest of the bits are not important.</li></ul>           | <ul><li>tcp.flags == 16</li><li>tcp.flags.ack == 1</li></ul>                              |
| <ul><li>Only SYN, ACK flags.</li><li>SYN and ACK are set. The rest of the bits are not important.</li></ul> | <ul><li>tcp.flags == 18</li><li>(tcp.flags.syn == 1) and (tcp.flags.ack == 1)</li></ul>   |
| <ul><li>Only RST flag.</li><li>RST flag is set. The rest of the bits are not important.</li></ul>           | <p><br></p><ul><li>tcp.flags == 4</li><li>tcp.flags.reset == 1</li></ul>                  |
| <ul><li>Only RST, ACK flags.</li><li>RST and ACK are set. The rest of the bits are not important.</li></ul> | <ul><li>tcp.flags == 20</li><li>(tcp.flags.reset == 1) and (tcp.flags.ack == 1)</li></ul> |
| <ul><li>Only FIN flag</li><li>FIN flag is set. The rest of the bits are not important.</li></ul>            | <ul><li>tcp.flags == 1</li><li>tcp.flags.fin == 1</li></ul>                               |

### TCP Connect Scans

TCP Connect Scan in a nutshell:

* Relies on the three-way handshake (needs to finish the handshake process).
* Usually conducted with nmap -sT command.
* Used by non-privileged users (only option for a non-root user).
* Usually has a windows size larger than 1024 bytes as the request expects some data due to the nature of the protocol.

| Open TCP Port                                                  | Open TCP Port                                                                       | Closed TCP Port                                |
| -------------------------------------------------------------- | ----------------------------------------------------------------------------------- | ---------------------------------------------- |
| <ul><li>SYN --></li><li><-- SYN, ACK</li><li>ACK --></li></ul> | <ul><li>SYN --></li><li><-- SYN, ACK</li><li>ACK --></li><li>RST, ACK --></li></ul> | <ul><li>SYN --></li><li><-- RST, ACK</li></ul> |

The images below show the three-way handshake process of the open and close TCP ports. Images and pcap samples are split to make the investigation easier and understand each case's details.

Open TCP port (Connect):

![Wireshark - tcp connect scan and open tcp port](https://lh7-us.googleusercontent.com/w8M-qpJ7Y5Zp5G4brnYIk3nNYeWiX1a6i8pj9VhQgXSfp8Oeu4bG1Wouhf77yD0_HoKTyNUtPH3TmH7kVFyjt02vSJswzDPMSvfa1ZKVGG4OkxwuHJ6M2KVYN5prmenEWEDrE4fpz3B9zj8TlDVOReA)

Closed TCP port (Connect):

![Wireshark - tcp connect scan and closed tcp port](https://lh7-us.googleusercontent.com/tekfKoIvWYNpcrnIaeEIxraH67Z4vdIK_BFbvsUcR4w3_soJ8KUeDpV9kKmY2olFaWJ44JbY87DYQPxhWYroo1q79H2h66E4Rszw3SU3aCRREWcrtgnNNI4PDAY8R7rA7tgLuamuWeiIVVr79-cDoY8)

The above images provide the patterns in isolated traffic. However, it is not always easy to spot the given patterns in big capture files. Therefore analysts need to use a generic filter to view the initial anomaly patterns, and then it will be easier to focus on a specific traffic point. The given filter shows the TCP Connect scan patterns in a capture file.

tcp.flags.syn==1 and tcp.flags.ack==0 and tcp.window\_size > 1024&#x20;

![Wireshark - tcp connect scan pattern](https://lh7-us.googleusercontent.com/tAbF8fxxA4d04GQsKNYqZ6NG19bLYTP5jP9DBo4YzlNaxUmkgvPcC1SqauVK6Ai924OMeayJUD5Vcl0uiaBKZX6hqVLgl9brfdTY1xcMhSDm8NhZEu69wCK6-I2TxwaQrRL9RofZ05bVfCiZDIfx0AU)

### SYN Scans

TCP SYN Scan in a nutshell:

* Doesn't rely on the three-way handshake (no need to finish the handshake process).
* Usually conducted with nmap -sS command.
* Used by privileged users.
* Usually have a size less than or equal to 1024 bytes as the request is not finished and it doesn't expect to receive data.

| Open TCP Port                                                | Close TCP Port                                |
| ------------------------------------------------------------ | --------------------------------------------- |
| <ul><li>SYN --></li><li><-- SYN,ACK</li><li>RST--></li></ul> | <ul><li>SYN --></li><li><-- RST,ACK</li></ul> |

Open TCP port (SYN):

![Wireshark - tcp syn scan and open port](https://lh7-us.googleusercontent.com/VyQCkRolBvLehe8PkAV7AHP5vor_vupfpokEV4gxfAk32t8h8RaC_Y7euOdDXZTGPeHNM5nk9dhl2mc1lnylexu4B-6BzcjizldFetNJNFe7wYYZHm1N5BjsOyL5r5-GZUA23UkGH4KX3NU_HXWPKfY)

Closed TCP port (SYN):

![Wireshark - tcp syn scan and closed port](https://lh7-us.googleusercontent.com/jYROFdrYsFINIcGutjZALCLSiWETkJFmgefusjUGvhCETYxBydenlDnDqY0JP2ovoEHmRS7Fu9WRSbgfCikHdCAL2dluSX-lZe0f_jqSFCvKu7rYPv51VQo17LEEEsAFsZwJsHSXFMfzgTPieEZ8g20)

The given filter shows the TCP SYN scan patterns in a capture file.

tcp.flags.syn==1 and tcp.flags.ack==0 and tcp.window\_size <= 1024 &#x20;

![Wireshark - tcp syn scan pattern](https://lh7-us.googleusercontent.com/x_dXfSWKutWZv2VAAadJ6imoI45K3wxmsreDRnzgO1SE8dlGCKYJDpYFU8Iqjwx09zDPemxONGDHWUnIWXOS5rGzc4ZKGLrKkLlqj0A3HDtbVH4daD_hWqyJqm3OYRq5-C4QXM4FY7j03PF56kkUqi0)

### UDP Scans

UDP Scan in a nutshell:

* Doesn't require a handshake process
* No prompt for open ports
* ICMP error message for close ports
* Usually conducted with nmap -sU command.

| Open UDP Port                    | Closed UDP Port                                                                                                   |
| -------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| <ul><li>UDP packet --></li></ul> | <ul><li>UDP packet --></li><li>ICMP Type 3, Code 3 message. (Destination unreachable, port unreachable)</li></ul> |

Closed (port no 69) and open (port no 68) UDP ports:

![Wireshark - udp scan with open and closed ports](https://lh7-us.googleusercontent.com/NptpzsWGggDPYgnD72ELjXiScAzWd0OaGD186S9gUbwbC_AIqGmCdDscuqmittvXqRGvCrw9snbwR0RDiNRpwZGrOKkgRKl66vaeJq1lrRy5IgYV28P-nmlLkE1T5yxAcFXYi141cUGxxnH6KeYjaUg)

The above image shows that the closed port returns an ICMP error packet. No further information is provided about the error at first glance, so how can an analyst decide where this error message belongs? The ICMP error message uses the original request as encapsulated data to show the source/reason of the packet. Once you expand the ICMP section in the packet details pane, you will see the encapsulated data and the original request, as shown in the below image.

![Wireshark - udp scan closed port and icmp packet encapsulation](https://lh7-us.googleusercontent.com/-0ooVgctt-NHwzUzRQN5-zvesZCF4G6qhUdAczOiRlCTKHHoxpWfdUeYHiaNcppjpFi9-rmj6lH9ALQSAN678WjYbOeN140WfBejQxu2_8qZOS1YgQeKZ1z-gSmcDX0-aUckRQ2HzqPXfOPcK0tSD7w)

The given filter shows the UDP scan patterns in a capture file.

icmp.type==3 and icmp.code==3    &#x20;

![Wireshark - udp port scan pattern](https://lh7-us.googleusercontent.com/Fdb5V9Zgy4EkuYiIJolwrI5naaYaPF6xiMRcJ8RTafrALAhqLOXLEgbDzcsm7yprYrlg0MrZMycoF7ncZhYSphmmh5pY56_7-beVOIORZZwKyjHGvC_O5VF0SGplLvtBbrL0ktwqFYGgdY3at7JtQf0)

<br>
