简体   繁体   English

C++ 中是否有替代“system()”命令的方法。?

[英]Is there an alternative to the 'system()' command in C++.?

I have read a lot of answers saying that the system() command is bad.我已经阅读了很多答案,说system()命令不好。 First off, why is it so bad?首先,为什么会如此糟糕? Second off, is there an alternative that doesn't produce such a security hole?其次,有没有不会产生这种安全漏洞的替代方案? I mostly want to know if there is a way to clear screen in C++.我最想知道是否有办法在 C++ 中清除屏幕。 In python I have a clear function that checks the os name and runs either system('cls') or system('clear') .在 python 中,我有一个 clear 函数来检查操作系统名称并运行system('cls')system('clear') Is this a security hole as well?这也是一个安全漏洞吗? If so, is there a python alternative?如果是这样,是否有 python 替代方案?

First off, why is it so bad?首先,为什么会如此糟糕?

Because you introduce dependencies to the OS in your code, and make it unportable.因为您在代码中引入了对操作系统的依赖关系,并使其不可移植。

Second off, is there an alternative that doesn't produce such a security hole?其次,有没有不会产生这种安全漏洞的替代方案?

No, the existing alternatives (POSIX compatible) fork() and execxx() or pipe() have the same problems of introducing OS dependencies and security holes.不,现有的替代方案(POSIX 兼容) fork()execxx()pipe()具有引入操作系统依赖项和安全漏洞的相同问题。

Is this a security hole as well?这也是一个安全漏洞吗?

The main secuity hole is introduced with commands constructed from parameters like主要安全漏洞是通过由参数构造的命令引入的,例如

void exec_ls(const std::string param) {
    std::string cmd;
    cmd = "ls -l " + param;
    system(cmd.c_str());

If someone manages to inject some additional command via param , eg如果有人设法通过param注入一些额外的命令,例如

 std::string param = "dir ; rm -rf /*";
                       // ^^^^^^^^^^^
 exec_ls(param);

they can call all kinds of malicious commands.他们可以调用各种恶意命令。 Another security hole comes from the point, that someone might replace cls or clear commands on your system with some malicious code.另一个安全漏洞来自于某人可能用一些恶意代码替换您系统上的clsclear命令。
The only way to get over this, is to secure your system in a way, that such isn't possible.克服这个问题的唯一方法是以某种方式保护您的系统,这是不可能的。

If so, is there a python alternative?如果是这样,是否有 python 替代方案?

Using a different programming language as an intermediate caller doesn't fix the problem I mentioned above.使用不同的编程语言作为中间调用者并不能解决我上面提到的问题。

system functions (across many language, including Python and C++) are not inherently "bad" but they present difficulties for correct use. system函数(跨多种语言,包括 Python 和 C++)本质上并不是“坏的”,但它们在正确使用方面存在困难。

Security安全

You need to be absolutely sure that whatever you're executing via system is secure.您需要绝对确保通过system执行的任何操作都是安全的。

If you write system("echo hello " + name) then you need to be absolutely sure that name cannot be controlled by a malicious user.如果您编写system("echo hello " + name)那么您需要绝对确保该name不会被恶意用户控制。 name = "; rm -rf /" would result in echo hello ; rm -rf / name = "; rm -rf /"将导致echo hello ; rm -rf / echo hello ; rm -rf / , so if that's coming from a user, via something like a web form or a database, then you need to exercise a lot of caution, and I would recommend a more sophisticated solution than system . echo hello ; rm -rf / ,所以如果它来自用户,通过网络表单或数据库之类的东西,那么你需要非常小心,我会推荐一个比system更复杂的解决方案。

A call like system("clear") is secure for your purposes.system("clear")这样的调用对您来说是安全的。

Usability可用性

System calls give you several outputs (I'll give an example for calls to a bash shell):系统调用为您提供了几个输出(我将举一个调用bash shell 的示例):

  • status code (whether the shell indicated an error condition)状态码(shell 是否指示错误情况)
  • contents of STDOUT标准输出的内容
  • contents of STDERR STDERR 的内容

system returns the status code. system返回状态码。 For commands like ls , you are interested in receiving STDOUT, and you may also check the status code.对于ls这样的命令,你有兴趣接收 STDOUT,你也可以查看状态码。 This is unwieldy with system .这对于system是笨拙的。

The Python subprocess module is generally accepted by the community as an easier way to manage these concerns. Python subprocess模块被社区普遍接受,作为管理这些问题的一种更简单的方法。

How to manage the console如何管理控制台

If you're trying to manage the console display, you may be interested in a library like ncurses which has broad OS support.如果您正在尝试管理控制台显示,您可能会对像ncurses这样具有广泛操作系统支持的库感兴趣。

Adding ncurses as a dependency could be heavy-handed, if clearing the screen is the only thing you need to do.如果清除屏幕是您唯一需要做的事情,那么将ncurses添加为依赖项可能会很严厉。 If that's the case, then I see nothing wrong with using system() like you're doing.如果是这种情况,那么我认为像您一样使用system()没有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM