Skip to content

Commit

Permalink
Added multi-account support & system proxy support (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
andro2157 authored Jun 29, 2022
1 parent 86c06f9 commit b258a11
Show file tree
Hide file tree
Showing 25 changed files with 1,312 additions and 569 deletions.
414 changes: 414 additions & 0 deletions DiscordTokenProtector/Context.cpp

Large diffs are not rendered by default.

364 changes: 16 additions & 348 deletions DiscordTokenProtector/Context.h

Large diffs are not rendered by default.

56 changes: 35 additions & 21 deletions DiscordTokenProtector/Crypto/Crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,41 @@ namespace Crypto {
}

#ifdef YUBIKEYSUPPORT
YubiKeyFile g_yubiFile;

YubiKeyFile::YubiKeyFile() {
if (!std::filesystem::exists(Config::getConfigPath() + YUBIKEY_KEY_FILE)) {
//create an empty file so that openFile doesn't fail (due to the std::ios::in flag)
std::ofstream(Config::getConfigPath() + YUBIKEY_KEY_FILE).close();
}
m_file.open(Config::getConfigPath() + YUBIKEY_KEY_FILE,
std::ios::in | std::ios::out | std::ios::binary,
_SH_DENYRW
);
if (!m_file.is_open())
throw std::runtime_error("Failed to open yubikey file!");
}

YubiKeyFile::~YubiKeyFile() {
m_file.close();
}

CryptoPP::SecByteBlock YubiKeyFile::generateKeyFile() {
CryptoPP::SecByteBlock keydata = CryptoUtils::randomSBB(YUBIKEY_DATA_LEN);
m_file.seekp(0);
m_file.write(reinterpret_cast<const char*>(keydata.data()), keydata.size());
m_file << std::flush;
return keydata;
}

CryptoPP::SecByteBlock YubiKeyFile::readKeyFile() {
std::ifstream file(Config::getConfigPath() + YUBIKEY_KEY_FILE, std::ios::binary);
CryptoPP::SecByteBlock keydata(YUBIKEY_DATA_LEN);
m_file.seekg(0);
m_file.read(reinterpret_cast<char*>(keydata.data()), YUBIKEY_DATA_LEN);
return keydata;
}

Yubi::Yubi() {
throwOnError(ykpiv_init(&m_state, true), "ykpiv_init");
throwOnError(ykpiv_connect(m_state, NULL), "ykpiv_connect");
Expand Down Expand Up @@ -200,27 +235,6 @@ namespace Crypto {
return "Unknown";
}

CryptoPP::SecByteBlock Yubi::generateKeyFile() {
CryptoPP::SecByteBlock keydata = CryptoUtils::randomSBB(YUBIKEY_DATA_LEN);
std::ofstream file(Config::getConfigPath() + YUBIKEY_KEY_FILE, std::ios::binary);
if (!file.is_open())
throw std::runtime_error("generateKeyFile : Failed to open YubiKey data file");

file.write(reinterpret_cast<const char*>(keydata.data()), keydata.size());
return keydata;
}

CryptoPP::SecByteBlock Yubi::readKeyFile() {
std::ifstream file(Config::getConfigPath() + YUBIKEY_KEY_FILE, std::ios::binary);
if (!file.is_open())
throw std::runtime_error("readKeyFile : Failed to open YubiKey data file");

CryptoPP::SecByteBlock keydata(YUBIKEY_DATA_LEN);
file.read(reinterpret_cast<char*>(keydata.data()), YUBIKEY_DATA_LEN);

return keydata;
}

void Yubi::throwOnError(ykpiv_rc err, const std::string& action) {
m_err = err;
if (err == YKPIV_OK) return;
Expand Down
17 changes: 14 additions & 3 deletions DiscordTokenProtector/Crypto/Crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ namespace Crypto {
constexpr auto YUBIKEY_KEY_FILE = L"yk.dat";
constexpr auto YUBIKEY_DATA_LEN = 256;

class YubiKeyFile {
public:
YubiKeyFile();
~YubiKeyFile();

CryptoPP::SecByteBlock generateKeyFile();
CryptoPP::SecByteBlock readKeyFile();

private:
std::fstream m_file;
};

extern YubiKeyFile g_yubiFile;

class Yubi {
public:
Yubi();
Expand All @@ -52,9 +66,6 @@ namespace Crypto {

std::string getModelName() const;

static CryptoPP::SecByteBlock generateKeyFile();
static CryptoPP::SecByteBlock readKeyFile();

private:
void throwOnError(ykpiv_rc err, const std::string& action);

Expand Down
66 changes: 65 additions & 1 deletion DiscordTokenProtector/Crypto/CryptoUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cryptopp/hex.h>
#include <cryptopp/osrng.h>
#include <cryptopp/sha.h>
#include <cryptopp/base64.h>

enum class EncryptionType {
HWID,
Expand All @@ -27,7 +28,7 @@ struct KeyData {
bool isEncrypted = false;

/*
Note: the size of key and iv won't be changed since they are should be multiples of CRYPTPROTECTMEMORY_BLOCK_SIZE (16)
Note: the size of key and iv won't be changed since they should be multiples of CRYPTPROTECTMEMORY_BLOCK_SIZE (16)
*/
void encrypt() {
if (isEncrypted) return;
Expand All @@ -54,6 +55,9 @@ namespace CryptoUtils {
constexpr auto ALPHANUM = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
constexpr auto ALPHANUM_LEN = 26 * 2 + 10;

constexpr auto PASSCHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!/:.;,?*$%-_()[]{}";
constexpr auto PASSCHARS_LEN = 80;

inline secure_string secureRandomString(size_t len, const char* charRange = ALPHANUM, size_t charRangeLen = ALPHANUM_LEN) {
using namespace CryptoPP;

Expand Down Expand Up @@ -111,4 +115,64 @@ namespace CryptoUtils {

return toHex(digest);
}

inline secure_string KD_encrypt(const secure_string& data, KeyData keydata) {
if (keydata.type == EncryptionType::HWID)
return Crypto::encryptHWID(data);
else if (keydata.type == EncryptionType::Password || keydata.type == EncryptionType::Yubi)
return Crypto::encrypt(data, keydata.key, keydata.iv);
else if (keydata.type == EncryptionType::HWIDAndPassword)
return Crypto::encrypt(Crypto::encryptHWID(data), keydata.key, keydata.iv);
else
throw std::runtime_error("unknown encryption type");
}

inline secure_string KD_decrypt(const secure_string& data, KeyData keydata) {
if (keydata.type == EncryptionType::HWID)
return Crypto::decryptHWID(data);
else if (keydata.type == EncryptionType::Password || keydata.type == EncryptionType::Yubi)
return Crypto::decrypt(data, keydata.key, keydata.iv);
else if (keydata.type == EncryptionType::HWIDAndPassword)
return Crypto::decryptHWID(Crypto::decrypt(data, keydata.key, keydata.iv));
else
throw std::runtime_error("unknown encryption type");
}

inline secure_string toBase64(const secure_string& in) {
using namespace CryptoPP;
secure_string out;

Base64Encoder encoder;
encoder.Put(reinterpret_cast<const byte*>(in.data()), in.size());
encoder.MessageEnd();

auto size = encoder.MaxRetrievable();
out.resize(size);
encoder.Get(reinterpret_cast<byte*>(out.data()), out.size());

return out;
}

inline secure_string fromBase64(const secure_string& in) {
using namespace CryptoPP;

secure_string out;

Base64Decoder decoder;
decoder.Put(reinterpret_cast<const byte*>(in.data()), in.size());
decoder.MessageEnd();

auto size = decoder.MaxRetrievable();
out.resize(size);
decoder.Get(reinterpret_cast<byte*>(out.data()), out.size());

return out;
}

inline void printSecByteBlock(const CryptoPP::SecByteBlock& data) {
for (const auto& c : data) {
std::cout << std::hex << std::setfill('0') << std::setw(2) << static_cast<uint16_t>(c) << " ";
}
std::cout << std::dec << std::endl;
}
}
9 changes: 0 additions & 9 deletions DiscordTokenProtector/Discord.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ enum class DiscordType {
DiscordCanary
};

//No need to store more info
struct DiscordUserInfo {
std::string fullUsername = "";//username#discriminator
std::string username = "";
std::string discriminator = "";
std::string id = "";
bool mfa = false;
};

class Discord {
private:
typedef LONG(NTAPI* NtResumeProcess)(HANDLE ProcessHandle);
Expand Down
4 changes: 4 additions & 0 deletions DiscordTokenProtector/DiscordTokenProtector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include "Menu/Menu.h"
#include "Context.h"

#include "Utils/CurlUtils.h"

#include "Storage/TokenManager.h"

void mainInit() {
try {
#ifdef _PROD
Expand Down
4 changes: 2 additions & 2 deletions DiscordTokenProtector/DiscordTokenProtector.rc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ MAINICON ICON "512x icon.ico"

VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,0,0
PRODUCTVERSION 0,0,0,9
PRODUCTVERSION 0,0,0,10
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -86,7 +86,7 @@ BEGIN
VALUE "LegalCopyright", "Copyright (C) 2021"
VALUE "OriginalFilename", "DiscordTokenProtector.exe"
VALUE "ProductName", "Discord Token Protector"
VALUE "ProductVersion", "0.0.0.9"
VALUE "ProductVersion", "0.0.0.10"
END
END
BLOCK "VarFileInfo"
Expand Down
28 changes: 16 additions & 12 deletions DiscordTokenProtector/DiscordTokenProtector.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,82 +61,82 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='PROD|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='PROD-YUBI|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='PROD-NOSTARTUP|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='PROD-YUBI-NOSTARTUP|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='PROD|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='PROD-YUBI|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='PROD-NOSTARTUP|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='PROD-YUBI-NOSTARTUP|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
Expand Down Expand Up @@ -478,7 +478,9 @@
<ClCompile Include="Menu\ImGuiAddon.cpp" />
<ClCompile Include="Menu\Menu.cpp" />
<ClCompile Include="Storage\Config.cpp" />
<ClCompile Include="Context.cpp" />
<ClCompile Include="Storage\SecureKV.cpp" />
<ClCompile Include="Storage\TokenManager.cpp" />
<ClCompile Include="Utils\Logger.cpp" />
<ClCompile Include="Network\NetworkManager.cpp" />
<ClCompile Include="Protection\FileCert.cpp" />
Expand All @@ -489,6 +491,8 @@
<ClInclude Include="Context.h" />
<ClInclude Include="Crypto\Crypto.h" />
<ClInclude Include="Crypto\CryptoUtils.h" />
<ClInclude Include="Utils\Structs.h" />
<ClInclude Include="Storage\TokenManager.h" />
<ClInclude Include="Utils\Exceptions.h" />
<ClInclude Include="Protection\IntegrityCheck.h" />
<ClInclude Include="Menu\Colors.h" />
Expand Down
12 changes: 12 additions & 0 deletions DiscordTokenProtector/DiscordTokenProtector.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@
<ClCompile Include="Protection\IntegrityCheck.cpp">
<Filter>Fichiers sources\Protection</Filter>
</ClCompile>
<ClCompile Include="Storage\TokenManager.cpp">
<Filter>Fichiers sources\Storage</Filter>
</ClCompile>
<ClCompile Include="Context.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Includes.h">
Expand Down Expand Up @@ -161,6 +167,12 @@
<ClInclude Include="Utils\Exceptions.h">
<Filter>Fichiers d%27en-tête\Utils</Filter>
</ClInclude>
<ClInclude Include="Storage\TokenManager.h">
<Filter>Fichiers d%27en-tête\Storage</Filter>
</ClInclude>
<ClInclude Include="Utils\Structs.h">
<Filter>Fichiers d%27en-tête\Utils</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="DiscordTokenProtector.rc">
Expand Down
3 changes: 2 additions & 1 deletion DiscordTokenProtector/Includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "Utils/Logger.h"
#include "Utils/Timer.h"
#include "Utils/Exceptions.h"
#include "Utils/Structs.h"

#define FATALERROR(msg)\
{\
Expand All @@ -27,7 +28,7 @@ __forceinline void FATALERROR_STR(std::string str) {
FATALERROR(str.c_str());
}

#define VER "dev-9-fix"
#define VER "dev-10"

#pragma comment(lib, "OpenGL32.lib")
#pragma comment(lib, "crypt32.lib")
Expand Down
Loading

0 comments on commit b258a11

Please sign in to comment.