Note aux francophones: j'ai rédigé ce write-up initialement en anglais, et je n'ai pas eu/pris le temps de le traduire en français. Je sais, je commence mal l'année 2022 (mais lisez ce write-up, l'épreuve était vraiment sympa =)
During the ph0wn CTF that took place virtually on December the 3rd, a strange challenge called Wazabee were available with the following description:
Hint: Do some OSINT on the challenge author to find what this can be about.
Jean Reno has created a network surveillance startup, called Wazabee. He
installed this Android application (download it) on his smartphone and it
looks like it is transmitting something important. But what? Help him,
and you might get to play in Wasabi 2.
Install the application on an Android smartphone (it is not malicious)
that supports Extended advertising and LE 2M. You can easily test your phone's
capabilities with the nRF Connect application. Go in "Device information" and
check the corresponding labels are green.
Your smartphone does not support it? Too bad :=) Get one! Or find another way
to solve the challenge (there are several solutions).
The flag follows the usual format: ph0wn{ .... } where it can any printable
character between the brackets.
We are provided with an Android APK that must be run on a smartphone that supports Extended advertising and LE 2M.
Well, let's have a look at it.
First of all, I don't own a smartphone that supports Extended advertising and LE 2M but I know a little of Bluetooth Low Energy. Extended advertisements and a bitrate of 2 Mbps have been introduced in the version 5 of the Bluetooth core specification, that allows any compatible device to send BLE advertisements on all channels (instead of the only 3 advertising channels 37, 38, and 39 as specified in previous versions) at 2 Mbit per second.
Second, a hint is given in the description, telling us to do a background search on the author of this challenge (Romain Cayre). A quick search revealed that Romain Cayre published a paper written in French, at the French SSTIC conference, called "WazaBee : attaque de réseaux Zigbee par détournement de puces Bluetooth Low Energy" (or "WazaBee: attacking Zigbee networks by subverting Bluetooth Low Energy chips").
This paper presents a way to make a BLE 5 compatible chip send Zigbee frames by abusing the GFSK modulation used in Bluetooth Low Energy PHY. This paper contains the word "Wazabee" in its title (which sounds like "wasabi" in French -- also a French movie starring Jean Reno) and is about Bluetooth Low Energy (and Zigbee). Well, that's becoming interesting !
Once disassembled using Jadx, the interesting code is found in the radio.sploit.phownchallenge.SecondFragment class:
@Override // androidx.fragment.app.Fragment
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
final BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter()
.getBluetoothLeAdvertiser();
final View v = getView();
if (!adapter.isLe2MPhySupported()) {
Snackbar.make(v, "2M PHY not supported!", -1).show();
} else if (!adapter.isLeExtendedAdvertisingSupported()) {
Snackbar.make(v, "LE Extended Advertising not supported!", -1).show();
} else {
This code checks that the 2 Mbps bitrate and extended advertising are supported, and then builds a payload that is sent in bluetooth low energy advertisements:
AdvertisingSetParameters.Builder parameters = new AdvertisingSetParameters.Builder()
.setLegacyMode(false)
.setInterval(160)
.setTxPowerLevel(1)
.setPrimaryPhy(1)
.setSecondaryPhy(2);
String a = getString(R.string.a);
String b = getString(R.string.b);
String c = getString(R.string.c);
String d = getString(R.string.d);
String e = getString(R.string.e);
String out = "";
int x = 0;
while (out.length() != 64) {
if (x % 4 == 0) {
out = a + out + b;
}
if (x % 4 == 1) {
out = c + out + d;
}
if (x % 4 == 2) {
out = d + out + c;
}
if (x % 4 == 3) {
out = b + out + a;
}
x++;
}
AdvertiseData data = new AdvertiseData.Builder()
.addManufacturerData(
4660,
dewhiten(hexStringToByteArray(out + e), 17, 16)
).build();
The referenced strings are the following:
Let's write some Python code to generate the same advertising data:
# generate out
a = '1b'
b = '03'
c = '3a'
d = 'f7'
e = '70afb33965fc08453b9b03773970af3303f73a1b70afb339fc08c564f73a9b03' \
'03f73a1bb239702f70afb33970afb33908c5647cf73a9b034dc68f5070afb339' \
'c68f504cb239702f514cc60fb239702f4dc68f50f73a9b0303f73a1b70afb339' \
'c464fc08aeb33970b239702ff73a9b03f73a9b0370afb33965fc0845aeb33970' \
'464fc0870afb33970afb33970afb339aeb33970aeb33970514cc60f70afb3390'
'3f73a1bc68f504c70afb3393970af33'
x = 0
out = ''
while len(out)!=64:
if x%4==0:
out = a + out + b
if x%4==1:
out = c + out + d
if x%4==2:
out = d + out + c
if x%4==3:
out = b + out + a
x += 1
# Payload is dewhitened before sending, because it will be whitened
# again when sent by the BLE chip =)
payload = out + e
print('advertising payload:\n%s' % payload)
data = bytearray.fromhex(payload)
This script produces the following output:
advertising payload:
03f73a1b03f73a1b03f73a1b03f73a1b
03f73a1b03f73a1b03f73a1b03f73a1b
70afb33965fc08453b9b03773970af33
03f73a1b70afb339fc08c564f73a9b03
03f73a1bb239702f70afb33970afb339
08c5647cf73a9b034dc68f5070afb339
c68f504cb239702f514cc60fb239702f
4dc68f50f73a9b0303f73a1b70afb339
c464fc08aeb33970b239702ff73a9b03
f73a9b0370afb33965fc0845aeb33970
464fc0870afb33970afb33970afb339a
eb33970aeb33970514cc60f70afb3390
This data is what is sent in BLE advertisements as manufacturer data, but the meaning is still unknown. It's now time to dig into Cayre's paper to understand what is going on !
We won't cover all the details of Cayre's paper (so read it if you are looking for more information) but only the most interesting aspects that may be useful to decode this data.
Cayre found out that Bluetooth Low Energy PHY layer uses a Gaussian Frequency Shift Keying (GFSK) modulation that can be considered (in a way) as an Minimum Shift Keying (MSK) modulation. This MSK modulation when used with a 2 Mbit per second bitrate may be used to generate a RF signal (composed of I/Q samples) that fits the O-QPSK modulation used by Zigbee. In short, a BLE chip can send BLE data at 2 Mbps on a specific BLE channel that would be interpreted by a Zigbee receiver (using an O-QPSK demodulator) as a valid series of bits representing a Zigbee frame.
Cayre provides in his paper a specific table (table 2) that can be used to convert a block of 4 bits of Zigbee data into a series of 31 bits that would be interpreted as a PN sequence used in Zigbee PHY layer. And this is exactly what this Android application does: it sends a series of bits using MSK modulation that will be decoded as PN sequences and then turned into a series of 4-bit blocks by a Zigbee receiver and interpreted as such. For the record, the payload is dewhitened before sending because the BLE chip will whiten the data (and undo this dewhitening) before sending it, thus ensuring our payload is sent as-is.
Therefore, we need to do some bit-level magic with our recovered advertising payload in order to extract the MSK-encoded PN sequences with the help of some Python code:
# reverse lookup table from Cayre's paper (table 2)
msk_to_block = {
'1100000011101111010111001101100':'0000',
'1001110000001110111101011100110':'1000',
'0101100111000000111011110101110':'0100',
'0100110110011100000011101111010':'1100',
'1101110011011001110000001110111':'0010',
'0111010111001101100111000000111':'1010',
'1110111101011100110110011100000':'0110',
'0000111011110101110011011001110':'1110',
'0011111100010000101000110010011':'0001',
'0110001111110001000010100011001':'1001',
'1010011000111111000100001010001':'0101',
'1011001001100011111100010000101':'1101',
'0010001100100110001111110001000':'0011',
'1000101000110010011000111111000':'1011',
'0001000010100011001001100011111':'0111',
'1111000100001010001100100110001':'1111'
}
def msk_to_chip(seq):
assert seq in msk_to_block
return msk_to_block[seq]
def byte_to_bits(b):
return bin(b)[2:].rjust(8,'0')[::-1]
# generate out
a = '1b'
b = '03'
c = '3a'
d = 'f7'
e = '70afb33965fc08453b9b03773970af3303f73a1b70afb339fc08c564f73a9b03' \
'03f73a1bb239702f70afb33970afb33908c5647cf73a9b034dc68f5070afb339' \
'c68f504cb239702f514cc60fb239702f4dc68f50f73a9b0303f73a1b70afb339' \
'c464fc08aeb33970b239702ff73a9b03f73a9b0370afb33965fc0845aeb33970' \
'464fc0870afb33970afb33970afb339aeb33970aeb33970514cc60f70afb3390'
'3f73a1bc68f504c70afb3393970af33'
x = 0
out = ''
while len(out)!=64:
if x%4==0:
out = a + out + b
if x%4==1:
out = c + out + d
if x%4==2:
out = d + out + c
if x%4==3:
out = b + out + a
x += 1
# Payload is dewhitened before sending, because it will be whitened
again when sent by the BLE chip =)
payload = out + e
data = bytearray.fromhex(payload)
# Convert payload into bits
data_bits = ''
for c in data:
data_bits += byte_to_bits(c)
# Split by blocks of 31 bits (MSK-encoded PN sequences)
# and convert to corresponding 4-bit chip
chips = [data_bits[32*i:32*(i+1)] for i in range(int(len(data_bits)/32))]
output = ''.join([msk_to_chip(chip[:31]) for chip in chips])
This small script produces the following output:
00000000000000000000000000000000
11100101001010000000111000010110
00001100111011100111011011011110
10011100101111001101011000001110
00111010110001100110111001011010
00111110111011101010101010111110
0000100111101000
A little bit more of Python to convert this into bytes:
# Group output by 8 bits and rebuild data
nbytes = int(len(output)/8.)
output_bytes = [chr(int(output[8*i:8*(i+1)][::-1],2)) for i in range(nbytes)]
print(''.join(output_bytes))
And this displays the following text (the first null bytes are omitted): §ph0wn{9=kpcvZ|wU}. These bytes may correspond to a Zigbee beacon, but we don't need to decode it as the flag is clearly readable.