Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++] Hardware breakpoints, HELP!

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Mon Jun 22, 2009 12:38 pm    Post subject: [C++] Hardware breakpoints, HELP! Reply with quote

Im trying to change the Esi at a sertant address, but this code doesent seem to work properly:

Code:
#include <Windows.h>
#include <TlHelp32.h>
#include <stdio.h>

BOOL EnableTokenPrivilege(LPTSTR pPrivilege){
   HANDLE hToken;                       
   TOKEN_PRIVILEGES token_privileges;                 
   DWORD dwSize;                       
   ZeroMemory (&token_privileges, sizeof(token_privileges));
   token_privileges.PrivilegeCount = 1;
   if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken))
   {
      char buf[MAX_PATH];
      sprintf_s(buf, sizeof(buf), "OpenProcessToken failed with error code: %d", GetLastError());
      MessageBox(NULL, buf, "Error!", 0);
      CloseHandle(hToken);
      return FALSE;
   }
   if(!LookupPrivilegeValue(NULL, pPrivilege, &token_privileges.Privileges[0].Luid))
   {
      char buf[MAX_PATH];
      sprintf_s(buf, sizeof(buf), "LookupPrivilegeValue failed with error code: %d", GetLastError());
      MessageBox(NULL, buf, "Error!", 0);
      CloseHandle(hToken);
      return FALSE;
   }
   token_privileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
   if(!AdjustTokenPrivileges ( hToken, FALSE, &token_privileges, 0, NULL, &dwSize))
   {
      char buf[MAX_PATH];
      sprintf_s(buf, sizeof(buf), "AdjustTokenPrivileges failed with error code: %d", GetLastError());
      MessageBox(NULL, buf, "Error!", 0);
      CloseHandle (hToken);
      return FALSE;
   }
   CloseHandle(hToken);
   return TRUE;
}

DWORD getPid(LPCSTR lpProcess){
   HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
   PROCESSENTRY32 pe32;
   Process32First(hSnap, &pe32);
   do{
      if(strcmp(pe32.szExeFile, lpProcess) == 0)
         break;
   }while(Process32Next(hSnap, &pe32));
   CloseHandle(hSnap);
   return strcmp(pe32.szExeFile, lpProcess) == 0 ? pe32.th32ProcessID : 0;
}

DWORD getThreads(DWORD pid){
   HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
   THREADENTRY32 te32;
   te32.dwSize = sizeof(THREADENTRY32);
   HANDLE hThread;
   DWORD threadAmount = 0;
   CONTEXT ct;
   ct.ContextFlags = CONTEXT_DEBUG_REGISTERS;
   Thread32First(hSnap, &te32);
   do{
      if(te32.th32OwnerProcessID == pid){
         threadAmount++;
         hThread = OpenThread(THREAD_ALL_ACCESS, TRUE, te32.th32ThreadID);
         if(!GetThreadContext(hThread, &ct))
            printf("GetThreadContext failed at thread %d with error code: %d\n", threadAmount, GetLastError());
         ct.Dr0 = 0x0101757C;
         ct.Dr7 |= 1;
         if(!SetThreadContext(hThread, &ct))
            printf("SetThreadContect failed at thread %d with error code %d\n", threadAmount, GetLastError());
         printf("Thread %d id: %d Process id: %d\n", threadAmount, te32.th32ThreadID, te32.th32OwnerProcessID);
      }
   }while(Thread32Next(hSnap, &te32));
   return threadAmount;
}

int main(){
   EnableTokenPrivilege(SE_DEBUG_NAME);
   DWORD pid = getPid("PINBALL.EXE");
   if(pid){
      printf("Process id: %d\n", pid);
      DWORD threads = getThreads(pid);
      CONTEXT ct;
      ct.ContextFlags = CONTEXT_DEBUG_REGISTERS;
      HANDLE hThread;
      DEBUG_EVENT de;
      DebugActiveProcess(pid);
      while(true){
         if(WaitForDebugEvent(&de, 4000)){
            switch(de.dwDebugEventCode){
               case EXCEPTION_DEBUG_EVENT:
                  printf("Debug Event occured! Exception code: %d\n", de.u.Exception.ExceptionRecord.ExceptionCode);
                  if(de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT){
                     printf("Breakpoint occured!\n");
                     if(de.u.Exception.ExceptionRecord.ExceptionAddress == PVOID(0x0101757C)){
                        if(hThread = OpenThread(THREAD_ALL_ACCESS, false, de.dwThreadId)){
                           SuspendThread(hThread);
                           ct.ContextFlags = CONTEXT_FULL;
                           if(!GetThreadContext(hThread, &ct))
                              printf("GetThreadContext failed with error code: %d", GetLastError());
                           ct.Esi = 10000;
                           if(!SetThreadContext(hThread, &ct))
                              printf("GetThreadContext failed with error code: %d", GetLastError());
                           ResumeThread(hThread);
                           CloseHandle(hThread);
                        }
                     }
                  }
                  ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
                  break;
               default:
                  ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_EXCEPTION_NOT_HANDLED);
            }
         }
         else
            printf("WaitForDebugEvent failed with error code: %d\n", GetLastError());
      }
      getchar();
   }
}


For this code, im trying to change the Esi value at 0x0101757C, which is where 3D pinball adds the value of Esi to the score. It works fine, until Pinball reach the address where it has to break. Then it freezes and my app constantly prints:

Code:
Debug Event occured! Exception code: -2147483644


Which i found to be the value of EXCEPTION_SINGLE_STEP.

Could anyone tell me where im messing up?
Back to top
View user's profile Send private message
dnsi0
I post too much
Reputation: 0

Joined: 04 Jan 2007
Posts: 2674

