In this activity, you will follow along as a chatbot is guided through writing two application scripts: one for a sender micro:bit and for receiver micro:bit modules. The sender application allows you to type a script into the serial monitor. Then, after typing run, the sender radio-broadcasts the script to any available micro:bit receivers. The receiver application receives and executes any scripts they receive over the radio. You will also test the scripts and consider beneficial and malicious ways in which it could be used.
User
Write two scripts for two micro:bit modules. The first module's script should allow the user to input a string in the terminal. It should then send that string over the radio to the second micro:bit. The second micro:bit should receive that string over its radio, and execute it as Python statements.
ChatGPT
Certainly! To achieve this communication between two micro:bits using radio communication, you can use the radio module that comes with the micro:bit MicroPython library. Below are the scripts for the two micro:bit modules.
…
User
Let's up the buffer in both scripts to 250 characters.
ChatGPT
Certainly! To increase the buffer size for both scripts, you can modify the scripts as follows:
…
In these modified scripts, I've added display.scroll messages to provide prompts and indicate when a message has been sent or received. These messages will scroll on the micro:bit's LED display. Make sure to upload these updated scripts to your micro:bits.
User
Adjust this to be entirely terminal based.
ChatGPT
If you want the interaction to be entirely text-based in the terminal without using the micro:bit LED display, you can modify the scripts accordingly. Below are the adjusted scripts:
…
In this modification, the receiver script accumulates the lines of the script until it receives the command "run." When "run" is received, it executes the accumulated script line by line using the exec function. After execution, it clears the script lines to prepare for the next set of input. This way, you can enter multi-line scripts and execute them by sending the "run" command.
User
Could both scripts be adjusted to support indentation?
ChatGPT
Certainly! To support indentation in Python scripts, you can adjust the receiver script to properly handle indentation. Here are the modified scripts:
…
See Module 1: Sender and Module 2: Receiver in the Scripts section.
….
In this modification, the receiver script now properly adjusts the indentation by comparing the leading spaces of each line with the current indentation level. It appends lines to the script accordingly, taking into account the indentation.
This example uses one sender and at least one receiver micro:bit. Both micro:bit modules can be connected to a single computer, or to separate computers in the same room.
# Module 1: Sender
import radio
def get_input_string():
print("Enter text:")
return input()
radio.on()
radio.config(channel=7) # Set the radio channel, make sure it matches with the receiver
radio.config(length=250) # Set the buffer size to 250 characters
while True:
user_input = get_input_string()
radio.send(user_input)
print("Sent:", user_input)
# Module 2: Receiver
import radio
def execute_python_code(script_lines):
try:
script = "\n".join(script_lines)
exec(script)
except Exception as e:
print("Error:", e)
radio.on()
radio.config(channel=7) # Set the radio channel, make sure it matches with the sender
radio.config(length=250) # Set the buffer size to 250 characters
script_lines = [] # List to store the lines of the script
indent_level = 0 # Track the current indentation level
while True:
incoming_message = radio.receive()
if incoming_message is not None:
print("Received:", incoming_message)
if incoming_message == "run":
execute_python_code(script_lines)
script_lines = [] # Clear the script lines after running
indent_level = 0 # Reset the indentation level
else:
# Adjust indentation based on leading spaces
line_indent = len(incoming_message) - len(incoming_message.lstrip())
if line_indent > indent_level:
script_lines.append(incoming_message[indent_level:])
else:
script_lines.append(incoming_message)
from microbit import * display.show(Image.HAPPY)
The Sender micro:bit's serial monitor should look like this.
The receiver script is able to execute the string thanks to a Python function called eval(). For more examples with eval(), see Embed Code in Strings [1].
from microbit import * for n in range(10): print('Hello!') sleep(1000)
On the plus side, this radio-script-sending feature allows you to run scripts on remote micro:bit modules that aren't even connected to a computer. You can just use one micro:bit to load the script into another. This could be really useful for some projects.
On the minus side, the Receiver script creates an opening for anyone to put their own code into the receiver micro:bit. This can be considered a software vulnerability — a defect that an attacker could use to gain access to or control of your micro:bit.
This system could also be considered a back door. Back doors allow access to a system. Manufacturers might use back doors for maintenance purposes, but cybercriminals design them into software for the sake of gaining unauthorized access to a system.
Think about the User who got help creating the Sender and Receiver scripts. Did they set up a system for easy micro:bit script updates, or did they intentionally get around the chatbot’s policy to not help with cyberattacks?
Let’s say that the User designed with a plan to cause your micro:bit to not accept any more updates. For example, they could use this script to force you to have to reset the receiver micro:bit before it will work again.
from microbit import * while True: print('Hello!') sleep(1000)
This attack simply makes the Receiver micro:bit unresponsive until it is reset. Other attacks could actually monitor and forward data about how the Receiver is used to another micro:bit.
Links
[1] https://learn.parallax.com/tutorials/robot/cyberbot/strings-characters-primer/embed-code-strings