Destroy unused code

On lunes, 22 de noviembre de 2010 0 comentarios

Destroy unused code

Well since i have been working on CSS i have been looking for ways to cover up code when [myg0t]wav eluded to a method that is pretty effective.

Problem: You have code that is only called once and then never called again, It's just sitting there waiting to be scanned.

What do you do?

Well this is my solution however it isn't the only solution. There is others and in fact there is others better than this. This is just an example.

PHP Code:
#pragma code_seg(".init")
CreateInterfaceFn CaptureFactorychar *pszFactoryModule )
{
    
CreateInterfaceFn fn NULL;
  
    while( 
fn == NULL )
    {
        
HMODULE hFactoryModule GetModuleHandleApszFactoryModule );

        if( 
hFactoryModule )
        {
            
fn reinterpret_castCreateInterfaceFn >( GetProcAddresshFactoryModule"CreateInterface" ) );
        }

        
Sleep10 );
    }

    return 
fn;
}
void *CaptureInterfaceCreateInterfaceFn fnchar *pszInterfaceName )
{
    
unsigned long *ptr NULL;

    while( 
ptr == NULL )
    {
        
ptr reinterpret_castunsigned long* >( fnpszInterfaceNameNULL ) );

        
Sleep10 );
    }

    if( 
ptr )
    {
        
GApp.AddToLogFileA"hook.log""(%s) 0x%X"pszInterfaceNameptr );
    }

    return 
ptr;
}
DWORD WINAPI HookThreadLPVOID lpParams )
{
    while( 
FindWindowA"Valve001"NULL ) == NULL )
        
Sleep100 );

    
GApp.AddToLogFileA"hook.log""Window Found" );

    while( 
GetModuleHandleA"engine.dll" ) == NULL || GetModuleHandleA"client.dll" ) == NULL )
        
Sleep100 );


    
CreateInterfaceFn fnClient        CaptureFactory"client.dll" );
    
CreateInterfaceFn fnEngine        CaptureFactory"engine.dll" );
    
CreateInterfaceFn fnVPhysics    CaptureFactory"vphysics.dll" );
    
CreateInterfaceFn fnVStd        CaptureFactory"vstdlib.dll" );
    
CreateInterfaceFn fnFileSystem    CaptureFactory"FileSystem_Steam.dll" );
    
CreateInterfaceFn fnMatSystem    CaptureFactory"MaterialSystem.dll" );

    
GClient                            reinterpret_castIBaseClientDLL* >( CaptureInterfacefnClientCLIENT_DLL_INTERFACE_VERSION ) );
    
GRenderView                        reinterpret_castIVRenderView* >( CaptureInterfacefnEngineVENGINE_RENDERVIEW_INTERFACE_VERSION ) );
    
GFileSystem                        reinterpret_castIFileSystem* >( CaptureInterfacefnFileSystemFILESYSTEM_INTERFACE_VERSION ) );
    
GPrediction                        reinterpret_castIPrediction* >( CaptureInterfacefnClientVCLIENT_PREDICTION_INTERFACE_VERSION ) );
    
GModelRender                    reinterpret_castIVModelRender* >( CaptureInterfacefnEngineVENGINE_HUDMODEL_INTERFACE_VERSION ) );
    
GEntList                        reinterpret_castIClientEntityList* >( CaptureInterfacefnClientVCLIENTENTITYLIST_INTERFACE_VERSION ) );
    
GEffects                        reinterpret_castIVEfx* >( CaptureInterfacefnEngineVENGINE_EFFECTS_INTERFACE_VERSION ) );
    
GCvarInterface                    reinterpret_castICvar* >( CaptureInterfacefnVStdCVAR_INTERFACE_VERSION ) );
    
GEngine                            reinterpret_castIVEngineClient* >( CaptureInterfacefnEngineVENGINE_CLIENT_INTERFACE_VERSION ) );
    
GEnginetrace                    reinterpret_castIEngineTrace* >( CaptureInterfacefnEngineINTERFACEVERSION_ENGINETRACE_CLIENT ) );
    
GModelinfo                        reinterpret_castIVModelInfo* >( CaptureInterfacefnEngineVMODELINFO_CLIENT_INTERFACE_VERSION ) );
    
