diff --git a/server/2015Remote/2015Remote.cpp b/server/2015Remote/2015Remote.cpp index 09d9f34..4fe6f49 100644 --- a/server/2015Remote/2015Remote.cpp +++ b/server/2015Remote/2015Remote.cpp @@ -232,8 +232,45 @@ static BOOL IsAgentMode() // CMy2015RemoteApp 初始化 +BOOL IsRunningAsAdmin() +{ + BOOL isAdmin = FALSE; + PSID administratorsGroup = NULL; + + SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY; + if (AllocateAndInitializeSid(&NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, + 0, 0, 0, 0, 0, 0, &administratorsGroup)) { + if (!CheckTokenMembership(NULL, administratorsGroup, &isAdmin)) { + isAdmin = FALSE; + } + + FreeSid(administratorsGroup); + } + + return isAdmin; +} + +BOOL LaunchAsAdmin(const char* szFilePath, const char* verb) +{ + SHELLEXECUTEINFOA shExecInfo; + ZeroMemory(&shExecInfo, sizeof(SHELLEXECUTEINFOA)); + shExecInfo.cbSize = sizeof(SHELLEXECUTEINFOA); + shExecInfo.fMask = SEE_MASK_DEFAULT; + shExecInfo.hwnd = NULL; + shExecInfo.lpVerb = verb; + shExecInfo.lpFile = szFilePath; + shExecInfo.nShow = SW_NORMAL; + + return ShellExecuteExA(&shExecInfo); +} + BOOL CMy2015RemoteApp::InitInstance() { + char curFile[MAX_PATH] = { 0 }; + GetModuleFileNameA(NULL, curFile, MAX_PATH); + if (!IsRunningAsAdmin() && LaunchAsAdmin(curFile, "runas")) + return FALSE; + // 首先处理服务命令行参数 if (HandleServiceCommandLine()) { return FALSE; // 服务命令已处理,退出 diff --git a/server/2015Remote/2015RemoteDlg.cpp b/server/2015Remote/2015RemoteDlg.cpp index bb43068..fe2108f 100644 --- a/server/2015Remote/2015RemoteDlg.cpp +++ b/server/2015Remote/2015RemoteDlg.cpp @@ -1030,6 +1030,12 @@ BOOL CMy2015RemoteDlg::OnInitDialog() AUTO_TICK(500, ""); CDialogEx::OnInitDialog(); + UPDATE_SPLASH(15, "正在注册主控信息..."); + THIS_CFG.SetStr("settings", "MainWnd", std::to_string((uint64_t)GetSafeHwnd())); + THIS_CFG.SetStr("settings", "SN", getDeviceID(getHwFallback)); + THIS_CFG.SetStr("settings", "PwdHash", GetPwdHash()); + THIS_CFG.SetStr("settings", "MasterHash", GetMasterHash()); + UPDATE_SPLASH(20, "正在初始化文件上传模块..."); int ret = InitFileUpload(GetHMAC()); g_hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, AfxGetInstanceHandle(), 0); @@ -1199,10 +1205,6 @@ BOOL CMy2015RemoteDlg::OnInitDialog() OnCancel(); return FALSE; } - THIS_CFG.SetStr("settings", "MainWnd", std::to_string((uint64_t)GetSafeHwnd())); - THIS_CFG.SetStr("settings", "SN", getDeviceID()); - THIS_CFG.SetStr("settings", "PwdHash", GetPwdHash()); - THIS_CFG.SetStr("settings", "MasterHash", GetMasterHash()); UPDATE_SPLASH(100, "启动完成!"); CloseSplash(); @@ -1790,7 +1792,7 @@ bool CMy2015RemoteDlg::CheckValid(int trail) auto settings = "settings", pwdKey = "Password"; // 验证口令 CPasswordDlg dlg(this); - static std::string hardwareID = getHardwareID(); + static std::string hardwareID = getHardwareID(getHwFallback); static std::string hashedID = hashSHA256(hardwareID); static std::string deviceID = getFixedLengthID(hashedID); CString pwd = THIS_CFG.GetStr(settings, pwdKey, "").c_str(); @@ -2771,7 +2773,7 @@ LRESULT CMy2015RemoteDlg::ShareClient(WPARAM wParam, LPARAM lParam) void CMy2015RemoteDlg::OnToolAuth() { CPwdGenDlg dlg; - std::string hardwareID = getHardwareID(); + std::string hardwareID = getHardwareID(getHwFallback); std::string hashedID = hashSHA256(hardwareID); std::string deviceID = getFixedLengthID(hashedID); dlg.m_sDeviceID = deviceID.c_str(); diff --git a/server/2015Remote/2015RemoteDlg.h b/server/2015Remote/2015RemoteDlg.h index 40b0557..3d13a83 100644 --- a/server/2015Remote/2015RemoteDlg.h +++ b/server/2015Remote/2015RemoteDlg.h @@ -6,6 +6,7 @@ #include "afxcmn.h" #include "TrueColorToolBar.h" #include "IOCPServer.h" +#include ////////////////////////////////////////////////////////////////////////// // 以下为特殊需求使用 @@ -106,6 +107,11 @@ enum { class CSplashDlg; // 前向声明 +inline std::string getHwFallback() { + IPConverter cvt; + return cvt.getPublicIP(); +} + // CMy2015RemoteDlg 对话框 class CMy2015RemoteDlg : public CDialogEx { diff --git a/server/2015Remote/CPasswordDlg.cpp b/server/2015Remote/CPasswordDlg.cpp index c6588ad..859acb9 100644 --- a/server/2015Remote/CPasswordDlg.cpp +++ b/server/2015Remote/CPasswordDlg.cpp @@ -7,6 +7,7 @@ #include "pwd_gen.h" #include "2015Remote.h" #include "common/skCrypter.h" +#include "2015RemoteDlg.h" // CPasswordDlg 对话框 @@ -196,7 +197,7 @@ void CPwdGenDlg::OnBnClickedButtonGenkey() getFixedLengthID(finalKey); m_sPassword = fixedKey.c_str(); m_EditPassword.SetWindowTextA(fixedKey.c_str()); - std::string hardwareID = getHardwareID(); + std::string hardwareID = getHardwareID(getHwFallback); std::string hashedID = hashSHA256(hardwareID); std::string deviceID = getFixedLengthID(hashedID); std::string hmac = genHMAC(pwdHash, m_sUserPwd.GetString()); diff --git a/server/2015Remote/pwd_gen.cpp b/server/2015Remote/pwd_gen.cpp index 58259e1..8e15bbf 100644 --- a/server/2015Remote/pwd_gen.cpp +++ b/server/2015Remote/pwd_gen.cpp @@ -89,13 +89,16 @@ std::string execCommand(const char* cmd) } // ȡӲ IDCPU + + Ӳ̣ -std::string getHardwareID() +std::string getHardwareID(fallback fb) { std::string cpuID = execCommand("wmic cpu get processorid"); std::string boardID = execCommand("wmic baseboard get serialnumber"); std::string diskID = execCommand("wmic diskdrive get serialnumber"); std::string combinedID = cpuID + "|" + boardID + "|" + diskID; + if (fb && combinedID.find("ERROR") != std::string::npos) { + return fb(); + } return combinedID; } @@ -144,9 +147,9 @@ std::string deriveKey(const std::string& password, const std::string& hardwareID return hashSHA256(password + " + " + hardwareID); } -std::string getDeviceID() +std::string getDeviceID(fallback fb) { - static std::string hardwareID = getHardwareID(); + static std::string hardwareID = getHardwareID(fb); static std::string hashedID = hashSHA256(hardwareID); static std::string deviceID = getFixedLengthID(hashedID); return deviceID; diff --git a/server/2015Remote/pwd_gen.h b/server/2015Remote/pwd_gen.h index 469cc35..0a4d82c 100644 --- a/server/2015Remote/pwd_gen.h +++ b/server/2015Remote/pwd_gen.h @@ -2,10 +2,11 @@ #include +typedef std::string(*fallback)(); // ɷ˹ܽм -std::string getHardwareID(); +std::string getHardwareID(fallback fb = NULL); std::string hashSHA256(const std::string& data); @@ -15,7 +16,7 @@ std::string getFixedLengthID(const std::string& hash); std::string deriveKey(const std::string& password, const std::string& hardwareID); -std::string getDeviceID(); +std::string getDeviceID(fallback fb = NULL); // Use HMAC to sign a message. uint64_t SignMessage(const std::string& pwd, BYTE* msg, int len);