PostPosted: Mon Jun 22, 2009 5:16 pm    Post subject: Re: [C++] Hardware breakpoints, HELP! Reply with quote

Anden100 wrote:
Im trying to change the Esi at a sertant address, but this code doesent seem to work properly:

Code:
#include <Windows.h>
#include <TlHelp32.h>
#include <stdio.h>

BOOL EnableTokenPrivilege(LPTSTR pPrivilege){
   HANDLE hToken;                       
   TOKEN_PRIVILEGES token_privileges;                 
   DWORD dwSize;                       
   ZeroMemory (&token_privileges, sizeof(token_privileges));
   token_privileges.PrivilegeCount = 1;
   if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken))
   {
      char buf[MAX_PATH];
      sprintf_s(buf, sizeof(buf), "OpenProcessToken failed with error code: %d", GetLastError());
      MessageBox(NULL, buf, "Error!", 0);
      CloseHandle(hToken);
      return FALSE;
   }
   if(!LookupPrivilegeValue(NULL, pPrivilege, &token_privileges.Privileges[0].Luid))
   {
      char buf[MAX_PATH];
      sprintf_s(buf, sizeof(buf), "LookupPrivilegeValue failed with error code: %d", GetLastError());
      MessageBox(NULL, buf, "Error!", 0);
      CloseHandle(hToken);
      return FALSE;
   }
   token_privileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
   if(!AdjustTokenPrivileges ( hToken, FALSE, &token_privileges, 0, NULL, &dwSize))
   {
      char buf[MAX_PATH];
      sprintf_s(buf, sizeof(buf), "AdjustTokenPrivileges failed with error code: %d", GetLastError());
      MessageBox(NULL, buf, "Error!", 0);
      CloseHandle (hToken);
      return FALSE;
   }
   CloseHandle(hToken);
   return TRUE;
}

DWORD getPid(LPCSTR lpProcess){
   HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
   PROCESSENTRY32 pe32;
   Process32First(hSnap, &pe32);
   do{
      if(strcmp(pe32.szExeFile, lpProcess) == 0)
         break;
   }while(Process32Next(hSnap, &pe32));
   CloseHandle(hSnap);
   return strcmp(pe32.szExeFile, lpProcess) == 0 ? pe32.th32ProcessID : 0;
}

DWORD getThreads(DWORD pid){
   HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
   THREADENTRY32 te32;
   te32.dwSize = sizeof(THREADENTRY32);
   HANDLE hThread;
   DWORD threadAmount = 0;
   CONTEXT ct;
   ct.ContextFlags = CONTEXT_DEBUG_REGISTERS;
   Thread32First(hSnap, &te32);
   do{
      if(te32.th32OwnerProcessID == pid){
         threadAmount++;
         hThread = OpenThread(THREAD_ALL_ACCESS, TRUE, te32.th32ThreadID);
         if(!GetThreadContext(hThread, &ct))
            printf("GetThreadContext failed at thread %d with error code: %d\n", threadAmount, GetLastError());
         ct.Dr0 = 0x0101757C;
         ct.Dr7 |= 1;
         if(!SetThreadContext(hThread, &ct))
            printf("SetThreadContect failed at thread %d with error code %d\n", threadAmount, GetLastError());
         printf("Thread %d id: %d Process id: %d\n", threadAmount, te32.th32ThreadID, te32.th32OwnerProcessID);
      }
   }while(Thread32Next(hSnap, &te32));
   return threadAmount;
}

int main(){
   EnableTokenPrivilege(SE_DEBUG_NAME);
   DWORD pid = getPid("PINBALL.EXE");
   if(pid){
      printf("Process id: %d\n", pid);
      DWORD threads = getThreads(pid);
      CONTEXT ct;
      ct.ContextFlags = CONTEXT_DEBUG_REGISTERS;
      HANDLE hThread;
      DEBUG_EVENT de;
      DebugActiveProcess(pid);
      while(true){
         if(WaitForDebugEvent(&de, 4000)){
            switch(de.dwDebugEventCode){
               case EXCEPTION_DEBUG_EVENT:
                  printf("Debug Event occured! Exception code: %d\n", de.u.Exception.ExceptionRecord.ExceptionCode);
                  if(de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT){
                     printf("Breakpoint occured!\n");
                     if(de.u.Exception.ExceptionRecord.ExceptionAddress == PVOID(0x0101757C)){
                        if(hThread = OpenThread(THREAD_ALL_ACCESS, false, de.dwThreadId)){
                           SuspendThread(hThread);
                           ct.ContextFlags = CONTEXT_FULL;
                           if(!GetThreadContext(hThread, &ct))
                              printf("GetThreadContext failed with error code: %d", GetLastError());
                           ct.Esi = 10000;
                           if(!SetThreadContext(hThread, &ct))
                              printf("GetThreadContext failed with error code: %d", GetLastError());
                           ResumeThread(hThread);
                           CloseHandle(hThread);
                        }
                     }
                  }
                  ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
                  break;
               default:
                  ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_EXCEPTION_NOT_HANDLED);
            }
         }
         else
            printf("WaitForDebugEvent failed with error code: %d\n", GetLastError());
      }
      getchar();
   }
}


For this code, im trying to change the Esi value at 0x0101757C, which is where 3D pinball adds the value of Esi to the score. It works fine, until Pinball reach the address where it has to break. Then it freezes and my app constantly prints:

Code:
Debug Event occured! Exception code: -2147483644


Which i found to be the value of EXCEPTION_SINGLE_STEP.

Could anyone tell me where im messing up?


You know why? HARDWARE BREAKPOINTS=EXCEPTION_SINGLE_STEP
SOFTWARE BREAKPOINTS (AKA INT3)=EXCEPTION_BREAK_POINT.

You aren't messing up just mistaken Laughing
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites