c - 如何禁用有关在 GCC 中使用弃用获取的警告?

标签 c gcc exploit gcc-warning gets

我正在运行一个 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=c99std=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/

相关文章:

javascript - Google Drive API——直接下载不同格式的视频

ruby - 如何在不使用空格字符的情况下运行 unix 命令,以便执行远程命令?

c++ - 如何在 C++ 中启动具有管理员权限的应用程序?

c - C 函数中保留的数据

c - GCC SSA 格式变量的含义

c++ - 如何正确引用匿名命名空间中的函数

linux - C Windows/Linux 缓冲区溢出漏洞

c - 我的开关盒出现错误

以区分大小写的方式计算文件中字符出现的次数

c - scanf仅交替工作