简体   繁体   English

使用 printf shell 命令将字节打印到文件中

[英]Print bytes into file using printf shell command

I am trying to print the following data into file, but i get the following error: "/bin/sh: 1: Syntax error: Unterminated quoted string".我试图将以下数据打印到文件中,但出现以下错误:“/bin/sh: 1: 语法错误:未终止的引号字符串”。 Here is my program:这是我的程序:

int main(void)
{
char* argv[] =  {"/bin/sh", "-c","printf '%b' '\x7f\x45\x4c\x46\x01\x00\x02\x03' > file.bin", NULL };
execve("/bin/sh", argv, NULL);

  return 1;
}

The problem is in the null byte in the middle of above text: \x7f\x45\x4c\x46\x01\x00\x02\x03" Is there any way to do this like above?问题出在上面文本中间的 null 字节: \x7f\x45\x4c\x46\x01\x00\x02\x03"有没有办法像上面那样做?

The problem with your code is that the backslashes are interpreted in the C string, while you want to pass it as argument for the sh command.您的代码的问题是反斜杠在 C 字符串中被解释,而您希望将其作为sh命令的参数传递。

Therefore you must double escape each backslash character.因此,您必须双重转义每个反斜杠字符。

int main(void)
{
    char* argv[] =  {"/bin/sh", "-c","printf '%b' '\\x7f\\x45\\x4c\\x46\\x01\\x00\\x02\\x03' > file.bin", NULL };
    execve("/bin/sh", argv, NULL);
    return 1;
}

There's a compiler extension that allows R"(printf '%b' '\x7f\x45\x4c\x46\x01\x00\x02\x03' > file.bin)" -- see Does C support raw string literals?有一个允许R"(printf '%b' '\x7f\x45\x4c\x46\x01\x00\x02\x03' > file.bin)"的编译器扩展——请参阅Does C support raw string literals? , but for portability don't use it. ,但为了便携性不要使用它。

Also, it's not possible to pass raw null byte into execve , see those questions此外,不可能将原始 null 字节传递给execve ,请参阅那些问题

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

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