Turning a Raspberry Pi Pico into a Bad USB
This guide will walk you through converting a Raspberry Pi Pico into a “Bad USB,” a device that can mimic a keyboard and execute pre-programmed scripts on a target computer. This is typically used for penetration testing and educational purposes.
Materials Needed:
- Raspberry Pi Pico
- Micro USB cable
- Computer with internet access
- MicroSD card (optional for storing scripts)
- Thonny IDE (or another code editor that supports MicroPython)
Step 1: Prepare the Raspberry Pi Pico
- Download the MicroPython Firmware:
- Visit the Raspberry Pi Pico MicroPython firmware page and download the latest .uf2 firmware file.
- Install the MicroPython Firmware:
- Connect your Raspberry Pi Pico to your computer using a Micro USB cable while holding the BOOTSEL button.
- Release the button once the Pico is connected; it should appear as a removable storage device.
- Drag and drop the .uf2 file onto the Pico’s storage. The Pico will reboot and be ready to run MicroPython.
Step 2: Install Thonny IDE
- Download and Install Thonny:
- Download Thonny IDE from the official website and install it on your computer.
- Set Up Thonny for Pico:
- Open Thonny.
- Go to TOOLS > OPTIONS > INTERPRETER.
- Select “MicroPython (Raspberry Pi Pico)” from the dropdown menu.
Step 3: Writing the Bad USB Script
- Create a Basic Script:
- In Thonny, write a simple script that simulates keyboard input. Below is an example that opens Notepad on a Windows machine and types “Hello, World!”:
import time
import machine
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
# Delay to allow time to remove the device after insertion
time.sleep(5)
# Initialize the keyboard
keyboard = Keyboard(usb_hid.devices)
# Press Windows + R to open Run dialog
keyboard.press(Keycode.WINDOWS, Keycode.R)
keyboard.release_all()
time.sleep(0.5)
# Type ‘notepad’ and press Enter
keyboard.write(‘notepad\n’)
time.sleep(1)
# Type ‘Hello, World!’
keyboard.write(‘Hello, World!’)
2. Upload the Script to the Pico:
- Save the script as
main.py
in Thonny. - Click the “Run” button in Thonny to upload the script to your Pico.
- The script will automatically execute whenever the Pico is connected to a computer.
Step 4: Testing the Bad USB
- Safely Eject the Pico from your computer.
- Plug the Pico into a Target Computer:
- Insert the Pico into the USB port of the target computer.
- After a few seconds, the script should start running, and you should see the Notepad application open with the text “Hello, World!” typed out.
Step 5: Customizing the Payload
- Modify the Script:
- You can customize the payload by editing the script in Thonny. For example, you can change the text, open different applications, or execute commands in the command prompt or terminal.
- Adding More Functionality:
- Consider adding more complex payloads, such as opening a web browser to a specific URL, downloading and executing files, or even creating reverse shells.
- Using CircuitPython Libraries:
- If you need more advanced features, you can use CircuitPython and additional libraries like
adafruit_hid
to create more sophisticated payloads.
- If you need more advanced features, you can use CircuitPython and additional libraries like
Step 6: Safety and Ethics
-
Use Responsibly:
- This tool should only be used for educational purposes or with explicit permission during penetration testing.
- Misuse of such a device can lead to legal consequences.
- Secure Your Device:
- Ensure that your Pico is not left unattended or plugged into an unauthorized machine.
Your Raspberry Pi Pico is now set up as a Bad USB, capable of simulating a keyboard and executing pre-defined scripts on a target computer. With this guide, you have learned how to prepare the Pico, write a basic script, and test the device.
Remember to use this tool responsibly and ethically. Happy coding!
Legal and Helpful Uses of a Bad USB
Bad USB devices, when used responsibly, can be powerful tools for IT professionals, educators, and support technicians. Here are three ways a Bad USB can be legally and beneficially employed:
1. Automated System Configuration
Purpose:
Efficiently set up new workstations by automating the installation of essential software, configuring system settings, and applying security policies.
Benefit:
Saves time and reduces human error, ensuring that all systems comply with company standards right out of the box.
2. Educational Demonstrations
Purpose:
Teach cybersecurity students about the risks associated with USB devices and demonstrate how a Bad USB can be used in real-world scenarios.
Benefit:
Provides a hands-on learning experience in a controlled environment, empowering students with knowledge on how to defend against such threats.
3. Emergency System Recovery
Purpose:
Quickly resolve technical issues by executing scripts that repair network settings, clear cache, or remove specific malware from a compromised system.
Benefit:
Offers a rapid and efficient solution for common problems, especially useful in remote support situations where immediate assistance is required.
Note:
These examples emphasize the importance of using Bad USB devices responsibly and ethically. Always ensure you have explicit permission before deploying such tools in any environment.