Skip to content

Commit 7fd98e0

Browse files
committed
Upgrade to v2.0
Set Timestamp address dynamically Add functions to hook Fix memory leaks/bugs Reconstruct the code Update README
1 parent 4b891bb commit 7fd98e0

File tree

10 files changed

+379
-280
lines changed

10 files changed

+379
-280
lines changed

HookSigntool.sln

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.28307.271
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29306.81
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "HookSigntool", "HookSigntool\HookSigntool.vcxproj", "{E3ACE1E9-7437-4DA6-8B12-1A9A1870AF33}"
77
EndProject
8-
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Test", "Test\Test.vcxproj", "{0FC1783D-6008-4BD2-99FE-37F80A6B3D2A}"
9-
EndProject
108
Global
119
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1210
Debug|x64 = Debug|x64
@@ -23,14 +21,6 @@ Global
2321
{E3ACE1E9-7437-4DA6-8B12-1A9A1870AF33}.Release|x64.Build.0 = Release|x64
2422
{E3ACE1E9-7437-4DA6-8B12-1A9A1870AF33}.Release|x86.ActiveCfg = Release|Win32
2523
{E3ACE1E9-7437-4DA6-8B12-1A9A1870AF33}.Release|x86.Build.0 = Release|Win32
26-
{0FC1783D-6008-4BD2-99FE-37F80A6B3D2A}.Debug|x64.ActiveCfg = Debug|x64
27-
{0FC1783D-6008-4BD2-99FE-37F80A6B3D2A}.Debug|x64.Build.0 = Debug|x64
28-
{0FC1783D-6008-4BD2-99FE-37F80A6B3D2A}.Debug|x86.ActiveCfg = Debug|Win32
29-
{0FC1783D-6008-4BD2-99FE-37F80A6B3D2A}.Debug|x86.Build.0 = Debug|Win32
30-
{0FC1783D-6008-4BD2-99FE-37F80A6B3D2A}.Release|x64.ActiveCfg = Release|x64
31-
{0FC1783D-6008-4BD2-99FE-37F80A6B3D2A}.Release|x64.Build.0 = Release|x64
32-
{0FC1783D-6008-4BD2-99FE-37F80A6B3D2A}.Release|x86.ActiveCfg = Release|Win32
33-
{0FC1783D-6008-4BD2-99FE-37F80A6B3D2A}.Release|x86.Build.0 = Release|Win32
3424
EndGlobalSection
3525
GlobalSection(SolutionProperties) = preSolution
3626
HideSolutionNode = FALSE

HookSigntool/HookSigntool.vcxproj

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,32 @@
2222
<VCProjectVersion>15.0</VCProjectVersion>
2323
<ProjectGuid>{E3ACE1E9-7437-4DA6-8B12-1A9A1870AF33}</ProjectGuid>
2424
<RootNamespace>HookSigntool</RootNamespace>
25-
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
25+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
2626
</PropertyGroup>
2727
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
2828
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
2929
<ConfigurationType>DynamicLibrary</ConfigurationType>
3030
<UseDebugLibraries>true</UseDebugLibraries>
31-
<PlatformToolset>v141</PlatformToolset>
31+
<PlatformToolset>v142</PlatformToolset>
3232
<CharacterSet>MultiByte</CharacterSet>
3333
</PropertyGroup>
3434
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
3535
<ConfigurationType>DynamicLibrary</ConfigurationType>
3636
<UseDebugLibraries>false</UseDebugLibraries>
37-
<PlatformToolset>v141</PlatformToolset>
37+
<PlatformToolset>v142</PlatformToolset>
3838
<WholeProgramOptimization>true</WholeProgramOptimization>
3939
<CharacterSet>MultiByte</CharacterSet>
4040
</PropertyGroup>
4141
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
4242
<ConfigurationType>DynamicLibrary</ConfigurationType>
4343
<UseDebugLibraries>true</UseDebugLibraries>
44-
<PlatformToolset>v141</PlatformToolset>
44+
<PlatformToolset>v142</PlatformToolset>
4545
<CharacterSet>MultiByte</CharacterSet>
4646
</PropertyGroup>
4747
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
4848
<ConfigurationType>DynamicLibrary</ConfigurationType>
4949
<UseDebugLibraries>false</UseDebugLibraries>
50-
<PlatformToolset>v141</PlatformToolset>
50+
<PlatformToolset>v142</PlatformToolset>
5151
<WholeProgramOptimization>true</WholeProgramOptimization>
5252
<CharacterSet>MultiByte</CharacterSet>
5353
</PropertyGroup>
@@ -117,6 +117,9 @@
117117
<ItemGroup>
118118
<ClCompile Include="main.cpp" />
119119
</ItemGroup>
120+
<ItemGroup>
121+
<ClInclude Include="mssign32.h" />
122+
</ItemGroup>
120123
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
121124
<ImportGroup Label="ExtensionTargets">
122125
</ImportGroup>

HookSigntool/HookSigntool.vcxproj.filters

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@
1919
<Filter>源文件</Filter>
2020
</ClCompile>
2121
</ItemGroup>
22+
<ItemGroup>
23+
<ClInclude Include="mssign32.h">
24+
<Filter>源文件</Filter>
25+
</ClInclude>
26+
</ItemGroup>
2227
</Project>

HookSigntool/main.cpp

Lines changed: 148 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -2,127 +2,175 @@
22
#define _CRT_SECURE_NO_WARNINGS
33

44
#include <Windows.h>
5-
#include <wincrypt.h>
6-
#include <stdlib.h>
5+
#include <wchar.h>
76
#include <detours.h>
8-
#include <direct.h>
9-
#include <io.h>
10-
#include<stdio.h>
7+
#include "mssign32.h"
118

9+
HMODULE hModCrypt32 = NULL, hModMssign32 = NULL, hModKernel32 = NULL;
1210
using fntCertVerifyTimeValidity = decltype(CertVerifyTimeValidity);
11+
using fntSignerSign = decltype(SignerSign);
12+
using fntSignerTimeStamp = decltype(SignerTimeStamp);
1313
using fntGetLocalTime = decltype(GetLocalTime);
14+
fntCertVerifyTimeValidity* pOldCertVerifyTimeValidity = NULL;
15+
fntSignerSign* pOldSignerSign = NULL;
16+
fntSignerTimeStamp* pOldSignerTimeStamp = NULL;
17+
fntGetLocalTime* pOldGetLocalTime = NULL;
1418

15-
int year = -1;
16-
int month = -1;
17-
int day = -1;
18-
int dayofweek = -1;
19-
int hour = -1;
20-
int minute = -1;
21-
int second = -1;
22-
int milliseconds = -1;
23-
24-
fntCertVerifyTimeValidity *pOldCertVerifyTimeValidity = NULL;
25-
fntGetLocalTime *pOldGetLocalTime = NULL;
19+
int year = -1, month = -1, day = -1, hour = -1, minute = -1, second = -1;
20+
WCHAR lpTimestamp[20];
2621

