Gets a UNICODE_STRING content in a remote process as wstring
| 485 | |
| 486 | // Gets a UNICODE_STRING content in a remote process as wstring |
| 487 | std::string GetRemoteUnicodeStr(HANDLE hProcess, UNICODE_STRING* u) { |
| 488 | if (!hProcess || hProcess == INVALID_HANDLE_VALUE || !u) { |
| 489 | LOG_A(LOG_ERROR, "GetRemoteUnicodeStr: Invalid parameters"); |
| 490 | return std::string(); |
| 491 | } |
| 492 | |
| 493 | if (!u->Buffer || u->Length == 0) { |
| 494 | return std::string(); // Empty string is valid |
| 495 | } |
| 496 | |
| 497 | // Sanity check for reasonable string length (64KB limit) |
| 498 | if (u->Length > 65536) { |
| 499 | LOG_A(LOG_WARNING, "GetRemoteUnicodeStr: String length too large: %u", u->Length); |
| 500 | return std::string(); |
| 501 | } |
| 502 | |
| 503 | std::wstring s; |
| 504 | //std::vector<wchar_t> commandLine(u->Length / sizeof(wchar_t)); |
| 505 | std::vector<wchar_t> uni(u->Length / sizeof(wchar_t) + 1, L'\0'); // Add space for null terminator |
| 506 | |
| 507 | if (!ReadProcessMemory(hProcess, u->Buffer, uni.data(), u->Length, NULL)) { |
| 508 | LOG_A(LOG_ERROR, "ProcessQuery: Could not ReadProcessMemory error: %lu", GetLastError()); |
| 509 | return std::string(); |
| 510 | } |
| 511 | else { |
| 512 | // Ensure null termination |
| 513 | uni[u->Length / sizeof(wchar_t)] = L'\0'; |
| 514 | s.assign(uni.begin(), uni.end() - 1); // Exclude the manually added null terminator |
| 515 | } |
| 516 | |
| 517 | std::string str = wstring2string(s); |
| 518 | return str; |
| 519 | } |
| 520 | |
| 521 | |
| 522 | wchar_t* GetFileNameFromPath(wchar_t* path) { |
no test coverage detected