首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从向量到虚拟Shellcode的Shellcode不工作。

从向量到虚拟Shellcode的Shellcode不工作。
EN

Stack Overflow用户
提问于 2022-01-21 20:32:14
回答 1查看 181关注 0票数 0

example2.cpp中解密后,我很难弄清楚为什么有效负载不能工作,当使用命令'example2.exe > out.txt‘执行编译后的exe时,我得到的外壳代码是有效的,不会在example.cpp中造成任何问题,因为我可以看到输出是hello world (至少在我的例子中是这样)

example.cpp

代码语言:javascript
复制
unsigned char out[] = "\x00\x00...";
int main()
{
    void *exec = VirtualAlloc(0, sizeof(out), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    memcpy(exec, out, sizeof(out));
    ((void(*)())exec)();
}

example2.cpp

代码语言:javascript
复制
void decrypt_run(){
    std::vector<unsigned char> decrypted(encrypted.size());
    // the encrypted cipher get decrypted and the vector decrypted is filled with unsigned chars
    unsigned char buf[decrypted.size()];
    // converting the vector to an unsigned char buffer to be passed to memcopy 
    std::copy(decrypted.begin(), decrypted.end(), buf);
    size_t shellcodesize = sizeof(buf);
    cout << buf << endl;  // prints the shellcode to the screen 
    //void *exec = VirtualAlloc(0, shellcodesize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    //memcpy(exec, buf, shellcodesize);
    //((void(*)())exec)();
}
int main()
{
    decrypt_run();
    return 0;
}

当取消对decrypt_run()中最后三行的注释时,程序完成时,除了self代码本身之外,没有任何输出。

同样,out.txt中相同的外壳代码也与example.cpp一起使用,它运行得完美无缺,但对example2.cpp却没有。

EN

回答 1

Stack Overflow用户

发布于 2022-01-22 00:24:40

在第一种情况下,您将在固定内存缓冲区中对外壳代码进行硬编码,然后将这些字节复制到分配的可执行内存中。这很好。

但是在第二个例子中,您有一个充满(解密)外壳代码字节的动态vector,然后分配一个动态数组,将外壳代码复制到该数组中,然后将外壳代码从该数组复制到可执行内存中。这个中间数组完全没有必要,应该删除,您可以将外壳代码字节直接从vector复制到可执行内存中,例如:

代码语言:javascript
复制
void decrypt_run(){
    std::vector<unsigned char> decrypted(encrypted.size());
    // the encrypted cipher get decrypted and the vector decrypted is filled with unsigned chars
    // passing the vector to memcopy 
    unsigned char *buf = decrypted.data();
    size_t shellcodesize = decrypted.size();
    cout << buf << endl;  // prints the shellcode to the screen 
    void *exec = VirtualAlloc(0, shellcodesize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    memcpy(exec, buf, shellcodesize);
    ((void(*)())exec)();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70807364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档