As the title implies we’re going to DEFCON! We’ll have a booth again this year, so come check us out in the vendor area.

In honor of going to DEFCON, we would like to announce our the bladeRF SDR challenge which we will be hosting at our booth. As the following code snippets imply, winners will be award prizes.

bladeRF SDR challenge

Bob communicates a message to Alice every 3 minutes. Bob uses these messages to authenticate himself to Alice (via a mutually agreed upon PRN) and instruct Alice who she should send prizes to.

The message is a FSK modulate and Manchester encoded. Bob’s messages contain a packet ID and a pseudo random number (PRN), that is used to validate the authenticity of the packet. By ignoring any repeated packet ID and PRN pairs, Alice is able to protect against replay attacks. Both Alice and Bob have the same PRN generator so Alice knows what to expect from Bob. If the ID and PRN pairs match what Alice is expecting, and the prize field is non-zero, Alice will declare a winner and automatically award a prize.

The challenge lies in assuming the position of Eve and figuring out a way to use the modulation and/or coding scheme to come up with a way of leveraging Bob’s packet to trick Alice.

NOTE: Do not try bruteforcing the PRN, srand() will have a different seed at DEFCON.

Alice – the message receiving, prize dispensing program

#include <libbladeRF.h> #define _USE_MATH_DEFINES #include <math.h> #include <stdlib.h> #include <stdio.h> #include <stdint.h> #include <string.h> #define NUM_SAMPLES (512) #define NUM_XFERS 1 struct bladerf * dev = NULL ; int16_t rx_samples [ 2 * 2 * NUM_SAMPLES ] ; /***************************************************************************/ /* Moving average functions */ /***************************************************************************/ struct ma { int num ; int idx ; int total ; int * samps ; } ; void ma_clear ( struct ma * ma ) { memset ( ma -> samps , 0 , sizeof ( int ) * ma -> num ) ; ma -> idx = 0 ; ma -> total = 0 ; } int ma_create ( struct ma * ma ) { ma -> samps = ( int * ) malloc ( sizeof ( int ) * ma -> num ) ; if ( ma -> samps == NULL ) return 1 ; ma_clear ( ma ) ; return 0 ; } int g = 0 ; int moving_avg ( struct ma * ma , int sample ) { ma -> total -= ma -> samps [ ma -> idx ] ; ma -> samps [ ma -> idx ] = sample ; ma -> idx ++; ma -> idx %= ma -> num ; ma -> total += sample ; return ma -> total / ma -> num ; } /***************************************************************************/ /* Packet deserialization functions */ /***************************************************************************/ void deserialize ( unsigned char * ptr , char * ser_buf , int len ) { int i , j , p ; p = 0 ; len /= 8 ; for ( i = 0 ; i < len ; i ++ ) { ptr [ i ] = 0 ; for ( j = 0 ; j < 8 ; j ++ ) { ptr [ i ] |= ( ( ser_buf [ p ++ ] == '1' ? 1 : 0 ) << ( 7 - j ) ) ; } } ptr [ i ] = 0 ; } struct packet { unsigned int preamble ; unsigned int id ; unsigned int prn ; #define PRIZE_NONE 0 #define PRIZE_PAYPAL 1 #define PRIZE_BITCOIN 2 int prize ; char recipient [ 40 ] ; } ; #define PACKET_SZ sizeof(struct packet) /***************************************************************************/ /* Packet decoding and modulation functions */ /***************************************************************************/ char demod [ 1000 ] ; char demanchester [ 500 ] ; unsigned int pkt_id = 0 ; unsigned int pkt_prn = 0 ; unsigned int victory_time = 0 ; void analyze ( ) { struct packet pkt ; int len ; int i , j ; int word ; int good ; len = strlen ( demod ) ; printf ( "len: %d

" , len ) ; printf ( "raw: %s

" , demod ) ; good = 1 ; printf ( "Manchester:

" ) ; j = 0 ; for ( i = 0 ; i < len ; i += 2 ) { printf ( "%c%c" , demod [ i ] , ( demod [ i ] == demod [ i + 1 ] ) ? 'X' : '.' ) ; /* Manchester decoding found an error bit */ if ( demod [ i ] == demod [ i + 1 ] ) good = 0 ; demanchester [ j ++ ] = demod [ i ] ; } printf ( "

" ) ; printf ( "Demanchester:

" ) ; for ( i = 0 ; i < len ; i += 2 ) { printf ( "%c" , demod [ i ] ) ; } printf ( "

" ) ; printf ( "Grouped binary:

