# Escaneo de puertos

## Bash

```bash
for PORT in {1..65535}; do (echo > /dev/tcp/X.X.X.X/$PORT) >/dev/null 2>&1 && echo $PORT is open; done
for PORT in {1..65535}; do timeout 1 bash -c "</dev/tcp/X.X.X.X/$PORT >/dev/null" && echo "port $PORT is open"; done
```

## Netcat

```shell
nc -zv <target> 1-65535
```

## Nmap

### Escaneo de puertos TCP

```shell
# Principales 1000 puertos TCP
nmap -sS --open -n <target> -oN nmap-main-tcp-ports.txt
# Todos los puertos TCP
nmap -sS -p- --open -n <target> -oN nmap-all-tcp-ports.txt
```

* -sS = escaneo TCP SYN (stealth/sigilo).
* -p- = todos los puertos TCP (1 al 65.535). Para incluir el puerto 0 (cero), se debe especificar explícitamente `-p0-65535`.
* \--open = mostrar solo puertos abiertos (o posiblemente abiertos).
* -n = sin ejecución de resolución DNS.
* \<target> = objetivo.
* -oN = guarda resultado del escaneo en archivo `nmap-all-tcp-ports.txt`.

### Escaneo de puertos UDP

```shell
# Principales 1000 puertos UDP
nmap -sU -n <target> -oN nmap-main-udp-ports.txt
# Todos los puertos UDP
nmap -sU -p- -n <target> -oN nmap-all-udp-ports.txt
```

* -sU = escaneo puertos UDP.
* -p- = todos los puertos TCP (1 al 65.535). Para incluir el puerto 0 (cero), se debe especificar explícitamente `-p0-65535`.
* -n = sin ejecución de resolución DNS.
* \<target> = objetivo.
* -oN = guarda resultado del escaneo en archivo `nmap-all-udp-ports.txt`.

### Escaneo de puertos TCP y UDP

```shell
nmap -sS -sU -p0-65535 --open -n <target> -oN nmap-all-tcp-udp-ports.txt
```

* -sS = escaneo puertos TCP (SYN - stealth/sigilo).
* -sU = escaneo puertos UDP.
* -p0-65535 = todos los puertos TCP y UDP (0 al 65.535).
* \--open = mostrar solo puertos abiertos (o posiblemente abiertos).
* -n = sin ejecución de resolución DNS.
* \<target> = objetivo.
* -oN = guarda resultado del escaneo en archivo `nmap-all-tcp-udp-ports.txt`.

## Invoke-Portscan

```powershell
Invoke-Portscan.ps1
Get-Help Invoke-Portscan
Invoke-Portscan -Hosts <target> -TopPorts <number-of-ports>
```

## Idle scan

### Nmap <a href="#idle-scan-nmap" id="idle-scan-nmap"></a>

```shell
# Identificar máquina zombie
nmap -O -v -n <zombie-IP-address> -p <zombie-port>
# Realizar idle scan
nmap -Pn -sI <zombie-IP-address>:<zombie-port> <target> -p <port>
```

### Hping <a href="#idle-scan-hping" id="idle-scan-hping"></a>

```shell
# Identificar máquina zombie
hping3 -S -r <zombie-IP-address> -p <zombie-port>
# Realizar idle scan
hping3 -a <zombie-IP-address> -S <target> -p <port>
```

## Evasión de firewall/IDS

### Nmap <a href="#evasion-de-firewall-ids-nmap" id="evasion-de-firewall-ids-nmap"></a>

```shell
# Fragmentation
nmap -sS -f <target>
# Decoys
nmap –sS –D <decoy-IP-01>,<decoy-IP-02>,<decoy-IP-03>,ME <target>
# Timing
nmap –sS –T<0-5> <target>
# Source port
nmap –sS --source-port <port> <target>
nmap -sS -g <port> <target>
```


---

# 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://pentesting.mrw0l05zyn.cl/escaneo-y-enumeracion/escaneo-de-puertos.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.