GEnginevgui                        reinterpret_castIEngineVGui* >( CaptureInterfacefnEngineVENGINE_VGUI_VERSION ) );
    
GDebugOverlay                    reinterpret_castIVDebugOverlay* >( CaptureInterfacefnEngineVDEBUG_OVERLAY_INTERFACE_VERSION ) );
    
GGameEventManager                reinterpret_castIGameEventManager2* >( CaptureInterfacefnEngineINTERFACEVERSION_GAMEEVENTSMANAGER2 ) );
    
GMatSystem                        reinterpret_castIMaterialSystem* >( CaptureInterfacefnMatSystem"VMaterialSystem079" ) );
    
GPhysicAPI                        reinterpret_castIPhysicsSurfaceProps* >( CaptureInterfacefnVPhysicsVPHYSICS_SURFACEPROPS_INTERFACE_VERSION ) );
    
GRandom                            reinterpret_castIUniformRandomStream* >( CaptureInterfacefnEngineVENGINE_CLIENT_RANDOM_INTERFACE_VERSION ) );
    
GShadowMgr                        reinterpret_castIShadowMgr* >( CaptureInterfacefnEngineENGINE_SHADOWMGR_INTERFACE_VERSION ) );
    
GGameUIFuncs                    reinterpret_castIGameUIFuncs* >( CaptureInterfacefnEngineVENGINE_GAMEUIFUNCS_VERSION ) );
    
GEngineSound                    reinterpret_castIEngineSound* >( CaptureInterfacefnEngineIENGINESOUND_CLIENT_INTERFACE_VERSION ) );

    
DWORD *pdwLevel reinterpret_castDWORD* >( reinterpret_castDWORD >( GClient_VTable.LevelInitPreEntity ) + 0xF );

    while( *
pdwLevel == NULL )
        
Sleep100 );

    while( *(
DWORD *)*pdwLevel == NULL )
        
Sleep100 );

    
GInput reinterpret_castCInput* >( *(DWORD *) *pdwLevel );

    
Core::VAC::DestroyAndMorphSectionGApp.m_LocalModule".init" ); //m_LocalModule is the "Tool" module, look at "code_seg" above

    
return 0;
}
#pragma code_seg() 
You can see the call to "Core::VAC::DestroyAndMorphSection"

Here is my Header file and CPP file for my "VAC" code, but can also be used as a general unused code destroyer and will help with much more than just hiding unused code from VAC2.

VAC.h
PHP Code:
#ifndef __VAC_HEADER__
#define __VAC_HEADER__
namespace Core{
    
namespace VAC
    
{
        
typedef struct
        
{
            
unsigned long start;
            
unsigned long end;
        } 
SectionInformation_t;

        
extern SectionInformation_t GetSectionInformationHMODULE hModule, const char *pszName );

        
extern void DestroyAndMorphSectionHMODULE hModule, const char *pszName );

        
extern void UseThisAndThenMorphItvoid );

        
extern void RunFramevoid );
    };
};
#endif 
VAC.cpp
PHP Code:
#include "stdafx.h"
#include "VAC.h"
namespace Core{
    
namespace VAC
    
{
        
std::vectorSectionInformation_t m_vMorphTheseSections;

        
SectionInformation_t GetSectionInformationHMODULE hModule, const char *pszName )
        {
            
SectionInformation_t si;

            
si.start    0;
            
si.end        0;

            if( 
hModule )
            {
                
IMAGE_DOS_HEADER *pDosHeader reinterpret_cast<IMAGE_DOS_HEADER *>( hModule );

                if( 
pDosHeader->e_magic != IMAGE_DOS_SIGNATURE )
                    return 
si;

                
IMAGE_NT_HEADERS *pNTHeaders reinterpret_cast<IMAGE_NT_HEADERS *>( ( DWORD )hModule pDosHeader->e_lfanew );

                if( 
pNTHeaders->Signature != IMAGE_NT_SIGNATURE )
                    return 
si;

                
IMAGE_SECTION_HEADER *pSectionHeader reinterpret_cast<IMAGE_SECTION_HEADER *>( ( DWORD )pNTHeaders +
                    
sizeofIMAGE_FILE_HEADER ) + sizeofDWORD ) + pNTHeaders->FileHeader.SizeOfOptionalHeader );