22+
LPCWSTR ReplaceTimeStamp(LPCWSTR lpOriginalTS) {
23+
if (!lpOriginalTS)
24+
return NULL;
25+
LPWSTR buf = new WCHAR[65];
26+
memset(buf, 0, sizeof(WCHAR) * 65);
27+
if (!_wcsicmp(lpOriginalTS, L"{CustomTimestampMarker-SHA1}")) {
28+
wcscat(buf, L"http://timestamp.pki.jemmylovejenny.tk/SHA1/");
29+
wcscat(buf, lpTimestamp);
30+
return buf;
31+
}
32+
else if (!_wcsicmp(lpOriginalTS, L"{CustomTimestampMarker-SHA256}")) {
33+
wcscat(buf, L"http://timestamp.pki.jemmylovejenny.tk/SHA256/");
34+
wcscat(buf, lpTimestamp);
35+
return buf;
36+
}
37+
else {
38+
return lpOriginalTS;
39+
}
40+
}
2741
LONG WINAPI NewCertVerifyTimeValidity(
28-
LPFILETIME pTimeToVerify,
29-
PCERT_INFO pCertInfo
42+
LPFILETIME pTimeToVerify,
43+
PCERT_INFO pCertInfo
3044
)
3145
{
32-
return 0;
46+
return 0;
47+
}
48+
HRESULT WINAPI NewSignerSign(
49+
_In_ SIGNER_SUBJECT_INFO* pSubjectInfo,
50+
_In_ SIGNER_CERT* pSignerCert,
51+
_In_ SIGNER_SIGNATURE_INFO* pSignatureInfo,
52+
_In_opt_ SIGNER_PROVIDER_INFO* pProviderInfo,
53+
_In_opt_ LPCWSTR pwszHttpTimeStamp,
54+
_In_opt_ PCRYPT_ATTRIBUTES psRequest,
55+
_In_opt_ LPVOID pSipData
56+
)
57+
{
58+
return (*pOldSignerSign)(pSubjectInfo, pSignerCert, pSignatureInfo, pProviderInfo, ReplaceTimeStamp(pwszHttpTimeStamp), psRequest, pSipData);
59+
}
60+
HRESULT WINAPI NewSignerTimeStamp(
61+
_In_ SIGNER_SUBJECT_INFO* pSubjectInfo,
62+
_In_ LPCWSTR pwszHttpTimeStamp,
63+
_In_opt_ PCRYPT_ATTRIBUTES psRequest,
64+
_In_opt_ LPVOID pSipData
65+
)
66+
{
67+
return (*pOldSignerTimeStamp)(pSubjectInfo, ReplaceTimeStamp(pwszHttpTimeStamp), psRequest, pSipData);
3368
}
34-
3569
void WINAPI NewGetLocalTime(
36-
LPSYSTEMTIME lpSystemTime
70+
LPSYSTEMTIME lpSystemTime
3771
)
3872
{
39-
pOldGetLocalTime(lpSystemTime);
40-
if (year >= 0)
41-
lpSystemTime->wYear = year;
42-
if (month >= 0)
43-
lpSystemTime->wMonth = month;
44-
if (day >= 0)
45-
lpSystemTime->wDay = day;
46-
if (dayofweek >= 0)
47-
lpSystemTime->wDayOfWeek = dayofweek;
48-
if (hour >= 0)
49-
lpSystemTime->wHour = hour;
50-
if (minute >= 0)
51-
lpSystemTime->wMinute = minute;
52-
if (second >= 0)
53-
lpSystemTime->wSecond = second;
54-
if (milliseconds >= 0)
55-
lpSystemTime->wMilliseconds = milliseconds;
73+
(*pOldGetLocalTime)(lpSystemTime);
74+
if (year >= 0)
75+
lpSystemTime->wYear = year;
76+
if (month >= 0)
77+
lpSystemTime->wMonth = month;
78+
if (day >= 0)
79+
lpSystemTime->wDay = day;
80+
if (hour >= 0)
81+
lpSystemTime->wHour = hour;
82+
if (minute >= 0)
83+
lpSystemTime->wMinute = minute;
84+
if (second >= 0)
85+
lpSystemTime->wSecond = second;
5686
}
5787

