3
這是使用ctype運行shellcode的代碼。 shellcode在64位linux上運行「whoami」。但是這個程序給了我一個「分段錯誤」。但我無法找出它的錯誤。代碼的結構是從: ctypes: Cast string to function?Python Ctype分割錯誤
#!/usr/bin/python
from ctypes import *
# /usr/bin/whoami
shellcode_data = ("\x6a\x3b\x58\x99\x48\xbb\x2f\x62\x69\x6e\x2f\x73\x68\x00\x53"
"\x48\x89\xe7\x68\x2d\x63\x00\x00\x48\x89\xe6\x52\xe8\x10\x00"
"\x00\x00\x2f\x75\x73\x72\x2f\x62\x69\x6e\x2f\x77\x68\x6f\x61"
"\x6d\x69\x00\x56\x57\x48\x89\xe6\x0f\x05");
shellcode = c_char_p(shellcode_data)
function = cast(shellcode, CFUNCTYPE(None))
function()
對於32位體系結構,這將是殼代碼:
shellcode_data = ("\x6a\x0b\x58\x99\x52\x66\x68\x2d\x63\x89\xe7\x68\x2f\x73\x68"
"\x00\x68\x2f\x62\x69\x6e\x89\xe3\x52\xe8\x10\x00\x00\x00\x2f"
"\x75\x73\x72\x2f\x62\x69\x6e\x2f\x77\x68\x6f\x61\x6d\x69\x00"
"\x57\x53\x89\xe1\xcd\x80");
謝謝。這解決了這個問題。 –
@ J.F.Sebastian事實上,現代Python版本將'shellcode_data'的值放入一個無法保護的頁面中。但是,我相信在這些情況下,斷言應該會失敗。 'create_string_buffer'目前似乎對cpython有效。 – phihag