diff --git a/PythonTest/PythonTest.py b/PythonTest/PythonTest.py index ae280f5..6a4461d 100644 --- a/PythonTest/PythonTest.py +++ b/PythonTest/PythonTest.py @@ -2,19 +2,17 @@ from win32com.client import Dispatch op=Dispatch("op.opsoft"); print("op ver:",op.Ver()); -hwnd=op.FindWindow("","Fire"); +hwnd=op.FindWindow("","新建文本文档.txt - 记事本"); r=op.SetDict(0,"dm_soft.txt"); print("SetDict:",r); r=0; if hwnd: - r=op.BindWindow(hwnd,"opengl","normal","windows",0); + r=op.BindWindow(hwnd,"gdi","normal","windows",0); if r: print("bind ok."); r=op.Sleep(1000); print("try screencap"); r=op.capture("screen.bmp"); - s = op.Ocr(0,0,106,50,"000000-0f0f0f",1.0); - print("ocr:",s); r,x,y=op.FindColor(0,0,121,159,"000000-050505"); print(r,x,y); if r: diff --git a/doc/struct.png b/doc/struct.png new file mode 100644 index 0000000..a3f799c Binary files /dev/null and b/doc/struct.png differ diff --git a/op.sln b/op.sln index abd3b7c..b96ce86 100644 --- a/op.sln +++ b/op.sln @@ -10,8 +10,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "opPS", "opPS\opPS.vcxproj", {EA90DCB4-3591-4FA3-9C10-08FEE47A4BFA} = {EA90DCB4-3591-4FA3-9C10-08FEE47A4BFA} EndProjectSection EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dll_test", "dll_test\dll_test.vcxproj", "{321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}" -EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ConsoleTest", "ConsoleTest\ConsoleTest.vcxproj", "{68E33E18-50FF-4418-AE8D-DD7D17E7A245}" EndProject Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "PythonTest", "PythonTest\PythonTest.pyproj", "{70EE008E-A047-4299-84AA-7C0079CFF4CE}" @@ -44,16 +42,6 @@ Global {E5FC54F2-4EE1-41E4-8E8B-95D2B22E246D}.Release|Any CPU.ActiveCfg = Release|Win32 {E5FC54F2-4EE1-41E4-8E8B-95D2B22E246D}.Release|x64.ActiveCfg = Release|x64 {E5FC54F2-4EE1-41E4-8E8B-95D2B22E246D}.Release|x86.ActiveCfg = Release|Win32 - {321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Debug|Any CPU.ActiveCfg = Debug|Win32 - {321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Debug|x64.ActiveCfg = Debug|x64 - {321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Debug|x64.Build.0 = Debug|x64 - {321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Debug|x86.ActiveCfg = Debug|Win32 - {321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Debug|x86.Build.0 = Debug|Win32 - {321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Release|Any CPU.ActiveCfg = Release|Win32 - {321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Release|x64.ActiveCfg = Release|x64 - {321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Release|x64.Build.0 = Release|x64 - {321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Release|x86.ActiveCfg = Release|Win32 - {321FEABA-0D97-4AF6-A3CD-5FA5D72FC807}.Release|x86.Build.0 = Release|Win32 {68E33E18-50FF-4418-AE8D-DD7D17E7A245}.Debug|Any CPU.ActiveCfg = Debug|Win32 {68E33E18-50FF-4418-AE8D-DD7D17E7A245}.Debug|x64.ActiveCfg = Debug|x64 {68E33E18-50FF-4418-AE8D-DD7D17E7A245}.Debug|x64.Build.0 = Debug|x64 diff --git a/op/3rd_party/kiero/kiero.cpp b/op/3rd_party/kiero/kiero.cpp index dea3e3f..881f90d 100644 --- a/op/3rd_party/kiero/kiero.cpp +++ b/op/3rd_party/kiero/kiero.cpp @@ -3,7 +3,7 @@ #include #ifdef KIERO_USE_MINHOOK -# include +#include #endif // Uncomment a needed graphical library (you can include all) @@ -708,6 +708,7 @@ void kiero::bind(uint16_t _index, void** _original, void* _function) void kiero::unbind() { if (g_renderType > 0) { + MH_RemoveHook(MH_ALL_HOOKS); kiero::shutdown(); //MH_DisableHook((void*)g_methodsTable[_index]); //MH_RemoveHook((void*)g_methodsTable[_index]); diff --git a/op/Bkbase.h b/op/Bkbase.h index 4c6a853..f96bd29 100644 --- a/op/Bkbase.h +++ b/op/Bkbase.h @@ -37,7 +37,20 @@ class Bkbase long get_widht(); long RectConvert(long&x1, long&y1, long&x2, long&y2); long get_image_type() { - return _display == BACKTYPE::NORMAL || _display == BACKTYPE::GDI ? -1 : 0; + switch (_display) + { + case BACKTYPE::NORMAL: + return 0; + case BACKTYPE::GDI: + return -1; + case BACKTYPE::DX: + return 0; + case BACKTYPE::OPENGL: + return -1; + default: + return 0; + } + } private: HWND _hwnd; diff --git a/op/DX9hook.cpp b/op/DX9hook.cpp index 4196588..97a9f57 100644 --- a/op/DX9hook.cpp +++ b/op/DX9hook.cpp @@ -113,7 +113,6 @@ long opengl_screen_capture() { g_shared_res_name, boost::interprocess::read_write); //Map the whole shared memory in this process - boost::interprocess::mapped_region region(shm, boost::interprocess::read_write); auto *p = static_cast(region.get_address()); @@ -139,6 +138,7 @@ void __stdcall hkglEnd(void) { opengl_screen_capture(); } void hook_init(HWND hwnd) { + g_hwnd = hwnd; sprintf(g_shared_res_name, SHARED_RES_NAME_FORMAT, hwnd); sprintf(g_mutex_name, MUTEX_NAME_FORMAT, hwnd); try { diff --git a/op/Injecter.cpp b/op/Injecter.cpp index 73a1850..7b421e0 100644 --- a/op/Injecter.cpp +++ b/op/Injecter.cpp @@ -28,7 +28,7 @@ BOOL Injecter::EnablePrivilege(BOOL enable) tp.PrivilegeCount = 1; tp.Privileges[0].Luid = luid; tp.Privileges[0].Attributes = enable ? SE_PRIVILEGE_ENABLED : 0; - if (!AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL)) + if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL)) return FALSE; // ¹Ø±ÕÁîÅƾä±ú @@ -38,51 +38,47 @@ BOOL Injecter::EnablePrivilege(BOOL enable) } -HMODULE Injecter::InjectDll(LPCTSTR commandLine, LPCTSTR dllPath/*, DWORD* pid, HANDLE* process*/) +long Injecter::InjectDll(DWORD pid, LPCTSTR dllPath,long& error_code) { - TCHAR* commandLineCopy = new TCHAR[32768]; // CreateProcess¿ÉÄÜÐÞ¸ÄÕâ¸ö - _tcscpy_s(commandLineCopy, 32768, commandLine); - int cdSize = _tcsrchr(commandLine, _T('\\')) - commandLine + 1; - TCHAR* cd = new TCHAR[cdSize]; - _tcsnccpy_s(cd, cdSize, commandLine, cdSize - 1); - // ´´½¨½ø³Ì²¢ÔÝÍ£ - STARTUPINFO startInfo = {}; - PROCESS_INFORMATION processInfo = {}; - if (!CreateProcess(NULL, commandLineCopy, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, cd, &startInfo, &processInfo)) - { - delete commandLineCopy; - delete cd; - return 0; - } - delete commandLineCopy; - delete cd; - + + auto jhandle=::OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); /**pid = processInfo.dwProcessId; *process = processInfo.hProcess;*/ - + if (!jhandle) { + error_code = ::GetLastError(); + return -1; + } DWORD dllPathSize = ((DWORD)_tcslen(dllPath) + 1) * sizeof(TCHAR); // ÉêÇëÄÚ´æÓÃÀ´´æ·ÅDLL·¾¶ - void* remoteMemory = VirtualAllocEx(processInfo.hProcess, NULL, dllPathSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE); + void* remoteMemory = VirtualAllocEx(jhandle, NULL, dllPathSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE); if (remoteMemory == NULL) { //setlog(L"ÉêÇëÄÚ´æʧ°Ü£¬´íÎó´úÂ룺%u\n", GetLastError()); - return 0; + error_code = ::GetLastError(); + return -2; } // дÈëDLL·¾¶ - if (!WriteProcessMemory(processInfo.hProcess, remoteMemory, dllPath, dllPathSize, NULL)) + if (!WriteProcessMemory(jhandle, remoteMemory, dllPath, dllPathSize, NULL)) { //setlog(L"дÈëÄÚ´æʧ°Ü£¬´íÎó´úÂ룺%u\n", GetLastError()); - return 0; + error_code = ::GetLastError(); + return -3; } // ´´½¨Ô¶Ï̵߳÷ÓÃLoadLibrary - HANDLE remoteThread = CreateRemoteThread(processInfo.hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)LoadLibrary, remoteMemory, 0, NULL); + auto lpfn=GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "LoadLibraryW"); + if (!lpfn) { + error_code = ::GetLastError(); + return -4; + } + HANDLE remoteThread = CreateRemoteThread(jhandle, NULL, 0, (LPTHREAD_START_ROUTINE)lpfn, remoteMemory, 0, NULL); if (remoteThread == NULL) { //setlog(L"´´½¨Ô¶Ïß³Ìʧ°Ü£¬´íÎó´úÂ룺%u\n", GetLastError()); - return NULL; + error_code = ::GetLastError(); + return -5; } // µÈ´ýÔ¶Ï߳̽áÊø WaitForSingleObject(remoteThread, INFINITE); @@ -91,11 +87,12 @@ HMODULE Injecter::InjectDll(LPCTSTR commandLine, LPCTSTR dllPath/*, DWORD* pid, GetExitCodeThread(remoteThread, &remoteModule); // »Ö¸´Ïß³Ì - ResumeThread(processInfo.hThread); + //ResumeThread(processInfo.hThread); // ÊÍ·Å CloseHandle(remoteThread); - VirtualFreeEx(processInfo.hProcess, remoteMemory, dllPathSize, MEM_DECOMMIT); - - return (HMODULE)remoteModule; + VirtualFreeEx(jhandle, remoteMemory, dllPathSize, MEM_DECOMMIT); + CloseHandle(jhandle); + error_code = 0; + return 1; } \ No newline at end of file diff --git a/op/Injecter.h b/op/Injecter.h index 34fb881..82cd868 100644 --- a/op/Injecter.h +++ b/op/Injecter.h @@ -5,7 +5,7 @@ class Injecter Injecter(); ~Injecter(); static BOOL EnablePrivilege(BOOL enable); - // ³ÌÐòÔËÐÐʱעÈëDLL£¬·µ»ØÄ£¿é¾ä±ú£¨64λ³ÌÐòÖ»ÄÜ·µ»ØµÍ32룩 - static HMODULE InjectDll(LPCTSTR commandLine, LPCTSTR dllPath/*, DWORD* pid, HANDLE* process*/); + // + static long InjectDll(DWORD pid, LPCTSTR dllPath,long& error_code); }; diff --git a/op/OpInterface.cpp b/op/OpInterface.cpp index a9f4c9b..c4ec8de 100644 --- a/op/OpInterface.cpp +++ b/op/OpInterface.cpp @@ -5,18 +5,20 @@ #include "Cmder.h" #include "Injecter.h" +#include "Tool.h" // OpInterface HRESULT OpInterface::Ver(BSTR* ret) { #ifndef _WIN64 - static const wchar_t* ver = L"0.2110.x86"; + const char* ver = "0.2110.x86"; #else static const wchar_t* ver = L"0.2110.x64"; #endif; - CComBSTR bstr; - bstr.Append(ver); - bstr.CopyTo(ret); + Tool::setlog("address=%d,str=%s",ver,ver); + CComBSTR newstr; + newstr.Append(ver); + newstr.CopyTo(ret); return S_OK; } @@ -56,9 +58,9 @@ STDMETHODIMP OpInterface::Sleep(LONG millseconds, LONG* ret) { STDMETHODIMP OpInterface::InjectDll(BSTR process_name, BSTR dll_name, LONG* ret) { //auto proc = _wsto_string(process_name); //auto dll = _wsto_string(dll_name); - Injecter::EnablePrivilege(TRUE); - auto h = Injecter::InjectDll(process_name, dll_name); - *ret = (h ? 1 : 0); + //Injecter::EnablePrivilege(TRUE); + //auto h = Injecter::InjectDll(process_name, dll_name); + *ret = 0; return S_OK; } diff --git a/op/WinApi.cpp b/op/WinApi.cpp index e353667..7533d1a 100644 --- a/op/WinApi.cpp +++ b/op/WinApi.cpp @@ -2596,49 +2596,11 @@ bool WinApi::ClientToScreen(LONG hwnd, LONG &x, LONG &y) } bool WinApi::FindWindow(wchar_t *class_name, wchar_t*title, LONG &rethwnd, DWORD parent) { - bool bret = false; - rethwnd = 0; - HWND p = NULL; - if (parent == 0) - p = ::GetWindow(GetDesktopWindow(), GW_CHILD); //»ñÈ¡×ÀÃæ´°¿ÚµÄ×Ó´°¿Ú - else - p = ::GetWindow((HWND)parent, GW_CHILD); //»ñÈ¡×ÀÃæ´°¿ÚµÄ×Ó´°¿Ú - if (p == NULL) - return bret; - p = ::GetWindow(p, GW_HWNDFIRST); - while (p != NULL) - { - if (::IsWindowVisible(p) && ::GetWindow(p, GW_OWNER) == 0) - { - if (wcslen(class_name) < 1 && wcslen(title) < 1) - { - rethwnd = (LONG)p; - bret = true; - break; - } - else - { - wchar_t WindowClassName[MAX_PATH] = { 0 }; - ::GetClassName(p, WindowClassName, MAX_PATH); - wchar_t WindowTitle[MAX_PATH] = { 0 }; - ::GetWindowText(p, WindowTitle, MAX_PATH); - if (wcslen(WindowClassName) > 1 && wcslen(WindowTitle) > 1) - { - wchar_t *strfindclass = wcsstr(WindowClassName, class_name); //Ä£ºýÆ¥Åä - wchar_t *strfindtitle = wcsstr(WindowTitle, title); //Ä£ºýÆ¥Åä - if ((wcslen(class_name) >= 1 && strfindclass) || (wcslen(title) >= 1 && strfindtitle)) - { - rethwnd = (LONG)p; - bret = true; - break; - } - } - } - } - p = ::GetWindow(p, GW_HWNDNEXT); //»ñÈ¡ÏÂÒ»¸ö´°¿Ú - } + if (class_name[0] == '\0') + class_name = nullptr; + rethwnd=(LONG)::FindWindowW(class_name, title); - return bret; + return 1; } bool WinApi::FindWindowByProcess(wchar_t *class_name, wchar_t *title, LONG &rethwnd, wchar_t *process_name, DWORD Pid) diff --git a/op/bkopengl.cpp b/op/bkopengl.cpp index 37ead4e..27a660f 100644 --- a/op/bkopengl.cpp +++ b/op/bkopengl.cpp @@ -2,7 +2,7 @@ #include "bkopengl.h" #include "Tool.h" - +#include "Injecter.h" bkopengl::bkopengl() { _process_id = 0; @@ -39,7 +39,16 @@ long bkopengl::Bind(HWND hwnd, long flag) { auto _dllptr = _process.modules().GetModule(_dllname); if (!_dllptr) { Tool::setlog(L"inject.."); - reg_ret = _process.modules().Inject(buff); + //reg_ret = _process.modules().Inject(buff); + /*long error_code = 0; + if (!Injecter::EnablePrivilege(true)) + Tool::setlog("Injecter::EnablePrivilege False."); + reg_ret.status = Injecter::InjectDll(id, buff, error_code); + Tool::setlog("inject ret=%d,error_code=%d", reg_ret.status, error_code); + */ + Tool::setlog(buff); + auto& modules = _process.modules(); + reg_ret = modules.Inject(buff); Tool::setlog(L"inject finish..."); } else { @@ -120,7 +129,7 @@ long bkopengl::capture(const std::wstring& file_name) { bih.biBitCount = 32;//ÿ¸öÏñËØ×Ö½Ú´óС bih.biCompression = BI_RGB; - bih.biHeight = -_height;//¸ß¶È + bih.biHeight = _height;//¸ß¶È bih.biPlanes = 1; bih.biSize = sizeof(BITMAPINFOHEADER); bih.biSizeImage = _width * _height * 4;//ͼÏñÊý¾Ý´óС diff --git a/op/op.vcxproj b/op/op.vcxproj index 89edf0b..98ce0bd 100644 --- a/op/op.vcxproj +++ b/op/op.vcxproj @@ -70,6 +70,8 @@ true true + D:\minhook\include;$(IncludePath) + D:\minhook\lib;$(LibraryPath) true @@ -227,6 +229,7 @@ NotUsing NotUsing + NotUsing