Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ePi5131 committed Jul 22, 2023
1 parent 10df15f commit 47d4b47
Show file tree
Hide file tree
Showing 26 changed files with 615 additions and 552 deletions.
91 changes: 91 additions & 0 deletions patch/hash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include "hash.hpp"

SHA256::SHA256(const std::string& filename) {
#if _DEBUG && 1 // 重いので
std::fill(std::begin(data), std::end(data), std::byte{});
#else
std::vector<uint8_t> buf;
{
auto hFile = CreateFileA(filename.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE) throw std::runtime_error("Failed to open file.");
SCOPE_EXIT_AUTO{ [hFile] {
CloseHandle(hFile);
} };

DWORD sizehigh;
auto sizelow = GetFileSize(hFile, &sizehigh);

buf.resize(sizelow);
DWORD read;
if (!ReadFile(hFile, buf.data(), sizelow, &read, nullptr)) throw std::runtime_error("Failed to read file.");
}
uint32_t H[8];

std::memcpy(H, H0, sizeof(H0));

auto process = [&](const byte* msg) {
uint32_t W[64];
for (size_t t = 0; t < 16; t++) {
W[t] = std::byteswap(*reinterpret_cast<const unsigned long*>(msg + t * 4));
}
for (size_t t = 16; t < 64; t++) {
W[t] = sigma1(W[t - 2]) + W[t - 7] + sigma0(W[t - 15]) + W[t - 16];
}
auto [a, b, c, d, e, f, g, h] = H;
for (size_t t = 0; t < 64; t++) {
auto T1 = h + Sigma1(e) + Ch(e, f, g) + K[t] + W[t];
auto T2 = Sigma0(a) + Maj(a, b, c);
h = g;
g = f;
f = e;
e = d + T1;
d = c;
c = b;
b = a;
a = T1 + T2;
}
H[0] += a;
H[1] += b;
H[2] += c;
H[3] += d;
H[4] += e;
H[5] += f;
H[6] += g;
H[7] += h;
};

auto buf_size_d64 = buf.size() / 64;
auto buf_size_m64 = buf.size() % 64;
for (size_t i = 0; i < buf_size_d64; i++) process(&buf[i * 64]);

uint8_t last_msg[64];
std::memcpy(last_msg, buf.data() + buf_size_d64 * 64, buf_size_m64);
last_msg[buf_size_m64] = 0x80;
if (buf_size_m64 < 56) {
std::memset(last_msg + buf_size_m64 + 1, 0, 58 - buf_size_m64);
auto size = buf.size();
last_msg[59] = static_cast<uint8_t>(size >> 29);
last_msg[60] = static_cast<uint8_t>(size >> 21);
last_msg[61] = static_cast<uint8_t>(size >> 13);
last_msg[62] = static_cast<uint8_t>(size >> 5);
last_msg[63] = static_cast<uint8_t>(size << 3);
process(last_msg);
}
else {
std::memset(last_msg + buf_size_m64 + 1, 0, 63 - buf_size_m64);
process(last_msg);

std::memset(last_msg, 0, 59);
auto size = buf.size();
last_msg[59] = static_cast<uint8_t>(size >> 29);
last_msg[60] = static_cast<uint8_t>(size >> 21);
last_msg[61] = static_cast<uint8_t>(size >> 13);
last_msg[62] = static_cast<uint8_t>(size >> 5);
last_msg[63] = static_cast<uint8_t>(size << 3);
process(last_msg);
}
for (size_t i = 0; i < 8; i++) {
*reinterpret_cast<unsigned long*>(data + i * 4) = _byteswap_ulong(H[i]);
}
#endif
}
117 changes: 15 additions & 102 deletions patch/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <bit>
#include <concepts>
#include <type_traits>
#include <stdexcept>
#include <ranges>

#include <Windows.h>

