我有一個dll,我注入了一個進程。它搜索'file://'直到找到無效符號。幾分鐘後,它會導致主流程崩潰。這是爲什麼?我該如何檢查?我發現CreateThread的堆棧大小越小,它崩潰的速度就越快,所以它可能會以某種方式堆棧溢出,但我沒有分配任何東西,而是分配一個結構。注入的DLL崩潰了所有者進程。爲什麼?
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);
}
您可以隨時登錄到文件以檢查崩潰發生的位置。畢竟,正如你所說,這似乎不是堆棧相關的,因爲你沒有使用遞歸或大型本地緩衝區。 –
不要使用pastie。粘貼源並確保它格式正確。 –