Malformed Packets and the Countdown Timer

In this first activity, you will experiment with the countdown timer app from the Send and Receive Packets activity. Its original form is vulnerable to malformed packet attacks you will learn how to harden it against them.  The malformed packet attack will cause the receiver to throw an exception.  At that point, it will become unresponsive until its script is restarted or reloaded with the Flash button.

This activity requires two micro:bit modules running different scripts, a Sender script and a Receiver script.

Parts

  • Two micro:bit modules.
  • Two USB A to micro-B cables.

Scripts

Running 2 micro:bit modules: You can either connect each micro:bit to a separate computer or connect both to separate USB ports on the same computer.

  • Decide which micro:bit you want to be the countdown Sender and which one you want to be the countdown Receiver.
  • Connect the two micro:bit modules to two USB ports with two USB cables.
  • Open two separate browsers and navigate each one to python.microbit.org/v/2.
  • If you are part of a class, and have been assigned a channel, make sure to adjust the script’s channel=7 to your assigned channel before you save and flash the scripts.
  • Enter, save, and flash the countdown_sender_w_malformed_packet_option script into the Sender micro:bit.
  • Enter, save, and flash the script countdown_receiver script into receiver micro:bit.  The Receiver Script is below the Sender Script on this page.
  • Before continuing, find the # Sends malformed packet if value is -1 comment and examine how the dictionary can be changed before transmitting.  

Sender Script: countdown_sender_w_malformed_packet_option

# countdown_sender_w_malformed_packet_option

from microbit import *
import radio

radio.on()
radio.config(channel=7,length=128)

sleep(1000)

print("Countdown App")
print("micro:bit sender")

while True:
    text = input("Enter countdown start: ")
    value = int(text)
    message = input("Enter message after countdown: ")
    
    dictionary = {  }
    dictionary['start'] = value
    dictionary['after'] = message
    
    # Sends malformed packet if value is -1
    if value is -1:
        dictionary['start'] = "Not a number!"

    packet = str(dictionary)
    
    print("Send: ", packet)
    radio.send(packet)
    
    print()

 

Receiver Script: countdown_receiver

# countdown_receiver.py

from microbit import *
import radio

radio.on()
radio.config(channel=7,length=128)

sleep(1000)

print("Countdown App")
print("micro:bit receiver\n")

while True:
    packet = radio.receive()
    if packet is not None:
        print("Receive: ", packet)

        print()
        print("Parse: ")

        dictionary = eval(packet)

        value = dictionary['start']
        message = dictionary['after']
        
        print("value = ", value)
        print("message = ", message, "\n")
        
        while value >= 0:
            print(value)
            sleep(1000)
            value = value - 1
            
        print(message)
        
        print()