{"id":1195,"date":"2025-07-23T18:04:36","date_gmt":"2025-07-23T10:04:36","guid":{"rendered":"http:\/\/8.141.27.105\/?p=1195"},"modified":"2025-07-23T18:04:37","modified_gmt":"2025-07-23T10:04:37","slug":"%e8%b0%83%e8%af%95%e5%99%a8%e5%88%9d%e6%8e%a2","status":"publish","type":"post","link":"http:\/\/n0ps1ed.top\/index.php\/2025\/07\/23\/%e8%b0%83%e8%af%95%e5%99%a8%e5%88%9d%e6%8e%a2\/","title":{"rendered":"\u8c03\u8bd5\u5668\u521d\u63a2"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_71 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #f4f4f4;color:#f4f4f4\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #f4f4f4;color:#f4f4f4\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/n0ps1ed.top\/index.php\/2025\/07\/23\/%e8%b0%83%e8%af%95%e5%99%a8%e5%88%9d%e6%8e%a2\/#ptrace%E5%87%BD%E6%95%B0\" title=\"ptrace\u51fd\u6570\">ptrace\u51fd\u6570<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/n0ps1ed.top\/index.php\/2025\/07\/23\/%e8%b0%83%e8%af%95%e5%99%a8%e5%88%9d%e6%8e%a2\/#%E5%87%BD%E6%95%B0%E5%8E%9F%E5%9E%8B\" title=\"\u51fd\u6570\u539f\u578b\">\u51fd\u6570\u539f\u578b<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/n0ps1ed.top\/index.php\/2025\/07\/23\/%e8%b0%83%e8%af%95%e5%99%a8%e5%88%9d%e6%8e%a2\/#%E4%BE%8B%E5%AD%90\" title=\"\u4f8b\u5b50\">\u4f8b\u5b50<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/n0ps1ed.top\/index.php\/2025\/07\/23\/%e8%b0%83%e8%af%95%e5%99%a8%e5%88%9d%e6%8e%a2\/#windows\" title=\"windows\">windows<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"http:\/\/n0ps1ed.top\/index.php\/2025\/07\/23\/%e8%b0%83%e8%af%95%e5%99%a8%e5%88%9d%e6%8e%a2\/#gdb\" title=\"gdb\">gdb<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"ptrace%E5%87%BD%E6%95%B0\"><\/span>ptrace\u51fd\u6570<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><code>ptrace<\/code>\u00a0\u662f Linux \u548c\u5176\u4ed6\u7c7b Unix \u7cfb\u7edf\u4e2d\u7528\u4e8e\u8fdb\u7a0b\u8c03\u8bd5\u548c\u8ddf\u8e2a\u7684\u5f3a\u5927\u7cfb\u7edf\u8c03\u7528\u3002\u5b83\u5141\u8bb8\u4e00\u4e2a\u8fdb\u7a0b\uff08\u8c03\u8bd5\u5668\uff09\u76d1\u89c6\u548c\u63a7\u5236\u53e6\u4e00\u4e2a\u8fdb\u7a0b\uff08\u88ab\u8c03\u8bd5\u8fdb\u7a0b\uff09\u7684\u6267\u884c\uff0c\u4fee\u6539\u5176\u5185\u5b58\u548c\u5bc4\u5b58\u5668\uff0c\u662f\u5b9e\u73b0 GDB\u3001Valgrind \u7b49\u8c03\u8bd5\u5de5\u5177\u7684\u57fa\u7840\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%87%BD%E6%95%B0%E5%8E%9F%E5%9E%8B\"><\/span>\u51fd\u6570\u539f\u578b<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sys\/ptrace.h>\n\nlong ptrace(enum __ptrace_request request, pid_t pid, \n            void *addr, void *data);<\/code><\/pre>\n\n\n\n<p><strong>request<\/strong>\uff1a\u64cd\u4f5c\u7c7b\u578b\uff0c\u5982\u00a0<code>PTRACE_TRACEME<\/code>\u3001<code>PTRACE_PEEKDATA<\/code>\u00a0\u7b49<\/p>\n\n\n\n<p><strong>pid<\/strong>\uff1a\u76ee\u6807\u8fdb\u7a0b\u7684 PID<\/p>\n\n\n\n<p><strong>addr<\/strong>\uff1a\u5185\u5b58\u5730\u5740\u6216\u5bc4\u5b58\u5668\u53f7\uff0c\u53d6\u51b3\u4e8e\u64cd\u4f5c\u7c7b\u578b<\/p>\n\n\n\n<p><strong>data<\/strong>\uff1a\u8f93\u5165 \/ \u8f93\u51fa\u6570\u636e\uff0c\u53d6\u51b3\u4e8e\u64cd\u4f5c\u7c7b\u578b<\/p>\n\n\n\n<p>\u64cd\u4f5c\u7c7b\u578b\u4e2d\uff0c\u4e3b\u8981\u7528\u5230\u00a0PTRACE_TRACEME\u3001PTRACE_ATTACH \u3001PTRACE_SINGLESTEP\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E4%BE%8B%E5%AD%90\"><\/span>\u4f8b\u5b50<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u4ee5<code>PTRACE_TRACEME<\/code>\u4e3a\u4f8b\uff0c\u5f53\u5b50\u8fdb\u7a0b\u6267\u884cptrace(<code>PTRACE_TRACEME<\/code>,&#8230;&#8230;)\u65f6\uff0c\u5185\u6838\u4f1a\u628a\u5b50\u8fdb\u7a0b\u7684PCB\uff08\u8fdb\u7a0b\u63a7\u5236\u5757\uff0c\u5728linux\u4e2d\u662ftask_struct\uff09\u4e2d\u7684<strong>ptrace\u6807\u5fd7<\/strong>\u4f4d\u7f6e\u4e3a<code><strong>PT_PTRACED<\/strong><\/code>\uff0c\u7136\u540e\u7ee7\u7eed\u6267\u884c\u5b50\u8fdb\u7a0b\uff0c\u5f53\u8be5\u8fdb\u7a0b\u9047\u89c1\u7279\u6b8a\u4e2d\u65ad\uff08int 3\uff09\u6216\u8005\u5f02\u5e38\u7684\u65f6\u5019\uff0c\u5c31\u4e0d\u4f1a\u50cf\u5f80\u5e38\u4e00\u6837\u4ea4\u7531\u5185\u6838\u5904\u7406\u4e86\uff0c\u800c\u662f\u76f4\u63a5\u8f6c\u4ea4\u7ed9\u76d1\u89c6\u5b83\u7684\u7236\u8fdb\u7a0b\u6765\u8fdb\u884c\u5904\u7406\u3002<\/p>\n\n\n\n<p>PTRACE_ATTACH\u4e5f\u662f\u5982\u6b64\uff0c\u5f53\u8fdb\u7a0bA\u60f3\u8981\u8c03\u5f0f\u8fdb\u7a0bB\uff0c\u6267\u884cptrace(PTRACE_ATTACH,1111(\u8fdb\u7a0bB\u7684Pid),&#8230;&#8230;)\u65f6\uff0c\u5185\u6838\u4f1a\u5148\u7ed9\u8fdb\u7a0bB\u53d1\u9001SIGSTOP\u8ba9\u5176\u505c\u6b62\uff0c\u7136\u540e\u628a\u8fdb\u7a0bB\u7684<strong>ptrace\u6807\u5fd7<\/strong>\u4f4d\u7f6e\u4e3a<code><strong>PT_PTRACED<\/strong><\/code>\uff08\u8fd9\u6837\u4ee5\u540e\u7684\u7279\u6b8a\u4e2d\u65ad\u548c\u5f02\u5e38\u5c31\u4f1a\u8f6c\u4ea4\u7ed9\u7236\u8fdb\u7a0b\u5904\u7406\uff09\uff0c\u63a5\u7740\u901a\u77e5\u8fdb\u7a0bA\uff0c\u8fdb\u7a0bA\u5c31\u53ef\u4ee5\u901a\u8fc7\u5404\u79cdptrace\u547d\u4ee4\u6765\u7ed9\u8fdb\u7a0bB\u53d1\u53f7\u65bd\u4ee4\u4e86\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5ptrace\u7684\u6838\u5fc3\u4f5c\u7528\u53ef\u4ee5\u6982\u62ec\u4e3a\uff0c\u628a\u67d0\u4e2a\u8fdb\u7a0b\uff08\u88ab\u8c03\u8bd5\u8005\uff09\u7684\u7279\u6b8a\u4e2d\u65ad\u548c\u5f02\u5e38\u5904\u7406\u6743\u4ece\u5185\u6838\u624b\u4e2d\u4ea4\u7ed9\u53e6\u5916\u4e00\u4e2a\u8fdb\u7a0b\uff08\u8c03\u8bd5\u8005\uff09\u5904\u7406\u3002\u5f53\u7136\uff0c\u9664\u6b64\u4e4b\u5916ptrace\u8fd8\u6709\u4e00\u4e9b\u5355\u6b65\u6267\u884c\u7684\u529f\u80fd\uff0c\u5176\u5b9e\u5c31\u662f\u628a\u5bf9\u5e94\u6807\u5fd7\u4f4d\u7f6e1\uff0c\u8fd9\u6837cpu\u5728\u6267\u884c\u4e00\u6b65\u4e4b\u540e\u5c31\u4f1a\u76f4\u63a5\u89e6\u53d1\u4e2d\u65ad\uff0c\u901a\u77e5\u8c03\u8bd5\u8005\uff0c\u8fd9\u91cc\u4e0d\u5c55\u5f00\u53d9\u8ff0\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"windows\"><\/span>windows<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>windows\u5904\u7406\u8c03\u8bd5\u8fdb\u7a0b\u7684\u903b\u8f91\u8ddflinux\u5dee\u4e0d\u591a\uff0c\u4e5f\u662f\u7528\u5230\u4e86PEB\uff08\u7c7b\u4f3clinux\u7684PCB\uff09\u7684<code>DEBUG_PROCESS<\/code>\u00a0\u6807\u5fd7\uff0c\u53ea\u4e0d\u8fc7\u628aptrace\u8fd9\u4e2a\u51fd\u6570\u7ed9\u7ec6\u5316\uff0c\u5207\u5272\u6210\u4e86\u5f88\u591a\u51fd\u6570\u6765\u5b9e\u73b0\uff0c\u4e0b\u9762\u662f\u8868\u683c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u529f\u80fd\u573a\u666f<\/th><th>Linux&nbsp;<code>ptrace<\/code>&nbsp;\u64cd\u4f5c<\/th><th>Windows \u5bf9\u5e94 API \/ \u673a\u5236<\/th><\/tr><\/thead><tbody><tr><td>\u5173\u8054\u88ab\u8c03\u8bd5\u8fdb\u7a0b\uff08\u521b\u5efa\uff09<\/td><td><code>fork<\/code>&nbsp;+&nbsp;<code>ptrace(PTRACE_TRACEME)<\/code><\/td><td><code>CreateProcess<\/code>\uff08\u5e26&nbsp;<code>DEBUG_PROCESS<\/code>&nbsp;\u6807\u5fd7\uff09<\/td><\/tr><tr><td>\u5173\u8054\u88ab\u8c03\u8bd5\u8fdb\u7a0b\uff08\u9644\u52a0\uff09<\/td><td><code>ptrace(PTRACE_ATTACH, pid)<\/code><\/td><td><code>DebugActiveProcess<\/code><\/td><\/tr><tr><td>\u63a5\u6536\u8c03\u8bd5\u4e8b\u4ef6<\/td><td><code>waitpid<\/code>\uff08\u914d\u5408\u4fe1\u53f7\u62e6\u622a\uff09<\/td><td><code>WaitForDebugEvent<\/code><\/td><\/tr><tr><td>\u7ee7\u7eed\u88ab\u8c03\u8bd5\u8fdb\u7a0b\u6267\u884c<\/td><td><code>ptrace(PTRACE_CONT)<\/code><\/td><td><code>ContinueDebugEvent<\/code><\/td><\/tr><tr><td>\u8bfb\u53d6 \/ \u4fee\u6539\u76ee\u6807\u8fdb\u7a0b\u5185\u5b58<\/td><td><code>ptrace(PTRACE_PEEKDATA\/POKEDATA)<\/code><\/td><td><code>ReadProcessMemory<\/code>&nbsp;\/&nbsp;<code>WriteProcessMemory<\/code><\/td><\/tr><tr><td>\u8bfb\u53d6 \/ \u4fee\u6539\u5bc4\u5b58\u5668\u72b6\u6001<\/td><td><code>ptrace(PTRACE_GETREGS\/SETREGS)<\/code><\/td><td><code>GetThreadContext<\/code>&nbsp;\/&nbsp;<code>SetThreadContext<\/code><\/td><\/tr><tr><td>\u8bbe\u7f6e\u65ad\u70b9 \/ \u5355\u6b65\u6267\u884c<\/td><td><code>ptrace(PTRACE_SINGLESTEP)<\/code><\/td><td>\u5f02\u5e38\u65ad\u70b9\uff08<code>INT 3<\/code>\uff09+ \u5355\u6b65\u6807\u5fd7\uff08<code>CONTEXT_SINGLESTEP<\/code>\uff09<\/td><\/tr><tr><td>\u8131\u79bb\u8c03\u8bd5\u5173\u8054<\/td><td><code>ptrace(PTRACE_DETACH)<\/code><\/td><td><code>DebugActiveProcessStop<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"gdb\"><\/span>gdb<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>gdb\u7684\u4e24\u79cd\u4f7f\u7528\u65b9\u5f0f\uff0c\u4e00\u79cd\u662f\u76f4\u63a5\u8c03\u8bd5\u2014\u2014gdb\u542f\u52a8\u540e\u5148fork\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u5b50\u8fdb\u7a0b\u4e2d\u6267\u884cptrace(PTRACE_TRACEME,&#8230;&#8230;)\u6210\u4e3a\u7236\u8fdb\u7a0b\u7684\u8c03\u8bd5\u8fdb\u7a0b\uff0c\u7136\u540e\u6267\u884c\u76ee\u6807\u7a0b\u5e8f\uff0c\u5373\u53ef\u901a\u8fc7gdb\u7236\u8fdb\u7a0b\u5b9e\u73b0\u5bf9\u76ee\u6807\u7a0b\u5e8f\u7684\u8c03\u8bd5\u3002<\/p>\n\n\n\n<p>\u53e6\u5916\u4e00\u79cd\u662f\u9644\u52a0\u8c03\u8bd5\uff0cgdb\u4e2d\u76f4\u63a5\u6267\u884cptrace(PTRACE_ATTACH,\u76ee\u6807pid,&#8230;&#8230;)\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u7740\u91cd\u8bf4\u4e00\u4e0b\u4e0b\u65ad\u70b9\u7684\u65b9\u5f0f\uff0c\u5728\u7528\u6237\u6267\u884c \u201dbreak \u65ad\u70b9\u4f4d\u7f6e\u201c\u4e4b\u540e\uff0cgdb\u4f1a\u5148\u628a\u65ad\u70b9\u4f4d\u7f6e\u7684\u6307\u4ee4\u5b58\u8d77\u6765\uff0c\u7136\u540e\u6539\u6210int 3(0xCC)\uff0c\u8fd9\u6837\u5728\u8fd0\u884c\u5230\u8fd9\u884c\u4ee3\u7801\u65f6\u5019\u5c31\u4f1a\u89e6\u53d1\u5f02\u5e38\u4e2d\u65ad\uff0c\u628a\u5904\u7406\u6743\u4ea4\u7ed9\u8c03\u8bd5\u8005\uff08\u5373\u7236\u8fdb\u7a0bgdb\uff09\uff0c\u7ee7\u7eed\u7684\u8bdd\uff0c\u5c31\u4f1a\u6267\u884c\u88ab\u6362\u6389\u7684\u90a3\u6761\u547d\u4ee4\uff0c\u6267\u884c\u5b8c\u4e4b\u540e\u518d\u6362\u56deint 3\uff0c\u4ee5\u4fbf\u4e0b\u4e00\u6b21\u89e6\u53d1\u65ad\u70b9\uff0c\u5b9e\u9645\u4e0a\uff0cwindows\u7684\u8c03\u8bd5\u5668\u7684\u65ad\u70b9\u4e5f\u662f\u8fd9\u79cd\u601d\u8def\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ptrace\u51fd\u6570 ptrace\u00a0\u662f Linux \u548c\u5176\u4ed6\u7c7b Unix \u7cfb\u7edf\u4e2d\u7528\u4e8e\u8fdb\u7a0b\u8c03\u8bd5\u548c\u8ddf\u8e2a\u7684\u5f3a\u5927\u7cfb\u7edf\u8c03\u7528\u3002\u5b83 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[29],"class_list":["post-1195","post","type-post","status-publish","format-standard","hentry","category-13","tag-29"],"_links":{"self":[{"href":"http:\/\/n0ps1ed.top\/index.php\/wp-json\/wp\/v2\/posts\/1195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/n0ps1ed.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/n0ps1ed.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/n0ps1ed.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/n0ps1ed.top\/index.php\/wp-json\/wp\/v2\/comments?post=1195"}],"version-history":[{"count":2,"href":"http:\/\/n0ps1ed.top\/index.php\/wp-json\/wp\/v2\/posts\/1195\/revisions"}],"predecessor-version":[{"id":1198,"href":"http:\/\/n0ps1ed.top\/index.php\/wp-json\/wp\/v2\/posts\/1195\/revisions\/1198"}],"wp:attachment":[{"href":"http:\/\/n0ps1ed.top\/index.php\/wp-json\/wp\/v2\/media?parent=1195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/n0ps1ed.top\/index.php\/wp-json\/wp\/v2\/categories?post=1195"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/n0ps1ed.top\/index.php\/wp-json\/wp\/v2\/tags?post=1195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}