
If you want to emulate using Pi Zero network adapter, keyboard, drive, and everything else, and at the same time - all this can be done. LibComposite comes to the rescue, however, in this case it must be remembered that there is no solution under Windows, the method is suitable only for Linux or Mac OS X. A detailed description of the user's actions to achieve the result described in the title is in continuation (
source ).
Step 0 - configuring the SD card
Download and install the latest distribution of Raspbian Jessie on a suitable SD card (must be sufficiently capacious), and increase the root partition.
Step 1 - The Core
You need to use Kernel 4.4, which does not come by default with the Rasbian distribution. But upgrading is quite easy, you just need to execute this console command:
')
sudo BRANCH = next rpi-updateAfter this, do the following:
echo "dtoverlay = dwc2" | sudo tee -a /boot/config.txt
echo "dwc2" | sudo tee -a / etc / modulesAnd finally, activate the libcomposite driver:
sudo echo "libcomposite" | sudo tee -a / etc / modulesStep 2 - Configuring Devices
Now you need to decide on what device to emulate - a network card, keyboard, or all of this together?
Configuring this with ConfigFS, the virtual file system in / sys /. It is automatically mounted at the start of the “raspberry”, so in the future you can work with it.
There is a great example for a device called USBArmory, an example of
this here . Everything else will be shown on the basis of it.
Create a configuration scriptIt will have to run every time the device starts. Create an isticktoit_usb in / usr / bin / file using your favorite text editor. We write here that:
sudo touch / usr / bin / isticktoit_usb #create the file
sudo chmod + x / usr / bin / isticktoit_usb #make it executable
sudo nano / usr / bin / isticktoit_usb #edit the fileAfter that, the script must be run automatically at the start. For better performance, you can create a systemd unit file, but for now let's get around to rc.local. (this is part of the old sysvinit system, but it runs on raspberry pi by default).
Open under the root /etc/rc.local and add this line (before !!!) with a line containing the word "exit":
sudo nano /etc/rc.local
/etc/rc.local ...
/ usr / bin / isticktoit_usb # libcomposite configuration
exitCreate a gadgetThis is a global configuration, so no matter how many USB gadgets you would like to use. You can change the serial number, manufacturer and product name in this block.
/ usr / bin / isticktoit_usbcd / sys / kernel / config / usb_gadget /
mkdir -p isticktoit
cd isticktoit
echo 0x1d6b> idVendor # Linux Foundation
echo 0x0104> idProduct # Multifunction Composite Gadget
echo 0x0100> bcdDevice # v1.0.0
echo 0x0200> bcdUSB # USB2
mkdir -p strings / 0x409
echo "fedcba9876543210"> strings / 0x409 / serialnumber
echo "Tobias Girstmair"> strings / 0x409 / manufacturer
echo "iSticktoit.net USB Device"> strings / 0x409 / product
mkdir -p configs / c.1 / strings / 0x409
echo "Config 1: ECM network"> configs / c.1 / strings / 0x409 / configuration
echo 250> configs / c.1 / MaxPower
# Add functions here
# see gadget configurations below
# End functions
ls / sys / class / udc> UDCEthernet adapterFirst of all, let's add this to our configuration file:
sudo nano / usr / bin / isticktoit_usb
/ usr / bin / isticktoit_usb # Add functions here
mkdir -p functions / ecm.usb0
# byte must be even
HOST = "48: 6f: 73: 74: 50: 43" # "HostPC"
SELF = "42: 61: 64: 55: 53: 42" # "BadUSB"
echo $ HOST> functions / ecm.usb0 / host_addr
echo $ SELF> functions / ecm.usb0 / dev_addr
ln -s functions / ecm.usb0 configs / c.1 /
# End functions
ls / sys / class / udc> UDC
#put this at the end of the file:
ifconfig $ N 10.0.0.1 netmask 255.255.255.252 up
route add -net default gw 10.0.0.2Save and exit, then go to host PC:
If there are problems with the automatic connection, disconnect and perform the following:
dmesg | grep cdc_ether
[13890.668557] cdc_ether 1-1: 1.2 eth0: register 'cdc_ether' at usb-0000: 00: 14.0-1, CDC Ethernet Device, 48: 6f: 73: 74: 50: 43
[13890.674117] usbcore: registered new interface driver cdc_ether
[13890.687619] cdc_ether 1-1: 1.2 enp0s20u1i2: renamed from eth0You can rename the adapter later, but for now let's call it enp0s20u1i2
sudo ifconfig enp0s20u1i2 10.0.0.2 netmask 255.255.255.252 upAnd connect via ssh to PI:
ssh 10.0.0.1 -l piKeyboard, Mouse, Joystick (HID)sudo nano / usr / bin / isticktoit_usb
/ usr / bin / isticktoit_usb # Add functions here
mkdir -p functions / hid.usb0
echo 1> functions / hid.0 / protocol
echo 1> functions / hid.0 / subclass
echo 8> functions / hid.0 / report_length
echo -ne \\ x05 \\ x01 \\ x09 \\ x06 \\ xa1 \\ x01 \\ x05 \\ x07 \\ x19 \\ xe0 \\ x29 \\ xe7 \ x15 \\ x00 \\ x25 \\ x01 \\ x75 \\ x01 \\ x95 \\ x08 \\ x81 \\ x02 \\ x95 \\ x01 \\ x75 \\ x08 \\ x81 \\ x03 \\ x95 \\ x05 \\ x75 \\ x01 \ \ x05 \\ x08 \\ x19 \\ x01 \\ x29 \\ x05 \\ x91 \\ x02 \\ x95 \\ x01 \\ x75 \\ x03 \\ x91 \\ x03 \\ x95 \\ x06 \\ x75 \\ x08 \\ x15 \\ x00 \\ x25 \\ x65 \\ x05 \\ x07 \\ x19 \\ x00 \\ x29 \\ x65 \\ x81 \\ x00 \ xc0> functions / hid.0 / report_desc
ln -s functions / hid.usb0 configs / c.1 /
# End functionsHere is the easiest way to send keystrokes to the device file:
echo "\ 0 \ 0 \ x4 \ 0 \ 0 \ 0 \ 0 \ 0"> / dev / hidg0 #press the A-button
echo "\ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0"> / dev / hidg0 #release all keysBut this is not the most practical way, so you should use
this example and download it to your PC. Then we extract it to the SD “raspberry” card and launch it.
On Pi:
cd PATH_TO_HARDPASS_REPO
make #compile the program
echo -n “hello world!” | sudo ./scan / dev / hidg0 1 2Here ‚1 'means the American keyboard layout, and“ 2 ″ - German-Austrian. The second number is needed to enter characters that are not on your keyboard (2 = Linux, 3 = Windows (although there are no drivers for this OS)).
Storage deviceIt is more difficult to do. You can only use the disk image file. The example creates a very small disk image file to save the host's ethernet configuration.
First, let's make a disc. This is quite a long process,
tutorial here .
Then again go to the configuration:
sudo nano / usr / bin / isticktoit_usb
/ usr / bin / isticktoit_usb # Add functions here
FILE = / home / pi / usbdisk.img
mkdir -p $ {FILE / img / d}
mount -o loop, ro, offset = 1048576 -t ext4 $ FILE $ {FILE / img / d}
mkdir -p functions / mass_storage.usb0
echo 1> functions / mass_storage.usb0 / stall
echo 0> functions / mass_storage.usb0 / lun.0 / cdrom
echo 0> functions / mass_storage.usb0 / lun.0 / ro
echo 0> functions / mass_storage.usb0 / lun.0 / nofua
echo $ FILE> functions / mass_storage.usb0 / lun.0 / file
ln -s functions / mass_storage.usb0 configs / c.1 /
# End functions
After that, a removable disk in FAT32 format should be available, which will be shown after the next connection of Pi to the main PC. To access files saved on Pi, you can unmount and then reconnect it somewhere else.
What else?
There are about 20 USB gadgets that can be emulated by the Linux kernel. Here is more
information about it !
Useful links:Tutorial by gbamanlibcomposite in the Kernel documentation->
hardpass - PiZero project (Keyboad emulation) <-
USBArmory Wiki Page