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 ;
1210using  fntCertVerifyTimeValidity = decltype (CertVerifyTimeValidity);
11+ using  fntSignerSign = decltype (SignerSign);
12+ using  fntSignerTimeStamp = decltype (SignerTimeStamp);
1313using  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+ }
2741LONG 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- 
3569void  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?" " 欢迎使用" 
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, " 文件不存在" 
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' :' 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" 初始化失败" 
165+         LocalFree (szArglist);
166+         if  (!HookFunctions ())
167+             MessageBoxW (NULL , L" 出现错误,无法Hook指定的函数\r\n 请关闭程序重试!" L" Hook失败" 
168+         MessageBoxW (NULL , lpTimestamp, L" 自定义时间戳为" 
169+     }
170+     return  1 ;
123171}
124172
125173extern  " C" int  attach ()
126174{
127- 	 return  0 ;
128- }
175+      return  0 ;
176+ }
0 commit comments