Skip to content

Commit

Permalink
initial commit - it works!
Browse files Browse the repository at this point in the history
  • Loading branch information
jixunmoe committed Dec 12, 2021
1 parent 53537b4 commit 78a66b2
Show file tree
Hide file tree
Showing 22 changed files with 926 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,7 @@ MigrationBackup/
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd
FodyWeavers.xsd


build/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "QMC2-crypto/vendor/TarsCpp"]
path = QMC2-crypto/vendor/TarsCpp
url = https://github.com/TarsCloud/TarsCpp.git
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# CMakeList.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required (VERSION 3.8)

project ("QMC2")

# Include sub-projects.
add_subdirectory ("QMC2-crypto")
add_subdirectory ("QMC2-decoder")
46 changes: 46 additions & 0 deletions CMakeSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"configurations": [
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": ""
},
{
"name": "Linux-GCC-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"cmakeExecutable": "cmake",
"remoteCopySourcesExclusionList": [ ".vs", ".git", "out" ],
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "linux_x64" ],
"remoteMachineName": "${defaultRemoteMachineName}",
"remoteCMakeListsRoot": "$HOME/.vs/${projectDirName}/${workspaceHash}/src",
"remoteBuildRoot": "$HOME/.vs/${projectDirName}/${workspaceHash}/out/build/${name}",
"remoteInstallRoot": "$HOME/.vs/${projectDirName}/${workspaceHash}/out/install/${name}",
"remoteCopySources": true,
"rsyncCommandArgs": "-t --delete --delete-excluded",
"remoteCopyBuildOutput": false,
"remoteCopySourcesMethod": "rsync"
},
{
"name": "x64-Release",
"generator": "Ninja",
"configurationType": "RelWithDebInfo",
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "msvc_x64_x64" ],
"variables": []
}
]
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Jixun

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
39 changes: 39 additions & 0 deletions QMC2-crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# CMakeList.txt : CMake project for QMC2-decoder, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)

include_directories(
vendor
vendor/TarsCpp/util/include
include
)

set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS YES CACHE BOOL "Export all symbols")

# We don't need all TarsCpp code
set(SOURCE_TEA
"vendor/TarsCpp/util/src/tc_base64.cpp"
"vendor/TarsCpp/util/src/tc_tea.cpp"
)

# Add source to this project's executable.
add_library (QMC2-crypto
${SOURCE_TEA}
"include/qmc2-crypto/IKeyDec.h"
"include/qmc2-crypto/StreamCencrypt.h"
"include/qmc2-crypto/IStreamEncryptAndDecrypt.h"
"include/qmc2-crypto/KeyDec.h"
"qmc2-crypto/StreamCencrypt.cpp"
"qmc2-crypto/KeyDec.cpp")

if (!MSVC)
target_link_libraries(QMC2-crypto m)
endif()

target_include_directories(QMC2-crypto PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)

# TODO: Add tests and install targets if needed.
10 changes: 10 additions & 0 deletions QMC2-crypto/include/qmc2-crypto/IKeyDec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include <cstdint>
#include <cstddef>

class IKeyDec {
protected:
uint8_t* decoded_key = nullptr;
virtual void GetKey(uint8_t*& key, size_t& key_size) = 0;
virtual void SetKey(const char* key, const size_t key_size) = 0;
};
12 changes: 12 additions & 0 deletions QMC2-crypto/include/qmc2-crypto/IStreamEncryptAndDecrypt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include <cstddef>
#include <cstdint>

class IStreamEncryptAndDecrypt {
public:
IStreamEncryptAndDecrypt() {};
~IStreamEncryptAndDecrypt() {};

virtual void StreamEncrypt(uint64_t offset, uint8_t* buf, size_t len) = 0;
virtual void StreamDecrypt(uint64_t offset, uint8_t* buf, size_t len) = 0;
};
16 changes: 16 additions & 0 deletions QMC2-crypto/include/qmc2-crypto/KeyDec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once
#include "IKeyDec.h"
#include <cstddef>

class KeyDec : public IKeyDec {
public:
KeyDec() {};
~KeyDec();

virtual void GetKey(uint8_t*& key, size_t& key_size) override;
virtual void SetKey(const char* key, const size_t key_size) override;

private:
uint8_t* key = nullptr;
size_t key_len = 0;
};
40 changes: 40 additions & 0 deletions QMC2-crypto/include/qmc2-crypto/StreamCencrypt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include "IStreamEncryptAndDecrypt.h"
#include "KeyDec.h"

