检测 Mac OS X 上的调试器
我试图检测我的进程是否正在调试器中运行,虽然在 Windows 中有很多解决方案,在 Linux 中我使用:
ptrace(PTRACE_ME,0,0,0)
并检查其返回值,但我没有设法在 Mac OS 上执行相同的基本检查X. 我尝试使用该
ptrace(PT_TRACE_ME,0,0,0)
调用,但即使在 gdb 下运行,它总是返回 0。
如果我将请求更改为 PT_DENY_ATTACH ,它会正确停止调试,但这不是我想要实现的目标。有什么想法吗?
I am trying to detect whether my process is being run in a debugger or not and, while in Windows there are many solutions and in Linux I use:
ptrace(PTRACE_ME,0,0,0)
and check its return value, I did not manage to perform the same basic check on Mac OS X.
I tried to use the
ptrace(PT_TRACE_ME,0,0,0)
call but it always returns 0 even when run under gdb.
If I change the request to PT_DENY_ATTACH
it correctly stops the debugging but that is not what I want to achieve. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只需从 调用函数
AmIBeingDebugged()
Apple 技术问答 QA1361,此处转载是因为 Apple 有时会破坏文档链接并使旧文档难以找到:You can just call the function
AmIBeingDebugged()
from Apple Technical Q&A QA1361, which is reproduced here because Apple sometimes breaks documentation links and makes old documentation hard to find:这会查看我们的进程中是否有活动的异常处理程序(对于 EXC_BREAKPOINT、EXC_BAD_ACCESS 等)。在调试器中不需要 ptrace 来实现此目的,因此仅依赖于要设置的 ptrace 标志并不是很理想。
http://reverse 中提到了这种方法。 put.as/wp-content/uploads/2012/07/Secuinside-2012-Presentation.pdf
我的博客文章对此进行了更详细的描述。
This looks and sees if there is an active exception handler in our process (for EXC_BREAKPOINT, EXC_BAD_ACCESS, etc). Ptrace is not required to achieve this in a debugger, thus relying only on a ptrace flag to be set is not quite ideal.
This approach is mentioned in http://reverse.put.as/wp-content/uploads/2012/07/Secuinside-2012-Presentation.pdf
My blog post describes this in more detail.
以下是来自 Apple 技术问答 QA1361:
用法:
Here's a Swift version of the function from Apple Technical Q&A QA1361:
Usage: