2018-11-12 20:29:46 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2018-11-08 20:57:01 +00:00
|
|
|
#include "hook/table.h"
|
|
|
|
|
|
|
|
#include "util/dprintf.h"
|
|
|
|
|
2018-11-12 20:29:46 +00:00
|
|
|
static void WINAPI my_GetSystemTimeAsFileTime(FILETIME *out);
|
|
|
|
static BOOL WINAPI my_GetLocalTime(SYSTEMTIME *out);
|
|
|
|
static BOOL WINAPI my_GetSystemTime(SYSTEMTIME *out);
|
|
|
|
static DWORD WINAPI my_GetTimeZoneInformation(TIME_ZONE_INFORMATION *tzinfo);
|
|
|
|
static BOOL WINAPI my_SetSystemTime(SYSTEMTIME *in);
|
|
|
|
static BOOL WINAPI my_SetTimeZoneInformation(TIME_ZONE_INFORMATION *tzinfo);
|
|
|
|
|
|
|
|
static BOOL (WINAPI * next_GetSystemTimeAsFileTime)(FILETIME *out);
|
|
|
|
static int64_t clock_current_day;
|
|
|
|
|
2018-11-23 19:23:16 +00:00
|
|
|
static const struct hook_symbol clock_hook_syms[] = {
|
2018-11-12 20:29:46 +00:00
|
|
|
{
|
|
|
|
.name = "GetSystemTimeAsFileTime",
|
|
|
|
.patch = my_GetSystemTimeAsFileTime,
|
|
|
|
.link = (void **) &next_GetSystemTimeAsFileTime,
|
2018-11-23 19:23:16 +00:00
|
|
|
}, {
|
2018-11-12 20:29:46 +00:00
|
|
|
.name = "GetLocalTime",
|
|
|
|
.patch = my_GetLocalTime,
|
|
|
|
}, {
|
|
|
|
.name = "GetSystemTime",
|
|
|
|
.patch = my_GetSystemTime,
|
|
|
|
}, {
|
|
|
|
.name = "GetTimeZoneInformation",
|
|
|
|
.patch = my_GetTimeZoneInformation,
|
2018-11-23 19:23:16 +00:00
|
|
|
}, {
|
2018-11-08 20:57:01 +00:00
|
|
|
.name = "SetSystemTime",
|
|
|
|
.patch = my_SetSystemTime,
|
|
|
|
}, {
|
|
|
|
.name = "SetTimeZoneInformation",
|
|
|
|
.patch = my_SetTimeZoneInformation,
|
2018-11-12 20:29:46 +00:00
|
|
|
},
|
2018-11-08 20:57:01 +00:00
|
|
|
};
|
|
|
|
|
2018-11-12 20:29:46 +00:00
|
|
|
/* FILETIME is expressed in 100ns i.e. 0.1us i.e. 10^-7 sec units.
|
|
|
|
No official name for these units is given so let's call them "jiffies". */
|
|
|
|
|
2018-11-23 23:23:41 +00:00
|
|
|
#define jiffies_per_sec 10000000LL
|
|
|
|
#define jiffies_per_hour (jiffies_per_sec * 3600LL)
|
|
|
|
#define jiffies_per_day (jiffies_per_hour * 24LL)
|
2018-11-12 20:29:46 +00:00
|
|
|
|
|
|
|
static void WINAPI my_GetSystemTimeAsFileTime(FILETIME *out)
|
2018-11-08 20:57:01 +00:00
|
|
|
{
|
2018-11-12 20:29:46 +00:00
|
|
|
FILETIME in;
|
|
|
|
int64_t day;
|
|
|
|
int64_t real_jiffies;
|
|
|
|
int64_t real_jiffies_biased;
|
|
|
|
int64_t real_time;
|
|
|
|
int64_t fake_time;
|
|
|
|
int64_t fake_jiffies_biased;
|
|
|
|
int64_t fake_jiffies;
|
|
|
|
|
|
|
|
if (out == NULL) {
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get and convert real jiffies */
|
|
|
|
|
|
|
|
next_GetSystemTimeAsFileTime(&in);
|
|
|
|
real_jiffies = (((int64_t) in.dwHighDateTime) << 32) | in.dwLowDateTime;
|
|
|
|
|
|
|
|
/* Apply bias: shift [02:00, 07:00) to [19:00, 24:00) */
|
|
|
|
|
|
|
|
real_jiffies_biased = real_jiffies + 17LL * jiffies_per_hour;
|
|
|
|
|
|
|
|
/* Split date and time */
|
|
|
|
|
|
|
|
day = real_jiffies_biased / jiffies_per_day;
|
|
|
|
real_time = real_jiffies_biased % jiffies_per_day;
|
|
|
|
|
|
|
|
/* Debug log */
|
|
|
|
|
|
|
|
if (clock_current_day != 0 && clock_current_day != day) {
|
|
|
|
dprintf("\n*** CLOCK JUMP! ***\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
clock_current_day = day;
|
|
|
|
|
|
|
|
/* We want to skip the final five hours, so scale time-of-day by 19/24. */
|
|
|
|
|
|
|
|
fake_time = (real_time * 19LL) / 24LL;
|
|
|
|
|
|
|
|
/* Un-split date and time */
|
|
|
|
|
|
|
|
fake_jiffies_biased = day * jiffies_per_day + fake_time;
|
|
|
|
|
|
|
|
/* Remove bias */
|
|
|
|
|
|
|
|
fake_jiffies = fake_jiffies_biased - 17LL * jiffies_per_hour;
|
|
|
|
|
|
|
|
/* Return result */
|
|
|
|
|
|
|
|
out->dwLowDateTime = fake_jiffies;
|
|
|
|
out->dwHighDateTime = fake_jiffies >> 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI my_GetLocalTime(SYSTEMTIME *out)
|
|
|
|
{
|
|
|
|
/* Use UTC to simplify things */
|
|
|
|
return my_GetSystemTime(out);
|
2018-11-08 20:57:01 +00:00
|
|
|
}
|
|
|
|
|
2018-11-12 20:29:46 +00:00
|
|
|
static BOOL WINAPI my_GetSystemTime(SYSTEMTIME *out)
|
|
|
|
{
|
|
|
|
FILETIME linear;
|
|
|
|
BOOL ok;
|
|
|
|
|
|
|
|
my_GetSystemTimeAsFileTime(&linear);
|
|
|
|
ok = FileTimeToSystemTime(&linear, out);
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static int last_second;
|
|
|
|
|
|
|
|
if (out->wSecond != last_second) {
|
|
|
|
dprintf("%04i/%02i/%02i %02i:%02i:%02i\n",
|
|
|
|
out->wYear,
|
|
|
|
out->wMonth,
|
|
|
|
out->wDay,
|
|
|
|
out->wHour,
|
|
|
|
out->wMinute,
|
|
|
|
out->wSecond);
|
|
|
|
}
|
|
|
|
|
|
|
|
last_second = out->wSecond;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD WINAPI my_GetTimeZoneInformation(TIME_ZONE_INFORMATION *tzinfo)
|
|
|
|
{
|
|
|
|
/* Use UTC to simplify things */
|
|
|
|
|
|
|
|
dprintf("%s\n", __func__);
|
|
|
|
|
|
|
|
if (tzinfo != NULL) {
|
|
|
|
memset(tzinfo, 0, sizeof(*tzinfo));
|
|
|
|
SetLastError(ERROR_SUCCESS);
|
|
|
|
|
|
|
|
return TIME_ZONE_ID_UNKNOWN;
|
|
|
|
} else {
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
|
|
|
|
return TIME_ZONE_ID_INVALID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI my_SetSystemTime(SYSTEMTIME *in)
|
2018-11-08 20:57:01 +00:00
|
|
|
{
|
|
|
|
dprintf("Prevented application from screwing with the system clock\n");
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-11-12 20:29:46 +00:00
|
|
|
static BOOL WINAPI my_SetTimeZoneInformation(TIME_ZONE_INFORMATION *in)
|
2018-11-08 20:57:01 +00:00
|
|
|
{
|
|
|
|
dprintf("Prevented application from screwing with the timezone\n");
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2018-11-12 20:29:46 +00:00
|
|
|
|
2018-11-23 19:23:16 +00:00
|
|
|
void clock_hook_init(void)
|
2018-11-12 20:29:46 +00:00
|
|
|
{
|
|
|
|
hook_table_apply(
|
|
|
|
NULL,
|
|
|
|
"kernel32.dll",
|
2018-11-23 19:23:16 +00:00
|
|
|
clock_hook_syms,
|
|
|
|
_countof(clock_hook_syms));
|
2018-11-12 20:29:46 +00:00
|
|
|
}
|