{"id":2369,"date":"2025-10-09T16:58:24","date_gmt":"2025-10-09T08:58:24","guid":{"rendered":"https:\/\/www.madbull.site\/?p=2369"},"modified":"2025-12-09T14:48:24","modified_gmt":"2025-12-09T06:48:24","slug":"c%e8%af%ad%e8%a8%80%e8%b0%83%e7%94%a8%e6%8c%87%e4%bb%a4%e9%81%bf%e5%85%8d%e5%91%bd%e4%bb%a4%e6%b3%a8%e5%85%a5","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=2369","title":{"rendered":"C\u8bed\u8a00\u8c03\u7528\u6307\u4ee4\u907f\u514d\u547d\u4ee4\u6ce8\u5165"},"content":{"rendered":"\n<p>\u5728C\u8bed\u8a00\u4e2d\uff0c\u6709\u65f6\u5019\u9700\u8981\u6267\u884c\u4e00\u4e9b\u6307\u4ee4\u5bf9\u6570\u636e\u505a\u4e00\u4e9b\u5feb\u6377\u65b9\u4fbf\u7684\u5904\u7406\uff0c\u5e76\u4e14\u83b7\u53d6\u6267\u884c\u7ed3\u679c\u6765\u67e5\u770b\u6307\u4ee4\u662f\u5426\u6267\u884c\u6210\u529f\u3002\u7ecf\u5e38\u7528\u7684 system \u6216\u8005 popen \u51fd\u6570\u3002\u4f46\u662f\uff0c\u5982\u679c\u6307\u4ee4\u4e2d\u7684\u53c2\u6570\u6765\u81ea\u5916\u90e8\uff0c\u90a3\u4e48\u5c31\u6709\u53ef\u80fd\u5b58\u5728\u547d\u4ee4\u6ce8\u5165\u6f0f\u6d1e\u3002\u8fd9\u5c31\u9700\u8981\u5bf9\u6307\u4ee4\u505a\u4e00\u4e9b\u8f6c\u4e49\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u672c\u6587\u8bb0\u5f55\u4e86\u4e00\u79cd\u65b9\u6cd5\uff0c\u4f7f\u7528 \u5355\u5f15\u53f7 \u5bf9 <strong>\u6307\u4ee4\u7684\u53c2\u6570<\/strong> \u505a\u8f6c\u4e49\uff0c\u6765\u907f\u514d\u6307\u4ee4\u6ce8\u5165\u6f0f\u6d1e\u3002<\/p>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u7f3a\u70b9\u662f\uff1a\u6307\u4ee4\u4e2d\u6240\u6709\u7684 <code>`\\$\\{<\/code> \u7b49\u65b9\u5f0f\u83b7\u53d6\u4fbf\u4ee4\u6216\u8005\u51fd\u6570\u7ed3\u679c\u7684\u65b9\u5f0f\u90fd\u4e0d\u4f1a\u751f\u6548\uff0c\u6240\u4ee5\u9700\u8981\u4ed4\u7ec6\u5206\u6790\u573a\u666f\u662f\u5426\u9002\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ gcc -Wall test.c -o test\n\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n\n\/**\n * \u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c shell \u8f6c\u4e49\uff0c\u4f7f\u5176\u53ef\u5b89\u5168\u7528\u4e8e popen\/system \u7b49\u8c03\u7528\u3002\n * \u4f7f\u7528\u5355\u5f15\u53f7\u5305\u88f9\uff0c\u5e76\u8f6c\u4e49\u5185\u90e8\u5355\u5f15\u53f7\u3002\n *\n * @param input: \u5f85\u8f6c\u4e49\u7684\u539f\u59cb\u5b57\u7b26\u4e32\n * @return: \u8f6c\u4e49\u540e\u7684\u65b0\u5b57\u7b26\u4e32\uff08\u9700\u7528 free() \u91ca\u653e\uff09\uff0c\u5931\u8d25\u8fd4\u56de NULL\n *\/\nchar* shell_escape(const char* input) {\n    if (input == NULL) {\n        return NULL;\n    }\n\n    size_t len = strlen(input);\n    \/\/ \u6700\u574f\u60c5\u51b5\uff1a\u6bcf\u4e2a\u5b57\u7b26\u90fd\u662f\u5355\u5f15\u53f7\uff0c\u9700\u8981\u6269\u5c55\u4e3a4\u500d + 2\u4e2a\u5916\u5c42\u5355\u5f15\u53f7 + 1\u4e2a\\0\n    \/\/ \u4f8b\u5982\uff1a' -&gt; '\\'' (4\u5b57\u7b26)\n    size_t max_len = len * 4 + 3;\n    char* escaped = (char*)malloc(max_len); \/\/ \u9700\u8981\u5728\u51fd\u6570\u5916\u90e8\u91ca\u653e\n    if (escaped == NULL) {\n        return NULL;\n    }\n\n    char* p = escaped;\n    *p++ = '\\'';  \/\/ \u5f00\u59cb\u5355\u5f15\u53f7\n\n    for (size_t i = 0; i &lt; len; i++) {\n        if (input&#91;i] == '\\'') {\n            \/\/ \u8f6c\u4e49\u5355\u5f15\u53f7\uff1a\u7ed3\u675f\u5f53\u524d\u5355\u5f15\u53f7\uff0c\u63d2\u5165\u8f6c\u4e49\u5355\u5f15\u53f7\uff0c\u518d\u5f00\u65b0\u5355\u5f15\u53f7\n            *p++ = '\\'';\n            *p++ = '\\\\';\n            *p++ = '\\'';\n            *p++ = '\\'';\n        } else {\n            *p++ = input&#91;i];\n        }\n    }\n\n    *p++ = '\\'';  \/\/ \u7ed3\u675f\u5355\u5f15\u53f7\n    *p = '\\0';\n\n    return escaped;\n}\n\n\/\/ \u6d4b\u8bd5\nint main() {\n\n    \/\/ \u6d4b\u8bd5\u6307\u4ee4\n    const char* filename = \"touch aa bb dd &amp;*()&amp;%{&#91;]]}'098feff%@#@0#1'921'2{{} jojfaefae \\\" '\/tmp\/t01'; touch \/tmp\/t02\";\n    char* escaped = shell_escape(filename);\n    if (escaped == NULL) {\n        perror(\"shell_escape\");\n        return 1;\n    }\n\n    char command&#91;512];\n    printf(\"filename &#91;%s]\\n\", escaped) ;\n    snprintf(command, sizeof(command), \"mkdir -p %s\", escaped);\n    printf(\"Executing: &#91;%s]\\n\", command);\n\n    FILE* fp = popen(command, \"r\");\n    if (fp == NULL) {\n        perror(\"popen\");\n        free(escaped);\n        return 1;\n    }\n\n    \/\/ \u8bfb\u53d6\u8f93\u51fa\n    char buffer&#91;256];\n    while (fgets(buffer, sizeof(buffer), fp) != NULL) {\n        printf(\"%s\", buffer);\n    }\n\n    pclose(fp);\n    \/\/ \u6ce8\u610f\uff1a\u4e00\u5b9a\u8981\u91ca\u653eescaped\u7684\u7a7a\u95f4\uff0c\u8fd9\u91cc\u662fshell_escape\u51fd\u6570\u5185\u90e8\u7533\u8bf7\u7684\u5185\u5b58\n    free(escaped);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u6d4b\u8bd5\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1060\" height=\"371\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2025\/10\/11.png\" alt=\"\" class=\"wp-image-2370\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u5728C\u8bed\u8a00\u4e2d\uff0c\u6709\u65f6\u5019\u9700\u8981\u6267\u884c\u4e00\u4e9b\u6307\u4ee4\u5bf9\u6570\u636e\u505a\u4e00\u4e9b\u5feb\u6377\u65b9\u4fbf\u7684\u5904\u7406\uff0c\u5e76\u4e14\u83b7\u53d6\u6267\u884c\u7ed3\u679c\u6765\u67e5\u770b\u6307\u4ee4\u662f\u5426\u6267\u884c\u6210\u529f\u3002\u7ecf\u5e38\u7528\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-my","format":"standard","meta":{"footnotes":""},"categories":[156,154],"tags":[175,237,19],"class_list":["post-2369","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-c","category-154","tag-c","tag-linux","tag-19"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2369"}],"version-history":[{"count":4,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2369\/revisions"}],"predecessor-version":[{"id":2440,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2369\/revisions\/2440"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}