vulnprogram.c: #include <stdio.h> #include <stdlib.h> #include <string.h> char passwd[]…
- User
vuln_program.c: #include <stdio.h> #include <stdlib.h> #include <string.h>
char passwd[] = "asd"; char usr_input[4];
void target() { printf("You have entered the correct passwd\n"); exit(0); }
void prompt(){ char buf[4]; gets(buf); strncpy(usr_input, buf, 4); }
int main(){ prompt(); if(strcmp(usr_input, passwd) == 0) { target(); }else { printf("Wrong passwd!\n"); exit(1); }
return 0;} gcc -fno-stack-protector -z execstack -fno-pie -no-pie -m32 -O0 -g vuln_program.c -o vuln_program Use a binary analyzing tool to get the address of target function. Tools like nm, readelf and objdump can fulfill this job.
Target function address is 08049196, now construct the attack string using your program. And store it in the attack_string file. Your program should be named as attack and take the target function address as the input. For example if 0xdeadbeaf is the target function address, and your code is written in python, then run "python3 attack.py deadbeef" will create an string stored in attack string file to be further used to exploit the vulnerability in the vulnerable program.
The following will try to help you understand how to construct an attack string: Stack layout of vulnerable program contains buf which is 4 bytes, Other vars which is 8 bytes, %ebp which is 4 bytes, %eip and &arg1 while the prompt function invoking. The goal is to overwrite the buffer until return address(%eip) on the stack has contains the target function address. Based on this, construct you attack string carefully. One thing to be aware is the address in little-endian format. For example, if target address is ”0xdeadbeef”, then the bytes of return address at RA will be RA[0]:ef, RA[1]:be, RA[2]:ad, RA[3]:de.
Stack layout of launching shellcode contains buffer, return address %eip, nop nop nop....., injected code. Overwrite the buffer in a specific way that:
- Overwrite the buffer with padding.
- Overwrite the return address(%eip) on the stack with a guessed address that probably will jump to the injected malicious code.
- nops(0x90) can be filled in the between the return address and injected malicious code to increase the chance that injected malicious code will be executed. The nop instruction will do nothing but jump to the instruction.
- The shellcode then provided as payload at the end of the overwrite.
The shellcode that is used to launch a shell is provided as following: "\x31\xc0\x31\xdb\xb0\x06\xcd\x80\x53\x68/tty\x68/dev\x89\xe3\x31\xc9\x66\xb9\x12\x27\xb0\x05\xcd\x80\x31\xc0\x50\x68//sh\x68/bin\x89\xe3\x50\x53\x89\xe1\x99\xb0\x0b\xcd\x80"
Use the attack program to generate the attack payload for this shellcode exploitation. attack.py: import sys
def generate_attack_string(target_addr): # Convert the hexadecimal address from a string to an integer addr_int = int(target_addr, 16)
- Assistant