Skip to content

Commit 1bfa075

Browse files
authored
Land rapid7#19518, Add support for RISC-V 32-bit / 64-bit Little Endian payloads
2 parents 9e0b0f5 + 8ba1034 commit 1bfa075

File tree

16 files changed

+831
-0
lines changed

16 files changed

+831
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
; build with:
2+
; nasm elf_dll_riscv32le_template.s -f bin -o template_riscv32le_linux_dll.bin
3+
4+
BITS 32
5+
6+
org 0
7+
8+
ehdr:
9+
db 0x7f, "ELF", 1, 1, 1, 0 ; e_ident
10+
db 0, 0, 0, 0, 0, 0, 0, 0
11+
dw 3 ; e_type = ET_DYN
12+
dw 0xF3 ; e_machine = EM_RISCV
13+
dd 1 ; e_version = EV_CURRENT
14+
dd _start ; e_entry = _start
15+
dd phdr - $$ ; e_phoff
16+
dd shdr - $$ ; e_shoff
17+
dd 0 ; e_flags
18+
dw ehdrsize ; e_ehsize
19+
dw phdrsize ; e_phentsize
20+
dw 2 ; e_phnum
21+
dw shentsize ; e_shentsize
22+
dw 2 ; e_shnum
23+
dw 1 ; e_shstrndx
24+
25+
ehdrsize equ $ - ehdr
26+
27+
phdr:
28+
dd 1 ; p_type = PT_LOAD
29+
dd 0 ; p_offset
30+
dd $$ ; p_vaddr
31+
dd $$ ; p_paddr
32+
dd 0xDEADBEEF ; p_filesz
33+
dd 0xDEADBEEF ; p_memsz
34+
dd 7 ; p_flags = rwx
35+
dd 0x1000 ; p_align
36+
37+
phdrsize equ $ - phdr
38+
39+
dd 2 ; p_type = PT_DYNAMIC
40+
dd 7 ; p_flags = rwx
41+
dd dynsection ; p_offset
42+
dd dynsection ; p_vaddr
43+
dd dynsection ; p_vaddr
44+
dd dynsz ; p_filesz
45+
dd dynsz ; p_memsz
46+
dd 0x1000 ; p_align
47+
48+
shdr:
49+
dd 1 ; sh_name
50+
dd 6 ; sh_type = SHT_DYNAMIC
51+
dd 0 ; sh_flags
52+
dd dynsection ; sh_addr
53+
dd dynsection ; sh_offset
54+
dd dynsz ; sh_size
55+
dd 0 ; sh_link
56+
dd 0 ; sh_info
57+
dd 8 ; sh_addralign
58+
dd 7 ; sh_entsize
59+
shentsize equ $ - shdr
60+
dd 0 ; sh_name
61+
dd 3 ; sh_type = SHT_STRTAB
62+
dd 0 ; sh_flags
63+
dd strtab ; sh_addr
64+
dd strtab ; sh_offset
65+
dd strtabsz ; sh_size
66+
dd 0 ; sh_link
67+
dd 0 ; sh_info
68+
dd 0 ; sh_addralign
69+
dd 0 ; sh_entsize
70+
71+
dynsection:
72+
; DT_INIT
73+
dd 0x0c
74+
dd _start
75+
; DT_STRTAB
76+
dd 0x05
77+
dd strtab
78+
; DT_SYMTAB
79+
dd 0x06
80+
dd strtab
81+
; DT_STRSZ
82+
dd 0x0a
83+
dd 0
84+
; DT_SYMENT
85+
dd 0x0b
86+
dd 0
87+
; DT_NULL
88+
dd 0x00
89+
dd 0
90+
dynsz equ $ - dynsection
91+
92+
strtab:
93+
db 0
94+
db 0
95+
strtabsz equ $ - strtab
96+
97+
global _start
98+
_start:
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
; build with:
2+
; nasm elf_dll_riscv64le_template.s -f bin -o template_riscv64le_linux_dll.bin
3+
4+
BITS 64
5+
6+
org 0
7+
8+
ehdr: ; Elf64_Ehdr
9+
db 0x7F, "ELF", 2, 1, 1, 0 ; e_ident
10+
db 0, 0, 0, 0, 0, 0, 0, 0 ;
11+
dw 3 ; e_type = ET_DYN
12+
dw 0xF3 ; e_machine = RISCV
13+
dd 1 ; e_version
14+
dq _start ; e_entry
15+
dq phdr - $$ ; e_phoff
16+
dq shdr - $$ ; e_shoff
17+
dd 0 ; e_flags
18+
dw ehdrsize ; e_ehsize
19+
dw phdrsize ; e_phentsize
20+
dw 2 ; e_phnum
21+
dw shentsize ; e_shentsize
22+
dw 2 ; e_shnum
23+
dw 1 ; e_shstrndx
24+
25+
ehdrsize equ $ - ehdr
26+
27+
phdr: ; Elf32_Phdr
28+
dd 1 ; p_type = PT_LOAD
29+
dd 7 ; p_flags = rwx
30+
dq 0 ; p_offset
31+
dq $$ ; p_vaddr
32+
dq $$ ; p_paddr
33+
dq 0xDEADBEEF ; p_filesz
34+
dq 0xDEADBEEF ; p_memsz
35+
dq 0x1000 ; p_align
36+
37+
phdrsize equ $ - phdr
38+
dd 2 ; p_type = PT_DYNAMIC
39+
dd 7 ; p_flags = rwx
40+
dq dynsection ; p_offset
41+
dq dynsection ; p_vaddr
42+
dq dynsection ; p_vaddr
43+
dq dynsz ; p_filesz
44+
dq dynsz ; p_memsz
45+
dq 0x1000 ; p_align
46+
47+
shdr:
48+
dd 1 ; sh_name
49+
dd 6 ; sh_type = SHT_DYNAMIC
50+
dq 0 ; sh_flags
51+
dq dynsection ; sh_addr
52+
dq dynsection ; sh_offset
53+
dq dynsz ; sh_size
54+
dd 0 ; sh_link
55+
dd 0 ; sh_info
56+
dq 8 ; sh_addralign
57+
dq 7 ; sh_entsize
58+
shentsize equ $ - shdr
59+
dd 0 ; sh_name
60+
dd 3 ; sh_type = SHT_STRTAB
61+
dq 0 ; sh_flags
62+
dq strtab ; sh_addr
63+
dq strtab ; sh_offset
64+
dq strtabsz ; sh_size
65+
dd 0 ; sh_link
66+
dd 0 ; sh_info
67+
dq 0 ; sh_addralign
68+
dq 0 ; sh_entsize
69+
70+
dynsection:
71+
; DT_INIT
72+
dq 0x0c
73+
dq _start
74+
; DT_STRTAB
75+
dq 0x05
76+
dq strtab
77+
; DT_SYMTAB
78+
dq 0x06
79+
dq strtab
80+
; DT_STRSZ
81+
dq 0x0a
82+
dq 0
83+
; DT_SYMENT
84+
dq 0x0b
85+
dq 0
86+
; DT_NULL
87+
dq 0x00
88+
dq 0
89+
90+
dynsz equ $ - dynsection
91+
92+
strtab:
93+
db 0
94+
db 0
95+
strtabsz equ $ - strtab
96+
97+
align 16
98+
global _start
99+
_start:
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
; build with:
2+
; nasm elf_riscv32le_template.s -f bin -o template_riscv32le_linux.bin
3+
4+
BITS 32
5+
6+
org 0x00010000
7+
8+
ehdr: ; Elf32_Ehdr
9+
db 0x7F, "ELF", 1, 1, 1, 0 ; e_ident
10+
db 0, 0, 0, 0, 0, 0, 0, 0 ;
11+
dw 2 ; e_type = ET_EXEC for an executable
12+
dw 0xF3 ; e_machine = RISCV
13+
dd 1 ; e_version
14+
dd _start ; e_entry
15+
dd phdr - $$ ; e_phoff
16+
dd 0 ; e_shoff
17+
dd 0 ; e_flags
18+
dw ehdrsize ; e_ehsize
19+
dw phdrsize ; e_phentsize
20+
dw 1 ; e_phnum
21+
dw 0 ; e_shentsize
22+
dw 0 ; e_shnum
23+
dw 0 ; e_shstrndx
24+
25+
ehdrsize equ $ - ehdr
26+
27+
phdr: ; Elf32_Phdr
28+
dd 1 ; p_type = PT_LOAD
29+
dd 0 ; p_offset
30+
dd $$ ; p_vaddr
31+
dd $$ ; p_paddr
32+
dd 0xDEADBEEF ; p_filesz
33+
dd 0xDEADBEEF ; p_memsz
34+
dd 7 ; p_flags = rwx
35+
dd 0x1000 ; p_align
36+
37+
phdrsize equ $ - phdr
38+
39+
global _start
40+
41+
_start:
42+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
; build with:
2+
; nasm elf_riscv64le_template.s -f bin -o template_riscv64le_linux.bin
3+
4+
BITS 64
5+
6+
org 0x00400000
7+
8+
ehdr: ; Elf32_Ehdr
9+
db 0x7F, "ELF", 2, 1, 1, 0 ; e_ident
10+
db 0, 0, 0, 0, 0, 0, 0, 0 ;
11+
dw 2 ; e_type = ET_EXEC for an executable
12+
dw 0xF3 ; e_machine = RISCV
13+
dd 1 ; e_version
14+
dq _start ; e_entry
15+
dq phdr - $$ ; e_phoff
16+
dq 0 ; e_shoff
17+
dd 0 ; e_flags
18+
dw ehdrsize ; e_ehsize
19+
dw phdrsize ; e_phentsize
20+
dw 1 ; e_phnum
21+
dw 0 ; e_shentsize
22+
dw 0 ; e_shnum
23+
dw 0 ; e_shstrndx
24+
25+
ehdrsize equ $ - ehdr
26+
27+
phdr: ; Elf32_Phdr
28+
dd 1 ; p_type = PT_LOAD
29+
dd 7 ; p_flags = rwx
30+
dq 0 ; p_offset
31+
dq $$ ; p_vaddr
32+
dq $$ ; p_paddr
33+
dq 0xDEADBEEF ; p_filesz
34+
dq 0xDEADBEEF ; p_memsz
35+
dq 0x1000 ; p_align
36+
37+
phdrsize equ $ - phdr
38+
39+
global _start
40+
41+
_start:
42+
84 Bytes
Binary file not shown.
246 Bytes
Binary file not shown.
120 Bytes
Binary file not shown.
416 Bytes
Binary file not shown.

