Skip to content

Commit 31cc672

Browse files
authored
labs/lab-10: Add x86_64 version
# Prerequisite Checklist Updated all reading materials, code, solutions to work on x86_64 assembly and use the extended ISA. Because this lab includes payloads for buffer overflows and the alignment on x64 is 16 bytes, I have also modified the overflow payloads on each task and included short explanations. Note: For the `overwrite-ret-addr` task I opted for a simple `printf()` call instead of `system()`, because the `system()` internals use some variables relative to `rbp`. Because of that, further analysis is needed to correct the offsets and so the task becomes a little bit too difficult. Signed-off-by: Matei Buzdea <[email protected]>
1 parent 0fe2837 commit 31cc672

File tree

33 files changed

+277
-263
lines changed

33 files changed

+277
-263
lines changed

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

Lines changed: 2 additions & 2 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

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,48 @@ section .text
2020
global main
2121

2222
main:
23-
push ebp
24-
mov ebp, esp
23+
push rbp
24+
mov rbp, rsp
2525

2626
; Fill data in buffer: buffer[i] = i + 1
2727
; ecx is buffer index (i), dl is buffer value (i + 1). dl needs to be ecx + 1.
2828
; Buffer length is 64 bytes.
29-
xor ecx, ecx
29+
xor rcx, rcx
3030
fill_byte:
3131
mov dl, cl
3232
inc dl
33-
mov byte [buffer + ecx], dl
34-
inc ecx
35-
cmp ecx, len
33+
mov byte [buffer + rcx], dl
34+
inc rcx
35+
cmp rcx, len
3636
jl fill_byte
3737

3838
; Text before printing buffer.
39-
push buffer_intro_message
39+
mov rdi, buffer_intro_message
40+
xor rax, rax
4041
call printf
41-
add esp, 4
4242

43-
xor ecx, ecx
43+
xor rcx, rcx
4444
print_byte:
45-
xor eax, eax
46-
mov al, byte[buffer + ecx]
47-
push ecx ; save ecx, printf may change it
45+
xor rax, rax
46+
mov al, byte[buffer + rcx]
47+
push rcx ; save ecx, printf may change it
48+
sub rsp, 8 ; align stack to 16 bytes
4849

4950
; Print current byte.
50-
push eax
51-
push byte_format
51+
mov rsi, rax
52+
mov rdi, byte_format
53+
xor rax, rax
5254
call printf
53-
add esp, 8
5455

55-
pop ecx ; restore ecx
56-
inc ecx
57-
cmp ecx, len
56+
add rsp, 8 ; restore stack
57+
pop rcx ; restore ecx
58+
inc rcx
59+
cmp rcx, len
5860
jl print_byte
5961

6062
; Print new line. C equivalent instruction is puts("").
61-
push null_string
63+
mov rdi, null_string
6264
call puts
63-
add esp, 4
6465

6566
leave
6667
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 ?= -g -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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ parent: 'Task: Buffer Overflow for Binary'
1212
In `check_string()`:
1313

1414
- `local_10` must be set to `0x4E305250` to call `win()` (carefully, use the little-endian encoding)
15-
- `local_10` is stored at stack - `0x10`
15+
- `local_10` is stored at `stack - 0x4`
1616
- 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"`
17+
- So the payload should consist of `44 (48 - 4)` `'A'` characters, followed by `"\x50\x52\x30\x4E"`

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

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

44

55
def run_executable():
6-
argument = 32 * "A" + "\x50\x52\x30\x4e"
6+
argument = 44 * "A" + "\x50\x52\x30\x4e"
77
subprocess.run(["./overflow_in_binary", argument])
88

99

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
static void win(void)
88
{
99
puts("Great success!");
10+
fflush(stdout);
1011
}
1112

1213
static void fail(void)
1314
{
1415
puts("Epic failure!");
16+
fflush(stdout);
1517
}
1618

1719
static void check_string(const char *str)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPR0N
1+
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPR0N
11.4 KB
Binary file not shown.

labs/lab-10/tasks/overflow-in-c/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ It is recommended to first take a look at the assembly file, then understand the
2323
> **HINT** To see the "real-world" reality, i.e., to find out what the difference is between the buffer and the variable we want to overwrite,
2424
> consult the equivalent assembly language file (`do_overflow.asm`), obtained by assembling the C code.
2525
> In this file, you can find the relative address of the buffer to `ebp` and the variable to `ebp`;
26-
> follow the sequence between lines `36` and `47`;
26+
> follow the sequence between lines `29` and `35` (it may vary depending on the compiler);
2727
> you have a mapping between the variable name and the relative offset to `ebp`.
2828
> With this information, you can create the string to transmit as a payload to the standard input of the program.
2929
> **NOTE** If you want to recompile the files run:

labs/lab-10/tasks/overflow-in-c/solution/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ parent: 'Task: Buffer Overflow for Program Written in C'
77

88
In `do_overflow.asm`:
99

10-
- `line 37` -> `sexy_var` is at `ebp - 16`
11-
- `line 47` -> start reading buffer at `ebp - 89`
12-
- 89 - 16 = 73 of `'A'`s
10+
- `line 29` -> `sexy_var` is at `ebp - 12`
11+
- `line 35` -> start reading buffer at `ebp - 96`
12+
- 96 - 12 = 84 of `'A'`s
1313
- and `0x5541494D` written in little-endian encoding
1414

15-
For exercise **Stack Canary**, when running `objdump` in `main()`, look carefully at the instruction at the addresses `4dc`, as well as the code around it.
15+
For exercise **Stack Canary**, when running `objdump` in `main()`, look carefully at the instruction at the addresses `128b`, as well as the code around it.

0 commit comments

Comments
 (0)