linux 和windows一样 内核允许的路径长度比界面(shell)上的要长,这样也导致一些危险程序把自己拷贝到较长的路径下导致用户无法发现
下面程序可以创建超长路径
FILE *fInput = fopen(fname, "rb");
string strCur;
int c = 0;
while (true)
{
if (mkdir("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", 0755) == 0)
{
if (chdir("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890") == 0)
{
c++;
strCur += "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890/";
if (c >= 50)
{
create_cp_file(fname, fInput);
chmod(fname, 0755);
break;
}
}
else
{
printf("err chdir %d\n", errno);
break;
}
}
else
{
printf("err mkdir %d\n", errno);
break;
}
}
printf("dirlen=%d\n", (int) strCur.length());
if (fInput)
{
fclose(fInput);
}
创建后你可以用cd cd cd ... 进入这个目录然后./hello 就能执行了

如果用opendir 递归去打开目录来枚举,就会遇到当opendir的参数 过长时返回失败,那么如果要扫描到这个文件可以用2种方法
(1)chdir 不断进入子目录
(2)寻找更好的api
对于(1)的话可能更通用,但chdir会导致进程的全局路径变量被修改,导致一些路径找不到的错误,使用这种方法要保证枚举是在独立的进程里进行的
对于方法(2)
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>
#define SYS_getdents64 61 // x64 系统调用号(见 /usr/include/asm/unistd_64.h)
struct linux_dirent64 {
ino64_t d_ino;
off64_t d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name[];
};
int main() {
int fd = open(".", O_RDONLY | O_DIRECTORY);
if (fd < 0) { perror("open"); return 1; }
char buf[4096];
long nread;
while ((nread = syscall(SYS_getdents64, fd, buf, sizeof(buf))) > 0) {
char *ptr = buf;
while (ptr < buf + nread) {
struct linux_dirent64 *dir = (struct linux_dirent64 *)ptr;
printf("文件名: %-20s\t类型: %d\n", dir->d_name, dir->d_type);
ptr += dir->d_reclen;
}
}
if (nread < 0) perror("getdents64");
close(fd);
return 0;
}
可以用如上代码实验,这里的打开目录可以使用openat替代,openat可以传递父目录的fd,从而支持逐级打开目录,但要考虑openat getdents64 等函数在任意内核下是否可用
getdents64这个用 getdirentries64 代替比较好

428

被折叠的 条评论
为什么被折叠?