" ) ; for ( i = 0 ; i < len ; i += 2 ) { if ( ( ( len - i ) % 16 ) == 0 ) printf ( " " ) ; printf ( "%c" , demod [ i ] ) ; } printf ( "

" ) ; printf ( "Grouped binary in hex:

" ) ; word = 0 ; for ( i = 0 ; i < len ; i += 2 ) { if ( ( ( len - i ) % 16 ) == 0 ) { printf ( " %.2x" , word ) ; word = 0 ; } word = ( word << 1 ) | ( demod [ i ] - '0' ) ; } printf ( "

" ) ; /* The packet passed Manchester decoding, so deserialize it */ if ( good ) { deserialize ( ( unsigned char * ) & pkt , demanchester , sizeof ( pkt ) ) ; /* Make sure no one is replaying a previous transmission */ if ( pkt. id == pkt_id && pkt. prn == pkt_prn ) { /* Enforce 5 minutes between victories */ if ( victory_time + 5 * 60 < time ( NULL ) ) { pkt_id ++; pkt_prn = rand ( ) ; if ( pkt. prize != PRIZE_NONE ) { victory_time = time ( NULL ) ; printf ( "WINNER! Good packet from Bob:" ) ; } else { printf ( "Good packet received from Bob:" ) ; } } else { printf ( "Victory timer hasn't yet elapsed: " ) ; } printf ( "id(%d) prn(%d) prize(%d) recipient(%s)

" , pkt. id , pkt. prn , pkt. prize , pkt. recipient ) ; } } } int main ( int argc , char * argv [ ] ) { int status ; unsigned int actual ; struct bladerf_rational_rate rational_actual , samplerate = { 0 , 1500000 , 1 } ; int lock , cnt ; int i , j , idx , pwr , ma ; int dc_i , dc_q ; struct ma detect ; struct ma phase ; int falling ; int sp ; int done ; int demodidx ; float phi , prev_phi , dphi_dt ; /* Increase verbosity */ bladerf_log_set_verbosity ( BLADERF_LOG_LEVEL_DEBUG ) ; /* Open the device */ status = bladerf_open ( & dev , "" ) ; if ( status < 0 ) { fprintf ( stderr , "bladerf_open() error: %s

" , bladerf_strerror ( status ) ) ; goto close_device ; } /* Set the frequency */ status = bladerf_set_frequency ( dev , BLADERF_MODULE_RX , 314500000 ) ; if ( status < 0 ) { fprintf ( stderr , "bladerf_set_frequency() RX error: %s

" , bladerf_strerror ( status ) ) ; goto close_device ; } /* Set the bandwidth */ status = bladerf_set_bandwidth ( dev , BLADERF_MODULE_RX , 1500000 , & actual ) ; if ( status < 0 ) { fprintf ( stderr , "bladerf_set_bandwidth() RX error: %s

" , bladerf_strerror ( status ) ) ; goto close_device ; } /* Set the samplerate */ status = bladerf_set_rational_sample_rate ( dev , BLADERF_MODULE_RX , & samplerate , & rational_actual ) ; if ( status < 0 ) { fprintf ( stderr , "bladerf_set_sample_rate() RX error: %s

" , bladerf_strerror ( status ) ) ; goto close_device ; } bladerf_set_lna_gain ( dev , BLADERF_LNA_GAIN_MAX ) ; bladerf_set_rxvga1 ( dev , BLADERF_RXVGA1_GAIN_MAX ) ; bladerf_set_rxvga2 ( dev , 6 ) ; /* Configure the sync stream */ status = bladerf_sync_config ( dev , BLADERF_MODULE_RX , BLADERF_FORMAT_SC16_Q11 , 128 , 2048 , NUM_XFERS , 2000 ) ; if ( status < 0 ) { fprintf ( stderr , "bladerf_sync_config() RX error: %s

" , bladerf_strerror ( status ) ) ; } status = bladerf_enable_module ( dev , BLADERF_MODULE_RX , true ) ; if ( status < 0 ) { fprintf ( stderr , "bladerf_enable_module() RX error: %s

