Advertisement
Guest User

Simple speed hack

a guest
Feb 19th, 2014
2,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.75 KB | None | 0 0
  1. #include <Windows.h>
  2.  
  3. #include "Detours/detours.h"
  4.  
  5. #pragma comment(lib, "Detours/detours.lib")
  6.  
  7. //#include "SpeedHack.h"
  8. // TODO: put in another file, and rename to something better
  9. template<class DataType>
  10. class SpeedHack {
  11.     DataType time_offset;
  12.     DataType time_last_update;
  13.  
  14.     double speed_;
  15.  
  16. public:
  17.     SpeedHack(DataType currentRealTime, double initialSpeed) {
  18.         time_offset = currentRealTime;
  19.         time_last_update = currentRealTime;
  20.  
  21.         speed_ = initialSpeed;
  22.     }
  23.  
  24.     // TODO: put lock around for thread safety
  25.     void setSpeed(DataType currentRealTime, double speed) {
  26.         time_offset = getCurrentTime(currentRealTime);
  27.         time_last_update = currentRealTime;
  28.  
  29.         speed_ = speed;
  30.     }
  31.  
  32.     // TODO: put lock around for thread safety
  33.     DataType getCurrentTime(DataType currentRealTime) {
  34.         DataType difference = currentRealTime - time_last_update;
  35.  
  36.         return (DataType)(speed_ * difference) + time_offset;
  37.     }
  38. };
  39.  
  40.  
  41. // function signature typedefs
  42. typedef DWORD     (WINAPI *GetTickCountType)(void);
  43. typedef ULONGLONG (WINAPI *GetTickCount64Type)(void);
  44.  
  45. typedef BOOL      (WINAPI *QueryPerformanceCounterType)(LARGE_INTEGER *lpPerformanceCount);
  46.  
  47. // globals
  48. GetTickCountType   g_GetTickCountOriginal;
  49. GetTickCount64Type g_GetTickCount64Original;
  50. GetTickCountType   g_TimeGetTimeOriginal;    // Same function signature as GetTickCount
  51.  
  52. QueryPerformanceCounterType g_QueryPerformanceCounterOriginal;
  53.  
  54.  
  55. const double kInitialSpeed = 1.0; // initial speed hack speed
  56.  
  57. //                                  (initialTime,      initialSpeed)
  58. SpeedHack<DWORD>     g_speedHack    (GetTickCount(),   kInitialSpeed);
  59. SpeedHack<ULONGLONG> g_speedHackULL (GetTickCount64(), kInitialSpeed);
  60. SpeedHack<LONGLONG>  g_speedHackLL  (0,                kInitialSpeed); // Gets set properly in DllMain
  61.  
  62. // function prototypes
  63.  
  64. DWORD     WINAPI GetTickCountHacked(void);
  65. ULONGLONG WINAPI GetTickCount64Hacked(void);
  66.  
  67. BOOL      WINAPI QueryPerformanceCounterHacked(LARGE_INTEGER *lpPerformanceCount);
  68.  
  69. DWORD     WINAPI KeysThread(LPVOID lpThreadParameter);
  70.  
  71. // functions
  72.  
  73. BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpvReserved) {
  74.     // TODO: split up this function for readability.
  75.  
  76.     if (dwReason == DLL_PROCESS_ATTACH) {
  77.         DisableThreadLibraryCalls(hinstDLL);
  78.        
  79.         HMODULE kernel32 = GetModuleHandleA("Kernel32.dll");
  80.         HMODULE winmm    = GetModuleHandleA("Winmm.dll");
  81.  
  82.         // TODO: check if the modules are even loaded.
  83.        
  84.         // Get all the original addresses of target functions
  85.         g_GetTickCountOriginal   = (GetTickCountType)   GetProcAddress(kernel32, "GetTickCount");
  86.         g_GetTickCount64Original = (GetTickCount64Type) GetProcAddress(kernel32, "GetTickCount64");
  87.  
  88.         g_TimeGetTimeOriginal = (GetTickCountType)GetProcAddress(winmm, "timeGetTime");
  89.  
  90.         g_QueryPerformanceCounterOriginal = (QueryPerformanceCounterType)GetProcAddress(kernel32, "QueryPerformanceCounter");
  91.        
  92.         // Setup the speed hack object for the Performance Counter
  93.         LARGE_INTEGER performanceCounter;
  94.         g_QueryPerformanceCounterOriginal(&performanceCounter);
  95.  
  96.         g_speedHackLL = SpeedHack<LONGLONG>(performanceCounter.QuadPart, kInitialSpeed);
  97.  
  98.         // Detour functions
  99.         DetourTransactionBegin();
  100.        
  101.             DetourAttach((PVOID*)&g_GetTickCountOriginal,   (PVOID)GetTickCountHacked);
  102.             DetourAttach((PVOID*)&g_GetTickCount64Original, (PVOID)GetTickCount64Hacked);
  103.  
  104.             // Detour timeGetTime to the hacked GetTickCount (same signature)
  105.             DetourAttach((PVOID*)&g_TimeGetTimeOriginal, (PVOID)GetTickCountHacked);
  106.  
  107.             DetourAttach((PVOID*)&g_QueryPerformanceCounterOriginal, (PVOID)QueryPerformanceCounterHacked);
  108.  
  109.         DetourTransactionCommit();
  110.  
  111.         CreateThread(NULL, 0, KeysThread, NULL, 0, NULL);
  112.     }
  113.  
  114.     return TRUE;
  115. }
  116.  
  117. void setAllToSpeed(double speed) {
  118.     g_speedHack.setSpeed(g_GetTickCountOriginal(), speed);
  119.  
  120.     g_speedHackULL.setSpeed(g_GetTickCount64Original(), speed);
  121.  
  122.     LARGE_INTEGER performanceCounter;
  123.     g_QueryPerformanceCounterOriginal(&performanceCounter);
  124.  
  125.     g_speedHackLL.setSpeed(performanceCounter.QuadPart, speed);
  126. }
  127.  
  128. DWORD WINAPI KeysThread(LPVOID lpThreadParameter) {
  129.     while (true) {
  130.         if (GetAsyncKeyState(VK_HOME) & 1) {
  131.             setAllToSpeed(1.0);
  132.         }
  133.         if (GetAsyncKeyState(VK_END) & 1) {
  134.             setAllToSpeed(5.0);
  135.         }
  136.  
  137.         Sleep(1);
  138.     }
  139.  
  140.     return 0;
  141. }
  142.  
  143.  
  144. DWORD WINAPI GetTickCountHacked(void) {
  145.     return g_speedHack.getCurrentTime(g_GetTickCountOriginal());
  146. }
  147.  
  148. ULONGLONG WINAPI GetTickCount64Hacked(void) {
  149.     return g_speedHackULL.getCurrentTime(g_GetTickCount64Original());
  150. }
  151.  
  152. BOOL WINAPI QueryPerformanceCounterHacked(LARGE_INTEGER *lpPerformanceCount) {
  153.     LARGE_INTEGER performanceCounter;
  154.  
  155.     BOOL result = g_QueryPerformanceCounterOriginal(&performanceCounter);
  156.  
  157.     lpPerformanceCount->QuadPart = g_speedHackLL.getCurrentTime(performanceCounter.QuadPart);
  158.  
  159.     return result;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement