Safe Penetration Testing Labs · intermediate · ~15 min
Walk a Bluetooth Low Energy advertisement payload and pull out the Complete Local Name field.
Walk TLVs with strict bounds checks; on type 0x09/0x08, bounded-copy the value.
TLV walking is a foundational pattern. Get it right once and every other TLV protocol falls out.
A BLE advertisement is a stream of TLVs (length / type / value). Every BLE sniffer parses them; we're going to parse one too — just the Local Name field, type 0x09 (Complete) or 0x08 (Shortened).
This is the same shape of code you'd write to walk an EXTHDR option list, a DHCP option list, or any TLV-encoded record.
[len1] [type1] [val1...] [len2] [type2] [val2...] ...
len is the number of bytes in type + value, so the next record
starts at offset + len + 1.
Implement
int extract_local_name(const uint8_t *adv, size_t n, char *out, size_t cap).
Walk the TLVs. When you hit type 0x09 or 0x08, copy the value bytes
into out (bounded by cap), NUL-terminate, and return the byte
count written.
Return -1 if:
cap == 0ncaplen. The next
record is at offset + len + 1.len == 0 record to spin the loop forever.Loop with offset + len + 1. Bounds-check before every read. Bail on len == 0 or out-of-range.