Windows 下使用 Linux 路径引发的路径搜索漏洞

在 Windows 上,如果使用 Linux 形式的路径,或者错误使用 Windows 相对路径,可能引发路径搜索漏洞(Untrusted Search Path Vulnerability)。

一个简单的测试用例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <Windows.h>

#pragma warning(disable: 4996)

void open_file(const char* path)
{
FILE* fp = fopen(path, "rb");
if (fp)
{
fclose(fp);
}
}

int main(int argc, char** argv)
{
open_file("/usr/local/ssl/openssl.cnf"); // Bad
open_file("\\usr\\local\\ssl\\openssl1.cnf"); // Bad
open_file(".\\usr\\local\\ssl\\openssl2.cnf"); // Good
open_file("usr\\local\\ssl\\openssl3.cnf"); // Good

LoadLibrary(L"/DummyTLS/dummyTLS.dll"); // Bad
LoadLibrary(L"\\DummyTLS\\dummyTLS1.dll"); // Bad
LoadLibrary(L".\\DummyTLS\\dummyTLS2.dll"); // Good
LoadLibrary(L"DummyTLS\\dummyTLS3.dll"); // Good

return 0;
}

编译好之后,把 EXE 放在 C 盘的某个文件夹下(为了更好地展示测试效果,请不要放在根目录下),打开 Process Monitor 监控 EXE 的行为。

使用 Process Monitor 监控进程行为

总结如下两点

  • Windows 不能正确处理 Linux 路径,不管分隔符是 / 还是 \\
    • 这样的路径会导致进程去磁盘根目录搜索文件
  • Windows 正确的相对路径,应该以 .\\ 开头,或者直接以非 /\\ 开头
    • 这样的路径会使用正常的搜索逻辑(当前目录、PATH 环境变量等)

今日心得

  • Process Monitor 的 NAME NOT FOUNDPATH NOT FOUND 都需要留意
  • 做漏洞研究不要光看不练,要多动手实践

参考文档

  1. https://hackerone.com/reports/608577
  2. https://www.ndss-symposium.org/ndss-paper/file-hijacking-vulnerability-the-elephant-in-the-room/
  3. https://houjingyi233.com/2021/08/01/new-dll-hijacking-scenario-found-by-accident/