x86_64 执行外壳代码失败:



我在64位Linux上使用Python 2.7。我有以下Python脚本女巫应该执行一个简单的Hello World shellcode。

import urllib2
import ctypes
shellcode = "xb8x01x00x00x00xbfx01x00x00x00x48xbexd8x00x60x00x00x00x00xbax0ex00x00x00x0fx05xb8x3cx00x00x00xbfx00x00x00x00x0fx05"

#Create buffer in memory
shellcode_buffer = ctypes.create_string_buffer(shellcode, len(shellcode))
#Funktionszeiger
shellcode_func  = ctypes.cast(shellcode_buffer, ctypes.CFUNCTYPE(ctypes.c_void_p))
#Shellcode execute
shellcode_func()

如果我运行python Scriptname.py则会出现内存访问错误。这里有人知道为什么我的脚本不起作用吗?

编辑: 原始 ASM 代码:

section .data
text db "Hello",10
section .text
global _start
_start:
;syscall sys_write(1, text, 14)
mov rax, 1
mov rdi, 1
mov rsi, text
mov rdx, 14
syscall
;syscall sys_exit(0)
mov rax, 60
mov rdi, 0
syscall

您将需要 python 代码来使您的shellcode在具有读/写/执行权限的内存位置中运行。因为它是运行 shell 代码的内存不在可执行内存中。您可以创建一个函数来为您执行此操作(testshellcode.py(:

import ctypes, mmap, sys
# Convert string to bytes object. Differs between Python2 and Python3
if sys.version_info >= (3, 0):
def b(string, charset='latin-1'):
if isinstance(string, bytes) and not isinstance(string, str):
return (string)
else:
return bytes(string, charset)
else:
def b(string):
return bytes(string)
def create_shellcode_function (shellcode_str):
shellcode_bytes = b(shellcode_str)
# Allocate memory with a RWX private anonymous mmap
exec_mem = mmap.mmap(-1, len(shellcode_bytes),
prot = mmap.PROT_READ | mmap.PROT_WRITE | mmap.PROT_EXEC,
flags = mmap.MAP_ANONYMOUS | mmap.MAP_PRIVATE)
# Copy shellcode from bytes object to executable memory
exec_mem.write(shellcode_bytes)
# Cast the memory to a C function object
ctypes_buffer = ctypes.c_int.from_buffer(exec_mem)
function = ctypes.CFUNCTYPE( ctypes.c_int64 )(ctypes.addressof(ctypes_buffer))
function._avoid_gc_for_mmap = exec_mem
# Return pointer to shell code function in executable memory
return function
# linux machine code
shellcode = "shell code string here"
# Create a pointer to our shell code and execute it with no parameters
create_shellcode_function(shellcode)()

代码应该适用于 Python2.7+ 和 Python3


即使您将 shell 代码字符串插入上面的测试程序中的字节对象中,它也会失败。您的 shell 代码字符串似乎缺少字符串本身(hello(;似乎没有正确编码,并且您依赖于text标签的静态内存位置。您将需要一个与位置无关的地址。

要修复代码使其与位置无关,您可以使用 RIP 相对寻址。将字符串放在代码部分之后的.text内,完全忘记.data。这个版本应该足够了(shellcode.asm(:

section .text
global _start
_start:
;syscall sys_write(1, text, text_len)
mov rax, 1
mov rdi, 1
lea rsi, [rel text]     ; RIP Relative addressing for Position independent code
mov rdx, text_len       ; text length computed by assembler
syscall
;syscall sys_exit(0)
mov rax, 60
mov rdi, 0
syscall
text db "Hello",10
text_len EQU $-text         ; Rather than hard coding length compute text length

使用 OBJDUMP 将 shell 代码程序转换为 shell 代码字符串可能会有问题。我写了一个Stackoverflow Answer,讨论了OBJDUMP方法的一些缺陷。如果您正在创建一个可执行文件来独立测试您的 shell 代码,那么最好将其组装并链接到可执行文件;使用 OBJCOPY 将可执行文件转换为二进制文件,然后使用某些内容(如 HEXDUMP(将二进制文件转换为 shell 代码字符串。以下命令应该有效:

nasm -f elf64 shellcode.asm -o shellcode.o 
ld shellcode.o -o shellcode
objcopy -O binary shellcode shellcode.bin

如果运行独立的二进制shellcode它应该输出:

你好

然后,您可以使用以下命令将shellcode.bin转换为外壳代码字符串:

hexdump -v -e '"\""x" 1/1 "%02x" ""' shellcode.bin

输出如下所示:

\xb8\x01\x00\x00\x00\xbf\x01\x00\x00\x00\x48\x8d\x35\x13\x00\x00\x00\xba\x06\x00\x00\x00\x00\x05\xb8\x3c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05\x48\x65\x6c

\x6c\x6f\x0a

然后,您可以在上面的 python 程序中插入此 shell 代码字符串 (testshellcode.py( 将此处的 shell 代码字符串替换为上面的字符串。您可以使用以下命令运行上述脚本:

python testshellcode.py

输出应为:

你好


这是更高级的,并且有 shell 代码教程解释了许多避免字符串中x00字节的技巧。

通常使用shell代码,您希望消除NULL(x00(字节以进行真正的字符串攻击。执行此操作的shellcode.asm版本可能如下所示:

section .text
global _start
_start:
jmp afterdata
text db "Hello",10
text_len EQU $-text
afterdata:
;syscall sys_write(1, text, text_len)
xor eax, eax
inc eax
mov edi, eax
lea rsi, [rel text]
xor edx, edx
mov dl, text_len
syscall
;syscall sys_exit(0)
xor eax, eax
mov al, 60
xor edi, edi
syscall

如果使用前面提到的命令创建 shell 代码字符串,HEXDUMP 应生成如下内容:

\xeb\x06\x48\x65\x6c\x6c\x6f\x0a\x31\xc0\xff\xc0\x89\xc7\x48\x8d\x35\xed\xff\xff\xff\x31\xd2\xb2\x06\x0f\x05\x31\xc0\

xb0\x3c\x31\xff\x0f\x05

此版本执行与代码相同的操作,但请注意字符串中没有x00字节。运行时,它也应该打印:

你好

最新更新