                for( 
unsigned int i 0pNTHeaders->FileHeader.NumberOfSectionsi++ )
                {
                    
IMAGE_SECTION_HEADER *pSection = &pSectionHeader];

                    if( 
pSection == NULL )
                        continue;

                    if( 
memcmppszName, ( CHAR* )pSection->NamestrlenpszName ) ) == )
                    {
                        
si.start    = ( ( DWORD )hModule + ( DWORD )pSection->VirtualAddress );
                        
si.end        si.start + ( DWORD pSection->SizeOfRawData;

                        break;
                    }
                }
            }

            return 
si;
        }

        
void DestroyAndMorphSectionHMODULE hModule, const char *pszName )
        {
            
SectionInformation_t si GetSectionInformationhModulepszName );

            if( 
si.start && si.end )
            {
                
m_vMorphTheseSections.push_backsi );
            }
        }

        
void UseThisAndThenMorphItvoid )
        {
            
_asm nop;        //0000
            
_asm nop;        //0001
            
_asm nop;        //0002
            
_asm nop;        //0003
            
_asm nop;        //0004
            
_asm nop;        //0005
            
_asm nop;        //0006
            
_asm nop;        //0007
            
_asm nop;        //0008
            
_asm nop;        //0009
            
_asm nop;        //0010
            
_asm nop;        //0011
            
_asm nop;        //0012
            
_asm nop;        //0013
            
_asm nop;        //0014
            
_asm nop;        //0015
            
_asm nop;        //0016
            
_asm nop;        //0017
            
_asm nop;        //0018
            
_asm nop;        //0019
            
_asm nop;        //0020
            
_asm nop;        //0021
            
_asm nop;        //0022
            
_asm nop;        //0023
            
_asm nop;        //0024
            
_asm nop;        //0025
            
_asm nop;        //0026
            
_asm nop;        //0027
            
_asm nop;        //0028
            
_asm nop;        //0029
            
_asm nop;        //0030
            
_asm nop;        //0031
            
_asm nop;        //0032
            
_asm retn;        //0033
        
}

        
void RandomizeCodeAtPlaceunsigned long beginunsigned long end )
        {
            
BYTE *ArrayOfCode reinterpret_castBYTE* >( begin );

            
int seed MD5_PseudoRandom( ( rand() % 9999 ) ^ 0xFFFFFFFF );

            
srandGetTickCount() ^ seed );

            
MEMORY_BASIC_INFORMATION mbi;

            
VirtualQuery( ( LPCVOID begin, &mbisizeofmbi ) );

            
VirtualProtectmbi.BaseAddressmbi.RegionSizePAGE_EXECUTE_READWRITE, &mbi.Protect );

            for( 
size_t i 0< ( end begin ); i++ )
            {
                
int CurrentRandomSeed = ( GetTickCount() ^ seed * ( ArrayOfCode] & 0xFFFF ) );

                
srandCurrentRandomSeed );

                
ArrayOfCode] = rand() % 0xFF;
            }

            
VirtualProtectmbi.BaseAddressmbi.RegionSizembi.ProtectNULL );

            
FlushInstructionCacheGetCurrentProcess(), ( LPCVOID begin, ( end begin ) );
        }

        
void RunFramevoid )
        {
            
RandomizeCodeAtPlace(
                ( 
unsigned long UseThisAndThenMorphIt,
                ( 
unsigned long UseThisAndThenMorphIt 32 );

            for( 
size_t i 0m_vMorphTheseSections.size(); i++ )
            {
                
SectionInformation_t si m_vMorphTheseSections];

                if( 
si.start == || si.end == ) continue;

                
RandomizeCodeAtPlacesi.startsi.end );
            }
        }
    };
}; 
The function "MD5_PseudoRandom" is a source engine function and can be replaced.

The "Core::VAC::UseThisAndThenMorphIt" function i call in DllMain and then proceed to overwrite it as well. There is no purpose for this besides changing the hash of your module in memory.

This does not effect the code in the file so if your module isn't hidden this will hardly matter, also you must call

"Core::VAC::RunFrame" in a thread or, like i prefer, in a hooked function which is called each frame.

0 comentarios:

Publicar un comentario