我在64位Linux上使用python2.7。我有下面的Python脚本,它应该执行一个简单的helloworld外壳代码。在
import urllib2
import ctypes
shellcode = "\xb8\x01\x00\x00\x00\xbf\x01\x00\x00\x00\x48\xbe\xd8\x00\x60\x00\x00\x00\x00\xba\x0e\x00\x00\x00\x0f\x05\xb8\x3c\x00\x00\x00\xbf\x00\x00\x00\x00\x0f\x05"
#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代码:
^{pr2}$
您将需要python代码,使shell代码在具有读/写/执行权限的内存位置运行。因为shell代码的内存不在可执行内存中。您可以创建一个为您完成此操作的函数(
testshellcode.py
):该代码应该可以与Python2.7+和Python3一起使用
即使您将shell代码字符串插入上面的测试程序中的byte对象,它也将失败。您的shell代码字符串似乎缺少字符串本身(
hello
);似乎没有正确编码,您依赖于text
标签的静态内存位置。你需要一个与职位无关的地址。在要修复代码使其与位置无关,可以使用RIP相对寻址。将字符串放在代码段后面的
^{pr2}$.text
内,完全忘掉.data
。{cd6>这个版本应该足够了:使用OBJDUMP将shell代码程序转换为shell代码字符串可能会有问题。我写了一篇Stackoverflow Answer讨论OBJDUMP方法的一些缺陷。如果要创建一个可执行文件来独立测试shell代码,那么最好将其汇编并链接到可执行文件;使用OBJCOPY将可执行文件转换为二进制文件,然后使用某种方法(如hextump)将二进制文件转换为shell代码字符串。以下命令应该有效:
如果运行独立的二进制文件
shellcode
,它应该输出:然后,可以使用以下命令将
shellcode.bin
转换为shell代码字符串:输出如下所示:
然后,您可以在上面的python程序中插入这个shell代码字符串(
testshellcode.py
),用上面的字符串替换shell code string here。您可以使用以下命令运行上面的脚本:输出应为:
这是更高级的,并且有一些shell代码教程解释了避免字符串中的
\x00
字节的许多技巧。在通常,在shell代码中,您希望消除NUL(
\x00
)字节来进行真正的字符串攻击。执行此操作的shellcode.asm
版本可能类似于:如果使用前面提到的命令创建shell代码字符串,HEXDUMP应该生成如下内容:
这个版本的操作与您的代码相同,但是请注意字符串中没有
\x00
字节。运行时,它也应打印:相关问题 更多 >
编程相关推荐