Skip to content

Commit

Permalink
Land #19518, Add support for RISC-V 32-bit / 64-bit Little Endian pay…
Browse files Browse the repository at this point in the history
…loads
  • Loading branch information
dwelch-r7 authored Nov 1, 2024
2 parents 9e0b0f5 + 8ba1034 commit 1bfa075
Show file tree
Hide file tree
Showing 16 changed files with 831 additions and 0 deletions.
98 changes: 98 additions & 0 deletions data/templates/src/elf/dll/elf_dll_riscv32le_template.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
; build with:
; nasm elf_dll_riscv32le_template.s -f bin -o template_riscv32le_linux_dll.bin

BITS 32

org 0

ehdr:
db 0x7f, "ELF", 1, 1, 1, 0 ; e_ident
db 0, 0, 0, 0, 0, 0, 0, 0
dw 3 ; e_type = ET_DYN
dw 0xF3 ; e_machine = EM_RISCV
dd 1 ; e_version = EV_CURRENT
dd _start ; e_entry = _start
dd phdr - $$ ; e_phoff
dd shdr - $$ ; e_shoff
dd 0 ; e_flags
dw ehdrsize ; e_ehsize
dw phdrsize ; e_phentsize
dw 2 ; e_phnum
dw shentsize ; e_shentsize
dw 2 ; e_shnum
dw 1 ; e_shstrndx

ehdrsize equ $ - ehdr

phdr:
dd 1 ; p_type = PT_LOAD
dd 0 ; p_offset
dd $$ ; p_vaddr
dd $$ ; p_paddr
dd 0xDEADBEEF ; p_filesz
dd 0xDEADBEEF ; p_memsz
dd 7 ; p_flags = rwx
dd 0x1000 ; p_align

phdrsize equ $ - phdr

dd 2 ; p_type = PT_DYNAMIC
dd 7 ; p_flags = rwx
dd dynsection ; p_offset
dd dynsection ; p_vaddr
dd dynsection ; p_vaddr
dd dynsz ; p_filesz
dd dynsz ; p_memsz
dd 0x1000 ; p_align

shdr:
dd 1 ; sh_name
dd 6 ; sh_type = SHT_DYNAMIC
dd 0 ; sh_flags
dd dynsection ; sh_addr
dd dynsection ; sh_offset
dd dynsz ; sh_size
dd 0 ; sh_link
dd 0 ; sh_info
dd 8 ; sh_addralign
dd 7 ; sh_entsize
shentsize equ $ - shdr
dd 0 ; sh_name
dd 3 ; sh_type = SHT_STRTAB
dd 0 ; sh_flags
dd strtab ; sh_addr
dd strtab ; sh_offset
dd strtabsz ; sh_size
dd 0 ; sh_link
dd 0 ; sh_info
dd 0 ; sh_addralign
dd 0 ; sh_entsize

dynsection:
; DT_INIT
dd 0x0c
dd _start
; DT_STRTAB
dd 0x05
dd strtab
; DT_SYMTAB
dd 0x06
dd strtab
; DT_STRSZ
dd 0x0a
dd 0
; DT_SYMENT
dd 0x0b
dd 0
; DT_NULL
dd 0x00
dd 0
dynsz equ $ - dynsection

strtab:
db 0
db 0
strtabsz equ $ - strtab

global _start
_start:
99 changes: 99 additions & 0 deletions data/templates/src/elf/dll/elf_dll_riscv64le_template.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
; build with:
; nasm elf_dll_riscv64le_template.s -f bin -o template_riscv64le_linux_dll.bin

BITS 64

org 0

ehdr: ; Elf64_Ehdr
db 0x7F, "ELF", 2, 1, 1, 0 ; e_ident
db 0, 0, 0, 0, 0, 0, 0, 0 ;
dw 3 ; e_type = ET_DYN
dw 0xF3 ; e_machine = RISCV
dd 1 ; e_version
dq _start ; e_entry
dq phdr - $$ ; e_phoff
dq shdr - $$ ; e_shoff
dd 0 ; e_flags
dw ehdrsize ; e_ehsize
dw phdrsize ; e_phentsize
dw 2 ; e_phnum
dw shentsize ; e_shentsize
dw 2 ; e_shnum
dw 1 ; e_shstrndx

ehdrsize equ $ - ehdr

