Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/6911cc4b-5d54-800b-88ae-dc7ac6ff2d3d
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== system() 的实现会把你给它的字符串交给 shell(通常是 /bin/sh -c)去解析和执行。shell 会识别并处理很多元字符(如 ;, &&, |, >, <, <code>, $() 等)以及通配符和引号。如果程序把不可信的外部输入(例如用户输入、文件名、表单字段)直接拼接到 system() 的命令字符串里,攻击者就能注入这些元字符来执行任意命令。 ==== 举个高层次的语义例子: 你想执行 ls <filename>,但如果你把用户输入直接拼进去: <syntaxhighlight>char cmd[256]; snprintf(cmd, sizeof cmd, "ls %s", user_input); system(cmd); </syntaxhighlight> 当 user_input 为 file.txt; rm -rf / 时,shell 会把它当成两条命令:ls file.txt 和 rm -rf / —— 这就是命令注入。 你之前给出的 void '' shellcode; system(/usr/bin/bash, "bash shellcode"); 在语法上和语义上都有问题:system() 只接受一个 char '' 命令字符串,它不会像 exec 那样接受多个参数,也不会直接执行内存里的 shellcode。 ==== <syntaxhighlight lang="c">#include <stdio.h> ==== #include <stdlib.h> #include <string.h> int main(void) { char user_input[200]; char cmd[512]; /'' 假设从外部得到一个文件名 ''/ fgets(user_input, sizeof user_input, stdin); user_input[strcspn(user_input, "\n")] = '\0'; /'' 直接拼接到命令里 —— 不安全! ''/ snprintf(cmd, sizeof cmd, "ls %s", user_input); system(cmd); /'' 这里会调用 /bin/sh -c "ls <user_input>" ''/ return 0; } </syntaxhighlight> 如果攻击者输入 file.txt; echo hacked > /tmp/pwned,就会同时执行不期望的命令。
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)