forked from Lyq1st/chromeExtInstaller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extInstaller.h
236 lines (206 loc) · 10.1 KB
/
extInstaller.h
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/********************************************************************
Created: 2014/10/12: 12:55
FileName: extInstaller.h
Author: Lyq ([email protected], [email protected])
Version: 1.0
Purpose:
*********************************************************************/
#pragma once
#define CHROME_SEC_PREFERENCE_PATH _T("\\Google\\Chrome\\User Data\\Default\\Secure Preferences")
#define CHROME_PREFERENCE_PATH _T("\\Google\\Chrome\\User Data\\Default\\Preferences")
#define CHROME_STATEFILE_PATH _T("\\Google\\Chrome\\User Data\\Local State")
#define CHROME_DEFAULT_DIR_PATH _T("\\Google\\Chrome\\User Data\\Default")
#define CHROME_REG_PATH "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe"
/********************************************************************
HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\com.my_company.my_application
or HKEY_CURRENT_USER\SOFTWARE\Google\Chrome\NativeMessagingHosts\com.my_company.my_application,
on x64 also can use
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Google\Chrome\NativeMessagingHosts\com.my_company.my_application
*********************************************************************/
#define CHROME_EXT_64BIT_REG_PATH _T("SOFTWARE\\Wow6432Node\\Google\\Chrome\\Extensions\\")
#define CHROME_EXT_32BIT_REG_PATH _T("SOFTWARE\\Google\\Chrome\\Extensions\\")
//HKEY_LOCAL_MACHINE
#define CHROME_UNINSTALL_REG_PATH "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Google Chrome"
#define CHROME_UNINSTALL_REG_PATH_T _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Google Chrome")
//HKEY_CURRENT_USER
//define some const string
#define CHROME_EXTENSION_NODE "extensions"
#define CHROME_SETTING_NODE "settings"
#define CHROME_VERSION_NODE "last_chrome_version"
#define CHROME_PLUGIN_NODE "plugins"
#define CHROME_STATE_NODE "state"
#define CHROME_APPDIR_PATH_NODE "last_internal_directory"
#define CHROME_PROTECTION_NODE "protection"
#define CHROME_MACS_NODE "macs"
#define CHROME_STATE_LAYER_1 "profile"
#define CHROME_STATE_LAYER_2 "preference_hashes"
#define ZIP_HEADER_FLAG "PK"
#define CHROME_BEFORE33_37_KEY_WORD "PrefMetricsService"
//resources file
#define CHROME_PAK_FILE "\\resources.pak"
//extension path
#define CHROME_EXTENSION_ROOT _T("\\Google\\Chrome\\User Data\\Default\\Extensions")
//define some const int value
#define ASCII_FS 13
#define ASCII_SUB 32
#define MAX_MACHINE_ID_LEN 128
#define CHROME_CRX_HEADER_LEN 0x235
#define CHROME_SUPPORT_LAST_VER 47
#define CHROME_SUPPORT_SEC_MINVER 37
#define CHROME_SUPPORT_SLIENT_VER 33
//native message passing support from version 28
#define CHROME_SUPPORT_MINVER 28
//currently, our chrome extension support from 31
#define CHROME_SUPPORT_CRX_MINVER 31
//update config
#define CHROME_UPDATE_URL_VALUE_NAME _T("update_url")
#define CHROME_UPDATE_URL_VALUE _T("https://clients2.google.com/service/update2/crx")
//rlz dll path
#define RLZDLL_PATH _T("rlz_id.dll")
/********************************************************************
********************************************************************
change these config to adapt your own extension
*********************************************************************
********************************************************************/
//your extension filename
#define SAMPLE_CRX_PATH _T("BDChromeExt.crx")
//extension tmp file, any name you like
#define TMP_CRX_ZIP_PATH _T("BDChromeExt.zip")
//your extension id
#define CHROME_SAMPLE_CRX_ID_A "fbdfdjidlbaaclpflhcdceeoemlkbehd"
#define CHROME_SAMPLE_CRX_ID_W _T("fbdfdjidlbaaclpflhcdceeoemlkbehd")
//extension version, use this create extension unzip path, format is
//in the json configure, if the key version is x.y. this value is x.y_0,
//for example, the version is 1.0.5, this value is 1.0.5_0, the version
//1.2, this value is 1.2_0
#define CHROME_SAMPLE_CRX_VER _T("1.0.5_0")
/********************************************************************
install extension by chrome extension store or drag *.crx file on chrome
extension tab page, then you can get the config generate by chrome
in 'Secure Preferences' file
Tips:You should remove the null value in the json format configure, and remove space, CRLF not in value which between key and value, then escape the '"' and '\'.
*********************************************************************/
#define CHROME_SAMPLE_EXT_OL_CONFIG_W _T("{\"active_permissions\":{\"api\":[\"nativeMessaging\",\"tabs\",\"webNavigation\"],\"explicit_host\":[\"http://*/*\",\"https://*/*\"]},\"creation_flags\":9,\"ephemeral_app\":false,\"from_bookmark\":false,\"from_webstore\":true,\"granted_permissions\":{\"api\":[\"nativeMessaging\",\"tabs\",\"webNavigation\"],\"explicit_host\":[\"http://*/*\",\"https://*/*\"]},\"initial_keybindings_set\":true,\"install_time\":\"13059566385771333\",\"location\":1,\"manifest\":{\"background\":{\"scripts\":[\"background.js\"]},\"description\":\"百度杀毒,保护您的上网安全\",\"icons\":{\"128\":\"images/install_logo.png\",\"16\":\"images/menu_logo.png\",\"48\":\"images/extension_logo.png\"},\"key\":\"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAooTracdic3lXa6l9nIxyLuWD2FDG1MMmzkNwUidGYI/a4aF9jisYnEMYxKJzHtk0Jh/13z9BjWWqm5ZUZbQaEKdmzcJA/aAlYe2rXOPpcoVw8XpVsZc9mKk9qz95DMmH1R4ENobhRfWsLlofdnAA15SqigHoX5/JdwLtzhhBCQiZBpWA8nh4Eozi+Th47+UyNvS9VPbQEQqGIIIwtd4YHGG8xZ/KymGEmc9v5Rh52DNzb2bq6QlMrEQee9+tLc+qy3M7BLD3qfRt4m2g+4faF3SuCACUlPNO0BF8yE7EG/iGcnszKDYj7ya82CGGl7not9A1tQ8zFft/koGRKd6o5wIDAQAB\",\"manifest_version\":2,\"name\":\"百度杀毒上网保护\",\"permissions\":[\"tabs\",\"webNavigation\",\"http://*/*\",\"https://*/*\",\"nativeMessaging\"],\"update_url\":\"https://clients2.google.com/service/update2/crx\",\"version\":\"1.0.5\"},\"path\":\"fbdfdjidlbaaclpflhcdceeoemlkbehd\\\\1.0.5_0\",\"state\":1,\"was_installed_by_default\":false,\"was_installed_by_oem\":false}")
/********************************************************************
if you have native application, change these configures
*********************************************************************/
//native application path
#define NAPP_PATH _T("ChromeNativeMessage.exe")
//register name
#define CHROME_NATIVEAPP_REG_PATH _T("SOFTWARE\\Google\\Chrome\\NativeMessagingHosts\\com.baidu.antivirus")
//host name
#define CHROME_SAMPLE_NATIVE_HOST_NAME "com.baidu.antivirus"
//host configure file path
#define CHROME_SAMPLE_NATIVE_APPCONFIG_FILE _T("\\com.baidu.antivirus.json")
//for test, just ignore it
#define TEST_CRX
#ifdef _TEST_CRX
#define _JSON_NODE_STRING _T("")
#define CHROME_SEC_PREFERENCE_PATH _T("\\Google\\Chrome\\User Data\\Default\\Secure Preferences_180_en")
#define CHROME_TEST_CONFIG_PATH _T("C:\\test.config")
#endif
#define CHROME_SEC_PREFERENCE_GEN_PATH _T("\\Google\\Chrome\\User Data\\Default\\Secure Preferences test")
#define CHROME_TEST_CRX_PATH "afbbkciigbkkonnbcagfkobemjhehfem"
#define CHROME_SAMPLE_CRX_ID_T _T("fbdfdjidlbaaclpflhcdceeoemlkbehd")
/////
#include "stdafx.h"
#include <shlobj.h>
#include <Shlwapi.h>
#include <Tlhelp32.h>
#include <Psapi.h>
#include "hmac_sha2.h"
#include "json/json.h"
#include "extInstallerAdapter.h"
#include "ZLib.h"
#include <fstream>
//using namespace std;
enum CHROME_EXT_INSTALL_ERRCODE
{
CHROME_EXT_PARSERES_SUCCESS = 0,
CHROME_EXT_NO_SEED = 1,
CHROME_EXT_PARSERES_FAILED = 2,
CHROME_EXT_OPENREG_FAILED = 3,
};
namespace chromeExtInstaller{
;
class CExtInstaller
{
public:
CExtInstaller();
~CExtInstaller();
//method
public:
bool InitEnv();
bool RegisterNativeApp();
bool InstallExtByRegister();
bool ForceFix();
bool FixCHROMEExtReg();
bool UnInstallCRX();
bool UnInstallNativeApp();
static std::string GetFileData (tstring szFileName);
protected:
private:
bool ClearExtRegister();
bool GetChromeResPathByPrefs(std::string &chromeResPath);
bool GetChromeVersion(std::string &chromeVersion);
int GetChromeMajorVersion(std::string chromeVersion);
tstring GetChromePreferencePath(bool bIsSecFile = true);
tstring GetChromeStateFilePath();
BOOL GetInstallPath(const std::string& regedit_path ,std::string& install_path);
bool GetChromeExePath(tstring& chmexe_path);
tstring GetChromeExtensionRoot();
bool PromotePrivilege(bool bEnable);
tstring CExtInstaller::GetChromeExtPath(tstring extID, tstring extVersion);
bool GetChromeResPathByReg(std::string &chromeAppPath, std::string chromeVersion);
bool IsExtEnable(int majorVersion);
std::string GenerateMessagePath();
std::string CExtInstaller::FormatJsonConfig(std::string config);
std::string GenerateExtConfigHMAC(std::string device_id,
std::string path,
std::string plaint,
std::string pre_hash_seed_bin);
tstring GetNativeAppConfigPath();
bool GenerateNativeAppConfig();
BOOL CreateDeepDirectory(LPCTSTR lpPathName,
LPSECURITY_ATTRIBUTES lpSecurityAttributes );
bool Is64System();
bool IsChromeRun();
bool GetMachineId(std::string &machine_id,int version);
void GenerateDeviceIdLikePrefMetricsServiceDid(std::string &machine_id);
CHROME_EXT_INSTALL_ERRCODE parsePAKRes(std::string &pre_hash_seed_bin ,std::string chromeResPath,int sourceKey,bool bByID= false);
CHROME_EXT_INSTALL_ERRCODE GetHMACSeed(std::string &pre_hash_seed_bin ,std::string chromeResPath, int version);
bool InstallCRX();
std::string GenerateHMAC(unsigned char *digest,
unsigned int digest_size,
bool isUpper = true);
void test2(std::string plaint);
//
public:
protected:
private:
std::string m_extConfigHMAC;
std::string m_chromeVersion;
std::string m_chromeResPath;
tstring m_CurrentDir;
};
bool UnicodeToMultiByte(const wchar_t* strUnicode, size_t strUnicodeLen, std::string& strUTF8, UINT uCodePage);
inline bool UnicodeToMultiByte(const wchar_t* strUnicode, size_t strUnicodeLen, std::string& strUTF8, UINT uCodePage)
{
int nUTF8Count = ::WideCharToMultiByte(uCodePage, 0, strUnicode, (int) strUnicodeLen, NULL, 0, NULL, NULL);
if (nUTF8Count <= 0)
return false;
if ((int) strUnicodeLen == -1)
strUTF8.resize(nUTF8Count - 1);
else
strUTF8.resize(nUTF8Count);
return ::WideCharToMultiByte(uCodePage, 0, strUnicode, (int) strUnicodeLen, &strUTF8[0], nUTF8Count, NULL, NULL) >= 0;
}
std::string WtoA(const std::wstring &input);
inline std::string WtoA(const std::wstring &input)
{
std::string strDest;
UnicodeToMultiByte(input.c_str(), input.length(), strDest, CP_UTF8);
return strDest;
}
CExtInstaller * GetExtInstaller();
}//namespace chromeExtInstaller