-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUptimeFaker.cpp
183 lines (148 loc) · 4.54 KB
/
UptimeFaker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// UptimeFaker.cpp : Defines the exported functions for the DLL application.
//
#include <Windows.h>
#include "Detours/detours.h"
#include <cstdint>
#ifdef _WIN64
#pragma comment(lib, "detours_x64.lib")
#else
#pragma comment(lib, "detours.lib")
#endif
int64_t AddedTimeInMS;
int64_t AddedTimeInQPCTicks;
static bool IsFunctionHooked( LPCWSTR dllName, LPCWSTR functionName )
{
return GetPrivateProfileIntW( dllName, functionName, 0, L".\\UptimeFaker.ini" ) != 0;
}
namespace Kernel32
{
HMODULE hModule;
decltype(::QueryPerformanceCounter)* OrgQueryPerformanceCounter;
BOOL WINAPI QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)
{
BOOL result = OrgQueryPerformanceCounter( lpPerformanceCount );
if ( result != 0 )
{
lpPerformanceCount->QuadPart += AddedTimeInQPCTicks;
}
return result;
}
decltype(::GetTickCount)* OrgGetTickCount;
DWORD WINAPI GetTickCount()
{
return static_cast<DWORD>(OrgGetTickCount() + AddedTimeInMS);
}
decltype(::GetTickCount64)* OrgGetTickCount64;
ULONGLONG WINAPI GetTickCount64()
{
return OrgGetTickCount64() + AddedTimeInMS;
}
void AttachModule()
{
hModule = LoadLibraryW( L"kernel32" );
if ( hModule != nullptr )
{
if ( IsFunctionHooked(L"kernel32", L"QueryPerformanceCounter") ) OrgQueryPerformanceCounter = (decltype(OrgQueryPerformanceCounter))GetProcAddress( hModule, "QueryPerformanceCounter" );
if ( IsFunctionHooked(L"kernel32", L"GetTickCount") ) OrgGetTickCount = (decltype(OrgGetTickCount))GetProcAddress( hModule, "GetTickCount" );
if ( IsFunctionHooked(L"kernel32", L"GetTickCount64") ) OrgGetTickCount64 = (decltype(OrgGetTickCount64))GetProcAddress( hModule, "GetTickCount64" );
DetourAttach( &(PVOID&)OrgQueryPerformanceCounter, QueryPerformanceCounter );
DetourAttach( &(PVOID&)OrgGetTickCount, GetTickCount );
DetourAttach( &(PVOID&)OrgGetTickCount64, GetTickCount64 );
}
}
void DetachModule()
{
DetourDetach( &(PVOID&)OrgQueryPerformanceCounter, QueryPerformanceCounter );
DetourDetach( &(PVOID&)OrgGetTickCount, GetTickCount );
DetourDetach( &(PVOID&)OrgGetTickCount64, GetTickCount64 );
}
void Free()
{
if ( hModule != nullptr )
{
FreeLibrary(hModule);
}
}
}
namespace Winmm
{
HMODULE hModule;
decltype(::timeGetTime)* OrgtimeGetTime;
DWORD WINAPI timeGetTime()
{
return static_cast<DWORD>(OrgtimeGetTime() + AddedTimeInMS);
}
decltype(::timeGetSystemTime)* OrgtimeGetSystemTime;
MMRESULT WINAPI timeGetSystemTime(LPMMTIME pmmt,UINT cbmmt)
{
MMRESULT result = OrgtimeGetSystemTime(pmmt, cbmmt);
if ( result == TIMERR_NOERROR )
{
if ( cbmmt == sizeof(MMTIME) && pmmt->wType == TIME_MS )
{
pmmt->u.ms += static_cast<DWORD>(AddedTimeInMS);
}
}
return result;
}
void AttachModule()
{
hModule = LoadLibraryW( L"winmm" );
if ( hModule != nullptr )
{
if ( IsFunctionHooked(L"winmm", L"timeGetTime") ) OrgtimeGetTime = (decltype(OrgtimeGetTime))GetProcAddress( hModule, "timeGetTime" );
if ( IsFunctionHooked(L"winmm", L"timeGetSystemTime") ) OrgtimeGetSystemTime = (decltype(OrgtimeGetSystemTime))GetProcAddress( hModule, "timeGetSystemTime" );
DetourAttach( &(PVOID&)OrgtimeGetTime, timeGetTime );
DetourAttach( &(PVOID&)OrgtimeGetSystemTime, timeGetSystemTime );
}
}
void DetachModule()
{
DetourDetach( &(PVOID&)OrgtimeGetTime, timeGetTime );
DetourDetach( &(PVOID&)OrgtimeGetSystemTime, timeGetSystemTime );
}
void Free()
{
if ( hModule != nullptr )
{
FreeLibrary(hModule);
}
}
}
void GetFakeTimeValues()
{
const int64_t AddedTimeInDays = static_cast<signed int>(GetPrivateProfileIntW( L"AddedUptime", L"AddUptimeDays", 0, L".\\UptimeFaker.ini" ));
// Calculate helper values
AddedTimeInMS = AddedTimeInDays * 24 * 60 * 60 * 1000;
LARGE_INTEGER QPCFreq;
::QueryPerformanceFrequency( &QPCFreq );
AddedTimeInQPCTicks = AddedTimeInDays * 24 * 60 * 60 * QPCFreq.QuadPart;
// Offset timers by PC uptime if ProcessTime option is enabled
if ( GetPrivateProfileIntW( L"AddedUptime", L"ProcessTime", 0, L".\\UptimeFaker.ini" ) != 0 )
{
LARGE_INTEGER QPCUptime;
::QueryPerformanceCounter( &QPCUptime );
AddedTimeInQPCTicks -= QPCUptime.QuadPart;
AddedTimeInMS -= ::GetTickCount64();
}
}
BOOL AttachFunctions()
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
Kernel32::AttachModule();
Winmm::AttachModule();
DetourTransactionCommit();
return TRUE;
}
BOOL DetachFunctions()
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
Winmm::DetachModule();
Kernel32::DetachModule();
DetourTransactionCommit();
Kernel32::Free();
Winmm::Free();
return TRUE;
}