From 3cf4dfced6cd25707f2b4b56c988add88785e5a6 Mon Sep 17 00:00:00 2001 From: Bryan Roe Date: Fri, 11 Jan 2019 10:59:24 -0800 Subject: [PATCH] Added MeshReset Tool --- meshreset/MeshReset.rc | 98 ++++++ meshreset/MeshReset.sln | 28 ++ meshreset/MeshReset.vcxproj | 241 +++++++++++++++ meshreset/MeshReset.vcxproj.filters | 64 ++++ meshreset/ReadMe.txt | 7 + meshreset/main.c | 448 ++++++++++++++++++++++++++++ meshreset/makefile | 58 ++++ meshreset/resource.h | 15 + meshreset/stdafx.cpp | 8 + meshreset/stdafx.h | 15 + meshreset/targetver.h | 13 + 11 files changed, 995 insertions(+) create mode 100644 meshreset/MeshReset.rc create mode 100644 meshreset/MeshReset.sln create mode 100644 meshreset/MeshReset.vcxproj create mode 100644 meshreset/MeshReset.vcxproj.filters create mode 100644 meshreset/ReadMe.txt create mode 100644 meshreset/main.c create mode 100644 meshreset/makefile create mode 100644 meshreset/resource.h create mode 100644 meshreset/stdafx.cpp create mode 100644 meshreset/stdafx.h create mode 100644 meshreset/targetver.h diff --git a/meshreset/MeshReset.rc b/meshreset/MeshReset.rc new file mode 100644 index 0000000..f802704 --- /dev/null +++ b/meshreset/MeshReset.rc @@ -0,0 +1,98 @@ +// Microsoft Visual C++ generated resource script. +// +#include "resource.h" + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include "afxres.h" + +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// English (United States) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US +#pragma code_page(1252) + +#ifdef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// TEXTINCLUDE +// + +1 TEXTINCLUDE +BEGIN + "resource.h\0" +END + +2 TEXTINCLUDE +BEGIN + "#include ""afxres.h""\r\n" + "\0" +END + +3 TEXTINCLUDE +BEGIN + "\r\n" + "\0" +END + +#endif // APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// +// Version +// + +VS_VERSION_INFO VERSIONINFO + FILEVERSION 0,0,0,1 + PRODUCTVERSION 0,0,0,0 + FILEFLAGSMASK 0x17L +#ifdef _DEBUG + FILEFLAGS 0x1L +#else + FILEFLAGS 0x0L +#endif + FILEOS 0x4L + FILETYPE 0x1L + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904b0" + BEGIN + VALUE "FileDescription", "Mesh Messenger" + VALUE "FileVersion", "0.0.0.1" + VALUE "InternalName", "MeshConsole" + VALUE "OriginalFilename", "MeshMessenger.exe" + VALUE "ProductName", "Mesh Messenger" + VALUE "ProductVersion", "0, 0, 0, 0" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x409, 1200 + END +END + +#endif // English (United States) resources +///////////////////////////////////////////////////////////////////////////// + + + +#ifndef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 3 resource. +// + + +///////////////////////////////////////////////////////////////////////////// +#endif // not APSTUDIO_INVOKED + diff --git a/meshreset/MeshReset.sln b/meshreset/MeshReset.sln new file mode 100644 index 0000000..540bb05 --- /dev/null +++ b/meshreset/MeshReset.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25420.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MeshReset", "MeshReset.vcxproj", "{03A09084-0576-45C5-97CA-B83B1A8688B8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {03A09084-0576-45C5-97CA-B83B1A8688B8}.Debug|x64.ActiveCfg = Debug|x64 + {03A09084-0576-45C5-97CA-B83B1A8688B8}.Debug|x64.Build.0 = Debug|x64 + {03A09084-0576-45C5-97CA-B83B1A8688B8}.Debug|x86.ActiveCfg = Debug|Win32 + {03A09084-0576-45C5-97CA-B83B1A8688B8}.Debug|x86.Build.0 = Debug|Win32 + {03A09084-0576-45C5-97CA-B83B1A8688B8}.Release|x64.ActiveCfg = Release|x64 + {03A09084-0576-45C5-97CA-B83B1A8688B8}.Release|x64.Build.0 = Release|x64 + {03A09084-0576-45C5-97CA-B83B1A8688B8}.Release|x86.ActiveCfg = Release|Win32 + {03A09084-0576-45C5-97CA-B83B1A8688B8}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/meshreset/MeshReset.vcxproj b/meshreset/MeshReset.vcxproj new file mode 100644 index 0000000..2dc41c4 --- /dev/null +++ b/meshreset/MeshReset.vcxproj @@ -0,0 +1,241 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {03A09084-0576-45C5-97CA-B83B1A8688B8} + MeshConsole + Win32Proj + MeshReset + 8.1 + + + + Application + MultiByte + true + v140_xp + + + Application + MultiByte + v140_xp + + + Application + MultiByte + true + v140 + + + Application + MultiByte + v140 + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + true + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + false + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + false + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + false + AllRules.ruleset + + + AllRules.ruleset + + + AllRules.ruleset + + + AllRules.ruleset + + + false + $(ProjectName) + + + false + + + $(ProjectName)64 + + + $(ProjectName)64 + + + + Disabled + ../../openssl/include;../../openssl/include/openssl;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_CONSOLE;MEMORY_CHECK;__STDC__;WINSOCK2;ILibWebServer_SESSION_TRACKING;MICROSTACK_NO_STDAFX;MICROSTACK_NOTLS;MICROSTACK_PROXY;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level3 + EditAndContinue + Cdecl + NoListing + + + true + Console + MachineX86 + false + true + ws2_32.lib;DbgHelp.lib;Setupapi.lib;Iphlpapi.lib;bcrypt.lib;%(AdditionalDependencies) + + + + + true + + + + + + + + + X64 + + + Disabled + ../../openssl/include;%(AdditionalIncludeDirectories) + WIN32;WIN64;_DEBUG;_CONSOLE;MEMORY_CHECK;__STDC__;_CRT_SECURE_NO_WARNINGS;WINSOCK2;ILibWebServer_SESSION_TRACKING;NOCOMMANDER;MICROSTACK_NO_STDAFX;MICROSTACK_NOTLS;MICROSTACK_PROXY;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebugDLL + + + Level3 + ProgramDatabase + + + Iphlpapi.lib;DbgHelp.lib;ws2_32.lib;Setupapi.lib;Psapi.lib;bcrypt.lib;%(AdditionalDependencies) + true + Console + MachineX64 + true + + + + + MinSpace + true + ../../openssl/include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_CONSOLE;WINSOCK2;NOCOMMANDER;MICROSTACK_NO_STDAFX;MICROSTACK_NOTLS;MICROSTACK_PROXY;%(PreprocessorDefinitions) + MultiThreaded + true + + + Level3 + ProgramDatabase + + + Iphlpapi.lib;DbgHelp.lib;Setupapi.lib;ws2_32.lib;Psapi.lib;Crypt32.lib;Wintrust.lib;Version.lib;Wtsapi32.lib;bcrypt.lib;%(AdditionalDependencies);Gdiplus.lib + $(OutDir)$(TargetName)$(TargetExt) + true + Console + true + true + MachineX86 + + + signtool.exe sign /sha1 fd5940d8fd585545614fea6da455f25d224b00c9 /d "MeshConsole" /du "http://opentools.homeip.net" "$(TargetPath)" + + + + + X64 + + + MinSpace + true + ../../openssl/include;%(AdditionalIncludeDirectories) + WIN32;WIN64;NDEBUG;_CONSOLE;WINSOCK2;NOCOMMANDER;MICROSTACK_NO_STDAFX;MICROSTACK_NOTLS;MICROSTACK_PROXY;%(PreprocessorDefinitions) + MultiThreaded + true + + + Level3 + ProgramDatabase + + + ws2_32.lib;DbgHelp.lib;Setupapi.lib;Psapi.lib;Iphlpapi.lib;bcrypt.lib;%(AdditionalDependencies) + true + Console + true + true + MachineX64 + + + + + + \ No newline at end of file diff --git a/meshreset/MeshReset.vcxproj.filters b/meshreset/MeshReset.vcxproj.filters new file mode 100644 index 0000000..ea81b6d --- /dev/null +++ b/meshreset/MeshReset.vcxproj.filters @@ -0,0 +1,64 @@ + + + + + + + Microstack\nossl + + + Microstack\nossl + + + Microstack\nossl + + + Microstack\nossl + + + Microstack + + + Microstack + + + Microstack + + + + + + + Microstack\nossl + + + Microstack\nossl + + + Microstack\nossl + + + Microstack\nossl + + + Microstack + + + Microstack + + + Microstack + + + + + + + + {24205618-7a0a-4260-916f-f08e93a87031} + + + {2a875bde-bc37-451d-94aa-dd141af887fd} + + + \ No newline at end of file diff --git a/meshreset/ReadMe.txt b/meshreset/ReadMe.txt new file mode 100644 index 0000000..bbb64fe --- /dev/null +++ b/meshreset/ReadMe.txt @@ -0,0 +1,7 @@ + +Micro LMS +------------------------------------------ + +Introduction + +A implementation of Intel AMT LMS using Microstack. diff --git a/meshreset/main.c b/meshreset/main.c new file mode 100644 index 0000000..4ab84be --- /dev/null +++ b/meshreset/main.c @@ -0,0 +1,448 @@ +/* +Copyright 2018 Intel Corporation + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#if defined(WIN32) || defined (_WIN32_WCE) +#ifndef MICROSTACK_NO_STDAFX +#include "stdafx.h" +#endif +#endif + +#if defined(WIN32) +#define _CRTDBG_MAP_ALLOC +#ifdef _DEBUG +#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__) +#define new DEBUG_NEW +#endif +#endif + +#if defined(WINSOCK2) + #include + #include +#elif defined(WINSOCK1) + #include + #include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "..\microstack\ILibParsers.h" +#include "..\microstack\ILibSimpleDataStore.h" + +#if defined(WIN32) & !defined(_CONSOLE) +#include "resource.h" +#endif + +#if defined(WIN32) && defined (_DEBUG) +#include +#endif + +// The following macros set and clear, respectively, given bits +// of the C runtime library debug flag, as specified by a bitmask. +#ifdef _DEBUG +#define SET_CRT_DEBUG_FIELD(a) \ + _CrtSetDbgFlag((a) | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)) +#define CLEAR_CRT_DEBUG_FIELD(a) \ + _CrtSetDbgFlag(~(a) & _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)) +#else +#define SET_CRT_DEBUG_FIELD(a) ((void) 0) +#define CLEAR_CRT_DEBUG_FIELD(a) ((void) 0) +#endif + +#ifdef MEMORY_CHECK +#ifdef WIN32 +// This routine place comments at the head of a section of debug output +void OutputHeading( const char * explanation ) +{ + _RPT1( _CRT_WARN, "\n\n%s:\n**************************************************************************\n", explanation ); +} +#endif +#endif + +void BreakSink(int s) +{ + UNREFERENCED_PARAMETER( s ); + signal(SIGINT, SIG_IGN); // To ignore any more ctrl-c interrupts + // TODO +} + +// Return 1 if the file exists +int isFileExists(char *filePath) +{ +#ifdef WIN32 + return(_access(filePath, 0) == 0 ? 1 : 0); +#else + return(access(filePath, 0) == 0 ? 1 : 0); +#endif +} + +// Find a process by name and kill it. +void killProcess(char *name) +{ + PROCESSENTRY32 pEntry; + HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0); + pEntry.dwSize = sizeof(pEntry); + BOOL hRes = Process32First(hSnapShot, &pEntry); + while (hRes) { + if (strcmp(pEntry.szExeFile, name) == 0) { + HANDLE hProc = OpenProcess(PROCESS_TERMINATE, 0, (DWORD)pEntry.th32ProcessID); + if (hProc != NULL) { + printf("Terminating process %p.\r\n", hProc); + TerminateProcess(hProc, 9); + CloseHandle(hProc); + } + } + hRes = Process32Next(hSnapShot, &pEntry); + } + CloseHandle(hSnapShot); +} + +// Find a process by name and kill it. +void killProcess2(char *name) +{ + snprintf(ILibScratchPad2, sizeof(ILibScratchPad2), "taskkill /F /T /IM %s", name); + system(ILibScratchPad2); +} + +// Find a process by name and kill it. +void killService(char *servicename) +{ + snprintf(ILibScratchPad2, sizeof(ILibScratchPad2), "taskkill /F /FI \"SERVICES eq %s\"", servicename); + system(ILibScratchPad2); +} + + +// Uninstall a service +int UninstallService(TCHAR* serviceName) +{ + int r = 0; + SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SC_MANAGER_CONNECT); + + if (serviceControlManager) + { + SC_HANDLE service = OpenService(serviceControlManager, serviceName, SERVICE_QUERY_STATUS | DELETE); + if (service) + { + SERVICE_STATUS serviceStatusEx; + if (QueryServiceStatus(service, &serviceStatusEx)) + { + if (serviceStatusEx.dwCurrentState == SERVICE_STOPPED) + { + if (DeleteService(service)) + { + printf("Mesh service removed successfully.\r\n"); + r = 1; + } + else + { + DWORD dwError = GetLastError(); + if (dwError == ERROR_ACCESS_DENIED) { + printf("Access denied while trying to remove mesh service.\r\n"); + } + else if (dwError == ERROR_INVALID_HANDLE) { + printf("Handle invalid while trying to remove mesh service.\r\n"); + } + else if (dwError == ERROR_SERVICE_MARKED_FOR_DELETE) { + printf("Mesh service already marked for deletion.\r\n"); + } + } + } + else + { + r = 2; + printf("Mesh service is still running.\r\n"); + } + } + CloseServiceHandle(service); + } + CloseServiceHandle(serviceControlManager); + } + return r; +} + +// SERVICE_STOPPED 1 The service is not running. +// SERVICE_START_PENDING 2 The service is starting. +// SERVICE_STOP_PENDING 3 The service is stopping. +// SERVICE_RUNNING 4 The service is running. +// SERVICE_CONTINUE_PENDING 5 The service continue is pending. +// SERVICE_PAUSE_PENDING 6 The service pause is pending. +// SERVICE_PAUSED 7 The service is paused. +// SERVICE_NOT_INSTALLED 100 The service is not installed. +int GetServiceState(LPCSTR servicename) +{ + int r = 0; + SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SC_MANAGER_CONNECT); + + if (serviceControlManager) + { + SC_HANDLE service = OpenService(serviceControlManager, servicename, SERVICE_QUERY_STATUS | DELETE); + if (service) + { + SERVICE_STATUS serviceStatusEx; + if (QueryServiceStatus(service, &serviceStatusEx)) + { + r = serviceStatusEx.dwCurrentState; + } + CloseServiceHandle(service); + } + else + { + r = 100; + } + CloseServiceHandle(serviceControlManager); + } + return r; +} + +// Start a Windows service +int LaunchService(LPCSTR servicename) +{ + int r = 0; + SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SERVICE_QUERY_STATUS | SERVICE_START); + + if (serviceControlManager) + { + SC_HANDLE service = OpenService(serviceControlManager, servicename, SERVICE_QUERY_STATUS | SERVICE_START); + if (service) + { + SERVICE_STATUS serviceStatusEx; + if (QueryServiceStatus(service, &serviceStatusEx)) + { + if (serviceStatusEx.dwCurrentState == SERVICE_STOPPED) { if (StartService(service, 0, NULL) == TRUE) { r = 1; printf("Started service.\r\n"); } } + else { r = 2; } + } + CloseServiceHandle(service); + } + CloseServiceHandle(serviceControlManager); + } + return r; +} + +// Stop a Windows service +int StopService(LPCSTR servicename) +{ + int r = 0; + SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SERVICE_QUERY_STATUS | SERVICE_STOP); + + if (serviceControlManager) + { + SC_HANDLE service = OpenService(serviceControlManager, servicename, SERVICE_QUERY_STATUS | SERVICE_STOP); + if (service) + { + SERVICE_STATUS serviceStatusEx; + if (QueryServiceStatus(service, &serviceStatusEx)) + { + if (serviceStatusEx.dwCurrentState != SERVICE_STOPPED) + { + if (ControlService(service, SERVICE_CONTROL_STOP, &serviceStatusEx) == FALSE) + { + // TODO: Unable to stop service + printf("Unable to stop service.\r\n"); + } + else + { + printf("Stopped service.\r\n"); + Sleep(3000); + r = 1; + } + } + } + CloseServiceHandle(service); + } + CloseServiceHandle(serviceControlManager); + } + return r; +} + +void removeDir(char* dir) +{ + SHFILEOPSTRUCT file_op = { + NULL, + FO_DELETE, + dir, + "", + FOF_NOCONFIRMATION | + FOF_NOERRORUI | + FOF_SILENT, + FALSE, + 0, + "" }; + int r = SHFileOperation(&file_op); + if (r == 0) { + printf("Removed folder: %s.\r\n", dir); + } else { + if (r != 124) { printf("Unable to remove folder (%d): %s.\r\n", r, dir); } + } +} + +// Remove CoreModule from the .db file +void removeMeshCore(char* file) { + if (ILibSimpleDataStore_Exists(file) != 1) return; + void* ILibSimpleDataStore = ILibSimpleDataStore_Create(file); + if (ILibSimpleDataStore == NULL) return; + printf("Checking %s...\r\n", file); + + if (ILibSimpleDataStore_Get(ILibSimpleDataStore, "CoreModule", NULL, 0) != 0) { + if (ILibSimpleDataStore_Delete(ILibSimpleDataStore, "CoreModule") == 1) { + printf("Removed CoreModule.\r\n"); + ILibSimpleDataStore_Compact(ILibSimpleDataStore); // While we are at it, compact the .db file. + } + } + + ILibSimpleDataStore_Close(ILibSimpleDataStore); +} + +int main(int argc, char **argv) +{ + // Shutdown on Ctrl + C + signal(SIGINT, BreakSink); + +#ifdef _POSIX + signal(SIGPIPE, SIG_IGN); +#ifdef _DEBUG + //mtrace(); +#endif +#endif + +#ifdef MEMORY_CHECK +#ifdef WIN32 + //SET_CRT_DEBUG_FIELD( _CRTDBG_DELAY_FREE_MEM_DF ); + _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF); +#endif +#endif + + if ((argc == 2) && (strcasecmp(argv[1], "reset") == 0)) { + int i; + char targetexe[_MAX_PATH + 40]; + size_t targetexelen = 0; + + // Reset the mesh agent & clear the core + printf("Attempting MeshAgent Reset...\r\n"); + + // Kill the agent 3 different ways. + killProcess("meshagent.exe"); + killProcess2("meshagent.exe"); + killService("Mesh Agent"); + Sleep(1000); + + // Stop the service + i = GetServiceState("Mesh Agent"); + if (i == 100) { printf("Mesh Agent is not installed.\r\n"); return; } + if (i != 1) { StopService("Mesh Agent"); } + + // Remove the core from the .db file + if (SHGetFolderPathA(NULL, CSIDL_PROGRAM_FILES | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, targetexe) != S_FALSE) { + targetexelen = strnlen_s(targetexe, _MAX_PATH + 40); + memcpy_s(targetexe + targetexelen, _MAX_PATH + 40 - targetexelen, "\\Mesh Agent\\MeshAgent.db\0", 25); + removeMeshCore(targetexe); + } else { + removeMeshCore("C:\\Program Files\\Mesh Agent\\MeshAgent.db"); + } + + // Remove the core from the .db file + if (SHGetFolderPathA(NULL, CSIDL_PROGRAM_FILESX86 | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, targetexe) != S_FALSE) { + targetexelen = strnlen_s(targetexe, _MAX_PATH + 40); + memcpy_s(targetexe + targetexelen, _MAX_PATH + 40 - targetexelen, "\\Mesh Agent\\MeshAgent.db\0", 25); + removeMeshCore(targetexe); + } else { + removeMeshCore("C:\\Program Files (x86)\\Mesh Agent\\MeshAgent.db"); + } + + // Start the agent again + LaunchService("Mesh Agent"); + printf("Done.\r\n"); + } else if ((argc == 2) && (strcasecmp(argv[1], "remove") == 0)) { + int i; + + printf("Attempting MeshAgent Removal...\r\n"); + + // Kill the agent 3 different ways. + killProcess("meshagent.exe"); + killProcess2("meshagent.exe"); + killService("Mesh Agent"); + Sleep(1000); + + // Stop & remove the service + i = GetServiceState("Mesh Agent"); + if (i != 100) { + if (i != 1) { StopService("Mesh Agent"); } + UninstallService("Mesh Agent"); + } + i = GetServiceState("Mesh Agent v2"); + if (i != 100) { + if (i != 1) { StopService("Mesh Agent v2"); } + UninstallService("Mesh Agent v2"); + } + + // Remove the MeshAgent v1 registry keys if present + if (RegDeleteKeyEx(HKEY_LOCAL_MACHINE, "Software\\Open Source\\MeshAgent", KEY_WOW64_32KEY, 0) == ERROR_SUCCESS) { printf("Removed WOW64 registry key: LOCALMACHINE\\Software\\Open Source\\MeshAgent.\r\n"); } + if (RegDeleteKeyEx(HKEY_CURRENT_USER, "Software\\Open Source\\MeshAgent", KEY_WOW64_32KEY, 0) == ERROR_SUCCESS) { printf("Removed WOW64 registry key: CURRENTUSER\\Software\\Open Source\\MeshAgent.\r\n"); } + if (RegDeleteKey(HKEY_LOCAL_MACHINE, "Software\\Open Source\\MeshAgent") == ERROR_SUCCESS) { printf("Removed registry key: LOCALMACHINE\\Software\\Open Source\\MeshAgent.\r\n"); } + if (RegDeleteKey(HKEY_CURRENT_USER, "Software\\Open Source\\MeshAgent") == ERROR_SUCCESS) { printf("Removed registry key: CURRENTUSER\\Software\\Open Source\\MeshAgent.\r\n"); } + + // Remove the MeshAgent v2 registry keys if present + if (RegDeleteKeyEx(HKEY_LOCAL_MACHINE, "Software\\Open Source\\MeshAgent2", KEY_WOW64_32KEY, 0) == ERROR_SUCCESS) { printf("Removed WOW64 registry key: LOCALMACHINE\\Software\\Open Source\\MeshAgent2.\r\n"); } + if (RegDeleteKeyEx(HKEY_CURRENT_USER, "Software\\Open Source\\MeshAgent2", KEY_WOW64_32KEY, 0) == ERROR_SUCCESS) { printf("Removed WOW64 registry key: CURRENTUSER\\Software\\Open Source\\MeshAgent2.\r\n"); } + if (RegDeleteKey(HKEY_LOCAL_MACHINE, "Software\\Open Source\\MeshAgent2") == ERROR_SUCCESS) { printf("Removed registry key: LOCALMACHINE\\Software\\Open Source\\MeshAgent2.\r\n"); } + if (RegDeleteKey(HKEY_CURRENT_USER, "Software\\Open Source\\MeshAgent2") == ERROR_SUCCESS) { printf("Removed registry key: CURRENTUSER\\Software\\Open Source\\MeshAgent2.\r\n"); } + + // Remove windows service keys + if (RegDeleteKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\Mesh Agent", KEY_WOW64_32KEY, 0) == ERROR_SUCCESS) { printf("Removed service keys.\r\n"); } + if (RegDeleteKey(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\Mesh Agent") == ERROR_SUCCESS) { printf("Removed service keys.\r\n"); } + + // Remove uninstall icon if present + if (RegDeleteKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\MeshCentralAgent", KEY_WOW64_32KEY, 0) == ERROR_SUCCESS) { printf("Removed WOW64 uninstall icon.\r\n"); } + if (RegDeleteKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\MeshCentralAgent") == ERROR_SUCCESS) { printf("Removed uninstall icon.\r\n"); } + + // Remote mesh agent folder + removeDir("C:\\Program Files\\Mesh Agent\\"); + removeDir("C:\\Program Files (x86)\\Mesh Agent\\"); + } else { + // Display help + printf("MeshCentral Agent Reset & Removal Tool v1. Usage:\r\n\r\n"); + printf(" MeshReset [RESET|REMOVE]\r\n"); + printf("\r\nThis tool should run with administrator privilages.\r\n"); + } + +#ifdef MEMORY_CHECK +#ifdef WIN32 + OutputHeading("Generating the final memory leak report\r\n"); + _CrtCheckMemory(); + _CrtDumpMemoryLeaks(); +#endif +#endif + +#ifdef _POSIX +#ifdef _DEBUG + //muntrace(); +#endif +#endif + +#ifdef _POSIX + exit(EXIT_SUCCESS); +#else + return 0; +#endif +} + diff --git a/meshreset/makefile b/meshreset/makefile new file mode 100644 index 0000000..923329a --- /dev/null +++ b/meshreset/makefile @@ -0,0 +1,58 @@ +SOURCES = main.c ../microstack/ILibLMS.c +SOURCES += ../microstack/ILibAsyncServerSocket.c ../microstack/ILibAsyncSocket.c ../microstack/ILibWebServer.c ../microstack/ILibWebClient.c ../microstack/ILibParsers.c ../microstack/md5.c ../microstack/sha1.c +SOURCES += ../heci/HECILinux.c ../heci/LMEConnection.c ../heci/PTHICommand.c +SOURCES += $(ADDITIONALSOURCES) + +OBJECTS = $(patsubst %.c,%.o, $(SOURCES)) + +EXENAME = microlms + +# Compiler command name +CC = gcc + +# need to be separate for dependency generation +INCDIRS = -I. -I../../microstack -I../../core + +CFLAGS ?= -g -Wall -D_POSIX -D_DEBUG -DMICROSTACK_NOTLS -fno-strict-aliasing $(INCDIRS) +LDFLAGS ?= -g -L. -lpthread -ldl -lutil -lrt + +.PHONY: all clean + +all: $(EXENAME) + +$(EXENAME): $(OBJECTS) + $(V)$(CC) $^ $(LDFLAGS) -o $@ + +release: + $(MAKE) $(MAKEFILE) CFLAGS="-O2 -Wall -D_POSIX -D_DEBUG -DMICROSTACK_NOTLS -D_DEBUGLMS -fno-strict-aliasing $(INCDIRS)" LDFLAGS="-L. -lpthread -ldl -lutil" + strip ./$(EXENAME) + +linux32: + $(MAKE) $(MAKEFILE) CFLAGS="-m32 -O2 -Wall -D_POSIX -D_DEBUG -DMICROSTACK_NOTLS -D_DEBUGLMS -fno-strict-aliasing $(INCDIRS)" LDFLAGS="-L. -lpthread -ldl -lutil -lrt -m32" + strip ./$(EXENAME) + +linux64: + $(MAKE) $(MAKEFILE) CFLAGS="-O2 -Wall -D_POSIX -D_DEBUG -DMICROSTACK_NOTLS -D_DEBUGLMS -fno-strict-aliasing $(INCDIRS)" LDFLAGS="-L. -lpthread -ldl -lrt -lutil" + strip ./$(EXENAME) + +clean: + rm -f *.o + rm -f *.o *~ microlms + rm -f ../heci/*.o + rm -f ../microstack/*.o + +depend: $(SOURCES) + $(CC) -M $(CFLAGS) $(SOURCES) $(HEADERS) > depend + +run:all + rm -f mtrax + set MALLOC_TRACE=mtrax + export MALLOC_TRACE; + ./$(EXENAME) + mtrace ./$(EXENAME) mtrax + +vrun:all + valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes --track-origins=yes ./signer_linux + +trace: + mtrace ./$(EXENAME) mtrax diff --git a/meshreset/resource.h b/meshreset/resource.h new file mode 100644 index 0000000..6d31350 --- /dev/null +++ b/meshreset/resource.h @@ -0,0 +1,15 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ generated include file. +// Used by MeshMessenger.rc +// + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 101 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1001 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif diff --git a/meshreset/stdafx.cpp b/meshreset/stdafx.cpp new file mode 100644 index 0000000..09fe45b --- /dev/null +++ b/meshreset/stdafx.cpp @@ -0,0 +1,8 @@ +// stdafx.cpp : source file that includes just the standard includes +// TinyMesh.pch will be the pre-compiled header +// stdafx.obj will contain the pre-compiled type information + +#include "stdafx.h" + +// TODO: reference any additional headers you need in STDAFX.H +// and not in this file diff --git a/meshreset/stdafx.h b/meshreset/stdafx.h new file mode 100644 index 0000000..b005a83 --- /dev/null +++ b/meshreset/stdafx.h @@ -0,0 +1,15 @@ +// stdafx.h : include file for standard system include files, +// or project specific include files that are used frequently, but +// are changed infrequently +// + +#pragma once + +#include "targetver.h" + +#include +#include + + + +// TODO: reference additional headers your program requires here diff --git a/meshreset/targetver.h b/meshreset/targetver.h new file mode 100644 index 0000000..6fe8eb7 --- /dev/null +++ b/meshreset/targetver.h @@ -0,0 +1,13 @@ +#pragma once + +// The following macros define the minimum required platform. The minimum required platform +// is the earliest version of Windows, Internet Explorer etc. that has the necessary features to run +// your application. The macros work by enabling all features available on platform versions up to and +// including the version specified. + +// Modify the following defines if you have to target a platform prior to the ones specified below. +// Refer to MSDN for the latest info on corresponding values for different platforms. +#ifndef _WIN32_WINNT // Specifies that the minimum required platform is Windows Vista. +#define _WIN32_WINNT 0x0600 // Change this to the appropriate value to target other versions of Windows. +#endif +