您将需要 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 代码字符串可能会出现问题。我写了一个堆栈溢出答案 https://stackoverflow.com/a/48109240/3857942讨论 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
它应该输出:
Hello
然后您可以转换shellcode.bin
到 shell 代码字符串:
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\x0f\x05\xb8 \x3c\x00\x00\x00\xbf\x00\x00\x00\x00\x0f\x05\x48\x65\x6c\x6c\x6f\x0a
然后您可以在上面的 python 程序中插入这个 shell 代码字符串(testshellcode.py
) 替换shell代码字符串在这里与上面的字符串。您可以使用以下命令运行上面的脚本:
python testshellcode.py
输出应该是:
Hello
这是更高级的,并且有 shell 代码教程,解释了许多要避免的技巧\x00
字符串中的字节。
通常,对于 shell 代码,您希望消除 NUL (\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
字符串中的字节。运行时,它也应该打印:
Hello