注入/挂钩目标应用程序崩溃
我已将 DLL 注入到目标应用程序中,并在其中挂钩了一些 WINAPI 函数 以及。其中之一是 DrawTextExW。我正在尝试将所有“l”字母替换为“!”前 它打印出来。我的解决方案在几秒钟内运行良好,但随后目标应用程序崩溃了。我实在不明白为什么。
这是功能:
编辑 - 工作解决方案:
int WINAPI DetouredDrawTextExW(__in HDC hdc,
__inout LPWSTR lpchText,
__in int cchText,
__inout LPRECT lprc,
__in UINT dwDTFormat,
__in LPDRAWTEXTPARAMS lpDTParams)
{
std::wstring s_wc(lpchText, cchText);
std::replace(s_wc.begin(), s_wc.end(), L'l', L'!');
return ::DrawTextExW(hdc, const_cast<wchar_t *>(s_wc.c_str()),
s_wc.length(), lprc, dwDTFormat, lpDTParams);
}
那么,有人可以向我指出我做错了什么吗?
I have injected my DLL into a target application where I've hooked few WINAPI-functions
as well. One of them is DrawTextExW. I'm trying to replace all 'l' letters to '!' before
it prints it out. My solution works fine for a few seconds, but then the target application crashes. I really don't understand why.
Here's the function:
Edit - Working solution:
int WINAPI DetouredDrawTextExW(__in HDC hdc,
__inout LPWSTR lpchText,
__in int cchText,
__inout LPRECT lprc,
__in UINT dwDTFormat,
__in LPDRAWTEXTPARAMS lpDTParams)
{
std::wstring s_wc(lpchText, cchText);
std::replace(s_wc.begin(), s_wc.end(), L'l', L'!');
return ::DrawTextExW(hdc, const_cast<wchar_t *>(s_wc.c_str()),
s_wc.length(), lprc, dwDTFormat, lpDTParams);
}
So, can somebody point it out to me what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现您忽略了
cchText
,您是否会收到一个非 NULL 终止的字符串,其cchText
值为正值,导致读取字符串末尾无效记忆?不过,该错误将在 s_wc 的构造函数中作为 Win32 异常出现。此外,您没有检查
dwDTFormat
参数中的DT_MODIFYSTRING
。如果存在该标志,则 ::DrawTextExW() 可能会覆盖无效内存。这将在 ::DrawTextExW() 中显示为 Win32 异常,或者在 s_wc 析构函数中显示为 C++ 异常。编辑
这是未编译、未经测试的代码,我认为它遵守
::DrawTextExW()
的约定I see that you ignore
cchText
, could you be receiving an non-NULL-terminated string with a positive value forcchText
, resulting in reading past the end of the string into invalid memory? That error would present as a Win32 exception in the constructor ofs_wc
, though.Also, you aren't checking for
DT_MODIFYSTRING
in thedwDTFormat
parameter. If that flag is present, then ::DrawTextExW() could be overwriting invalid memory. That would present as a Win32 exception in ::DrawTextExW() or perhaps as a C++ exception in thes_wc
destructor.edit
Here's uncompiled, untested code that I believe obeys the contract of
::DrawTextExW()