forked from open-education-hub/operating-systems
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
data/lab/arena: Add exec-shellcode challenge
A challenge to exercise working with mmap() and memory copying. Signed-off-by: Razvan Deaconescu <[email protected]>
- Loading branch information
Showing
18 changed files
with
456 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
content/chapters/data/lab/solution/exec-shellcode/src/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
CFLAGS ?= -Wall -Wextra | ||
CPPFLAGS ?= -I../utils | ||
|
||
.PHONY: all clean | ||
|
||
all: exec_shellcode | ||
|
||
exec_shellcode: exec_shellcode.o ../utils/log/log.o | ||
|
||
../utils/log/log.o: ../utils/log/log.c ../utils/log/log.h | ||
|
||
clean: | ||
-rm -f exec_shellcode exec_shellcode | ||
-rm -f *~ |
48 changes: 48 additions & 0 deletions
48
content/chapters/data/lab/solution/exec-shellcode/src/exec_shellcode.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <sys/mman.h> | ||
|
||
#include "utils.h" | ||
|
||
static void *shellcode_mapping; | ||
|
||
static void usage(const char * const argv0) | ||
{ | ||
fprintf(stderr, "Usage: %s shellcode_file\n", argv0); | ||
} | ||
|
||
static void create_shellcode_mapping(void) | ||
{ | ||
/* TODO 2: Create mapping to fit the shellcode. */ | ||
shellcode_mapping = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | ||
DIE(shellcode_mapping == MAP_FAILED, "mmap"); | ||
} | ||
|
||
static void read_shellcode(const char * const fname) | ||
{ | ||
/* TODO 8: Read content from file in shellcode. */ | ||
FILE *f; | ||
|
||
f = fopen(fname, "rb"); | ||
DIE(f == NULL, "fopen"); | ||
|
||
fread(shellcode_mapping, sysconf(_SC_PAGESIZE), 1, f); | ||
|
||
fclose(f); | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
if (argc != 2) { | ||
usage(argv[0]); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
create_shellcode_mapping(); | ||
read_shellcode(argv[1]); | ||
|
||
((void (*)(void)) shellcode_mapping)(); | ||
|
||
return 0; | ||
} |
15 changes: 15 additions & 0 deletions
15
content/chapters/data/lab/solution/exec-shellcode/tests/brk.asm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
BITS 64 | ||
; call brk(0) | ||
; rax <- 12 (__NR_brj) | ||
; rdi <- 0 | ||
; TODO 3: Make brk syscall. | ||
mov rax, 12 | ||
xor rdi,rdi | ||
syscall | ||
|
||
; call exit_group(0) | ||
; rax <- 231 (__NR_exit_group) | ||
; rdi <- 0 (exit status) | ||
mov rax, 231 | ||
xor rdi, rdi | ||
syscall |
4 changes: 4 additions & 0 deletions
4
content/chapters/data/lab/support/exec-shellcode/tests/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/brk | ||
/getpid | ||
/helloworld | ||
/openfile |
34 changes: 34 additions & 0 deletions
34
content/chapters/data/lab/support/exec-shellcode/tests/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
SRC_PATH ?= ../src | ||
FULL_SRC_PATH = $(realpath $(SRC_PATH)) | ||
CPPFLAGS = -I. -I$(realpath $(SRC_PATH)) -I../utils | ||
CFLAGS = -Wall -Wextra | ||
# Remove the line below to disable debugging support. | ||
CFLAGS += -g -O0 | ||
|
||
SRCS = $(wildcard *.asm) | ||
SHELLCODES = $(patsubst %.asm,%,$(SRCS)) | ||
|
||
.PHONY: all src check lint clean | ||
|
||
all: $(SHELLCODES) src | ||
|
||
$(SHELLCODES): %:%.asm | src | ||
nasm -o $@ $< | ||
|
||
src: | ||
make -C $(FULL_SRC_PATH) | ||
|
||
check: $(SHELLCODES) | ||
make -C $(FULL_SRC_PATH) clean | ||
make clean | ||
make -i SRC_PATH=$(FULL_SRC_PATH) | ||
./run_all_tests.sh | ||
|
||
lint: | ||
-cd .. && checkpatch.pl -f src/*.c | ||
-cd .. && checkpatch.pl -f tests/*.sh | ||
-cd .. && cpplint --recursive src/ | ||
-cd .. && shellcheck tests/*.sh | ||
|
||
clean: | ||
-rm -f *~ |
12 changes: 12 additions & 0 deletions
12
content/chapters/data/lab/support/exec-shellcode/tests/brk.asm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
BITS 64 | ||
; call brk(0) | ||
; rax <- 12 (__NR_brj) | ||
; rdi <- 0 | ||
; TODO 3: Make brk syscall. | ||
|
||
; call exit_group(0) | ||
; rax <- 231 (__NR_exit_group) | ||
; rdi <- 0 (exit status) | ||
mov rax, 231 | ||
xor rdi, rdi | ||
syscall |
12 changes: 12 additions & 0 deletions
12
content/chapters/data/lab/support/exec-shellcode/tests/getpid.asm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
BITS 64 | ||
; call getpid() | ||
; rax <- 39 (__NR_write) | ||
mov rax, 39 | ||
syscall | ||
|
||
; call exit_group(0) | ||
; rax <- 231 (__NR_exit_group) | ||
; rdi <- 0 (exit status) | ||
mov rax, 231 | ||
xor rdi, rdi | ||
syscall |
41 changes: 41 additions & 0 deletions
41
content/chapters/data/lab/support/exec-shellcode/tests/graded_test.inc.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/bin/bash | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
# | ||
# Print test result. Printed message should fit in 72 characters. | ||
# | ||
# Print format is: | ||
# | ||
# description ...................... passed ... NNN | ||
# description ...................... failed ... NNN | ||
# 32 chars 24 chars 6 3 3 | ||
# | ||
|
||
print_test() | ||
{ | ||
func="$1" | ||
result="$2" | ||
points="$3" | ||
|
||
if test "$points" -gt 999; then | ||
points=999 | ||
fi | ||
|
||
printf "%-32s " "${func:0:31}" | ||
printf "........................" | ||
if test "$result" -eq 0; then | ||
printf " passed ... %3d\n" "$points" | ||
else | ||
printf " failed ... 0\n" | ||
fi | ||
} | ||
|
||
run_test() | ||
{ | ||
func="$1" | ||
points="$2" | ||
|
||
# Run in subshell. | ||
(eval "$func") | ||
print_test "$func" "$?" "$points" | ||
} |
25 changes: 25 additions & 0 deletions
25
content/chapters/data/lab/support/exec-shellcode/tests/helloworld.asm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
BITS 64 | ||
; use jmp / call trick to get string address in RCX | ||
jmp hello | ||
back: | ||
; call write(1, "Hello, World!\n", 14); | ||
; rax <- 1 (__NR_write) | ||
; rdi <- 1 (stdout fileno) | ||
; rsi <- "Hello, World!\n" | ||
; rdx <- 14 (string length) | ||
mov rax, 1 | ||
mov rdi, 1 | ||
pop rsi | ||
mov rdx, 14 | ||
syscall | ||
|
||
; call exit_group(0) | ||
; rax <- 231 (__NR_exit_group) | ||
; rdi <- 0 (exit status) | ||
mov rax, 231 | ||
xor rdi, rdi | ||
syscall | ||
|
||
hello: | ||
call back | ||
db "Hello, World!", 10, 0 |
25 changes: 25 additions & 0 deletions
25
content/chapters/data/lab/support/exec-shellcode/tests/openfile.asm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
BITS 64 | ||
; use jmp / call trick to get filename address in RCX | ||
jmp filename | ||
back: | ||
; call open("uberfile", O_RDWR | O_TRUNC | O_CREAT, 0644) | ||
; rax <- 2 (__NR_open) | ||
; rdi <- "uberfile" | ||
; rsi <- 578 (O_RDWR | O_TRUNC | O_CREAT - 01102) | ||
; rdx <- 420 (0644) | ||
mov rax, 2 | ||
pop rdi | ||
mov rsi, 578 | ||
mov rdx, 420 | ||
syscall | ||
|
||
; call exit_group(0) | ||
; rax <- 231 (__NR_exit_group) | ||
; rdi <- 0 (exit status) | ||
mov rax, 231 | ||
xor rdi, rdi | ||
syscall | ||
|
||
filename: | ||
call back | ||
db "uberfile", 0 |
23 changes: 23 additions & 0 deletions
23
content/chapters/data/lab/support/exec-shellcode/tests/run_all_tests.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/bash | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
if test -z "$SRC_PATH"; then | ||
SRC_PATH=../src | ||
fi | ||
|
||
export SRC_PATH | ||
|
||
( | ||
./test_helloworld.sh | ||
./test_getpid.sh | ||
./test_openfile.sh | ||
./test_brk.sh | ||
) | tee results.txt | ||
|
||
total=$(grep '\( passed \| failed \)' results.txt | rev | cut -d ' ' -f 1 | rev | paste -s -d'+' | bc) | ||
echo "" | ||
echo -n "Total: " | ||
echo -n " " | ||
LC_ALL=C printf "%3d/100\n" "$total" | ||
|
||
rm results.txt |
39 changes: 39 additions & 0 deletions
39
content/chapters/data/lab/support/exec-shellcode/tests/test_brk.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/bin/bash | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
source graded_test.inc.sh | ||
|
||
shellcode=./brk | ||
|
||
if test -z "$SRC_PATH"; then | ||
SRC_PATH=../src | ||
fi | ||
|
||
test_brk() | ||
{ | ||
if test ! -f "$shellcode"; then | ||
echo "No such file $shellcode" 1>&2 | ||
exit 1 | ||
fi | ||
|
||
objdump -D -M intel -b binary -m i386:x86-64 "$shellcode" > /dev/null 2>&1 | ||
if test $? -ne 0; then | ||
echo "Incorrect shellcode file" 1>&2 | ||
exit 1 | ||
fi | ||
|
||
timeout -k 1 3 "$SRC_PATH"/exec_shellcode "$shellcode" > /dev/null 2>&1 | ||
if test $? -ne 0; then | ||
echo "Program runs unsuccessfully" 1>&2 | ||
exit 1 | ||
fi | ||
|
||
timeout -k 1 3 strace "$SRC_PATH"/exec_shellcode "$shellcode" 2>&1 | grep -A 1 close | tail -2 | grep 'brk(NULL)[ \t]\+= 0x' > /dev/null 2>&1 | ||
if test $? -ne 0; then | ||
echo "brk not called correctly" 1>&2 | ||
exit 1 | ||
fi | ||
exit 0 | ||
} | ||
|
||
run_test test_brk 25 |
39 changes: 39 additions & 0 deletions
39
content/chapters/data/lab/support/exec-shellcode/tests/test_getpid.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/bin/bash | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
source graded_test.inc.sh | ||
|
||
shellcode=./getpid | ||
|
||
if test -z "$SRC_PATH"; then | ||
SRC_PATH=../src | ||
fi | ||
|
||
test_getpid() | ||
{ | ||
if test ! -f "$shellcode"; then | ||
echo "No such file $shellcode" 1>&2 | ||
exit 1 | ||
fi | ||
|
||
objdump -D -M intel -b binary -m i386:x86-64 "$shellcode" > /dev/null 2>&1 | ||
if test $? -ne 0; then | ||
echo "Incorrect shellcode file" 1>&2 | ||
exit 1 | ||
fi | ||
|
||
timeout -k 1 3 "$SRC_PATH"/exec_shellcode "$shellcode" > /dev/null 2>&1 | ||
if test $? -ne 0; then | ||
echo "Program runs unsuccessfully" 1>&2 | ||
exit 1 | ||
fi | ||
|
||
timeout -k 1 3 strace "$SRC_PATH"/exec_shellcode "$shellcode" 2>&1 | grep -A 1 close | tail -2 | grep 'getpid()[ \t]\+= [0-9]\+' > /dev/null 2>&1 | ||
if test $? -ne 0; then | ||
echo "getpid() not called (successfully)" 1>&2 | ||
exit 1 | ||
fi | ||
exit 0 | ||
} | ||
|
||
run_test test_getpid 25 |
Oops, something went wrong.