【发布时间】:2023-04-03 02:09:01
【问题描述】:
当我在 main 函数中执行 shellcode 时,它工作正常。但是,当我通过 main 调用的其他函数执行它时,会导致分段错误。据我所知,函数调用应该影响堆栈,而shellcode应该在堆中。我的代码有问题吗?
shellcode由matesploit生成,我使用qemu-arm运行程序。
代码是:
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
// msfvenom -p linux/armle/exec CMD=/bin/pwd -f c
unsigned char buf[] =
"\x01\x30\x8f\xe2\x13\xff\x2f\xe1\x78\x46\x0a\x30\x01\x90\x01"
"\xa9\x92\x1a\x0b\x27\x01\xdf\x2f\x62\x69\x6e\x2f\x70\x77\x64";
void runShellCode(){
unsigned char *pShellCode = (unsigned char *)calloc(1, 4096);
memcpy(pShellCode, buf, sizeof(buf));
(*(void(*)()) pShellCode)();
}
int main(int argc, char *argv[])
{
// uncomment these lines it will work perfectly fine
// unsigned char *pShellCode = (unsigned char *)calloc(1, 4096);
// memcpy(pShellCode, buf, sizeof(buf));
// (*(void(*)()) pShellCode)();
runShellCode();
return 0;
}
编译和运行的cmd:
arm-linux-gnueabi-gcc test.c -o test_arm -static
qemu-arm test_arm
shellcode的反汇编代码(不正确所以删除)
使用 mmap() 方式更新代码。但是,如果 main() 的参数是 void,它工作正常。虽然参数是int argc, char *argv[],但会导致SEGV。
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <sys/mman.h>
// msfvenom -p linux/armle/exec CMD=/bin/pwd -f c
unsigned char buf[] =
"\x01\x30\x8f\xe2\x13\xff\x2f\xe1\x78\x46\x0a\x30\x01\x90\x01"
"\xa9\x92\x1a\x0b\x27\x01\xdf\x2f\x62\x69\x6e\x2f\x70\x77\x64";
int main(int argc, char *argv[])
//int main(void)
{
unsigned char *pShellCode = (unsigned char *)calloc(1, 4096);
memcpy(pShellCode, buf, sizeof(buf));
void (*sc) () = NULL;
sc = mmap (0, 4096, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
memcpy (sc, pShellCode, 4096);
__builtin___clear_cache (sc, sc + sizeof(sc));
sc();
return 0;
}
【问题讨论】:
-
如果您能向我们提供您的
buf[]数组的反汇编列表,并解释它试图做什么,将会很有帮助。 -
shellcode 正在尝试运行 cmd '/bin/pwd',正如评论所说,我更新了反汇编列表。
-
那个反汇编列表在我看来像是胡言乱语。