SA:MP ASI Fast Exit (slow /q /quit game exit fix)

rraggerr

проверенный какой-то
Автор темы
1,631
853
Версия SA-MP
  1. 0.3.7 (R1)
  2. 0.3.7-R2
  3. 0.3.7-R3
  4. 0.3.7-R4
  5. 0.3.7-R5
  6. 0.3DL
Описание: Самый мгновенный и небольшой по коду выход из игры. Не имеет всяких конфигов и прочего дерьма.
Код не имеет проверок на ошибки, ошибки защиты/подмены памяти.
Создается шеллкод прямо в gta_sa.exe в align-участке между функциями дабы избежать аллокации.
После создания шеллкода плагин выгружается из игры.

Установка: закинуть в папку с gta_sa.exe

Известная проблема: решить что же вам делать с сэкономленным временем
Source:
#if !defined( __clang__ )
#error  #LLVMGANG
#endif

extern "C"
{
    __declspec( dllimport ) long __stdcall NtProtectVirtualMemory( void* Process, void** Base, unsigned long* Size, unsigned long Protection, unsigned long* OldProtection );
    __declspec( dllimport ) long __stdcall NtFlushInstructionCache( void* Process, const void* Base, unsigned long Size );
    __declspec( dllimport ) long __stdcall NtTerminateProcess( void* Process, long Status );
}

namespace
{
    struct ListEntry
    {
        const ListEntry* next;
    };

    struct LoadedModule
    {
        ListEntry load_order;
        char padding[ 0x14 ];
        unsigned base;
        char name_padding[ 0x10 ];
        unsigned short name_length;
        char name_padding_end[ 2 ];
        const wchar_t* name;
    };

    struct LoaderData
    {
        char padding[ 0x0C ];
        ListEntry load_order;
    };

    struct Peb
    {
        char padding[ 0x0C ];
        const LoaderData* loader;
    };

    struct DosHeader
    {
        char padding[ 0x3C ];
        unsigned nt_offset;
    };

    struct ImageHeader
    {
        char padding[ 8 ];
        unsigned char timestamp_low;
    };

    struct QuitSite
    {
        unsigned timestamp_low : 8;
        unsigned rva : 24;
    };

    constexpr unsigned gta_stub_address = 0x57DB14; // .text padding between functions

    constexpr QuitSite samp_quit_sites[] = {
            { 0x7A, 0x64D70 }, // R1
            { 0x94, 0x64E40 }, // R2
            { 0x43, 0x68270 }, // R3
            { 0xCD, 0x689A0 }, // R4
            { 0x9E, 0x689E0 }, // R5
            { 0x30, 0x68420 }, // DL
    };

    [[nodiscard]] unsigned FindSampBase( ) noexcept
    {
        const Peb* peb;
        __asm__ volatile( "movl %%fs:0x30, %0" : "=r"( peb ) );
        const auto& modules = peb->loader->load_order;
        constexpr wchar_t target[] = L"samp.dll";

        for ( const auto* entry = modules.next; entry != &modules; entry = entry->next )
        {
            const auto* module = reinterpret_cast< const LoadedModule* >( entry );
            if ( module->name_length != sizeof( target ) - sizeof( wchar_t ) || module->name[ 4 ] != L'.' )
            {
                continue;
            }

            unsigned i = 0;
            while ( i != 8 && ( module->name[ i ] | 0x20 ) == target[ i ] )
            {
                ++i;
            }
            if ( i == 8 )
            {
                return module->base;
            }
        }
        return 0;
    }

    void InstallStub( ) noexcept
    {
        const unsigned displacement = reinterpret_cast< unsigned >( &NtTerminateProcess ) - ( gta_stub_address + 9u );
        // push 0; push -1; call NtTerminateProcess
        const unsigned char stub[] = {
                0x6A,
                0,
                0x6A,
                0xFF,
                0xE8,
                static_cast< unsigned char >( displacement ),
                static_cast< unsigned char >( displacement >> 8 ),
                static_cast< unsigned char >( displacement >> 16 ),
                static_cast< unsigned char >( displacement >> 24 ),
                0xC3,
        };

        auto* const process = reinterpret_cast< void* >( -1 ); // NtCurrentProcess
        auto* const destination = reinterpret_cast< volatile unsigned char* >( gta_stub_address );
        auto* page = reinterpret_cast< void* >( gta_stub_address );

        unsigned long size = sizeof( stub );
        unsigned long old_protect = 0;

        NtProtectVirtualMemory( process, &page, &size, 0x40ul, &old_protect );
        for ( unsigned i = 0; i != sizeof( stub ); ++i )
        {
            destination[ i ] = stub[ i ];
        }
        NtProtectVirtualMemory( process, &page, &size, old_protect, &old_protect );

        NtFlushInstructionCache( process, reinterpret_cast< const void* >( gta_stub_address ), sizeof( stub ) );
    }

    void PatchQuit( unsigned Address ) noexcept
    {
        auto* const destination = reinterpret_cast< volatile unsigned long long* >( Address );
        auto previous = *destination;

        const unsigned displacement = gta_stub_address - ( Address + 5u );
        const auto replacement = ( previous & ~0xFFFFFFFFFFull ) | ( static_cast< unsigned long long >( displacement ) << 8 ) | 0xE9;
        auto* const process = reinterpret_cast< void* >( -1 ); // NtCurrentProcess

        unsigned long region_size = sizeof( long long );
        unsigned long old_protect = 0;

        auto* page = reinterpret_cast< void* >( Address );

        NtProtectVirtualMemory( process, &page, &region_size, 0x40ul, &old_protect );

        __atomic_compare_exchange_n( destination, &previous, replacement, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST );

        NtProtectVirtualMemory( process, &page, &region_size, old_protect, &old_protect );
    }

    void PatchSampQuit( unsigned Base ) noexcept
    {
        const auto& image = *reinterpret_cast< const ImageHeader* >( Base + reinterpret_cast< const DosHeader* >( Base )->nt_offset );
        for ( const auto& site : samp_quit_sites )
        {
            if ( image.timestamp_low == site.timestamp_low )
            {
                PatchQuit( Base + site.rva );
                return;
            }
        }
    }
} // namespace

extern "C" int __stdcall DllMain( void*, unsigned long Reason, void* ) noexcept
{
    if ( Reason != 1ul )
    {
        return 1;
    }

    InstallStub( );
    PatchQuit( 0x57D860 );
    PatchSampQuit( FindSampBase( ) );
 
    return 0;
}
 

Вложения

  • !!fast-exit.asi
    2 KB · Просмотры: 1
  • Вау
Реакции: вайега52