[英]libcurl crashes in a Windows service
我的服務從某些進程接收數據,對其進行解析,然后將HTTP帖子發送到我的PHP服務器。
當我開始編寫代碼時,它是一個普通的64位程序。 完成后,我將其轉換為服務,但是當服務嘗試發送數據時會發生一些崩潰。
原因尚不清楚,因為我在服務的其他位置使用libcurl時沒有問題。
我的接收器是這樣的:
while (true)
{
memset(pipe_buffer, 0, 10000);
cres = ReadFile(pipe, pipe_buffer, 10000, &read, 0);
ofile << "[*] got a packet with length : " << read << endl;
if (read > 0 && cres) {
ofile << "[*] " << pipe_buffer << endl;
// send the request
string payload;
payload += "data=";
payload += pipe_buffer;
ofile << "[*] sending post : " << url << "?" << payload<< endl;
CURL *curl;
curl = curl_easy_init();
if (!curl) {
ofile << "[!] curl failed to init" << endl;
return 0;
}
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // crashes start here
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload.c_str());
curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
else {
// the client may dissconnect , wait for it to connect again
DisconnectNamedPipe(pipe); ConnectNamedPipe(pipe, 0);
}
}
每次我都會遇到非常不同且奇怪的錯誤。
它們大多數來自libcurl調用的RtlFreeHeap()
,以及curl_easy_perform()
使用的某些WSA函數的整數除以零。
從curl_easy_setopt()
開始,任何libcurl函數都可能發生崩潰。
在普通程序中,相同的代碼可以正常工作。
編輯:挖掘后,此函數導致損壞,以前的curl使用未發生崩潰的原因是,除了此之后,我不使用此函數,然后我創建了一個與該線程並行工作的接收器線程使用此功能,普通程序也不會崩潰,因為該功能僅用於服務(不在本地系統中運行的程序可以使用EnumWindows)我認為Poco net不會崩潰,因為它基於c ++而不是c並使用new / delete分配和釋放內存,但是由於curl和其他函數而導致的崩潰從_malloc_base和類似的c分配函數開始
功能代碼:
wstring GetCommandLineRemote(DWORD id) {
PROCESS_BASIC_INFORMATION pbInfo = { 0 };
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, 0, id);
if (!hProc || hProc == INVALID_HANDLE_VALUE) return wstring(L"");
auto status = fNtQueryInformationProcess(hProc, ProcessBasicInformation, &pbInfo, sizeof(pbInfo), NULL);
if (!NT_SUCCESS(status)) { CloseHandle(hProc); return wstring(L""); }
BPEB bbeb = { 0 };
BOOL result;
result = ReadProcessMemory(hProc, (void*)pbInfo.PebBaseAddress, &bbeb, sizeof(BPEB), 0);
if (!result) { CloseHandle(hProc); return wstring(L""); }
BRTL_USER_PROCESS_PARAMETERS parameters = { 0 };
result = ReadProcessMemory(hProc, (void*)((uintptr_t)bbeb.ProcessParameters), ¶meters, sizeof(BRTL_USER_PROCESS_PARAMETERS), 0);
if (!result) { CloseHandle(hProc); return wstring(L""); }
UNICODE_STRING CommandLine = { 0 };
CommandLine.Length = parameters.CommandLine.Length;
CommandLine.MaximumLength = parameters.CommandLine.MaximumLength;
CommandLine.Buffer = new WCHAR[CommandLine.MaximumLength];
result = ReadProcessMemory(hProc, (void*)parameters.CommandLine.Buffer, CommandLine.Buffer, parameters.MaximumLength, 0);
if (!result) { CloseHandle(hProc); return wstring(L""); }
CloseHandle(hProc);
wstring wCommandLine = CommandLine.Buffer;
delete CommandLine.Buffer;
return wCommandLine;
}
我正在使用此功能通過進程開始時使用的命令行來區分幫助程序進程的實例
因此查找實例的機制如下所示:
vector<DWORD> enum_ids(wstring proc_name) {
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
vector<DWORD> ids;
PROCESSENTRY32W entry = { 0 };
entry.dwSize = sizeof(entry);
if (!Process32FirstW(snap, &entry)) return ids;
do {
wstring p_name = entry.szExeFile;
auto check_pos = p_name.find(proc_name);
if (check_pos != wstring::npos) {
ofile << "[*] found process instance with id : " << entry.th32ProcessID << endl;
ids.push_back(entry.th32ProcessID);
}
} while (Process32NextW(snap, &entry));
return ids;
}
DWORD find_process(wstring proc_name,wstring unique) {
DWORD process_id = 0;
auto ids = enum_ids(proc_name);
for (auto id : ids) {
wstring wCommandLine = GetCommandLineRemote(id);
auto check_pos = wCommandLine.find(unique);
if (check_pos != wstring::npos) {
process_id = id;
break;
}
//if (id == 83004) { process_id = id; break; } 83004 is example , if I used this instead of the above comparison code no errors occur so I assumed the errors come from GetCommandLineRemote
}
return process_id;
}
然后在服務主線程中:
CreateThread(0, 0, recieve, 0, 0, 0);
CreateThread(0, 0, FindParticularInstance, (char*)"ch", 0, 0);
現在,在找到錯誤源之后,此函數將如何處理所有這些錯誤以及如何防止此錯誤發生?
結構的定義(來自NiroSoft和流程黑客):
typedef struct _CURDIR
{
UNICODE_STRING DosPath;
PVOID Handle;
} CURDIR, *PCURDIR;
typedef struct _RTL_DRIVE_LETTER_CURDIR
{
WORD Flags;
WORD Length;
ULONG TimeStamp;
STRING DosPath;
} RTL_DRIVE_LETTER_CURDIR, *PRTL_DRIVE_LETTER_CURDIR;
typedef struct _BRTL_USER_PROCESS_PARAMETERS
{
ULONG MaximumLength;
ULONG Length;
ULONG Flags;
ULONG DebugFlags;
PVOID ConsoleHandle;
ULONG ConsoleFlags;
PVOID StandardInput;
PVOID StandardOutput;
PVOID StandardError;
CURDIR CurrentDirectory;
UNICODE_STRING DllPath;
UNICODE_STRING ImagePathName;
UNICODE_STRING CommandLine;
PVOID Environment;
ULONG StartingX;
ULONG StartingY;
ULONG CountX;
ULONG CountY;
ULONG CountCharsX;
ULONG CountCharsY;
ULONG FillAttribute;
ULONG WindowFlags;
ULONG ShowWindowFlags;
UNICODE_STRING WindowTitle;
UNICODE_STRING DesktopInfo;
UNICODE_STRING ShellInfo;
UNICODE_STRING RuntimeData;
RTL_DRIVE_LETTER_CURDIR CurrentDirectores[32];
ULONG EnvironmentSize;
} BRTL_USER_PROCESS_PARAMETERS, *PBRTL_USER_PROCESS_PARAMETERS;
typedef struct _BPEB
{
UCHAR InheritedAddressSpace;
UCHAR ReadImageFileExecOptions;
UCHAR BeingDebugged;
UCHAR BitField;
ULONG ImageUsesLargePages : 1;
ULONG IsProtectedProcess : 1;
ULONG IsLegacyProcess : 1;
ULONG IsImageDynamicallyRelocated : 1;
ULONG SpareBits : 4;
PVOID Mutant;
PVOID ImageBaseAddress;
PPEB_LDR_DATA Ldr;
PBRTL_USER_PROCESS_PARAMETERS ProcessParameters;
PVOID SubSystemData;
PVOID ProcessHeap;
PRTL_CRITICAL_SECTION FastPebLock;
PVOID AtlThunkSListPtr;
PVOID IFEOKey;
ULONG CrossProcessFlags;
ULONG ProcessInJob : 1;
ULONG ProcessInitializing : 1;
ULONG ReservedBits0 : 30;
union
{
PVOID KernelCallbackTable;
PVOID UserSharedInfoPtr;
};
ULONG SystemReserved[1];
ULONG SpareUlong;
} BPEB, *PBPEB;
導致堆損壞的確切行是:
CommandLine.Buffer = new WCHAR[CommandLine.MaximumLength];
result = ReadProcessMemory(hProc, (void*)parameters.CommandLine.Buffer, CommandLine.Buffer, parameters.MaximumLength, 0);
ReadProcessMemory成功,但導致使用curl函數時出現堆損壞
更改此行:
CommandLine.Buffer = new WCHAR[CommandLine.MaximumLength];
對此:
CommandLine.Buffer = (wchar_t*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, CommandLine.MaximumLength);
然后釋放緩沖區,然后再返回HeapFree而不是delete來解決問題
我沒有遇到這樣的問題,如果ReadProcessMemory讀入分配給new的緩沖區中,則會引起此類奇怪的錯誤。
該函數的文檔對此一無所知
有人遇到過這樣的問題嗎?
在GetCommandLineRemote()
,當您分配CommandLine.Buffer
,您正在過度分配。 MaximumLength
以字節表示, WCHAR
大小為2個字節。 您對new[]
使用分配的內存比實際需要多2倍,但這是可以的,因為您讀取的字節不會比分配的多。 您應該將MaximumLength
除以sizeof(WCHAR)
以避免在使用new WCHAR[]
時過度分配:
CommandLine.Buffer = new WCHAR[(CommandLine.MaximumLength / sizeof(WCHAR)) + 1];
但是,不能保證您讀取的UNICODE_STRING
數據以null終止,但是您在構造最終wstring
時將其視為已處理。 您應該考慮Length
(也用字節表示),而不要依賴空終止符:
wstring wCommandLine(CommandLine.Buffer, CommandLine.Length / sizeof(WCHAR));
更重要的是,您將使用delete
而不是delete[]
釋放CommandLine.Buffer
,因此從那時起,您的代碼就具有未定義的行為 。 用new
分配的內存通過delete
釋放。 用new[]
分配的內存可以用delete[]
釋放。 您不能互換它們。
另外, enum_ids()
,在enum_ids()
,您正在泄漏CreateToolhelp32Snapshot()
返回的HANDLE
。 使用CloseHandle()
需要使用CloseHandle()
將其關閉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.