Skip to content

Commit 8dbbf34

Browse files
Monitor Lock the TargetList.txt file instead of using semaphore_inject
1 parent aa1feca commit 8dbbf34

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

APIHOOK/DllInject/DllInject.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ DWORD ReadTargetList(LPCWCHAR szCurrentDirectory, LPDWORD dwPIdList) {
9292
StringCbCat(szListDirectory, MAX_PATH, L"TargetList.txt");
9393
OutputDebugString(szListDirectory);
9494
OutputDebugString(L"\n");
95-
hFile = CreateFile(szListDirectory, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
95+
hFile = CreateFile(szListDirectory, GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
9696
if (INVALID_HANDLE_VALUE == hFile)
9797
{
98+
printf_s("error: %d\n", GetLastError()); system("PAUSE");
9899
OutputDebugString(L"TargetList.txt NOT FOUND\n");
99100
return 0;
100101
}

APIHOOK/DllUnInject/DllUnInject.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ void ReadTargetListAndDo(LPCWCHAR szCurrentDirectory) {
5959
StringCbCat(szListDirectory, MAX_PATH, L"TargetList.txt");
6060
OutputDebugString(szListDirectory);
6161
OutputDebugString(L"\n");
62-
hFile = CreateFile(szListDirectory, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
62+
hFile = CreateFile(szListDirectory, GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
6363
if (INVALID_HANDLE_VALUE == hFile)
6464
{
65+
printf_s("error: %d\n", GetLastError()); system("PAUSE");
6566
OutputDebugString(L"TargetList.txt NOT FOUND\n");
6667
return;
6768
}

APIHOOK/Monitor/Monitor.cpp

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ void StartMonitor()
5959
{
6060
OutputDebugString(TEXT("Do start \n"));
6161

62+
/*
6263
HANDLE hMutexSingleton = NULL;
6364
6465
hMutexSingleton = CreateMutex(NULL, TRUE, TEXT("APIHOOK_Monitor_Mutex_Singleton"));
@@ -74,21 +75,51 @@ void StartMonitor()
7475
OutputDebugString(TEXT("CreateMutex ERROR\n"));
7576
return;
7677
}
78+
*/
79+
80+
HANDLE hTargetListFile = NULL;
81+
TCHAR szTargetListFilePath[MAX_PATH];
82+
83+
GetCurrentDirectory(MAX_PATH, szTargetListFilePath);
84+
StringCbCat(szTargetListFilePath, MAX_PATH, TEXT("\\TargetList.txt"));
85+
hTargetListFile = CreateFile(szTargetListFilePath, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
86+
if (INVALID_HANDLE_VALUE == hTargetListFile)
87+
{
88+
OutputDebugString(TEXT("CreateFile ERROR\n"));
89+
switch (GetLastError())
90+
{
91+
case ERROR_FILE_NOT_FOUND:
92+
OutputDebugString(TEXT("TargetList.txt is not found\n"));
93+
printf_s("TargetList.txt is not found\n");
94+
break;
95+
case ERROR_SHARING_VIOLATION:
96+
OutputDebugString(TEXT("Monitor.exe is already running\n"));
97+
printf_s("Monitor.exe is already running\n");
98+
break;
99+
default:
100+
printf_s("create file targetlist error: %d\n", GetLastError());
101+
break;
102+
}
103+
return;
104+
}
77105

78106
Log();
79107

108+
CloseHandle(hTargetListFile);
109+
hTargetListFile = NULL;
110+
111+
/*
80112
ReleaseMutex(hMutexSingleton);
81113
CloseHandle(hMutexSingleton);
82114
hMutexSingleton = NULL;
83-
115+
*/
84116
}
85117

86118

87119
void StopMonitor()
88120
{
89121
OutputDebugString(TEXT("Do stop \n"));
90-
91-
MyCreateProcess(TEXT("DllUnInject.dll"));
122+
MyCreateProcess(TEXT("DllUnInject.exe"));
92123

93124
}
94125

@@ -103,15 +134,13 @@ void RestartMonitor()
103134

104135
void MyCreateProcess(LPCTSTR szProcessName)
105136
{
106-
TCHAR szCurrentDirectory[MAX_PATH];
107137
TCHAR szProcessPath[MAX_PATH];
108138
STARTUPINFO si;
109139
PROCESS_INFORMATION pi;
110140

111141
ZeroMemory(&si, sizeof(si));
112142
ZeroMemory(&pi, sizeof(pi));
113-
GetCurrentDirectory(MAX_PATH, szCurrentDirectory);
114-
StringCbCopy(szProcessPath, MAX_PATH, szCurrentDirectory);
143+
GetCurrentDirectory(MAX_PATH, szProcessPath);
115144
StringCbCat(szProcessPath, MAX_PATH, TEXT("\\"));
116145
StringCbCat(szProcessPath, MAX_PATH, szProcessName);
117146
OutputDebugString(TEXT("CreateProcess "));
@@ -127,4 +156,4 @@ void MyCreateProcess(LPCTSTR szProcessName)
127156
WaitForSingleObject(pi.hProcess, INFINITE);
128157
OutputDebugString(szProcessName);
129158
OutputDebugString(TEXT(" Finished\n"));
130-
}
159+
}

0 commit comments

Comments
 (0)