我正在运行一个 CTF,我目前正在编写一个利用 C 的 gets
函数的问题。我了解该功能已弃用且很危险,我绝不会在任何其他情况下使用它。不幸的是,gcc
编译了我的代码,当我在 gets
函数被命中时运行二进制文件时,我收到一条友好的错误消息:
warning: this program uses gets(), which is unsafe.
这通常会很好,因为它会警告你 get 是不安全的,但不幸的是,在我的 CTF 中,我认为这个错误消息让问题变得有点太容易了。您知道我将如何禁用此警告吗?谢谢!
$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
最佳答案
注意:我刚刚意识到您的问题标题似乎放错了位置 - 您收到的警告来自 macOS,关于执行使用 gets()
的程序。与使用GCC编译无关。
:-/无论如何,我让我的答案活着以供引用。
正如评论:我在谷歌上搜索了一些关于您要查找的内容,但似乎没有可靠的方法可以在执行程序时禁用此警告。有人建议重建 /usr/lib/libSystem.B.dylib
如果它确实有效,但没有任何结果或经验,但我个人认为这有点过于极端,甚至可能是有害的。 - 我不推荐这种技术。
如果你真的想创建一个漏洞利用程序,请尝试通过一个自定义函数来重建 gets()
并将该函数命名为稍有不同,例如 f.e. gets_c()
。这应该是从 macOS 禁用此警告的解决方法。
旧答案(关于 GCC 本身):
首先,您似乎正在使用符合 C99 或 C89/C90 的编译器,或者使用 std=c99
或 std=c89
/ 进行编译std=c90
选项,因为只有符合 C11 之前标准的编译器会警告 gets()
已被弃用。
ISO/IEC 删除了 C11 中的 gets()
函数。如果您使用 C11 或更新的符合标准的编译器进行编译,则在代码中使用 gets()
的隐式声明时会出现错误:
"error: implicit declaration of function
'gets'
; did you mean'fgets'
? [-Werror=implicit-function-declaration
]"
如果您想在编译时抑制警告,请在编译时使用 -Wno-deprecated-declarations
选项来禁用对已弃用声明的诊断。
来自 GCC 在线文档:
-Wno-deprecated-declarations
Do not warn about uses of functions, variables, and types marked as deprecated by using the deprecated attribute. (see Function Attributes, see Variable Attributes, see Type Attributes.)
Source: https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Warning-Options.html
如果您想在代码中嵌入对警告的抑制,请使用 David´s deleted answer 中使用的方法,通过使用 #pragma 实现对
:-Wno-deprecated-declarations
的抑制
char str[256];
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
gets(str);
#pragma GCC diagnostic pop
关于c - 如何禁用有关在 GCC 中使用弃用获取的警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62056703/