lib/msf/util/exe.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,21 @@ def self.to_executable(framework, arch, plat, code = '', opts = {})
194194
end
195195
# XXX: Add remaining MIPSLE systems here
196196
end
197+
198+
if arch.index(ARCH_RISCV32LE)
199+
if plat.index(Msf::Module::Platform::Linux)
200+
return to_linux_riscv32le_elf(framework, code)
201+
end
202+
# TODO: Add remaining RISCV32LE systems here
203+
end
204+
205+
if arch.index(ARCH_RISCV64LE)
206+
if plat.index(Msf::Module::Platform::Linux)
207+
return to_linux_riscv64le_elf(framework, code)
208+
end
209+
# TODO: Add remaining RISCV64LE systems here
210+
end
211+
197212
nil
198213
end
199214

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

1257+
# Create a RISC-V 64-bit LE Linux ELF containing the payload provided in +code+
1258+
#
1259+
# @param framework [Msf::Framework]
1260+
# @param code [String]
1261+
# @param opts [Hash]
1262+
# @option [String] :template
1263+
# @return [String] Returns an elf
1264+
def self.to_linux_riscv64le_elf(framework, code, opts = {})
1265+
to_exe_elf(framework, opts, "template_riscv64le_linux.bin", code)
1266+
end
1267+
1268+
# Create a RISC-V 64-bit LE Linux ELF_DYN containing the payload provided in +code+
1269+
#
1270+
# @param framework [Msf::Framework]
1271+
# @param code [String]
1272+
# @param opts [Hash]
1273+
# @option [String] :template
1274+
# @return [String] Returns an elf
1275+
def self.to_linux_riscv64le_elf_dll(framework, code, opts = {})
1276+
to_exe_elf(framework, opts, "template_riscv64le_linux_dll.bin", code)
1277+
end
1278+
1279+
# Create a RISC-V 32-bit LE Linux ELF containing the payload provided in +code+
1280+
#
1281+
# @param framework [Msf::Framework]
1282+
# @param code [String]
1283+
# @param opts [Hash]
1284+
# @option [String] :template
1285+
# @return [String] Returns an elf
1286+
def self.to_linux_riscv32le_elf(framework, code, opts = {})
1287+
to_exe_elf(framework, opts, "template_riscv32le_linux.bin", code)
1288+
end
1289+
1290+
# Create a RISC-V 32-bit LE Linux ELF_DYN containing the payload provided in +code+
1291+
#
1292+
# @param framework [Msf::Framework]
1293+
# @param code [String]
1294+
# @param opts [Hash]
1295+
# @option [String] :template
1296+
# @return [String] Returns an elf
1297+
def self.to_linux_riscv32le_elf_dll(framework, code, opts = {})
1298+
to_exe_elf(framework, opts, "template_riscv32le_linux_dll.bin", code)
1299+
end
1300+
12421301
# self.to_exe_vba
12431302
#
12441303
# @param exes [String]
@@ -2125,6 +2184,10 @@ def self.to_executable_fmt(framework, arch, plat, code, fmt, exeopts)
21252184
to_linux_mipsbe_elf(framework, code, exeopts)
21262185
when ARCH_MIPSLE
21272186
to_linux_mipsle_elf(framework, code, exeopts)
2187+
when ARCH_RISCV32LE
2188+
to_linux_riscv32le_elf(framework, code, exeopts)
2189+
when ARCH_RISCV64LE
2190+
to_linux_riscv64le_elf(framework, code, exeopts)
21282191
end
21292192
elsif plat && plat.index(Msf::Module::Platform::BSD)
21302193
case arch
@@ -2153,6 +2216,10 @@ def self.to_executable_fmt(framework, arch, plat, code, fmt, exeopts)
21532216
to_linux_armle_elf_dll(framework, code, exeopts)
21542217
when ARCH_AARCH64
21552218
to_linux_aarch64_elf_dll(framework, code, exeopts)
2219+
when ARCH_RISCV32LE
2220+
to_linux_riscv32le_elf_dll(framework, code, exeopts)
2221+
when ARCH_RISCV64LE
2222+
to_linux_riscv64le_elf_dll(framework, code, exeopts)
21562223
end
21572224
end
21582225
when 'macho', 'osx-app'

0 commit comments

Comments
 (0)