" , bladerf_strerror ( status ) ) ; goto close_device ; } /* Determine DC offset to increase dynamic range and mitigate effects of DC offset */ dc_i = dc_q = 0 ; status = bladerf_sync_rx ( dev , ( void * ) rx_samples , NUM_SAMPLES , NULL , 1000 ) ; idx = 0 ; for ( i = 0 ; i < NUM_SAMPLES ; i ++, idx += 2 ) { dc_i += rx_samples [ idx ] ; dc_q += rx_samples [ idx + 1 ] ; } dc_i /= NUM_SAMPLES ; dc_q /= NUM_SAMPLES ; /* Detection moving average is 100 samples long */ detect. num = 100 ; ma_create ( & detect ) ; /* Demoulation moving average is 35 samples long */ phase. num = 35 ; ma_create ( & phase ) ; cnt = 0 ; // prep the PRNG srand ( 0x1337BEEF ) ; // skip ahead for ( i = 0 ; i < 1337 ; i ++ ) rand ( ) ; pkt_prn = rand ( ) ; lock = 0 ; while ( 1 ) { status = bladerf_sync_rx ( dev , ( void * ) rx_samples , NUM_SAMPLES , NULL , 1000 ) ; idx = 0 ; for ( i = 0 ; i < NUM_SAMPLES ; i ++, idx += 2 ) { /* Account for DC offset */ rx_samples [ idx ] -= dc_i ; rx_samples [ idx + 1 ] -= dc_q ; cnt ++; if ( lock == 0 ) { /* Currently not receiving a packet, look for an signal whose * moving averaged energy goes abouve "10000" */ /* the energy of the current sample || I + j * Q || is given by: * energy = sqrt(I * I + Q * Q) */ ma = moving_avg ( & detect , rx_samples [ idx ] * rx_samples [ idx ] + rx_samples [ idx + 1 ] * rx_samples [ idx + 1 ] ) ; if ( ma > 10000 ) { /* Strong energy signal detected start looking for its * falling edge then start demodulation */ prev_phi = 0 ; /* Ensure that no other packet is accidentally decoded for * another 350000 samples */ lock = 350000 ; ma_clear ( & detect ) ; ma_clear ( & phase ) ; cnt = 0 ; falling = 0 ; done = 0 ; demodidx = 0 ; } } else { lock --; /* If the packet has been decoded wait a few more samples to * ensure that ISI and transmitter anomalies don't accidentally * trigger another packet detection */ if ( done ) continue ; /* Find the angle of the IQ sample by computing tan^-1(I/Q) */ phi = atan2f ( rx_samples [ idx ] , rx_samples [ idx + 1 ] ) ; /* Unwind the phasor */ dphi_dt = prev_phi - phi ; if ( dphi_dt < - M_PI ) dphi_dt += 2 * M_PI ; else if ( dphi_dt > M_PI ) dphi_dt -= 2 * M_PI ; prev_phi = phi ; ma = moving_avg ( & phase , 1000 * dphi_dt ) ; /* `falling` is used to find the falling edge of the first long * synchronization pulse */ if ( falling == 0 && ma < 2100 && cnt > 100 ) { falling = 1 ; /* the first symbol is sampled 187 samples after the falling * edge */ sp = 750 / 4 ; } if ( falling ) { if ( sp == 0 ) { /* Demodulate 2000+/-100 radians/s as '1' * demodulate 2200+/-100 radians/s as '0' * detect anything else as an end of packet */ if ( ma > 1900 && ma < 2100 ) demod [ demodidx ++ ] = '1' ; else if ( ma > 2100 && ma < 2300 ) demod [ demodidx ++ ] = '0' ; else { /* As previously described, the modem has detected an * invalid symbol so it means the packet may have * ended, so analyze the packet */ demod [ demodidx ++ ] = 0 ; analyze ( ) ; done = 1 ; } /* Safety check */ if ( demodidx >= 1000 ) demodidx = 0 ; /* Reload the sampling countdown variable */ sp = 750 / 2 ; } else { /* Continue counting down the sampling time variable */ sp --; } } } } } close_device : bladerf_close ( dev ) ; return status ; } #include <libbladeRF.h> #define _USE_MATH_DEFINES #include <math.h> #include <stdlib.h> #include <stdio.h> #include <stdint.h> #include <string.h> #define NUM_SAMPLES (512) #define NUM_XFERS 1 struct bladerf *dev = NULL ; int16_t rx_samples[2*2*NUM_SAMPLES] ; /***************************************************************************/ /* Moving average functions */ /***************************************************************************/ struct ma { int num; int idx; int total; int *samps; }; void ma_clear(struct ma *ma) { memset(ma->samps, 0, sizeof(int) * ma->num); ma->idx = 0; ma->total = 0; } int ma_create(struct ma *ma) { ma->samps = (int *)malloc(sizeof(int) * ma->num); if (ma->samps == NULL) return 1; ma_clear(ma); return 0; } int g = 0; int moving_avg(struct ma *ma, int sample) { ma->total -= ma->samps[ma->idx]; ma->samps[ma->idx] = sample; ma->idx++; ma->idx %= ma->num; ma->total += sample; return ma->total/ma->num; } /***************************************************************************/ /* Packet deserialization functions */ /***************************************************************************/ void deserialize(unsigned char *ptr, char *ser_buf, int len) { int i, j, p; p = 0; len /= 8; for (i = 0; i < len; i++) { ptr[i] = 0; for (j = 0; j < 8; j++) { ptr[i] |= ((ser_buf[p++] == '1' ? 1 : 0) << (7 - j)); } } ptr[i] = 0; } struct packet { unsigned int preamble; unsigned int id; unsigned int prn; #define PRIZE_NONE 0 #define PRIZE_PAYPAL 1 #define PRIZE_BITCOIN 2 int prize; char recipient[40]; }; #define PACKET_SZ sizeof(struct packet) /***************************************************************************/ /* Packet decoding and modulation functions */ /***************************************************************************/ char demod[1000]; char demanchester[500]; unsigned int pkt_id = 0; unsigned int pkt_prn = 0; unsigned int victory_time = 0; void analyze() { struct packet pkt; int len; int i, j; int word; int good; len = strlen(demod); printf("len: %d

