Emulating various devices with Pi Zero - how to do it?



    If you want to emulate a network adapter, keyboard, drive, and everything else with Pi Zero, all at the same time, you can do it all. LibComposite comes to the rescue, however, in this case you need to remember 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 header is in the sequel ( source ).

    Step 0 - configure the SD card


    Download and install the latest Raspbian Jessie distribution on a suitable SD card (should be capacious enough), and increase the root partition.

    Step 1 - The Kernel


    You need to use Kernel 4.4, which does not come by default with the Rasbian distribution. But updating is easy enough, you just need to execute this console command:

    sudo BRANCH = next rpi-update

    After that, do this:

    echo "dtoverlay = dwc2" | sudo tee -a /boot/config.txt
    echo "dwc2" | sudo tee -a / etc / modules


    Finally, activate the libcomposite driver:

    sudo echo “libcomposite” | sudo tee -a / etc / modules

    Step 2 - configure devices


    Now you need to decide which device to emulate - a network card, keyboard, or all of this together?

    We configure this using 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 here . Everything else will be shown on its basis.

    We create a configuration script.

    You will have to run it every time you start the device. Create the isticktoit_usb in / usr / bin / file using your favorite text editor. We write this:
    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 file


    After that, the script must be run automatically at startup. For best performance, you can create a systemd unit file, but for now, let's get around rc.local. (This is part of the old sysvinit system, but it runs on raspberry pi by default).

    Open the root /etc/rc.local and add a line (before !!!) a line containing the word "exit":

    sudo nano /etc/rc.local
    /etc/rc.local.../usr
    / bin / isticktoit_usb # libcomposite configuration
    exit


    Creating a gadget

    This is a global configuration, so it doesn't 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> bcdDbc 0d0b
    USB0cho 0x0100.0 bxd0e0b USB0cho 0x000 0x USB 0x0b0d0b0d0b0d0e0b
    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> UDC


    Ethernet adapter

    First 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
    # first byte of address 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 very end of the file:
    ifconfig $ N 10.0.0.1 netmask 255.255.255.252 up
    route add -net default gw 10.0.0.2


    We save and exit, then go to the host PC:
    If there are problems with automatic connection, disconnect and do this:

    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 eth0 You can


    rename the adapter even after, while let's call it enp0s20u1i2

    sudo ifconfig enp0s20u1i2 10.0.0.2 netmask 255.255.255.252 up

    and connect via ssh to PI:

    ssh 10.0.0.1 -l pi

    Keyboard, 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 functions


    Here 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 keys


    But 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 “raspberry” SD card and start it.

    On Pi:
    cd PATH_TO_HARDPASS_REPO
    make #compile the program
    echo -n "hello world!" | sudo ./scan / dev / hidg0 1 2


    Here ‚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 device

    This is harder to do. You can use only a disk image file. The example creates a very small disk image file to save the ethernet host configuration.
    First, let's make a disk. This is a fairly lengthy process, the tutorial is here .
    Then again we 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 there should be a removable disk in FAT32 format is available, which will be shown after the next Pi connection to the main PC. To access files stored on the Pi, you can run unmount and then reconnect it somewhere else.

    What else?



    In total there are about 20 USB gadgets that can be emulated by the Linux kernel. Here is more information about this !

    Useful links:
    Tutorial by gbaman
    libcomposite in the Kernel documentation
    -> hardpass - PiZero project (Keyboad emulation) <- USBArmory Wiki Page

    Also popular now: