我有一个在进程中注入的 dll。它搜索“file://”,直到找到无效符号。几分钟后,它使主进程崩溃。这是为什么?我该如何检查?我发现 CreateThread 上的堆栈大小越小,它崩溃的速度就越快,因此它可能会以某种方式堆栈溢出,但我没有分配任何东西,而是分配了一个结构。
BOOL APIENTRY DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
CreateThread(NULL, 500, SampleFunction, 0, 0, NULL);
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
}
/* Return success */
return TRUE;
}
int Send(char* strDataToSend) {
HWND hWnd = FindWindow(NULL, "Test");
if (hWnd) {
COPYDATASTRUCT cpd;
cpd.dwData = 0;
cpd.cbData = (strlen(strDataToSend) + 1) * 2;
cpd.lpData = (PVOID)strDataToSend;
SendMessage(hWnd, WM_COPYDATA, (WPARAM) hWnd, (LPARAM)&cpd);
}
}
int isurl(char c) {
char* chars = "-._~:/?#[]@!$&'()*+,;=%";
for(int i = 0; i < strlen(chars); i++) {
if (chars[i] == c || isalnum(c)) {
return 1;
}
}
return 0;
}
TESTDLLMAPI void WINAPI SampleFunction(void) {
MessageBox(0,"LOADED !",0,0);
MEMORY_BASIC_INFORMATION info;
MEMORY_BASIC_INFORMATION* pinfo = &info;
while(1) {
int cnt = 0;
unsigned long addr = 0;
do {
ZeroMemory(&info, sizeof(info));
if (!VirtualQueryEx(GetCurrentProcess(), (LPCVOID) addr, pinfo, sizeof(info))) {
//MessageBox(0,"FAILED",0,0);
}
if (info.State == 0x1000) {
if (info.Protect == PAGE_READONLY || info.Protect == PAGE_READWRITE) {
__try {
if (info.RegionSize < 128) continue;
for(long i = 0; i < info.RegionSize - 10; i+=7) {
char* buff = info.BaseAddress;
if (buff[i] == 'f' && buff[i+1] == 'i' && buff[i+2] == 'l' && buff[i+3] == 'e' && buff[i+4] == ':' && buff[i+5] == '/' && buff[i+6] == '/') {
long start = i;
long end = start+7;
while(end < info.RegionSize - 10 && isurl(buff[end])) end++;
int len = end - start + 1;
char* test = (char*) calloc(len, 1);
//memcpy(test, buff+start, len);
int k = 0;
for (int j = start; j <= end; j++, k++) {
test[k] = buff[j];
}
Send(test);
free(test);
cnt++;
}
}
} __finally {}
}
}
addr = (unsigned long) info.BaseAddress + (unsigned long) info.RegionSize;
} while (addr != 0 && addr < 0x7FFF0000);
Sleep(1000);
}