Python C类型分段错误
这是使用ctype运行外壳代码的代码。外壳代码在64位linux上运行“whoami”。但是这个程序给了我一个“分段错误”。但我无法找出其中的错误。代码的结构来自: 对于32位体系结构,这将是外壳代码:Python C类型分段错误,python,shellcode,ctype,Python,Shellcode,Ctype,这是使用ctype运行外壳代码的代码。外壳代码在64位linux上运行“whoami”。但是这个程序给了我一个“分段错误”。但我无法找出其中的错误。代码的结构来自: 对于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" "\x
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");
防止在现代处理器和操作系统上执行随机数据。要避开它,打电话。您还应该将外壳代码定义为二进制而不是字符串,如下所示:
#!/usr/bin/python
import ctypes
shellcode_data = (b"\x6a\x3b\x58\x99\x48\xbb\x2f\x62\x69\x6e\x2f\x73\x68\x00\x53"
b"\x48\x89\xe7\x68\x2d\x63\x00\x00\x48\x89\xe6\x52\xe8\x10\x00"
b"\x00\x00\x2f\x75\x73\x72\x2f\x62\x69\x6e\x2f\x77\x68\x6f\x61"
b"\x6d\x69\x00\x56\x57\x48\x89\xe6\x0f\x05")
shellcode = ctypes.create_string_buffer(shellcode_data)
function = ctypes.cast(shellcode, ctypes.CFUNCTYPE(None))
addr = ctypes.cast(function, ctypes.c_void_p).value
libc = ctypes.CDLL('libc.so.6')
pagesize = libc.getpagesize()
addr_page = (addr // pagesize) * pagesize
for page_start in range(addr_page, addr + len(shellcode_data), pagesize):
assert libc.mprotect(page_start, pagesize, 0x7) == 0
function()
@J.F.Sebastian事实上,现代Python版本将
shellcode\u data
的值放入了一个无法保护的页面中。然而,我认为在这些情况下,这一主张应该失败create\u string\u buffer
目前似乎在cpython上工作。
#!/usr/bin/python
import ctypes
shellcode_data = (b"\x6a\x3b\x58\x99\x48\xbb\x2f\x62\x69\x6e\x2f\x73\x68\x00\x53"
b"\x48\x89\xe7\x68\x2d\x63\x00\x00\x48\x89\xe6\x52\xe8\x10\x00"
b"\x00\x00\x2f\x75\x73\x72\x2f\x62\x69\x6e\x2f\x77\x68\x6f\x61"
b"\x6d\x69\x00\x56\x57\x48\x89\xe6\x0f\x05")
shellcode = ctypes.create_string_buffer(shellcode_data)
function = ctypes.cast(shellcode, ctypes.CFUNCTYPE(None))
addr = ctypes.cast(function, ctypes.c_void_p).value
libc = ctypes.CDLL('libc.so.6')
pagesize = libc.getpagesize()
addr_page = (addr // pagesize) * pagesize
for page_start in range(addr_page, addr + len(shellcode_data), pagesize):
assert libc.mprotect(page_start, pagesize, 0x7) == 0
function()