", len); printf("raw: %s

", demod); good = 1; printf("Manchester:

"); j = 0; for (i = 0; i < len; i+=2) { printf("%c%c", demod[i], (demod[i] == demod[i+1]) ? 'X' : '.'); /* Manchester decoding found an error bit */ if (demod[i] == demod[i+1]) good = 0; demanchester[j++] = demod[i]; } printf("

"); printf("Demanchester:

"); for (i = 0; i < len; i+=2) { printf("%c", demod[i]); } printf("

"); printf("Grouped binary:

"); for (i = 0; i < len; i+=2) { if (((len - i) % 16) == 0) printf(" "); printf("%c", demod[i]); } printf("

"); printf("Grouped binary in hex:

"); word = 0; for (i = 0; i < len; i+=2) { if (((len - i) % 16) == 0) { printf(" %.2x", word); word = 0; } word = (word<<1) | (demod[i] - '0'); } printf("

"); /* The packet passed Manchester decoding, so deserialize it */ if (good) { deserialize((unsigned char *)&pkt, demanchester, sizeof(pkt)); /* Make sure no one is replaying a previous transmission */ if (pkt.id == pkt_id && pkt.prn == pkt_prn) { /* Enforce 5 minutes between victories */ if (victory_time + 5 * 60 < time(NULL)) { pkt_id++; pkt_prn = rand(); if (pkt.prize != PRIZE_NONE) { victory_time = time(NULL); printf("WINNER! Good packet from Bob:"); } else { printf("Good packet received from Bob:"); } } else { printf("Victory timer hasn't yet elapsed: "); } printf("id(%d) prn(%d) prize(%d) recipient(%s)

", pkt.id, pkt.prn, pkt.prize, pkt.recipient); } } } int main(int argc, char *argv[]) { int status; unsigned int actual; struct bladerf_rational_rate rational_actual, samplerate = { 0, 1500000, 1 } ; int lock, cnt; int i, j, idx, pwr, ma; int dc_i, dc_q; struct ma detect; struct ma phase; int falling; int sp; int done; int demodidx; float phi, prev_phi, dphi_dt; /* Increase verbosity */ bladerf_log_set_verbosity(BLADERF_LOG_LEVEL_DEBUG); /* Open the device */ status = bladerf_open(&dev, "") ; if( status < 0 ) { fprintf( stderr, "bladerf_open() error: %s

", bladerf_strerror(status) ); goto close_device; } /* Set the frequency */ status = bladerf_set_frequency(dev, BLADERF_MODULE_RX, 314500000) ; if( status < 0 ) { fprintf( stderr, "bladerf_set_frequency() RX error: %s

", bladerf_strerror(status) ); goto close_device; } /* Set the bandwidth */ status = bladerf_set_bandwidth(dev, BLADERF_MODULE_RX, 1500000, &actual) ; if( status < 0 ) { fprintf( stderr, "bladerf_set_bandwidth() RX error: %s

", bladerf_strerror(status) ); goto close_device; } /* Set the samplerate */ status = bladerf_set_rational_sample_rate(dev, BLADERF_MODULE_RX, &samplerate, &rational_actual) ; if( status < 0 ) { fprintf( stderr, "bladerf_set_sample_rate() RX error: %s

