使用 Android NDK 演示 printf 或 __android_log_print 漏洞

Demonstrating printf or __android_log_print Vulnerabilities With Android NDK

本文关键字:android log print 漏洞 Android NDK 演示 printf 使用      更新时间:2023-10-16

我有兴趣通过 NDK 应用程序演示printf漏洞。需要明确的是,我知道要登录控制台,我们可以使用 __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "Print : %d %s",someVal, someStr); .我已经尝试过了,我知道它有效。但我明确想演示printf()的漏洞,特别是使用 %n 说明符写入指向位置。

有没有办法使printf()达到这种效果,或者是否有可能通过__android_log_print()来实现这一点?我尝试使用 android/log.h 标头,但它不起作用。

我可以通过运行一些东西来让应用程序崩溃 printf(%s%s%s%s%s%s%s%s%s%s).但同样,我无法操纵指针。

出于一般知识目的,为什么printf()首先不起作用,__android_log_print()如何防止这些漏洞利用?

你确实意识到Android是开源的。

从寻找__android_log_print()开始并找到它:https://android.googlesource.com/platform/system/core/+/refs/heads/master/liblog/logger_write.cpp

int __android_log_print(int prio, const char* tag, const char* fmt, ...) {
  va_list ap;
  char buf[LOG_BUF_SIZE];
  va_start(ap, fmt);
  vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
  va_end(ap);
  return __android_log_write(prio, tag, buf);
}

我最终看了:https://android.googlesource.com/platform/bionic/+/refs/heads/master/libc/stdio/vfprintf.cpp

第 453-454 行:

  case 'n':
    __fortify_fatal("%%n not allowed on Android");

代码中还引用了通过 FORTIFY 实现的额外安全性,这在以下博客文章中进行了描述:

https://android-developers.googleblog.com/2017/04/fortify-in-android.html

Android 特别不支持%n格式说明符,因为它们容易受到攻击。

https://android.googlesource.com/platform/bionic/+/400b073ee38ecc2a38234261b221e3a7afc0498e/tests/stdio_test.cpp#328