Skip to content

Commit

Permalink
修复绑定失败时,引起程序崩溃的问题;setlog函数添加调用栈跟踪
Browse files Browse the repository at this point in the history
  • Loading branch information
WallBreaker2 committed Mar 27, 2021
1 parent 2695c5b commit 2c1759a
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 120 deletions.
4 changes: 2 additions & 2 deletions include/op.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
/* at Tue Jan 19 11:14:07 2038
*/
/* Compiler settings for op.idl:
Oicf, W1, Zp8, env=Win32 (32b run), target_arch=X86 8.01.0622
protocol : dce , ms_ext, c_ext, robust
Oicf, W1, Zp8, env=Win64 (32b run), target_arch=AMD64 8.01.0622
protocol : all , ms_ext, c_ext, robust
error checks: allocation ref bounds_check enum stub_data
VC __declspec() decoration level:
__declspec(uuid()), __declspec(selectany), __declspec(novtable)
Expand Down
4 changes: 2 additions & 2 deletions include/op.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
/* at Tue Jan 19 11:14:07 2038
*/
/* Compiler settings for op.idl:
Oicf, W1, Zp8, env=Win32 (32b run), target_arch=X86 8.01.0622
protocol : dce , ms_ext, c_ext, robust
Oicf, W1, Zp8, env=Win64 (32b run), target_arch=AMD64 8.01.0622
protocol : all , ms_ext, c_ext, robust
error checks: allocation ref bounds_check enum stub_data
VC __declspec() decoration level:
__declspec(uuid()), __declspec(selectany), __declspec(novtable)
Expand Down
2 changes: 1 addition & 1 deletion op/3rd_party/src/kiero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ int kiero::bind(uint16_t _index, void** _original, void* _function)
int r2 = MH_EnableHook((void*)g_methodsTable[_index]);


return r1 == MH_OK && r2 == MH_OK;
return r1 == MH_OK && r2 == MH_OK ? 1 : 0;
}
return 0;
#endif
Expand Down
9 changes: 4 additions & 5 deletions op/Bkbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ long bkbase::BindWindow(long hwnd, const wstring& sdisplay, const wstring& smous
UnBindWindow();
//step 2.check hwnd
if (!::IsWindow(HWND(hwnd))) {
setlog("%s error无效的窗口句柄:%d",__FUNCTION__, hwnd);
setlog("无效的窗口句柄");
return 0;
}

Expand Down Expand Up @@ -103,10 +103,9 @@ long bkbase::BindWindow(long hwnd, const wstring& sdisplay, const wstring& smous
return 0;
}
//step 6.try bind
if(!_pbkdisplay->Bind((HWND)hwnd, display)||
!_bkmouse->Bind((HWND)hwnd, mouse)||
!_keypad->Bind((HWND)hwnd, keypad)) {
setlog("try bind error!");
if (_pbkdisplay->Bind((HWND)hwnd, display) != 1 ||
_bkmouse->Bind((HWND)hwnd, mouse) != 1 ||
_keypad->Bind((HWND)hwnd, keypad) != 1) {
UnBindWindow();
return 0;
}
Expand Down
9 changes: 8 additions & 1 deletion op/IDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ long IDisplay::Bind(HWND hwnd, long flag) {
bind_init();
//step 3. 调用特定的绑定函数

_bind_state = BindEx(hwnd, flag) ? 1 : 0;
if (BindEx(hwnd, flag) == 1) {
_bind_state = 1;
}
else {
bind_release();
_bind_state = 0;
}


return _bind_state;

Expand Down
129 changes: 75 additions & 54 deletions op/bkdx_gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,72 +31,87 @@ bkdo::~bkdo()

long bkdo::BindEx(HWND hwnd, long render_type) {
_hwnd = hwnd;
if (render_type == RDT_GL_NOX)
return BindNox(hwnd, render_type);
_render_type = render_type;
RECT rc;
//获取客户区大小
::GetClientRect(hwnd, &rc);
_width = rc.right - rc.left;
_height = rc.bottom - rc.top;
//bind_init();
long bind_ret = 0;
if (render_type == RDT_GL_NOX) {
}
DWORD id;
::GetWindowThreadProcessId(_hwnd, &id);
bind_ret = BindNox(hwnd, render_type);
}else{
_render_type = render_type;
RECT rc;
//获取客户区大小
::GetClientRect(hwnd, &rc);
_width = rc.right - rc.left;
_height = rc.bottom - rc.top;
//bind_init();
if (render_type == RDT_GL_NOX) {
}
DWORD id;
::GetWindowThreadProcessId(_hwnd, &id);



//attach 进程
blackbone::Process proc;
NTSTATUS hr;
//attach 进程
blackbone::Process proc;
NTSTATUS hr;

hr = proc.Attach(id);
hr = proc.Attach(id);

long bind_ret = 0;
if (NT_SUCCESS(hr)) {
wstring dllname = g_op_name;
//检查是否与插件相同的32/64位,如果不同,则使用另一种dll
BOOL is64 = proc.modules().GetMainModule()->type == blackbone::eModType::mt_mod64;
if (is64 != OP64) {
dllname = is64 ? L"op_x64.dll" : L"op_x86.dll";
}

bool injected = false;
//判断是否已经注入
auto _dllptr = proc.modules().GetModule(dllname);
auto mods = proc.modules().GetAllModules();
if (_dllptr) {
injected = true;
}
else {
auto iret = proc.modules().Inject(m_opPath + L"\\" + dllname);
injected = (iret ? true : false);
}
if (injected) {
using my_func_t = long(__stdcall*)(HWND, int);
auto pSetXHook = blackbone::MakeRemoteFunction<my_func_t>(proc, dllname, "SetXHook");
if (pSetXHook) {
auto cret = pSetXHook(hwnd, render_type);
bind_ret = cret.result();
if (NT_SUCCESS(hr)) {
wstring dllname = g_op_name;
//检查是否与插件相同的32/64位,如果不同,则使用另一种dll
BOOL is64 = proc.modules().GetMainModule()->type == blackbone::eModType::mt_mod64;
if (is64 != OP64) {
dllname = is64 ? L"op_x64.dll" : L"op_x86.dll";
}

bool injected = false;
//判断是否已经注入
auto _dllptr = proc.modules().GetModule(dllname);
auto mods = proc.modules().GetAllModules();
if (_dllptr) {
injected = true;
}
else {
setlog(L"remote function not found.");
wstring opFile = m_opPath + L"\\" + dllname;
if (::PathFileExistsW(opFile.data())) {
auto iret = proc.modules().Inject(opFile);
injected = (iret ? true : false);
}
else {
setlog(L"file:<%s> not exists!", opFile.data());
}

}
if (injected) {
using my_func_t = long(__stdcall*)(HWND, int);
auto pSetXHook = blackbone::MakeRemoteFunction<my_func_t>(proc, dllname, "SetXHook");
if (pSetXHook) {
auto cret = pSetXHook(hwnd, render_type);
bind_ret = cret.result();
}
else {
setlog(L"remote function not found.");
}
}
else {
setlog(L"Inject false.");
}
}
else {
setlog(L"Inject false.");
setlog(L"attach false.");
}




proc.Detach();
}
else {
setlog(L"attach false.");

if (bind_ret == -1) {
setlog("UnknownError");
}
else if (bind_ret == -2) {
setlog("NotSupportedError");
}
else if (bind_ret == -3) {
setlog("ModuleNotFoundError");
}
proc.Detach();

return bind_ret;
}
//long bkdo::UnBind(HWND hwnd) {
Expand Down Expand Up @@ -163,7 +178,7 @@ long bkdo::BindNox(HWND hwnd, long render_type) {

//attach 进程
blackbone::Process proc;
NTSTATUS hr;
NTSTATUS hr = -1;


wstring dllname = L"op_x64.dll";
Expand All @@ -183,8 +198,14 @@ long bkdo::BindNox(HWND hwnd, long render_type) {
injected = true;
}
else {
auto iret = proc.modules().Inject(m_opPath + L"\\" + dllname);
injected = (iret ? true : false);
wstring opFile = m_opPath + L"\\" + dllname;
if (::PathFileExistsW(opFile.data())) {
auto iret = proc.modules().Inject(opFile);
injected = (iret ? true : false);
}
else {
setlog(L"file:<%s> not exists!", opFile.data());
}
}
if (injected) {
using my_func_t = long(__stdcall*)(HWND, int);
Expand Down
2 changes: 1 addition & 1 deletion op/globalVar.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ constexpr auto MUTEX_NAME_FORMAT = L"op_shared_mem_%d";

#define MAKE_OP_VERSION(a,b,c,d) _TOSTRING(a##.##b##.##c##.##d)

#define OP_VERSION MAKE_OP_VERSION(0,3,8,0)
#define OP_VERSION MAKE_OP_VERSION(0,3,9,0)
//模块句柄
extern HINSTANCE gInstance;
//是否显示错误信息
Expand Down
69 changes: 31 additions & 38 deletions op/helpfunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>
#include "globalVar.h"
#define USE_BOOST_STACK_TRACE
#ifdef USE_BOOST_STACK_TRACE
#include <boost/stacktrace.hpp>
#endif

std::wstring _s2wstring(const std::string&s) {
std::string strLocale = setlocale(LC_ALL, "");
const char* chSrc = s.c_str();
Expand Down Expand Up @@ -46,40 +52,17 @@ long Path2GlobalPath(const std::wstring&file, const std::wstring& curr_path, std
long setlog(const wchar_t* format, ...) {
va_list args;
wchar_t buf[512];
SYSTEMTIME sys;
GetLocalTime(&sys);
wchar_t tm[128];
wsprintf(tm, L"[%4d/%02d/%02d %02d:%02d:%02d.%03d]",
sys.wYear, sys.wMonth, sys.wDay,
sys.wHour, sys.wMinute, sys.wSecond,
sys.wMilliseconds);
va_start(args, format);
vswprintf(buf, format, args);
va_end(args);
if (gShowError == 1) {
MessageBoxW(NULL, buf, L"error", MB_ICONERROR);
}
else if (gShowError == 2) {
wchar_t dll_path[MAX_PATH];
::GetModuleFileNameW(gInstance, dll_path, MAX_PATH);
wstring fname = dll_path;
fname = fname.substr(0, fname.rfind(L'\\'));
fname += L"\\op.log";
std::wfstream file;
file.open(fname, std::ios::app | std::ios::out);
if (!file.is_open())
return 0;
file << tm << buf << std::endl;
file.close();
}
else if (gShowError == 3) {
std::wcout << tm << buf << std::endl;
}
wstring tmpw = buf;
string tmps = _ws2string(tmpw);

return 1;
return setlog(tmps.data());
}

long setlog(const char* format, ...) {
std::stringstream ss(std::wstringstream::in | std::wstringstream::out);
va_list args;
char buf[512];
SYSTEMTIME sys;
Expand All @@ -92,25 +75,34 @@ long setlog(const char* format, ...) {
va_start(args, format);
vsprintf(buf, format, args);
va_end(args);
ss << tm << "info: " << buf << std::endl;
#ifdef USE_BOOST_STACK_TRACE
ss << "<stack>\n"
<< boost::stacktrace::stacktrace() << std::endl;
#endif // USE_BOOST_STACK_TRACE


string s = ss.str();
if (gShowError == 1) {
MessageBoxA(NULL, buf, "error", MB_ICONERROR);
MessageBoxA(NULL, s.data(), "error", MB_ICONERROR);
}
else if (gShowError == 2) {
wchar_t dll_path[MAX_PATH];
::GetModuleFileNameW(gInstance, dll_path, MAX_PATH);
wstring fname = dll_path;
fname = fname.substr(0, fname.rfind(L'\\'));
fname += L"\\op.log";
/* wchar_t dll_path[MAX_PATH];
::GetModuleFileNameW(gInstance, dll_path, MAX_PATH);
wstring fname = dll_path;
fname = fname.substr(0, fname.rfind(L'\\'));
fname += L"\\op.log";*/
std::fstream file;
file.open(fname, std::ios::app | std::ios::out);
file.open("__op.log", std::ios::app | std::ios::out);
if (!file.is_open())
return 0;
file << tm << buf << std::endl;
file << s << std::endl;
file.close();
}
else if(gShowError==3){
std::cout << tm << buf << std::endl;
else if (gShowError == 3) {
std::cout << s << std::endl;
}

return 1;
}

Expand Down Expand Up @@ -184,4 +176,5 @@ void replacew(wstring& str, const wstring&oldval, const wstring& newval) {
x0 = idx + dx;
idx = str.find(oldval, x0);
}
}
}

3 changes: 3 additions & 0 deletions op/helpfunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ int kmp(const T& s, const T& t) {
return j == s.size() ? i - j : -1;
}




#endif // !__TOOL_H_


Expand Down
6 changes: 4 additions & 2 deletions op/libop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ long libop::GetBasePath(std::wstring& path){
wchar_t basepath[1024];
::GetModuleFileName(gInstance, basepath, 1024);
path = basepath;
int index = path.rfind(L'\\');
path = path.substr(0, index);
size_t index = path.rfind(L'\\');
if (index != std::wstring::npos) {
path = path.substr(0, index);
}
return S_OK;
}

Expand Down
Loading

0 comments on commit 2c1759a

Please sign in to comment.