Skip to content

Commit 18252f2

Browse files
committed
relay: error on unsupported ad type
1 parent 1dd8fc0 commit 18252f2

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

python_cli/relay_master.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
from struct import pack, unpack
1313

1414
from sniffle.pcap import PcapBleWriter
15-
from sniffle.sniffle_hw import SniffleHW, BLE_ADV_AA, PacketMessage, DebugMessage, StateMessage, \
16-
MeasurementMessage, SnifferState
17-
from sniffle.packet_decoder import DPacketMessage, DataMessage, LlDataContMessage, AdvIndMessage, \
18-
AdvDirectIndMessage, ScanRspMessage, ConnectIndMessage, str_mac, LlControlMessage
19-
from sniffle.relay_protocol import RelayServer, MessageType
15+
from sniffle.sniffle_hw import SniffleHW, BLE_ADV_AA, PacketMessage, DebugMessage, \
16+
StateMessage, MeasurementMessage, SnifferState
17+
from sniffle.packet_decoder import DPacketMessage, DataMessage, LlDataContMessage, \
18+
AdvIndMessage, AdvDirectIndMessage, ScanRspMessage, ConnectIndMessage, \
19+
str_mac, LlControlMessage, AdvertMessage
20+
from sniffle.relay_protocol import RelayServer, MessageType, ErrorCode
2021

2122
"""
2223
Relay attack principles:
@@ -153,6 +154,10 @@ def main():
153154

154155
# obtain the target's advertisement and scan response, share it with relay slave
155156
adv, scan_rsp = scan_target(mac_bytes)
157+
if not adv or not scan_rsp:
158+
print("Error: Advertisement type must be ADV_IND. Aborting.")
159+
conn.send_err(ErrorCode.INVALID_ADV)
160+
return
156161
conn.send_msg(MessageType.ADVERT, adv.body)
157162
conn.send_msg(MessageType.SCAN_RSP, scan_rsp.body)
158163

@@ -351,14 +356,17 @@ def scan_target(mac):
351356
if not isinstance(msg, PacketMessage):
352357
continue
353358
dpkt = DPacketMessage.decode(msg)
354-
if isinstance(dpkt, AdvIndMessage) or isinstance(dpkt, AdvDirectIndMessage):
359+
if isinstance(dpkt, AdvIndMessage):
355360
if advPkt is None:
356361
print("Found advertisement.")
357362
advPkt = dpkt
358363
elif isinstance(dpkt, ScanRspMessage):
359364
if scanRspPkt is None:
360365
print("Found scan response.")
361366
scanRspPkt = dpkt
367+
elif isinstance(dpkt, AdvertMessage):
368+
print("Received incompatible advertisement of type %s." % dpkt.pdutype)
369+
return None, None
362370

363371
print("Target Advertisement:")
364372
print(advPkt)

python_cli/sniffle/relay_protocol.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class MessageType(enum.Enum):
4848
CONN_REQ = 3 # CONNECT_IND (peripheral -> central)
4949
PING = 4 # network latency test
5050
PRELOAD = 5 # preloaded encrypted conn param changes
51+
ERROR = 6 # message sent if something went wrong
52+
53+
class ErrorCode(enum.Enum):
54+
INVALID_ADV = 0 # unsupported advertisement type
5155

5256
class RelaySocketWrapper:
5357
def __init__(self, sock, peer_addr):
@@ -61,10 +65,13 @@ def recv_msg(self):
6165
body = recvall(self.sock, mlen)
6266
return MessageType(mtype), body
6367

64-
def send_msg(self, mtype, body):
68+
def send_msg(self, mtype: MessageType, body):
6569
hdr = struct.pack("<HH", mtype.value, len(body))
6670
self.sock.sendall(hdr + body)
6771

72+
def send_err(self, err_code: ErrorCode):
73+
self.send_msg(MessageType.ERROR, bytes([err_code.value]))
74+
6875
def connect_relay(peer_ip, port=7352):
6976
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
7077
peer_addr = (peer_ip, port)

0 commit comments

Comments
 (0)