# Port Scanner

Port Scanner (scanner de porta) é uma técnica com o objetivo de mapear as portas TCP e UDP. Neste teste ele identifica o status das portas como aberta, fechada ou filtrada por um firewall.

Também é possível identificar qual serviço (e sua respectiva versão) está sendo executado em determinada porta.

### MassScan

Esse sem dúvida o Port Scanner mais rápido atualmente, sendo capaz de enviar 10 milhões de pacotes por segundo, o equivale a varredura de toda a internet em apenas 6 mnutos.

#### Instalação

```bash
sudo apt install masscan
```

#### Utilização

```bash
sudo masscan -p80 192.168.1.0/24
sudo masscan -p80 192.168.1.0/24 --rate=1000 -e eth0 --router-ip 192.168.1.1
```

### Hping3

O Significado das Flags devolvidas no hping3 são:

| FLAG | DESCRIÇÃO         |
| ---- | ----------------- |
| `R`  | RESET             |
| `S`  | SYN               |
| `A`  | ACK               |
| `F`  | FIN               |
| `P`  | PUSH              |
| `U`  | URGENT            |
| `X`  | not standard 0x40 |
| `Y`  | not standard 0x80 |

#### Bypass com ping (ICMP)

Mesmo quando o firewall está bloqueando o PING (enviando ou recebendo através do ICMP) ainda sim é possível utilizar através do `hping3` utilizando um dos comandos abaixo:

```bash
hping3 --icmp <ip>
hping --syn <ip>
hping3 --syn -c 1 -p <destination_port>
```

#### Bypass via DNS

A porta 53 (DNS) geralmente é liberado pelo firewall, então podemos utilizar o tamanho da janela como 100

```bash
hping3 --syn <ip> -s 53 -p 80 -k -w 100
```

#### Varredura X-MAS

```bash
hping3 <ip> --xmas -p 80 -c 3
```

### Netcat

#### TCP

```bash
# Verificando uma única porta
nc -vnz <ip> <port>

# Verificando um range de portas
nc -vnz <ip> 20-25

# Verificando todas as portas
# -w 2 = Tempo de timeout | -z = modo Zero I/O, onde não envia dados
nc -nvv -w 2 -z <ip> 1-65535
```

#### UDP

```bash
nc -nv -u -z -w 1 <IP> <port>
```

### Metasploit

```bash
msfconsole
use auxiliary/scanner/portscan/tcp
SET RHOSTS <ip>
exploit
```

### NMAP

Essa sem dúvida é uma ótima ferramenta (na minha opinião o melhor Port Scanner), mas devido a sua grande quantidade de informações, decidi deixar uma página para explicar melhor sobre o Nmap.

{% content-ref url="/pages/-M18NMcT4te\_aux5rd-l" %}
[NMAP](/knowledge-base/ataques/tools/nmap.md)
{% endcontent-ref %}

### Bash

Sim, isso mesmo. Com os próprios recursos do bash, podemos criar nosso próprio port scanner. Isso pode ser útil quando não temos nenhuma ferramenta disponivel.

```bash
#!/bin/bash
host=<ip_alvo>
for port in {1..65535}; do
    timeout .1 bash -c "echo >/dev/tcp/$host/$port" &&
        echo "port $port is open"
done
echo "Done"
```

### Powershell

```bash
Test-NetConnection -ComputerName <ip> -Port <port>
```


---

# 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://mysther.gitbook.io/knowledge-base/ataques/infra/port-scanner.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.