58-
BOOL WINAPI DllMain(
59-
_In_ HINSTANCE hinstDLL,
60-
_In_ DWORD fdwReason,
61-
_In_ LPVOID lpvReserved)
88+
bool HookFunctions()
6289
{
63-
if (fdwReason == DLL_PROCESS_ATTACH)
64-
{
65-
//Get config file
66-
char buf[260];
67-
char* p = strstr(GetCommandLineA(), "-config ");
68-
if (p) {
69-
strcat_s(buf, p);
70-
}
71-
else {
72-
_getcwd(buf, sizeof(buf));
73-
strcat_s(buf, "\\hook.ini");
74-
}
75-
year = GetPrivateProfileInt("Time", "Year", -1, buf);
76-
month = GetPrivateProfileInt("Time", "Month", -1, buf);
77-
day = GetPrivateProfileInt("Time", "Day", -1, buf);
78-
dayofweek = GetPrivateProfileInt("Time", "DayOfWeek", -1, buf);
79-
hour = GetPrivateProfileInt("Time", "Hour", -1, buf);
80-
minute = GetPrivateProfileInt("Time", "Minute", -1, buf);
81-
second = GetPrivateProfileInt("Time", "Second", -1, buf);
82-
milliseconds = GetPrivateProfileInt("Time", "Milliseconds", -1, buf);
90+
if ((hModCrypt32 = LoadLibraryW(L"crypt32.dll")) == NULL
91+
|| (hModMssign32 = LoadLibraryW(L"mssign32.dll")) == NULL
92+
|| (hModKernel32 = LoadLibraryW(L"kernel32.dll")) == NULL)
93+
return false;
8394

84-
memset(buf, 0, sizeof(buf));
85-
strcpy_s(buf, getenv("APPDATA"));
86-
strcat_s(buf, "\\TrustAsia\\DSignTool\\hook");
95+
if ((pOldCertVerifyTimeValidity = (fntCertVerifyTimeValidity*)GetProcAddress(hModCrypt32, "CertVerifyTimeValidity")) == NULL
96+
|| (pOldSignerSign = (fntSignerSign*)GetProcAddress(hModMssign32, "SignerSign")) == NULL
97+
|| (pOldSignerTimeStamp = (fntSignerTimeStamp*)GetProcAddress(hModMssign32, "SignerTimeStamp")) == NULL
98+
|| (pOldGetLocalTime = (fntGetLocalTime*)GetProcAddress(hModKernel32, "GetLocalTime")) == NULL)
99+
return false;
87100

88-
//Check is first run
89-
if (_access(buf, 0)) {
90-
if (MessageBoxA(NULL, "欢迎使用JemmyLoveJenny修改版的数字签名工具\r\n初次使用时建议您先阅读README了解修改版的变化\r\n是否打开README?", "欢迎使用", MB_YESNO) == IDYES) {
91-
memset(buf, 0, sizeof(buf));
92-
_getcwd(buf, sizeof(buf));
93-
strcat_s(buf, "\\README.txt");
94-
if (!_access(buf, 0)) {
95-
char buf2[260];
96-
strcpy_s(buf2, "notepad.exe \"");
97-
strcat_s(buf2, buf);
98-
strcat_s(buf2, "\"");
99-
WinExec(buf2, 1);
100-
}
101-
else {
102-
strcat_s(buf, " 不存在");
103-
MessageBoxA(NULL, buf, "文件不存在", MB_OK | MB_ICONERROR);
104-
}
105-
}
106-
memset(buf, 0, sizeof(buf));
107-
strcpy_s(buf, getenv("APPDATA"));
108-
strcat_s(buf, "\\TrustAsia\\DSignTool\\hook");
109-
FILE *fp;
110-
fp = fopen(buf, "w+");
111-
fclose(fp);
112-
}
101+
if (DetourTransactionBegin() != NO_ERROR
102+
|| DetourAttach(&(PVOID&)pOldCertVerifyTimeValidity, NewCertVerifyTimeValidity) != NO_ERROR
103+
|| DetourAttach(&(PVOID&)pOldSignerSign, NewSignerSign) != NO_ERROR
104+
|| DetourAttach(&(PVOID&)pOldSignerTimeStamp, NewSignerTimeStamp) != NO_ERROR
105+
|| DetourAttach(&(PVOID&)pOldGetLocalTime, NewGetLocalTime) != NO_ERROR
106+
|| DetourTransactionCommit() != NO_ERROR)
107+
return false;
108+
return true;
109+
}
110+
bool ParseConfig(LPWSTR lpCommandLineConfig, LPWSTR lpCommandLineTimestamp)
111+
{
112+
LPWSTR buf = new WCHAR[260];
113+
memset(buf, 0, sizeof(WCHAR) * 260);
114+
if (_wgetcwd(buf, 260) == NULL)
115+
return false;
116+
wcscat(buf, L"\\");
117+
if (lpCommandLineConfig) {
118+
if ((wcschr(lpCommandLineConfig, L':') - lpCommandLineConfig) == 1) {
119+
memset(buf, 0, sizeof(WCHAR) * 260);
120+
wsprintfW(buf, lpCommandLineConfig);
121+
}
122+
else {
123+
wcscat(buf, lpCommandLineConfig);
124+
}
125+
}
126+
else {
127+
wcscat(buf, L"hook.ini");
128+
}
113129

114-
pOldCertVerifyTimeValidity = (fntCertVerifyTimeValidity *)GetProcAddress(LoadLibraryW(L"crypt32.dll"), "CertVerifyTimeValidity");
115-
pOldGetLocalTime = (fntGetLocalTime *)GetProcAddress(LoadLibraryW(L"kernel32.dll"), "GetLocalTime");
130+
year = GetPrivateProfileIntW(L"Time", L"Year", -1, buf);
131+
month = GetPrivateProfileIntW(L"Time", L"Month", -1, buf);
132+
day = GetPrivateProfileIntW(L"Time", L"Day", -1, buf);
133+
hour = GetPrivateProfileIntW(L"Time", L"Hour", -1, buf);
134+
minute = GetPrivateProfileIntW(L"Time", L"Minute", -1, buf);
135+
second = GetPrivateProfileIntW(L"Time", L"Second", -1, buf);
116136

117-
DetourTransactionBegin();
118-
DetourAttach(&(PVOID&)pOldCertVerifyTimeValidity, NewCertVerifyTimeValidity);
119-
DetourAttach(&(PVOID&)pOldGetLocalTime, NewGetLocalTime);
120-
DetourTransactionCommit();
121-
}
122-
return 1;
137+
if (lpCommandLineTimestamp)
138+
wsprintfW(lpTimestamp, lpCommandLineTimestamp);
139+
else
140+
GetPrivateProfileStringW(L"Timestamp", L"Timestamp", NULL, lpTimestamp, 20, buf);
141+
return true;
142+
}
143+
BOOL WINAPI DllMain(
144+
_In_ HINSTANCE hinstDLL,
145+
_In_ DWORD fdwReason,
146+
_In_ LPVOID lpvReserved
147+
)
148+
{
149+
if (fdwReason == DLL_PROCESS_ATTACH)
150+
{
151+
LPWSTR* szArglist = NULL;
152+
int nArgs = 0;
153+
szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
154+
155+
int iconfig = -1, its = -1;
156+
157+
for (int i = 0; i <= nArgs - 2; i++) {
158+
if (!wcscmp(szArglist[i], L"-config"))
159+
iconfig = i + 1;
160+
if (!wcscmp(szArglist[i], L"-ts"))
161+
its = i + 1;
162+
}
163+
if (!ParseConfig(iconfig >= 0 ? szArglist[iconfig] : NULL, its >= 0 ? szArglist[its] : NULL))
164+
MessageBoxW(NULL, L"配置初始化失败,请检查hook.ini和命令行参数!", L"初始化失败", MB_ICONERROR);
165+
LocalFree(szArglist);
166+
if (!HookFunctions())
167+
MessageBoxW(NULL, L"出现错误,无法Hook指定的函数\r\n请关闭程序重试!", L"Hook失败", MB_ICONERROR);
168+
MessageBoxW(NULL, lpTimestamp, L"自定义时间戳为", MB_OK);
169+
}
170+
return 1;
123171
}
124172

125173
extern "C" __declspec(dllexport) int attach()
126174
{
127-
return 0;
128-
}
175+
return 0;
176+
}

0 commit comments

Comments
 (0)