phdr: ; Elf32_Phdr
dd 1 ; p_type = PT_LOAD
dd 7 ; p_flags = rwx
dq 0 ; p_offset
dq $$ ; p_vaddr
dq $$ ; p_paddr
dq 0xDEADBEEF ; p_filesz
dq 0xDEADBEEF ; p_memsz
dq 0x1000 ; p_align

phdrsize equ $ - phdr
dd 2 ; p_type = PT_DYNAMIC
dd 7 ; p_flags = rwx
dq dynsection ; p_offset
dq dynsection ; p_vaddr
dq dynsection ; p_vaddr
dq dynsz ; p_filesz
dq dynsz ; p_memsz
dq 0x1000 ; p_align

shdr:
dd 1 ; sh_name
dd 6 ; sh_type = SHT_DYNAMIC
dq 0 ; sh_flags
dq dynsection ; sh_addr
dq dynsection ; sh_offset
dq dynsz ; sh_size
dd 0 ; sh_link
dd 0 ; sh_info
dq 8 ; sh_addralign
dq 7 ; sh_entsize
shentsize equ $ - shdr
dd 0 ; sh_name
dd 3 ; sh_type = SHT_STRTAB
dq 0 ; sh_flags
dq strtab ; sh_addr
dq strtab ; sh_offset
dq strtabsz ; sh_size
dd 0 ; sh_link
dd 0 ; sh_info
dq 0 ; sh_addralign
dq 0 ; sh_entsize

dynsection:
; DT_INIT
dq 0x0c
dq _start
; DT_STRTAB
dq 0x05
dq strtab
; DT_SYMTAB
dq 0x06
dq strtab
; DT_STRSZ
dq 0x0a
dq 0
; DT_SYMENT
dq 0x0b
dq 0
; DT_NULL
dq 0x00
dq 0

dynsz equ $ - dynsection

strtab:
db 0
db 0
strtabsz equ $ - strtab

align 16
global _start
_start:
42 changes: 42 additions & 0 deletions data/templates/src/elf/exe/elf_riscv32le_template.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
; build with:
; nasm elf_riscv32le_template.s -f bin -o template_riscv32le_linux.bin

BITS 32

org 0x00010000

ehdr: ; Elf32_Ehdr
db 0x7F, "ELF", 1, 1, 1, 0 ; e_ident
db 0, 0, 0, 0, 0, 0, 0, 0 ;
dw 2 ; e_type = ET_EXEC for an executable
dw 0xF3 ; e_machine = RISCV
dd 1 ; e_version
dd _start ; e_entry
dd phdr - $$ ; e_phoff
dd 0 ; e_shoff
dd 0 ; e_flags
dw ehdrsize ; e_ehsize
dw phdrsize ; e_phentsize
dw 1 ; e_phnum
dw 0 ; e_shentsize
dw 0 ; e_shnum
dw 0 ; e_shstrndx

ehdrsize equ $ - ehdr

phdr: ; Elf32_Phdr
dd 1 ; p_type = PT_LOAD
dd 0 ; p_offset
dd $$ ; p_vaddr
dd $$ ; p_paddr
dd 0xDEADBEEF ; p_filesz
dd 0xDEADBEEF ; p_memsz
dd 7 ; p_flags = rwx
dd 0x1000 ; p_align

phdrsize equ $ - phdr

global _start

_start:

42 changes: 42 additions & 0 deletions data/templates/src/elf/exe/elf_riscv64le_template.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
; build with:
; nasm elf_riscv64le_template.s -f bin -o template_riscv64le_linux.bin

BITS 64

org 0x00400000

ehdr: ; Elf32_Ehdr
db 0x7F, "ELF", 2, 1, 1, 0 ; e_ident
db 0, 0, 0, 0, 0, 0, 0, 0 ;
dw 2 ; e_type = ET_EXEC for an executable
dw 0xF3 ; e_machine = RISCV
dd 1 ; e_version
dq _start ; e_entry
dq phdr - $$ ; e_phoff
dq 0 ; e_shoff
dd 0 ; e_flags
dw ehdrsize ; e_ehsize
dw phdrsize ; e_phentsize
dw 1 ; e_phnum
dw 0 ; e_shentsize
dw 0 ; e_shnum
dw 0 ; e_shstrndx

ehdrsize equ $ - ehdr