Expand All @@ -38,128 +40,40 @@ struct SHA256 {

inline constexpr static uint32_t H0[] = { 0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19 };

static uint32_t Sigma0(uint32_t x) {
inline static uint32_t Sigma0(uint32_t x) {
return std::rotr(x, 2) ^ std::rotr(x, 13) ^ std::rotr(x, 22);
}

static uint32_t Sigma1(uint32_t x) {
inline static uint32_t Sigma1(uint32_t x) {
return std::rotr(x, 6) ^ std::rotr(x, 11) ^ std::rotr(x, 25);
}

static uint32_t sigma0(uint32_t x) {
inline static uint32_t sigma0(uint32_t x) {
return std::rotr(x, 7) ^ std::rotr(x, 18) ^ (x >> 3);
}

static uint32_t sigma1(uint32_t x) {
inline static uint32_t sigma1(uint32_t x) {
return std::rotr(x, 17) ^ std::rotr(x, 19) ^ (x >> 10);
}

static uint32_t Ch(uint32_t x, uint32_t y, uint32_t z) {
inline static uint32_t Ch(uint32_t x, uint32_t y, uint32_t z) {
return (x & y) ^ (~x & z);
}

static uint32_t Maj(uint32_t x, uint32_t y, uint32_t z) {
inline static uint32_t Maj(uint32_t x, uint32_t y, uint32_t z) {
return (x & y) ^ (y & z) ^ (z & x);
}

public:

std::byte data[32];

SHA256(const std::string& filename) {
#if _DEBUG && 1 // 重いので
std::fill(std::begin(data), std::end(data), std::byte{});
#else
std::vector<uint8_t> buf;
{
auto hFile = CreateFileA(filename.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE) throw std::runtime_error("Failed to open file.");
SCOPE_EXIT_AUTO{[hFile]{
CloseHandle(hFile);
}};

DWORD sizehigh;
auto sizelow = GetFileSize(hFile, &sizehigh);

buf.resize(sizelow);
DWORD read;
if (!ReadFile(hFile, buf.data(), sizelow, &read, nullptr)) throw std::runtime_error("Failed to read file.");
}
uint32_t H[8];
std::memcpy(H, H0, sizeof(H0));

auto process = [&](const byte* msg) {
uint32_t W[64];
for (size_t t = 0; t < 16; t++) {
W[t] = _byteswap_ulong(*reinterpret_cast<const unsigned long*>(msg + t * 4));
}
for (size_t t = 16; t < 64; t++) {
W[t] = sigma1(W[t - 2]) + W[t - 7] + sigma0(W[t - 15]) + W[t - 16];
}
auto a = H[0];
auto b = H[1];
auto c = H[2];
auto d = H[3];
auto e = H[4];
auto f = H[5];
auto g = H[6];
auto h = H[7];
for (size_t t = 0; t < 64; t++) {
auto T1 = h + Sigma1(e) + Ch(e, f, g) + K[t] + W[t];
auto T2 = Sigma0(a) + Maj(a, b, c);
h = g;
g = f;
f = e;
e = d + T1;
d = c;
c = b;
b = a;
a = T1 + T2;
}
H[0] += a;
H[1] += b;
H[2] += c;
H[3] += d;
H[4] += e;
H[5] += f;
H[6] += g;
H[7] += h;
};

auto buf_size_d64 = buf.size() / 64;
auto buf_size_m64 = buf.size() % 64;
for (size_t i = 0; i < buf_size_d64; i++) process(&buf[i * 64]);

uint8_t last_msg[64];
std::memcpy(last_msg, buf.data() + buf_size_d64 * 64, buf_size_m64);
last_msg[buf_size_m64] = 0x80;
if (buf_size_m64 < 56) {
std::memset(last_msg + buf_size_m64 + 1, 0, 58 - buf_size_m64);
auto size = buf.size();
last_msg[59] = static_cast<uint8_t>(size >> 29);
last_msg[60] = static_cast<uint8_t>(size >> 21);
last_msg[61] = static_cast<uint8_t>(size >> 13);
last_msg[62] = static_cast<uint8_t>(size >> 5);
last_msg[63] = static_cast<uint8_t>(size << 3);
process(last_msg);
}
else {
std::memset(last_msg + buf_size_m64 + 1, 0, 63 - buf_size_m64);
process(last_msg);

std::memset(last_msg, 0, 59);
auto size = buf.size();
last_msg[59] = static_cast<uint8_t>(size >> 29);
last_msg[60] = static_cast<uint8_t>(size >> 21);
last_msg[61] = static_cast<uint8_t>(size >> 13);
last_msg[62] = static_cast<uint8_t>(size >> 5);
last_msg[63] = static_cast<uint8_t>(size << 3);
process(last_msg);
}
for (size_t i = 0; i < 8; i++) {
*reinterpret_cast<unsigned long*>(data + i * 4) = _byteswap_ulong(H[i]);
}
#endif
SHA256(const std::string& filename);

template<std::ranges::input_range R>
requires std::same_as<std::ranges::range_value_t<R>, std::byte>
constexpr SHA256(R&& range) noexcept {
std::ranges::copy(std::forward<R>(range), std::ranges::begin(data));
}

template<std::integral... T> requires(sizeof...(T) == std::extent_v<decltype(data)>)
Expand Down Expand Up @@ -190,7 +104,7 @@ struct SHA256 {
};

inline bool operator==(const SHA256& a, const SHA256& b) {
return std::equal(std::begin(a.data), std::end(a.data), std::begin(b.data));
return std::ranges::equal(a.data, b.data);
}

struct FNV1_32 {
Expand Down Expand Up @@ -231,4 +145,3 @@ struct FNV1_32 {
return hash;
}
};

2 changes: 1 addition & 1 deletion patch/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ HMODULE WINAPI init_t::LoadLibraryAWrap(LPCSTR lpLibFileName) {
#endif
#ifdef PATCH_SWITCH_WARNING_OLD_LSW
else if (lstrcmpiA(filename, "lwcolor.auc") == 0) {
static const SHA256 r940_hash(0xc7, 0xe2, 0x51, 0xde, 0xd2, 0xf8, 0x21, 0xcb, 0x1b, 0xc6, 0xb1, 0x9a, 0x66, 0x43, 0xd3, 0x0d, 0xa4, 0xeb, 0xd6, 0x97, 0x1e, 0x34, 0x1a, 0xb2, 0x11, 0xd9, 0x41, 0x1d, 0xcc, 0xbf, 0x9a, 0x18);
static constinit SHA256 r940_hash(binstr("c7e251ded2f821cb1bc6b19a6643d30da4ebd6971e341ab211d9411dccbf9a18"));
SHA256 hash(lpLibFileName);
if (hash == r940_hash) {
auto ret = patch_resource_message_w(PATCH_RS_PATCH_OLD_LSW, MB_ICONEXCLAMATION | MB_YESNO);
Expand Down
23 changes: 15 additions & 8 deletions patch/overwrite_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,21 @@ inline class overwrite_resource_t {
auto default_resource_hmod_ptr = (HMODULE*)(GLOBAL::aviutl_base + OFS::AviUtl::default_resource_hmod);
default_default_resource_hmod = std::exchange(*default_resource_hmod_ptr, GLOBAL::patchaul_hinst);

const char code[] =
"\x50" // push eax
"\xff\x15xxxx" // call [i32]
"\x58" // pop eax
"\xc3"; // ret

OverWriteOnProtectHelper h(GLOBAL::aviutl_base + 0x0548e0, sizeof(code) - 1);
memcpy((void*)(GLOBAL::aviutl_base + 0x0548e0), code, sizeof(code) - 1);
//const char code[] =
// "\x50" // push eax
// "\xff\x15xxxx" // call [i32]
// "\x58" // pop eax
// "\xc3"; // ret

static constinit auto code = binstr_array(
"50" // push eax
"ff15" PATCH_BINSTR_DUMMY_32(3) // call [i32]
"58" // pop eax
"c3" // ret
);

OverWriteOnProtectHelper h(GLOBAL::aviutl_base + 0x0548e0, std::ranges::size(code));
h.copy_from(code.data());
h.store_i32(3, &InitAtResourceLoaded_ptr);

}
Expand Down
1 change: 1 addition & 0 deletions patch/patch.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="hash.cpp" />
<ClCompile Include="init.cpp" />
<ClCompile Include="overwrite_resource.cpp" />
<ClCompile Include="patch.cpp" />
Expand Down
1 change: 1 addition & 0 deletions patch/patch.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@
<ClCompile Include="patch_change_disp_scene.cpp">
<Filter>feature</Filter>
</ClCompile>
<ClCompile Include="hash.cpp" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="patch.rc" />
Expand Down
47 changes: 21 additions & 26 deletions patch/patch_aup_layer_setting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,34 +127,29 @@ namespace patch {
}
}
*/
char code_put[] = {
/*\x8b*/"\x0d\x50\x62\x14\x00" // mov ecx,dword ptr [exedit+146250]
"\x3b\xc1" // cmp eax,ecx
"\x7c\xe4" // jl 100326c4
"\xb8\x10\x27\x00\x00" // mov eax,0x2710 ; 10000
"\x48" // dec eax
"\x8b\x0c\x85\x98\x84\x18\x00"// mov ecx,dword ptr [eax*4+ exedit+188498]
"\x85\xc9" // test ecx,ecx
"\x74\x09" // jz skip,9 ; 100326fa
"\x8b\xd0" // mov edx,eax
"\xc1\xea\x01" // shr edx,1
"\xc6\x04\x2a\x01" // mov byte ptr [edx+ebp],01
"\x85\xc0" // test eax,eax
"\x75\xe7" // jnz back,19 ; 100326e5
"\x90" // nop
"\x90" // nop
};

*(int*)(&code_put[1]) = GLOBAL::exedit_base + 0x146250;
*(int*)(&code_put[18]) = GLOBAL::exedit_base + 0x188498;

OverWriteOnProtectHelper h(GLOBAL::exedit_base + 0x0326c3, 61);
auto code = binstr_array(
/*8b*/"0d" PATCH_BINSTR_DUMMY_32(1) // mov ecx,dword ptr [i32(exedit+146250)]
"3bc1" // cmp eax,ecx
"7ce4" // jl 100326c4
"b810270000" // mov eax,0x2710 ; 10000
"48" // dec eax
"8b0c85" PATCH_BINSTR_DUMMY_32(18) // mov ecx,dword ptr [eax*4+ i32(exedit+188498)]
"85c9" // test ecx,ecx
"7409" // jz skip,9 ; 100326fa
"8bd0" // mov edx,eax
"c1ea01" // shr edx,1
"c6042a01" // mov byte ptr [edx+ebp],01
"85c0" // test eax,eax
"75e7" // jnz back,19 ; 100326e5
"6690" // nop
);

OverWriteOnProtectHelper h(GLOBAL::exedit_base + 0x0326c3, 20 + code.size());
h.store_i8(0, 0x1c);

memcpy(reinterpret_cast<void*>(h.address()+20), code_put, sizeof(code_put) - 1);
h.copy_from(20, code.data(), code.size());
h.store_i32(20 + 1, GLOBAL::exedit_base + 0x146250);
h.store_i32(20 + 18, GLOBAL::exedit_base + 0x188498);
}


}
void switching(bool flag) {
enabled = flag;
Expand Down
Loading

0 comments on commit 47d4b47

Please sign in to comment.