Skip to content

Commit 4f53261

Browse files
authored
Merge pull request #11 from shaymargolis/more-stuff
shellblocks: primitive: Use mno-shared to avoid use of $gp
2 parents 3bdbf88 + 3a49179 commit 4f53261

6 files changed

Lines changed: 38 additions & 12 deletions

File tree

shellblocks/primitives/print.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class ShellcodePrimitivePrint(ShellcodePrimitive):
55
def __init__(self, nickname: str, print_function: int, print_string: str):
66
super().__init__(
77
nickname,
8-
["print.S", "utils.h"],
8+
["print.S", "utils_asm.h"],
99
"print.S",
1010
"print.h"
1111
)

shellblocks/shellcode_primitive.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def generate(self, path: Path):
5050
"mips-linux-gnu-gcc-9",
5151
"-nostdlib",
5252
"-ffreestanding",
53+
"-mno-shared",
5354
"-c", self.sources[0],
5455
"-o", "final.o",
5556
"-O3"

shellblocks/src/goto.S

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "goto.h"
22

3+
.set noreorder
4+
35
.global start
46
start:
57
lui $v0, %hi(GOTO_ADDRESS)

shellblocks/src/print.S

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#include "print.h"
2+
#include "utils_asm.h"
3+
4+
.set noreorder
25

36
.global start
47
start:
58
// Save $ra
69
addiu $sp, -4
710
sw $ra, 0($sp)
811

9-
// Get $pc using bal
10-
bal code
11-
nop
12-
code:
13-
// bal somehow compiles to "bal + nop" so 2 opcodes
14-
addiu $a0, $ra, (print_string - code + 4)
15-
nop
12+
// Calculate address of `print_string`
13+
// relative to current $pc
14+
GET_PC($v0)
15+
GET_ADDRESS($a0, print_string, $v0)
1616

1717
lui $v0, %hi(PRINT_FUNCTION_ADDRESS)
1818
addiu $v0, %lo(PRINT_FUNCTION_ADDRESS)
@@ -27,10 +27,12 @@ code:
2727
// Jump over the printed string, to ensure we can run
2828
// another primitive after this one.
2929
b end_of_code
30+
nop
3031

3132
print_string:
3233
.asciiz PRINT_STRING
3334
.align 2
3435

3536
end_of_code:
3637
nop
38+

shellblocks/src/utils_asm.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef SHELLCODE_BLOCKS_UTILS_ASM_H
2+
#define SHELLCODE_BLOCKS_UTILS_ASM_H
3+
4+
#define GET_PC(dst) \
5+
bal get_ip_reference; \
6+
nop; \
7+
get_ip_reference: \
8+
move dst, $ra
9+
10+
#define GET_ADDRESS(dst, label, base) \
11+
move dst, base; \
12+
addiu dst, (label - get_ip_reference)
13+
14+
#endif // !SHELLCODE_BLOCKS_UTILS_ASM_H

tests/test_jump_hook.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
SECTOR_SIZE = 0x2000
1010

1111

12+
@pytest.mark.parametrize('shellcode_run_addr', [
13+
(0x82000010),
14+
(0xbc100010),
15+
(0xbcd00010),
16+
(0x91100118),
17+
])
1218
@pytest.mark.parametrize('jump_hook_location', [
1319
0x81000010,
1420
0xbc000010,
@@ -21,11 +27,12 @@
2127
0xbcf00070,
2228
0x910f0218,
2329
])
24-
def test_jump_hook_sanity(temp_dir_path, jump_hook_location, jump_hook_goto):
30+
def test_jump_hook_sanity(temp_dir_path, shellcode_run_addr, jump_hook_location, jump_hook_goto):
2531
# Generate shellcode
2632
# ------------------
2733
shellcode_address = 0xbfc00000
2834
jump_hook_sector = int(jump_hook_location/SECTOR_SIZE) * SECTOR_SIZE
35+
shellcode_run_sector = int(shellcode_run_addr/SECTOR_SIZE) * SECTOR_SIZE
2936

3037
step = ShellcodeStep(
3138
"first_step",
@@ -57,15 +64,15 @@ def test_jump_hook_sanity(temp_dir_path, jump_hook_location, jump_hook_goto):
5764
# --------------------
5865

5966
mu = Uc(UC_ARCH_MIPS, UC_MODE_32 | UC_MODE_BIG_ENDIAN)
60-
mu.mem_map(shellcode_address, 0x2000)
67+
mu.mem_map(shellcode_run_sector, 0x2000)
6168
mu.mem_map(jump_hook_sector, 0x2000)
6269

6370
# write machine code to be emulated to memory
64-
mu.mem_write(shellcode_address, shellcode)
71+
mu.mem_write(shellcode_run_addr, shellcode)
6572
mu.mem_write(jump_hook_sector, b"\x00" * 0x1000)
6673

6774
# emulate code in infinite time & unlimited instructions
68-
mu.emu_start(shellcode_address, shellcode_address + len(shellcode))
75+
mu.emu_start(shellcode_run_addr, shellcode_run_addr + len(shellcode))
6976

7077
assert mu.mem_read(jump_hook_location, len(EXPECTED_HOOK)) == EXPECTED_HOOK
7178
assert mu.mem_read(jump_hook_location+len(EXPECTED_HOOK), 1) == (b"\x00")

0 commit comments

Comments
 (0)