phdr: ; Elf32_Phdr
dd 1 ; p_type = PT_LOAD
dd 7 ; p_flags = rwx
dq 0 ; p_offset
dq $$ ; p_vaddr
dq $$ ; p_paddr
dq 0xDEADBEEF ; p_filesz
dq 0xDEADBEEF ; p_memsz
dq 0x1000 ; p_align

phdrsize equ $ - phdr

global _start

_start:

Binary file added data/templates/template_riscv32le_linux.bin
Binary file not shown.
Binary file added data/templates/template_riscv32le_linux_dll.bin
Binary file not shown.
Binary file added data/templates/template_riscv64le_linux.bin
Binary file not shown.
Binary file added data/templates/template_riscv64le_linux_dll.bin
Binary file not shown.
67 changes: 67 additions & 0 deletions lib/msf/util/exe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,21 @@ def self.to_executable(framework, arch, plat, code = '', opts = {})
end
# XXX: Add remaining MIPSLE systems here
end

if arch.index(ARCH_RISCV32LE)
if plat.index(Msf::Module::Platform::Linux)
return to_linux_riscv32le_elf(framework, code)
end
# TODO: Add remaining RISCV32LE systems here
end

if arch.index(ARCH_RISCV64LE)
if plat.index(Msf::Module::Platform::Linux)
return to_linux_riscv64le_elf(framework, code)
end
# TODO: Add remaining RISCV64LE systems here
end

nil
end

Expand Down Expand Up @@ -1239,6 +1254,50 @@ def self.to_linux_mipsbe_elf(framework, code, opts = {})
to_exe_elf(framework, opts, "template_mipsbe_linux.bin", code, true)
end

# Create a RISC-V 64-bit LE Linux ELF containing the payload provided in +code+
#
# @param framework [Msf::Framework]
# @param code [String]
# @param opts [Hash]
# @option [String] :template
# @return [String] Returns an elf
def self.to_linux_riscv64le_elf(framework, code, opts = {})
to_exe_elf(framework, opts, "template_riscv64le_linux.bin", code)
end

# Create a RISC-V 64-bit LE Linux ELF_DYN containing the payload provided in +code+
#
# @param framework [Msf::Framework]
# @param code [String]
# @param opts [Hash]
# @option [String] :template
# @return [String] Returns an elf
def self.to_linux_riscv64le_elf_dll(framework, code, opts = {})
to_exe_elf(framework, opts, "template_riscv64le_linux_dll.bin", code)
end

# Create a RISC-V 32-bit LE Linux ELF containing the payload provided in +code+
#
# @param framework [Msf::Framework]
# @param code [String]
# @param opts [Hash]
# @option [String] :template
# @return [String] Returns an elf
def self.to_linux_riscv32le_elf(framework, code, opts = {})
to_exe_elf(framework, opts, "template_riscv32le_linux.bin", code)
end

# Create a RISC-V 32-bit LE Linux ELF_DYN containing the payload provided in +code+
#
# @param framework [Msf::Framework]
# @param code [String]
# @param opts [Hash]
# @option [String] :template
# @return [String] Returns an elf
def self.to_linux_riscv32le_elf_dll(framework, code, opts = {})
to_exe_elf(framework, opts, "template_riscv32le_linux_dll.bin", code)
end

# self.to_exe_vba
#
# @param exes [String]
Expand Down Expand Up @@ -2125,6 +2184,10 @@ def self.to_executable_fmt(framework, arch, plat, code, fmt, exeopts)
to_linux_mipsbe_elf(framework, code, exeopts)
when ARCH_MIPSLE
to_linux_mipsle_elf(framework, code, exeopts)
when ARCH_RISCV32LE
to_linux_riscv32le_elf(framework, code, exeopts)
when ARCH_RISCV64LE
to_linux_riscv64le_elf(framework, code, exeopts)
end
elsif plat && plat.index(Msf::Module::Platform::BSD)
case arch
Expand Down Expand Up @@ -2153,6 +2216,10 @@ def self.to_executable_fmt(framework, arch, plat, code, fmt, exeopts)
to_linux_armle_elf_dll(framework, code, exeopts)
when ARCH_AARCH64
to_linux_aarch64_elf_dll(framework, code, exeopts)
when ARCH_RISCV32LE
to_linux_riscv32le_elf_dll(framework, code, exeopts)
when ARCH_RISCV64LE
to_linux_riscv64le_elf_dll(framework, code, exeopts)
end
end
when 'macho', 'osx-app'
Expand Down
Loading

0 comments on commit 1bfa075

Please sign in to comment.