Implement service-generated authorization capability

This commit is contained in:
yuanyuanxiang
2025-04-16 23:31:34 +08:00
parent 6c4663751a
commit c6f70136e1
9 changed files with 395 additions and 4 deletions

Binary file not shown.

View File

@@ -21,6 +21,8 @@
#include <vector>
#include "KeyBoardDlg.h"
#include "InputDlg.h"
#include "CPasswordDlg.h"
#include "pwd_gen.h"
#ifdef _DEBUG
#define new DEBUG_NEW
@@ -899,15 +901,177 @@ VOID CMy2015RemoteDlg::OnOnlineKeyboardManager()
SendSelectedCommand(&bToken, sizeof(BYTE));
}
std::vector<std::string> splitString(const std::string& str, char delimiter) {
std::vector<std::string> result;
std::stringstream ss(str);
std::string item;
while (std::getline(ss, item, delimiter)) {
result.push_back(item);
}
return result;
}
std::string joinString(const std::vector<std::string>& tokens, char delimiter) {
std::ostringstream oss;
for (size_t i = 0; i < tokens.size(); ++i) {
oss << tokens[i];
if (i != tokens.size() - 1) { // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>Ԫ<EFBFBD>غ<EFBFBD><D8BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӷָ<D3B7><D6B8><EFBFBD>
oss << delimiter;
}
}
return oss.str();
}
#define REG_SETTINGS "Software\\YAMA\\Settings"
// д<><D0B4><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD>ֽڰ棩
bool WriteAppSettingA(const std::string& keyName, const std::string& value) {
HKEY hKey;
LONG result = RegCreateKeyExA(
HKEY_CURRENT_USER,
REG_SETTINGS,
0,
NULL,
0,
KEY_WRITE,
NULL,
&hKey,
NULL
);
if (result != ERROR_SUCCESS) {
Mprintf("<EFBFBD>޷<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: %d\n", result);
return false;
}
result = RegSetValueExA(
hKey,
keyName.c_str(),
0,
REG_SZ,
reinterpret_cast<const BYTE*>(value.c_str()),
static_cast<DWORD>(value.length() + 1)
);
RegCloseKey(hKey);
return result == ERROR_SUCCESS;
}
// <20><>ȡ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD>ֽڰ棩
bool ReadAppSettingA(const std::string& keyName, std::string& outValue) {
HKEY hKey;
LONG result = RegOpenKeyExA(
HKEY_CURRENT_USER,
REG_SETTINGS,
0,
KEY_READ,
&hKey
);
if (result != ERROR_SUCCESS) {
return false;
}
char buffer[256];
DWORD bufferSize = sizeof(buffer);
DWORD type = 0;
result = RegQueryValueExA(
hKey,
keyName.c_str(),
nullptr,
&type,
reinterpret_cast<LPBYTE>(buffer),
&bufferSize
);
RegCloseKey(hKey);
if (result == ERROR_SUCCESS && type == REG_SZ) {
outValue = buffer;
return true;
}
return false;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õĴ<C3B5><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͻ<EFBFBD><CFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EBA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD>ȥ<EFBFBD><C8A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܵ<EFBFBD><DCB5><EFBFBD>Ȩ<EFBFBD><C8A8><EFBFBD><EFBFBD>
int CanBuildClient() {
std::string freeTrail;
auto b = ReadAppSettingA("free_trial", freeTrail);
if (!b || freeTrail.empty())
freeTrail = "10";
return atoi(freeTrail.c_str());
}
bool UpdateFreeTrial(int n) {
return WriteAppSettingA("free_trial", std::to_string(n));
}
void CMy2015RemoteDlg::OnOnlineBuildClient()
{
auto n = CanBuildClient();
if (n<=0) {
auto THIS_APP = (CMy2015RemoteApp*)AfxGetApp();
auto settings = "settings", pwdKey = "Password";
// <20><>֤<EFBFBD><D6A4><EFBFBD><EFBFBD>
CPasswordDlg dlg;
std::string hardwareID = getHardwareID();
std::string hashedID = hashSHA256(hardwareID);
std::string deviceID = getFixedLengthID(hashedID);
CString pwd = THIS_APP->m_iniFile.GetStr(settings, pwdKey, "");
dlg.m_sDeviceID = deviceID.c_str();
dlg.m_sPassword = pwd;
if (pwd.IsEmpty() && IDOK != dlg.DoModal() || dlg.m_sPassword.IsEmpty())
return;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>20250209 - 20350209: SHA256
auto v = splitString(dlg.m_sPassword.GetBuffer(), '-');
if (v.size() != 6)
{
THIS_APP->m_iniFile.SetStr(settings, pwdKey, "");
MessageBox("<EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!", "<EFBFBD><EFBFBD>ʾ", MB_ICONINFORMATION);
return;
}
std::vector<std::string> subvector(v.begin() + 2, v.end());
std::string password = v[0] + " - " + v[1] + ": " + PWD_HASH256;
std::string finalKey = deriveKey(password, deviceID);
std::string hash256 = joinString(subvector, '-');
std::string fixedKey = getFixedLengthID(finalKey);
if (hash256 != fixedKey) {
THIS_APP->m_iniFile.SetStr(settings, pwdKey, "");
if (pwd.IsEmpty() || (IDOK != dlg.DoModal() || hash256 != fixedKey)) {
if (!dlg.m_sPassword.IsEmpty())
MessageBox("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20>޷<EFBFBD><DEB7><EFBFBD><EFBFBD>ɷ<EFBFBD><C9B7><EFBFBD><EFBFBD><EFBFBD>!", "<EFBFBD><EFBFBD>ʾ", MB_ICONWARNING);
return;
}
}
// <20>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
auto pekingTime = ToPekingTime(nullptr);
char curDate[9];
std::strftime(curDate, sizeof(curDate), "%Y%m%d", &pekingTime);
if (curDate < v[0] || curDate > v[1]) {
THIS_APP->m_iniFile.SetStr(settings, pwdKey, "");
MessageBox("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!", "<EFBFBD><EFBFBD>ʾ", MB_ICONINFORMATION);
return;
}
if (dlg.m_sPassword != pwd)
THIS_APP->m_iniFile.SetStr(settings, pwdKey, dlg.m_sPassword);
}
// TODO: <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEB4A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
CBuildDlg Dlg;
Dlg.m_strIP = ((CMy2015RemoteApp*)AfxGetApp())->m_iniFile.GetStr("settings", "localIp", "");
CString Port;
Port.Format("%d", ((CMy2015RemoteApp*)AfxGetApp())->m_iniFile.GetInt("settings", "ghost"));
Dlg.m_strPort = Port;
Dlg.DoModal();
int Port = ((CMy2015RemoteApp*)AfxGetApp())->m_iniFile.GetInt("settings", "ghost");
Dlg.m_strPort = Port <= 0 ? "6543" : std::to_string(Port).c_str();
if (IDOK == Dlg.DoModal() && n > 0) {
UpdateFreeTrial(n - 1);
}
}

View File

@@ -240,6 +240,7 @@
<ClInclude Include="AudioDlg.h" />
<ClInclude Include="Buffer.h" />
<ClInclude Include="BuildDlg.h" />
<ClInclude Include="CPasswordDlg.h" />
<ClInclude Include="CpuUseage.h" />
<ClInclude Include="EditDialog.h" />
<ClInclude Include="FileManagerDlg.h" />
@@ -248,6 +249,7 @@
<ClInclude Include="InputDlg.h" />
<ClInclude Include="IOCPServer.h" />
<ClInclude Include="KeyBoardDlg.h" />
<ClInclude Include="pwd_gen.h" />
<ClInclude Include="RegisterDlg.h" />
<ClInclude Include="Resource.h" />
<ClInclude Include="ScreenSpyDlg.h" />
@@ -270,6 +272,7 @@
<ClCompile Include="AudioDlg.cpp" />
<ClCompile Include="Buffer.cpp" />
<ClCompile Include="BuildDlg.cpp" />
<ClCompile Include="CPasswordDlg.cpp" />
<ClCompile Include="CpuUseage.cpp" />
<ClCompile Include="EditDialog.cpp" />
<ClCompile Include="FileManagerDlg.cpp" />
@@ -278,6 +281,7 @@
<ClCompile Include="InputDlg.cpp" />
<ClCompile Include="IOCPServer.cpp" />
<ClCompile Include="KeyBoardDlg.cpp" />
<ClCompile Include="pwd_gen.cpp" />
<ClCompile Include="RegisterDlg.cpp" />
<ClCompile Include="ScreenSpyDlg.cpp" />
<ClCompile Include="ServicesDlg.cpp" />
@@ -306,6 +310,7 @@
<Image Include="res\Bitmap_5.bmp" />
<Image Include="res\delete.bmp" />
<Image Include="res\keyboard.ico" />
<Image Include="res\password.ico" />
<Image Include="res\toolbar1.bmp" />
<Image Include="res\toolbar2.bmp" />
<Image Include="res\update.bmp" />

View File

@@ -0,0 +1,52 @@
// CPasswordDlg.cpp: 实现文件
//
#include "stdafx.h"
#include "CPasswordDlg.h"
#include "afxdialogex.h"
#include "pwd_gen.h"
#include "2015Remote.h"
// CPasswordDlg 对话框
IMPLEMENT_DYNAMIC(CPasswordDlg, CDialogEx)
CPasswordDlg::CPasswordDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_DIALOG_PASSWORD, pParent)
, m_sDeviceID(_T(""))
, m_sPassword(_T(""))
{
}
CPasswordDlg::~CPasswordDlg()
{
}
void CPasswordDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT_DEVICEID, m_EditDeviceID);
DDX_Control(pDX, IDC_EDIT_DEVICEPWD, m_EditPassword);
DDX_Text(pDX, IDC_EDIT_DEVICEID, m_sDeviceID);
DDV_MaxChars(pDX, m_sDeviceID, 19);
DDX_Text(pDX, IDC_EDIT_DEVICEPWD, m_sPassword);
DDV_MaxChars(pDX, m_sPassword, 37);
}
BEGIN_MESSAGE_MAP(CPasswordDlg, CDialogEx)
END_MESSAGE_MAP()
BOOL CPasswordDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// TODO: 在此添加额外的初始化
m_hIcon = LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ICON_PASSWORD));
SetIcon(m_hIcon, FALSE);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}

