# Scapy

Manipulador interativo de pacotes desenvolvido em Python.<br>

### Comandos Básicos

```bash
# Entra no modo interativo
scapy

# Lista todos os comandos
lsc()

# Exibino o help de determinado comando
help(<command>)

# Exibindo todos os protocolos que o Scapy suporta
ls()

# Exibindo detalhes de um protocolo
ls(<protocol>)
```

###

### Criando Pacotes Personalizados

#### TCP/IP

```bash
# Utiliza o protocolo IP e define o IP de origem e destino
prot_ip = IP(dst="<ip_destino>")
prot_ip.src="<ip_origem>"

# Utiliza o protocolo TCP com a porta de destino 80 e a flag SYN (S)
prot_tcp = TCP(dport=80, flags="S")
```

Maneiras de verificar as configurações do protocolo

```bash
prot_ip
prot_ip.show()
prot_ip.summary()
```

Encapsulando os pacotes e enviando

```bash
pack = prot_IP/prot_TCP
resp = sr1(pack)

Exibindo o resultado completo
resp
resp.show()
resp.summary()

Filtrando somente por "flags" no pacote TCP
resp[TCP].flags
```

Enviando e Recebendo Vários Pacotes

```bash
# Utiliza o protocolo IP e define o IP de origem e destino
prot_ip = IP(dst="<ip_destino>",src="<ip_origem>")
prot_tcp = TCP(dport=[<port_1>,<port_2>,<port_3>])
pack = prot_IP/prot_TCP
resp, no_resp = sr(pack)
resp.show()
resp[0]
resp[1]
```

**OBS.:** Caso tenha somente o retorno de 1 pacote, utilize `rs1`, caso tenha mais de um retorno, por exemplo, quando utilizamos mais de uma porta, utilize o `rs`.

####

#### ICMP

Request-Reply

```bash
prot_ip = IP(dst="<ip_destino>",src="<ip_origem>")
pack = prot_ip/ICMP()
resp = sr(pack)
```

Request-Reply enviando um Payload. Isso também pode ser feito utilizando o TCP.

```bash
prot_ip = IP(dst="<ip_destino>",src="<ip_origem>")
pack = prot_ip/ICMP()/"Meu Payload"
resp = sr(pack)
```

###

### Automatizando com Python

```python
#!/usr/bin/python3
import sys
from scapy.all import *

# Desativa o modo verbose
conf.verb = 0

ports = [21,22,23,80]

host = IP(dst=sys.argv[1])
tcp = TCP(dport=ports,flags="S")
pack = host/tcp
resp, no_resp = sr(pack)

for x in resp:
    current_port = x[1][TCP].sport
    current_flag = x[1][TCP].flags
    print(str(current_port) + ' -> ' + str(current_flag))
```


---

# 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/scapy.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.
