2025-06-14 23:40:11 +08:00
|
|
|
|
|
|
|
|
|
|
#ifdef _WINDOWS
|
2025-04-16 23:31:34 +08:00
|
|
|
|
#include "stdafx.h"
|
2025-06-14 23:40:11 +08:00
|
|
|
|
#else
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
#define Mprintf
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2025-04-16 23:31:34 +08:00
|
|
|
|
#include "pwd_gen.h"
|
2025-06-14 23:40:11 +08:00
|
|
|
|
#include <vector>
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
#include <wincrypt.h>
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
#include "common/commands.h"
|
2025-04-16 23:31:34 +08:00
|
|
|
|
|
|
|
|
|
|
#pragma comment(lib, "Advapi32.lib")
|
2025-07-19 21:37:50 +08:00
|
|
|
|
#pragma comment(lib, "bcrypt.lib")
|
2025-04-16 23:31:34 +08:00
|
|
|
|
|
|
|
|
|
|
// ִ<><D6B4>ϵͳ<CFB5><CDB3><EFBFBD><EFBFBD><EEA3AC>ȡӲ<C8A1><D3B2><EFBFBD><EFBFBD>Ϣ
|
2025-10-15 04:32:59 +08:00
|
|
|
|
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;
|
2025-04-16 23:31:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>ȡӲ<C8A1><D3B2> ID<49><44>CPU + <20><><EFBFBD><EFBFBD> + Ӳ<>̣<EFBFBD>
|
2025-10-15 04:32:59 +08:00
|
|
|
|
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;
|
2025-04-16 23:31:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ʹ<><CAB9> SHA-256 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ
|
2025-10-15 04:32:59 +08:00
|
|
|
|
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();
|
2025-04-16 23:31:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-15 04:32:59 +08:00
|
|
|
|
std::string genHMAC(const std::string& pwdHash, const std::string& superPass)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string key = hashSHA256(superPass);
|
|
|
|
|
|
std::vector<std::string> list({ "g","h","o","s","t" });
|
|
|
|
|
|
for (int i = 0; i < list.size(); ++i)
|
|
|
|
|
|
key = hashSHA256(key + " - " + list.at(i));
|
|
|
|
|
|
return hashSHA256(pwdHash + " - " + key).substr(0, 16);
|
2025-06-28 04:03:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-16 23:31:34 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> 16 <20>ַ<EFBFBD><D6B7><EFBFBD>Ψһ<CEA8>豸 ID
|
2025-10-15 04:32:59 +08:00
|
|
|
|
std::string getFixedLengthID(const std::string& hash)
|
|
|
|
|
|
{
|
|
|
|
|
|
return hash.substr(0, 4) + "-" + hash.substr(4, 4) + "-" + hash.substr(8, 4) + "-" + hash.substr(12, 4);
|
2025-04-16 23:31:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-15 04:32:59 +08:00
|
|
|
|
std::string deriveKey(const std::string& password, const std::string& hardwareID)
|
|
|
|
|
|
{
|
|
|
|
|
|
return hashSHA256(password + " + " + hardwareID);
|
2025-04-16 23:31:34 +08:00
|
|
|
|
}
|
2025-06-14 23:40:11 +08:00
|
|
|
|
|
2025-10-15 04:32:59 +08:00
|
|
|
|
std::string getDeviceID()
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string hardwareID = getHardwareID();
|
|
|
|
|
|
std::string hashedID = hashSHA256(hardwareID);
|
|
|
|
|
|
std::string deviceID = getFixedLengthID(hashedID);
|
|
|
|
|
|
return deviceID;
|
2025-06-14 23:40:11 +08:00
|
|
|
|
}
|
2025-07-19 21:37:50 +08:00
|
|
|
|
|
2025-10-15 04:32:59 +08:00
|
|
|
|
uint64_t SignMessage(const std::string& pwd, BYTE* msg, int len)
|
|
|
|
|
|
{
|
|
|
|
|
|
BCRYPT_ALG_HANDLE hAlg = nullptr;
|
|
|
|
|
|
BCRYPT_HASH_HANDLE hHash = nullptr;
|
|
|
|
|
|
BYTE hash[32]; // SHA256 = 32 bytes
|
|
|
|
|
|
DWORD hashObjectSize = 0;
|
|
|
|
|
|
DWORD dataLen = 0;
|
|
|
|
|
|
PBYTE hashObject = nullptr;
|
|
|
|
|
|
uint64_t result = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_SHA256_ALGORITHM, nullptr, BCRYPT_ALG_HANDLE_HMAC_FLAG) != 0)
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (BCryptGetProperty(hAlg, BCRYPT_OBJECT_LENGTH, (PUCHAR)&hashObjectSize, sizeof(DWORD), &dataLen, 0) != 0) {
|
|
|
|
|
|
BCryptCloseAlgorithmProvider(hAlg, 0);
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
hashObject = (PBYTE)HeapAlloc(GetProcessHeap(), 0, hashObjectSize);
|
|
|
|
|
|
if (!hashObject) {
|
|
|
|
|
|
BCryptCloseAlgorithmProvider(hAlg, 0);
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (BCryptCreateHash(hAlg, &hHash, hashObject, hashObjectSize,
|
|
|
|
|
|
(PUCHAR)pwd.data(), static_cast<ULONG>(pwd.size()), 0) != 0) {
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, hashObject);
|
|
|
|
|
|
BCryptCloseAlgorithmProvider(hAlg, 0);
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (BCryptHashData(hHash, msg, len, 0) != 0) {
|
|
|
|
|
|
BCryptDestroyHash(hHash);
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, hashObject);
|
|
|
|
|
|
BCryptCloseAlgorithmProvider(hAlg, 0);
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (BCryptFinishHash(hHash, hash, sizeof(hash), 0) != 0) {
|
|
|
|
|
|
BCryptDestroyHash(hHash);
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, hashObject);
|
|
|
|
|
|
BCryptCloseAlgorithmProvider(hAlg, 0);
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
memcpy(&result, hash, sizeof(result));
|
|
|
|
|
|
|
|
|
|
|
|
BCryptDestroyHash(hHash);
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, hashObject);
|
|
|
|
|
|
BCryptCloseAlgorithmProvider(hAlg, 0);
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
2025-07-19 21:37:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-15 04:32:59 +08:00
|
|
|
|
bool VerifyMessage(const std::string& pwd, BYTE* msg, int len, uint64_t signature)
|
|
|
|
|
|
{
|
|
|
|
|
|
uint64_t computed = SignMessage(pwd, msg, len);
|
|
|
|
|
|
return computed == signature;
|
2025-07-19 21:37:50 +08:00
|
|
|
|
}
|