#include <cstddef>
#include <cstdint>

class StreamCencrypt : public IStreamEncryptAndDecrypt {
public:
StreamCencrypt() {};
~StreamCencrypt() {};

void StreamEncrypt(uint64_t offset, uint8_t* buf, size_t len) override;
void StreamDecrypt(uint64_t offset, uint8_t* buf, size_t len) override;

void SetKeyDec(KeyDec* key_dec);

bool CheckCallerLegal() {
return true;
}
private:
uint32_t key_hash = 0;

// RC4 vars
uint8_t* rc4_key = nullptr;
uint8_t* S = nullptr;
uint8_t* S2 = nullptr;
size_t N = 0;

void InitRC4KSA();
void GetHashBase();
uint8_t mapL(uint64_t offset);

uint64_t GetSegmentKey(uint64_t a, uint64_t b);
void Uninit();
void EncASegment(uint8_t* sbox, size_t offset, uint8_t* buf, size_t len);
void EncFirstSegment(size_t offset, uint8_t* buffer, size_t size);
void ProcessByRC4(size_t offset, uint8_t* buffer, size_t buffer_size);
};
88 changes: 88 additions & 0 deletions QMC2-crypto/qmc2-crypto/KeyDec.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "qmc2-crypto/KeyDec.h"

#include <util/tc_base64.h>
#include <util/tc_tea.h>

#include <cmath>
#include <vector>
#include <cassert>

using tars::TC_Base64;
using tars::TC_Tea;

KeyDec::~KeyDec()
{
if (key) {
delete[] key;
key = nullptr;
}

if (key_len) {
key_len = 0;
}
}

void KeyDec::GetKey(uint8_t*& key_out, size_t& key_len_out)
{
if (key && key_len > 0) {
key_len_out = key_len;
key_out = new uint8_t[key_len];
memcpy(key_out, key, key_len);
}
else {
key_len_out = 0;
}
}

void SimpleMakeKey(uint8_t seed, size_t len, uint8_t* buf) {
for (int i = 0; len > i; ++i) {
buf[i] = (uint8_t)(fabs(tan((float)seed + (double)i * 0.1)) * 100.0);
}
}

void KeyDec::SetKey(const char* key, const size_t key_size)
{
TC_Base64 b64;
TC_Tea tea;
size_t decode_len = key_size / 4 * 3 + 4;
// should be 0x210
std::vector<uint8_t> ekey_decoded;
ekey_decoded.resize(decode_len);

uint8_t simple_key_buf[8] = { 0 };
SimpleMakeKey(106, 8, simple_key_buf);
#if _DEBUG
// 69 56 46 38 2b 20 15 0b
assert(simple_key_buf[0] == 0x69);
assert(simple_key_buf[1] == 0x56);
assert(simple_key_buf[2] == 0x46);
assert(simple_key_buf[3] == 0x38);
assert(simple_key_buf[4] == 0x2b);
assert(simple_key_buf[5] == 0x20);
assert(simple_key_buf[6] == 0x15);
assert(simple_key_buf[7] == 0x0b);
#endif

decode_len = b64.decode(key, key_size, ekey_decoded.data());

uint8_t tea_key[16];
for (int i = 0; i < 16; i += 2) {
tea_key[i + 0] = simple_key_buf[i / 2];
tea_key[i + 1] = ekey_decoded[i / 2];
}

if (this->key) {
delete[] this->key;
this->key = nullptr;
}

this->key = new uint8_t[decode_len * 2]();

// 拷贝前 8 个字节
memcpy(this->key, ekey_decoded.data(), 8u);

std::vector<char> decrypted_buf;
tea.decrypt(reinterpret_cast<const char*>(tea_key), reinterpret_cast<const char*>(ekey_decoded.data()) + 8, decode_len - 8, decrypted_buf);
key_len = decrypted_buf.size() + 8;
memcpy(&this->key[8], decrypted_buf.data(), decrypted_buf.size());
}
Loading

0 comments on commit 78a66b2

Please sign in to comment.