View File

@@ -0,0 +1,33 @@
#pragma once
#include <afx.h>
#include <afxwin.h>
#include "Resource.h"
// 密码的哈希值
#define PWD_HASH256 "61f04dd637a74ee34493fc1025de2c131022536da751c29e3ff4e9024d8eec43"
// CPasswordDlg 对话框
class CPasswordDlg : public CDialogEx
{
DECLARE_DYNAMIC(CPasswordDlg)
public:
CPasswordDlg(CWnd* pParent = nullptr); // 标准构造函数
virtual ~CPasswordDlg();
enum { IDD = IDD_DIALOG_PASSWORD };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
HICON m_hIcon;
CEdit m_EditDeviceID;
CEdit m_EditPassword;
CString m_sDeviceID;
CString m_sPassword;
virtual BOOL OnInitDialog();
};

View File

@@ -0,0 +1,117 @@
#include "stdafx.h"
#include "pwd_gen.h"
#pragma comment(lib, "Advapi32.lib")
// ִ<><D6B4>ϵͳ<CFB5><CDB3><EFBFBD><EFBFBD><EEA3AC>ȡӲ<C8A1><D3B2><EFBFBD><EFBFBD>Ϣ
std::string execCommand(const char* cmd) {
// <20><><EFBFBD>ùܵ<C3B9><DCB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><DABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĺܵ<C4B9>
HANDLE hStdOutRead, hStdOutWrite;
if (!CreatePipe(&hStdOutRead, &hStdOutWrite, &saAttr, 0)) {
Mprintf("CreatePipe failed with error: %d\n", GetLastError());
return "ERROR";
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
// <20><><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdOutput = hStdOutWrite; // <20><><EFBFBD><EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><D8B6>򵽹ܵ<F2B5BDB9>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (!CreateProcess(
NULL, // Ӧ<>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
(LPSTR)cmd, // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
NULL, // <20><><EFBFBD>̰<EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>
NULL, // <20>̰߳<DFB3>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>
TRUE, // <20>Ƿ<EFBFBD><C7B7>̳о<CCB3><D0BE><EFBFBD>
0, // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־
NULL, // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
NULL, // <20><>ǰĿ¼
&si, // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
&pi // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
)) {
Mprintf("CreateProcess failed with error: %d\n", GetLastError());
return "ERROR";
}
// <20>ر<EFBFBD>д<EFBFBD><D0B4><EFBFBD>˾<EFBFBD><CBBE><EFBFBD>
CloseHandle(hStdOutWrite);
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
char buffer[128];
std::string result = "";
DWORD bytesRead;
while (ReadFile(hStdOutRead, buffer, sizeof(buffer), &bytesRead, NULL) && bytesRead > 0) {
result.append(buffer, bytesRead);
}
// <20>رն<D8B1>ȡ<EFBFBD>˾<EFBFBD><CBBE><EFBFBD>
CloseHandle(hStdOutRead);
// <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
WaitForSingleObject(pi.hProcess, INFINITE);
// <20>رս<D8B1><D5BD>̺<EFBFBD><CCBA>߳̾<DFB3><CCBE><EFBFBD>
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
// ȥ<><C8A5><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><D0B7>Ϳո<CDBF>
result.erase(remove(result.begin(), result.end(), '\n'), result.end());
result.erase(remove(result.begin(), result.end(), '\r'), result.end());
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
return result;
}
// <20><>ȡӲ<C8A1><D3B2> ID<49><44>CPU + <20><><EFBFBD><EFBFBD> + Ӳ<>̣<EFBFBD>
std::string getHardwareID() {
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;
return combinedID;
}
// ʹ<><CAB9> SHA-256 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ
std::string hashSHA256(const std::string& data) {
HCRYPTPROV hProv;
HCRYPTHASH hHash;
BYTE hash[32];
DWORD hashLen = 32;
std::ostringstream result;
if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT) &&
CryptCreateHash(hProv, CALG_SHA_256, 0, 0, &hHash)) {
CryptHashData(hHash, (BYTE*)data.c_str(), data.length(), 0);
CryptGetHashParam(hHash, HP_HASHVAL, hash, &hashLen, 0);
for (DWORD i = 0; i < hashLen; i++) {
result << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i];
}
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
}
return result.str();
}
// <20><><EFBFBD><EFBFBD> 16 <20>ַ<EFBFBD><D6B7><EFBFBD>Ψһ<CEA8>豸 ID
std::string getFixedLengthID(const std::string& hash) {
return hash.substr(0, 4) + "-" + hash.substr(4, 4) + "-" + hash.substr(8, 4) + "-" + hash.substr(12, 4);
}
std::string deriveKey(const std::string& password, const std::string& hardwareID) {
return hashSHA256(password + " + " + hardwareID);
}

View File

@@ -0,0 +1,20 @@
#pragma once
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include <wincrypt.h>
// <20><><EFBFBD><EFBFBD><EFBFBD>ɷ<EFBFBD><C9B7><EFBFBD><EFBFBD>˹<EFBFBD><CBB9>ܽ<EFBFBD><DCBD>м<EFBFBD><D0BC><EFBFBD>
std::string getHardwareID();
std::string hashSHA256(const std::string& data);
std::string getFixedLengthID(const std::string& hash);
std::string deriveKey(const std::string& password, const std::string& hardwareID);

Binary file not shown.

After

Width:  |  Height:  |  Size: 766 B

Binary file not shown.