【问题标题】:C++ Make a Program Write Over ItselfC++ 让程序自己重写
【发布时间】:2018-05-04 01:14:37
【问题描述】:

几天前我发布了question on a similar topic (and one a couple years ago),但我决定继续前进。我正在尝试将 C++ 代码注入 C++ 代码(以某种可移植的方式,不使用操作系统特定的功能,并尝试以独立于编译器/工具链的方式)。我基本上想这样做是为了尝试执行运行时 C++ 脚本。我写了一个小测试程序(它真的有点杂乱无章):Main.cpp:

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
#include <vector>
#include <tuple>

constexpr char hexmap[] = { '0', '1', '2', '3', '4', '5', '6', '7',
        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

std::string HexStr( unsigned char *data, int len )
{
    std::string s( len * 2, ' ' );
    for( int i = 0; i < len; ++i ) {
        s[ 2 * i ] = hexmap[ ( data[ i ] & 0xF0 ) >> 4 ];
        s[ 2 * i + 1 ] = hexmap[ data[ i ] & 0x0F ];
    }
    return s;
}
/*I am aware there is a standard GC and that this is 
by no means production.*/
template< typename T, unsigned short ARRAY >
struct GarbageCollector
{
    std::vector< T* > ts;
    GarbageCollector() = default;
    ~GarbageCollector() {
        for( T* i : ts )
            delete i;
    }
};

template< typename T >
struct GarbageCollector< T, 1 >
{
    std::vector< T* > ts;
    GarbageCollector() = default;
    ~GarbageCollector() {
        for( T* i : ts )
            delete[] i;
    }
};


std::tuple< char*, std::streamoff > ReadBinaryBuffer( 
        std::string fileName, GarbageCollector< char, 1 >* gc )
{
    std::ifstream binaryData;
    binaryData.open( "Source.obj", std::ios::binary );
    if( binaryData.fail() ) {
        std::cerr << "Failed to open file!\n";
        return { "Failed to open file!\n", 1 };
    }
    binaryData.seekg( 0, std::ios::end );
    std::streamoff i = binaryData.tellg();
    char* buffer = new char[ i ];
    binaryData.seekg( 0, std::ios::beg );
    binaryData.read( buffer, i );
    binaryData.close();
    gc->ts.push_back( buffer );
    return { buffer, i };
}

std::string ReadBinary( std::string fileName )
{
    GarbageCollector< char, 1 > gc;
    auto result = ReadBinaryBuffer( fileName, &gc );
    std::string stringBuffer;
    stringBuffer.assign( std::get< 0 >( result ), std::get< 1 >( result ) );
    return stringBuffer;
}
std::string ReadBinary( std::tuple< char*, 
        std::streamoff > bufferContainer )
{
    std::string stringBuffer;
    stringBuffer.assign( std::get< 0 >( bufferContainer ), 
            std::get< 1 >( bufferContainer ) );
    return stringBuffer;
}
extern "C"
{
    int test() {
        return 3;
    }
    int( *cmpp )();
}
int main( int argc, char* args )
{
    cmpp = &test;
    auto binary = ReadBinary( "Source.obj" );
    auto function = binary.substr( 347, 56 );
    const char* code = function.c_str();
    std::cout << HexStr( ( unsigned char* ) ( code ), function.size() );
    //strcpy( ( char* )cmpp, ( ( char* ) code ) );
    char* testp = ( char* ) cmpp;
    char* testpp = ( char* ) code;
    for( size_t i = 0; i < 54; ++i ) {
        *testp++ = *testpp++;
    }
    cmpp();
    char close;
    std::cin >> close;
    return 0;
}

来源.cpp:

extern "C"
{
    int calc()
    {
        int q = 30 * 123;
        for( int i = 0; i < 10; ++i )
            q *= i;
        return q;
    }
}

基本上我尝试用 malloc 和 new 分配一大块内存,但我想也许我可以覆盖已经专用于进程内存的内存(这就是为什么我有 test 指向的函数 cmpp 并尝试覆盖它)。但是我收到写访问错误。我看了一下this post,从其中一个answers 看来,可以在没有访问冲突的情况下覆盖程序自己的内存(这是我想要做的),错误地不少于。有人可以详细说明一下,并告诉我如何以一种可能有点可移植的方式使用任何非标准功能(或至少一个可以/被抽象掉的功能)吗?

【问题讨论】:

    标签: c++ reflection architecture code-injection runtime-compilation


    【解决方案1】:

    默认情况下,您的程序将被加载到只读和执行内存中,不允许写入(至少在任何现代操作系统上)。不同的保护措施是出于安全原因,如果有人破坏了您的软件,他们不应该对您这样做,例如泄露信息。

    此请求存储在二进制文件中,由链接器完成。您可以更改链接器以请求将程序加载到可写内存中,但这远非最佳且不可移植。

    更好的方法是从您的操作系统(mmap et al on linux)请求一个可执行和可写的页面,但据我所知,也没有可移植的方法来做到这一点。

    【讨论】:

    • 感谢您的回复,非常感谢! :) 一个单一翻译单元的程序呢?链接器不会参与正确吗?
    • 仍将使用链接器。有外部代码,例如您的 C 运行时(gcc 上的 glibc,Windows 上的 VC++ 运行时等),并将任何只读数据链接到具有适当权限的页面等。您仍然可以通过请求 .text 可写来执行此操作链接器,但由于它永远不会增加大小,如果你想要更多空间,无论如何你都必须在以后分配它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 2010-09-12
    相关资源
    最近更新 更多