Improve: Ask for running master with administrator

This commit is contained in:
yuanyuanxiang
2025-12-05 21:21:45 +01:00
parent 001f0682ae
commit 5d9854da11
6 changed files with 62 additions and 12 deletions

View File

@@ -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; // 服务命令已处理,退出

View File

@@ -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();

View File

@@ -6,6 +6,7 @@
#include "afxcmn.h"
#include "TrueColorToolBar.h"
#include "IOCPServer.h"
#include <common/location.h>
//////////////////////////////////////////////////////////////////////////
// 以下为特殊需求使用
@@ -106,6 +107,11 @@ enum {
class CSplashDlg; // 前向声明
inline std::string getHwFallback() {
IPConverter cvt;
return cvt.getPublicIP();
}
// CMy2015RemoteDlg 对话框
class CMy2015RemoteDlg : public CDialogEx
{

View File

@@ -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());

View File

@@ -89,13 +89,16 @@ std::string execCommand(const char* cmd)
}
// <20><>ȡӲ<C8A1><D3B2> ID<49><44>CPU + <20><><EFBFBD><EFBFBD> + Ӳ<>̣<EFBFBD>
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;

View File

@@ -2,10 +2,11 @@
#include <string>
typedef std::string(*fallback)();
// <20><><EFBFBD><EFBFBD><EFBFBD>ɷ<EFBFBD><C9B7><EFBFBD><EFBFBD>˹<EFBFBD><CBB9>ܽ<EFBFBD><DCBD>м<EFBFBD><D0BC><EFBFBD>
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);