From 56f3e0558633176cc26c43fe39afb7e1f863d081 Mon Sep 17 00:00:00 2001 From: eliboa Date: Mon, 10 Sep 2018 17:24:39 +0200 Subject: [PATCH] Fixed : RC=-50 when injecting when app is launched at Win startup Added : Auto kill existing app process(es) Added : Clean remaining keys from regestry ( 21976 bytes TegraRcmGUI/TegraRcmGUIDlg.h | 2 +- 7 files changed, 184 insertions(+), 84 deletions(-) diff --git a/TegraRcmGUI/DialogTab01.h b/TegraRcmGUI/DialogTab01.h index 404709f..a0e685b 100644 --- a/TegraRcmGUI/DialogTab01.h +++ b/TegraRcmGUI/DialogTab01.h @@ -2,6 +2,7 @@ #include #include "TegraRcmGUIDlg.h" #include "TegraRcm.h" +#include "afxole.h" // DialogTab01 dialog @@ -19,7 +20,7 @@ public: #endif TCHAR* PAYLOAD_FILE; BOOL PREVENT_AUTOINJECT = TRUE; - BOOL DELAY_AUTOINJECT = FALSE; + BOOL DELAY_AUTOINJECT = TRUE; CArray m_ListBox; int m_SelectedItem = -1; CFont m_BoldFont; @@ -38,4 +39,5 @@ public: afx_msg void OnDblclkList1(); afx_msg void OnLbnSelchangeList1(); afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor); + COleDropTarget FileDropTarget; }; diff --git a/TegraRcmGUI/DialogTab03.cpp b/TegraRcmGUI/DialogTab03.cpp index f7c71ce..9eb4abf 100644 --- a/TegraRcmGUI/DialogTab03.cpp +++ b/TegraRcmGUI/DialogTab03.cpp @@ -1,31 +1,3 @@ -/* -DialogTab03.cpp - -MIT License - -Copyright(c) 2018 eliboa - -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. - -*/ -// - #include "stdafx.h" #include "TegraRcmGUI.h" #include "DialogTab03.h" @@ -55,6 +27,7 @@ void DialogTab03::DoDataExchange(CDataExchange* pDX) BOOL DialogTab03::OnInitDialog() { CDialog::OnInitDialog(); + CleanRegestry(); string value = m_TegraRcm->GetPreset("AUTO_INJECT"); if (value == "TRUE") @@ -72,25 +45,18 @@ BOOL DialogTab03::OnInitDialog() checkbox->SetCheck(BST_CHECKED); } - HKEY hKey; - const std::string key = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; - const std::string subkey = "TegraRcmGUI"; - - // Open Run Registry location - LONG lnRes = RegOpenKeyExA(HKEY_CURRENT_USER, - key.c_str(), 0, KEY_READ, &hKey); - - if (ERROR_SUCCESS == lnRes) + TCHAR szPath[MAX_PATH]; + if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, szPath))) { - lnRes = RegQueryValueExA(hKey, subkey.c_str(), NULL, NULL, NULL, NULL); - if (lnRes != ERROR_FILE_NOT_FOUND) - { + PathAppend(szPath, _T("\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\TegraRcmGUI.lnk")); + std::ifstream infile(szPath); + if (infile.good()) { + infile.close(); CMFCButton*checkbox = (CMFCButton*)GetDlgItem(RUN_WINSTART); checkbox->SetCheck(BST_CHECKED); } } - return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } @@ -166,48 +132,72 @@ void DialogTab03::OnBnClickedWinstart() // Get application absolute path TCHAR szPath[_MAX_PATH]; - VERIFY(::GetModuleFileName(AfxGetApp()->m_hInstance, szPath, _MAX_PATH)); - // Convert path to ANSI string - int size = WideCharToMultiByte(CP_UTF8, 0, szPath, -1, NULL, 0, NULL, NULL); - if (size > 0) { - buffer.resize(size); - WideCharToMultiByte(CP_UTF8, 0, szPath, -1, (LPSTR)(&buffer[0]), buffer.size(), NULL, NULL); + if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, szPath))) + { + // Remove shortcut + PathAppend(szPath, _T("\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\TegraRcmGUI.lnk")); + remove(CW2A(szPath)); } - std::string appPath(&buffer[0]); - std::string keyValue; - keyValue.append("\""); - keyValue.append(appPath); - keyValue.append("\" /autostart"); - + + // Create new shortcut + if (IsCheckChecked) CreateLink(); +} + + +void DialogTab03::CreateLink() +{ + TCHAR szAppPath[_MAX_PATH]; + VERIFY(::GetModuleFileName(AfxGetApp()->m_hInstance, szAppPath, _MAX_PATH)); + + TCHAR szPath[_MAX_PATH]; + if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, szPath))) + { + PathAppend(szPath, _T("\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\TegraRcmGUI.lnk")); + } + CoInitializeEx(NULL, 0); + HRESULT hres = 0; + IShellLink* psl; + if (SUCCEEDED(hres)) { + hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL, IID_IShellLink, (LPVOID*)&psl); + if (SUCCEEDED(hres)) { + IPersistFile* ppf; + + // Set the path to the shortcut target and add the description. + psl->SetPath(szAppPath); + psl->SetDescription(L"TegraRcmGUI"); + psl->SetIconLocation(szAppPath, 0); + + hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); + if (SUCCEEDED(hres)) { + hres = ppf->Save(szPath, TRUE); + ppf->Release(); + } + psl->Release(); + } + + } + CoUninitialize(); +} + +void DialogTab03::CleanRegestry() { + HKEY hKey; + const std::string key = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; + const std::string subkey = "TegraRcmGUI"; + // Open Run Registry location - LONG lnRes = RegOpenKeyEx(HKEY_CURRENT_USER, - _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), - 0L, KEY_WRITE, - &hKey); + LONG lnRes = RegOpenKeyExA(HKEY_CURRENT_USER, + key.c_str(), 0, KEY_READ, &hKey); if (ERROR_SUCCESS == lnRes) { - if (IsCheckChecked) - { - // Set full application path with a keyname to registry - lnRes = RegSetValueExA(hKey, - key.c_str(), - 0, - REG_SZ, - (LPBYTE)(keyValue.c_str()), - keyValue.size() + 1); - } - else + lnRes = RegQueryValueExA(hKey, subkey.c_str(), NULL, NULL, NULL, NULL); + if (lnRes != ERROR_FILE_NOT_FOUND) { + //Remove regestry value lnRes = RegDeleteValueA(hKey, key.c_str()); - } - if (ERROR_SUCCESS != lnRes) - { - AfxMessageBox(_T("Failed to set/unset at startup")); + + // Create new shortcut + CreateLink(); } } - else - { - AfxMessageBox(_T("Failed to access registry")); - } -} +} \ No newline at end of file diff --git a/TegraRcmGUI/DialogTab03.h b/TegraRcmGUI/DialogTab03.h index 67390cb..0fd25e0 100644 --- a/TegraRcmGUI/DialogTab03.h +++ b/TegraRcmGUI/DialogTab03.h @@ -1,8 +1,27 @@ #pragma once +//#define _WIN32_DCOM +//#include +//#include +//#pragma comment(lib, "taskschd.lib") +//#pragma comment(lib, "comsupp.lib") + #include #include "TegraRcmGUIDlg.h" #include "TegraRcm.h" +#include "stdafx.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + // DialogTab03 dialog class DialogTab03 : public CDialogEx @@ -31,5 +50,7 @@ public: afx_msg void OnClickedMinToTray(); afx_msg void OnBnClickedInstallDriver(); afx_msg void OnBnClickedWinstart(); + void CreateLink(); + afx_msg void CleanRegestry(); }; diff --git a/TegraRcmGUI/TegraRcm.cpp b/TegraRcmGUI/TegraRcm.cpp index 801ea95..2df416a 100644 --- a/TegraRcmGUI/TegraRcm.cpp +++ b/TegraRcmGUI/TegraRcm.cpp @@ -453,7 +453,7 @@ void TegraRcm::AppendLog(string message) { // DISABLED - return; + //return; // Get time @@ -608,7 +608,6 @@ void TegraRcm::BitmapDisplay(int IMG) // void TegraRcm::LookUp() { - // Exit when PAUSE_LKP_DEVICE flag is TRUE if (PAUSE_LKP_DEVICE) return; @@ -646,7 +645,7 @@ void TegraRcm::LookUp() } // On change RCM status - if (rc != m_RC) + if (rc != m_RC || m_RC == -99) { m_RC = rc; //CStatic*pCtrl0 = (CStatic*) m_Parent->GetDlgItem(RCM_PIC_4); @@ -661,7 +660,12 @@ void TegraRcm::LookUp() m_Ctrltb1->GetDlgItem(PAYLOAD_PATH)->GetWindowTextW(file); // Trigger auto inject if payload injection scheduled - if (!FIRST_LOOKUP && DELAY_AUTOINJECT && file.GetLength() > 0) + //if (!FIRST_LOOKUP && DELAY_AUTOINJECT && file.GetLength() > 0) + if (FIRST_LOOKUP) { + if(AUTOINJECT_CURR) DELAY_AUTOINJECT = TRUE; + else DELAY_AUTOINJECT = FALSE; + } + if (DELAY_AUTOINJECT && file.GetLength() > 0) { BitmapDisplay(LOADING); @@ -763,18 +767,23 @@ int TegraRcm::Smasher(TCHAR args[]) int rc = -50; if (NULL != ret) { + AppendLog("Ret is not null"); WaitForSingleObject(pi.hProcess, INFINITE); DWORD exit_code; if (FALSE != GetExitCodeProcess(pi.hProcess, &exit_code)) { + AppendLog("GetExitCodeProcess != FALSE"); if (STILL_ACTIVE != exit_code) { rc = exit_code; + AppendLog("Real exit code"); } else { rc = -52; + AppendLog("RC = -52"); } + } else { @@ -783,6 +792,9 @@ int TegraRcm::Smasher(TCHAR args[]) CloseHandle(pi.hProcess); CloseHandle(pi.hThread); } + else { + AppendLog("Ret is null"); + } return rc; } // @@ -811,6 +823,75 @@ ULONGLONG TegraRcm::GetDllVersion(LPCTSTR lpszDllName) } return ullVersion; } + +void TegraRcm::KillRunningProcess(CString process) { + + CString compare; + DWORD processID; + DWORD currentProcessID = GetCurrentProcessId(); + bool procRunning = false; + + HANDLE hProcessSnap; + PROCESSENTRY32 pe32; + hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + + if (hProcessSnap == INVALID_HANDLE_VALUE) { + procRunning = false; + } + else { + pe32.dwSize = sizeof(PROCESSENTRY32); + if (Process32First(hProcessSnap, &pe32)) { // Gets first running process + if (pe32.szExeFile == process) { + procRunning = true; + } + else { + // loop through all running processes looking for process + while (Process32Next(hProcessSnap, &pe32)) { + // Set to an AnsiString instead of Char[] to make compare easier + compare = pe32.szExeFile; + processID = pe32.th32ProcessID; + if (compare == process && processID != currentProcessID) { + // if found process is running, set to true and break from loop + procRunning = true; + HWND hProcess = find_main_window(processID); + SendMessage(hProcess, WM_CLOSE, 0, 0); + DWORD err = GetLastError(); + int test = 1; + //break; + } + } + } + // clean the snapshot object + CloseHandle(hProcessSnap); + } + } +} + + +struct handle_data { + unsigned long process_id; + HWND window_handle; +}; +BOOL CALLBACK enum_windows_callback(HWND handle, LPARAM lParam) +{ + handle_data& data = *(handle_data*)lParam; + unsigned long process_id = 0; + GetWindowThreadProcessId(handle, &process_id); + if (data.process_id != process_id) + return TRUE; + data.window_handle = handle; + return FALSE; +} + +HWND TegraRcm::find_main_window(unsigned long process_id) +{ + handle_data data; + data.process_id = process_id; + data.window_handle = 0; + EnumWindows(enum_windows_callback, (LPARAM)&data); + return data.window_handle; +} + TCHAR* TegraRcm::GetAbsolutePath(TCHAR* relative_path, DWORD dwFlags) { diff --git a/TegraRcmGUI/TegraRcm.h b/TegraRcmGUI/TegraRcm.h index b8e14eb..403f76e 100644 --- a/TegraRcmGUI/TegraRcm.h +++ b/TegraRcmGUI/TegraRcm.h @@ -16,6 +16,7 @@ #include #include #include "afxcmn.h" +#include #pragma comment (lib, "setupapi.lib") class TegraRcm @@ -23,6 +24,7 @@ class TegraRcm public: TegraRcm(CDialog* pParent = NULL); ~TegraRcm(); + public: void InitCtrltbDlgs(CDialog* pCtrltb1, CDialog* pCtrltb2, CDialog* pCtrltb3); int GetRcmStatus(); @@ -41,6 +43,10 @@ public: void LookUp(); int Smasher(TCHAR args[]); char* GetRelativeFilename(char *currentDirectory, char *absoluteFilename); + + + void KillRunningProcess(CString process); + HWND find_main_window(unsigned long process_id); BOOL CmdShow = TRUE; // Notify Icon @@ -58,7 +64,7 @@ public: BOOL PAUSE_LKP_DEVICE = FALSE; BOOL AUTOINJECT_CURR = FALSE; - BOOL DELAY_AUTOINJECT = FALSE; + BOOL DELAY_AUTOINJECT = TRUE; BOOL WAITING_RECONNECT = FALSE; BOOL ASK_FOR_DRIVER = FALSE; BOOL MIN_TO_TRAY_CURR = FALSE; diff --git a/TegraRcmGUI/TegraRcmGUI.rc b/TegraRcmGUI/TegraRcmGUI.rc index 14996cf419514d8084f3adef483cf3e62a9b1cae..a09adbd436e5ecd785fff4e67b8b3c2b6a93f72d 100644 GIT binary patch delta 67 zcmcbzn(@YJ#tk#r8BI3NWIxKmXfiooz!1opDDavINYx5+0;$Qh!t#?}NpNl6Alf9v U9L(UdSzm*jk$zc013Ag?f?J) delta 65 zcmcbyn(@kN#tk#r8I3m2WIxKmXf!!qz!1opDDavINYx5+0;$Qh!t#?}NpNl6Alf9f S*+PSZk=c;JV6&s