Guest User

Untitled

a guest
Jul 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.31 KB | None | 0 0
  1. //uploaded by sahar.
  2. // Remove rarely used stuff from windows.h
  3. #define WIN32_LEAN_AND_MEAN
  4. #define WIN32_EXTRA_LEAN
  5.  
  6. // Include headers
  7. #include <windows.h>
  8. #include <dirent.h>
  9. #include <stdio.h>
  10. #include <time.h>
  11.  
  12. // All background music in GunZ is loaded into memory.
  13. // This is what this constant is.
  14. #define FSOUND_LOADMEMORY 0x00008000
  15.  
  16.  
  17. // Signature searching functions
  18. #define FindSig( pBytes ) FindSigEx( pBytes, sizeof( pBytes ) )
  19. #define FindProc( pBytes ) FramePtr( FindSig( pBytes ) )
  20.  
  21. void CheckValidPtr( void * ptr, int nSize )
  22. {
  23.     if (IsBadReadPtr( ptr, nSize ))
  24.     {
  25.         char szBuf[256];
  26.         sprintf( szBuf, "MEMORY_ACCESS_VIOLATION occurs at %.8X...\n\nPerhaps you injected into the wrong process or memory permissions have omitted read.\nRegardless, thread will now exit...", ptr );
  27.         MessageBoxA( 0, szBuf, "Signature searcher error", MB_ICONERROR );
  28.         ExitThread( 0 );
  29.     }
  30. }
  31.  
  32. unsigned long FindSigEx( unsigned char *pBytes, int nSize )
  33. {
  34.     for(int i = 0x401000; i < 0x65FFFF; ++i)
  35.     {
  36.         for(int j = 0; j < nSize; ++j)
  37.         {
  38.             CheckValidPtr( (void*)i, nSize );
  39.             if ((*(unsigned char*)(i + j)) != pBytes[j] && pBytes[j]!=0xEE)
  40.                 break;
  41.             if (j==nSize-1)
  42.                 return i;
  43.         }
  44.     }
  45.     return 0;
  46. }
  47.  
  48. // Signature searching functions
  49. unsigned long FramePtr( unsigned long ulAddr )
  50. {
  51.     while(ulAddr>0x401000)
  52.     {
  53.         CheckValidPtr( (void*)ulAddr, sizeof(void*) );
  54.         if (((*(unsigned long*)ulAddr)&0xFFFFFF)==0xEC8B55)
  55.             return ulAddr;
  56.         ulAddr--;
  57.     }
  58.     return 0;
  59. }
  60.  
  61. // Signature; this is actually the flags used for the sounds
  62. BYTE g_pSignature[] = {0x68, 0x32, 0x81, 0x08, 0x00};
  63.  
  64. // IAT entry. We will replace this 0x66E708
  65. DWORD * g_pFSOUND_Stream_Open = *(DWORD**) ((FindSig( g_pSignature ) + 11) + (*(DWORD*)(FindSig( g_pSignature ) + 7))+2),
  66.  
  67. // Save the original value 
  68.         g_dwOriginal_FSOUND_Stream_Open = *g_pFSOUND_Stream_Open;
  69.    
  70. // Create a structure. This is a linked list structure.
  71. typedef struct _LIST
  72. {
  73.     public:
  74.         _LIST( ){
  75.             g_szData = NULL, g_pNext = NULL, g_iEntries = 0; }
  76.            
  77.         int g_iEntries;
  78.         char * g_szData;
  79.         _LIST * g_pNext;
  80. } LIST;
  81.  
  82. LIST * pMusicList = new LIST( ); // Create a new list
  83.    
  84. // Clear the entry list, and delete any allocated memory   
  85. void ClearList( )
  86. {
  87.     LIST * pCurrent = pMusicList;
  88.     pCurrent->g_iEntries = 0;
  89.     while( pCurrent->g_pNext )
  90.     {
  91.         if (pCurrent->g_szData)
  92.             delete pCurrent->g_szData;
  93.         LIST * pLast = pCurrent;
  94.         pCurrent = pCurrent->g_pNext;
  95.         delete pLast;
  96.     }
  97.     delete pCurrent;
  98.     pMusicList = new LIST( );
  99. }
  100.  
  101. // Add an entry
  102. void AddList( char * szString )
  103. {
  104.     LIST * pCurrent = pMusicList;
  105.     pCurrent->g_iEntries++;
  106.    
  107.     while( pCurrent->g_pNext )
  108.         pCurrent = pCurrent->g_pNext;
  109.        
  110.     pCurrent->g_szData = new char[strlen( szString ) + 5];
  111.     strcpy( pCurrent->g_szData, "bgm/" );
  112.     strcat( pCurrent->g_szData, szString );
  113.     pCurrent->g_pNext  = new LIST( );
  114. }
  115.  
  116. // Choose a random entry
  117. char * RandomList( )
  118. {
  119.     LIST * pCurrent = pMusicList;
  120.     if (!pCurrent->g_iEntries)
  121.         return( NULL );
  122.     int iRandom = time(NULL) % pCurrent->g_iEntries;
  123.     for(int i = 0; i < iRandom; i++)
  124.         pCurrent = pCurrent->g_pNext;
  125.     return( pCurrent->g_szData );
  126. }
  127.        
  128. // This is the fmod hook. This is what is responsible for
  129. // Opening sound data.
  130. void * WINAPI FSOUND_Stream_OpenHook( char * szData, unsigned int uiMode, int nOff, int nLen )
  131. {
  132.     char * szNewData; // Our new sound file
  133.    
  134.     DIR * pDir = opendir( "bgm" ); // Open the "bgm" directory in GunZ for reading
  135.    
  136.     if (!pDir) // If the directory could not open
  137.             goto EndOpen; // Return to the original FSOUND_Stream_Open
  138.            
  139.     ClearList( ); // Clear the list of any entries
  140.    
  141.     while( dirent * dInfo = readdir( pDir ) ) // While files in the directory exist
  142.         if ( !stricmp( &dInfo->d_name[strlen( dInfo->d_name ) - 4], ".wav" ) || // Check if the file extention is .wav
  143.              !stricmp( &dInfo->d_name[strlen( dInfo->d_name ) - 4], ".raw" ) || // .raw
  144.              !stricmp( &dInfo->d_name[strlen( dInfo->d_name ) - 4], ".mp2" ) || // .mp2
  145.              !stricmp( &dInfo->d_name[strlen( dInfo->d_name ) - 4], ".mp3" ) || // .mp3
  146.              !stricmp( &dInfo->d_name[strlen( dInfo->d_name ) - 4], ".ogg" )    // .ogg
  147.             )
  148.                 AddList( dInfo->d_name ); // Then add the filename into the list
  149.            
  150.     closedir( pDir ); // Close the directory
  151.     szNewData = RandomList( ); // Choose a random entry/filename
  152.     if (!szNewData) // If no random entry was fetched(Meaning no entries are on the list)
  153.         goto EndOpen; // Return to original function
  154.     uiMode &= ~FSOUND_LOADMEMORY; // Omit the FSOUND_LOADMEMORY flag from the parameters
  155.     szData = szNewData; // Replace the filedata to ours( This is the song file )
  156.     nLen = 0; // No length; Offset was set to 0
  157.     nOff = 0; // No offset; Play from the beginning
  158.    
  159. EndOpen: // Return to the original FSOUND_Stream_Open function
  160.     return ((void*(WINAPI*)( char *, unsigned int, int, int ))g_dwOriginal_FSOUND_Stream_Open)( szData, uiMode, nOff, nLen );
  161. }
  162.  
  163. // Where the DLL begins
  164. extern "C" bool APIENTRY DllMain( HINSTANCE hInst, DWORD dwReason, LPVOID lpReserved )
  165. {
  166.     if (dwReason == DLL_PROCESS_ATTACH) // If this function was called because the DLL is attached to a process
  167.         * g_pFSOUND_Stream_Open = DWORD( FSOUND_Stream_OpenHook ); // Replace the IAT entry; GunZ will now go to our function everytime it tries to play a sound
  168.     return( true ); // Return true; DLL was successfully injected
  169. }
Add Comment
Please, Sign In to add comment