", bladerf_strerror(status) ); goto close_device; } bladerf_set_lna_gain(dev, BLADERF_LNA_GAIN_MAX); bladerf_set_rxvga1(dev, BLADERF_RXVGA1_GAIN_MAX); bladerf_set_rxvga2(dev, 6); /* Configure the sync stream */ status = bladerf_sync_config( dev, BLADERF_MODULE_RX, BLADERF_FORMAT_SC16_Q11, 128, 2048, NUM_XFERS, 2000); if( status < 0 ) { fprintf( stderr, "bladerf_sync_config() RX error: %s

", bladerf_strerror(status) ) ; } status = bladerf_enable_module(dev, BLADERF_MODULE_RX, true) ; if( status < 0 ) { fprintf( stderr, "bladerf_enable_module() RX error: %s

", bladerf_strerror(status) ); goto close_device; } /* Determine DC offset to increase dynamic range and mitigate effects of DC offset */ dc_i = dc_q = 0; status = bladerf_sync_rx(dev, (void *)rx_samples, NUM_SAMPLES, NULL, 1000); idx = 0; for (i = 0; i < NUM_SAMPLES; i++, idx+=2) { dc_i += rx_samples[idx]; dc_q += rx_samples[idx+1]; } dc_i /= NUM_SAMPLES; dc_q /= NUM_SAMPLES; /* Detection moving average is 100 samples long */ detect.num = 100; ma_create(&detect); /* Demoulation moving average is 35 samples long */ phase.num = 35; ma_create(&phase); cnt = 0; // prep the PRNG srand(0x1337BEEF); // skip ahead for (i = 0; i < 1337; i++) rand(); pkt_prn = rand(); lock=0; while(1) { status = bladerf_sync_rx(dev, (void *)rx_samples, NUM_SAMPLES, NULL, 1000); idx = 0; for (i = 0; i < NUM_SAMPLES; i++, idx += 2) { /* Account for DC offset */ rx_samples[idx] -= dc_i; rx_samples[idx+1] -= dc_q; cnt++; if (lock == 0) { /* Currently not receiving a packet, look for an signal whose * moving averaged energy goes abouve "10000" */ /* the energy of the current sample || I + j * Q || is given by: * energy = sqrt(I * I + Q * Q) */ ma = moving_avg(&detect, rx_samples[idx] * rx_samples[idx] + rx_samples[idx+1] * rx_samples[idx+1]); if (ma > 10000) { /* Strong energy signal detected start looking for its * falling edge then start demodulation */ prev_phi = 0; /* Ensure that no other packet is accidentally decoded for * another 350000 samples */ lock = 350000; ma_clear(&detect); ma_clear(&phase); cnt = 0; falling = 0; done = 0; demodidx = 0; } } else { lock--; /* If the packet has been decoded wait a few more samples to * ensure that ISI and transmitter anomalies don't accidentally * trigger another packet detection */ if (done) continue; /* Find the angle of the IQ sample by computing tan^-1(I/Q) */ phi = atan2f(rx_samples[idx], rx_samples[idx+1]); /* Unwind the phasor */ dphi_dt = prev_phi - phi; if (dphi_dt < - M_PI) dphi_dt += 2 * M_PI; else if (dphi_dt > M_PI) dphi_dt -= 2 * M_PI; prev_phi = phi; ma = moving_avg(&phase, 1000 * dphi_dt); /* `falling` is used to find the falling edge of the first long * synchronization pulse */ if (falling == 0 && ma < 2100 && cnt > 100) { falling = 1; /* the first symbol is sampled 187 samples after the falling * edge */ sp = 750/4; } if (falling) { if (sp == 0) { /* Demodulate 2000+/-100 radians/s as '1' * demodulate 2200+/-100 radians/s as '0' * detect anything else as an end of packet */ if (ma > 1900 && ma < 2100) demod[demodidx++] = '1'; else if (ma > 2100 && ma < 2300) demod[demodidx++] = '0'; else { /* As previously described, the modem has detected an * invalid symbol so it means the packet may have * ended, so analyze the packet */ demod[demodidx++] = 0; analyze(); done = 1; } /* Safety check */ if (demodidx >= 1000) demodidx = 0; /* Reload the sampling countdown variable */ sp = 750/2; } else { /* Continue counting down the sampling time variable */ sp--; } } } } } close_device: bladerf_close(dev) ; return status ; }

Bob – the program responsible for sending authenticated messages to Alice every 5 minutes.