windows 如何在启用显示隐藏文件和文件夹的情况下使用 C\C++ 隐藏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2210673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to hide using C\C++ with Show hidden Files and Folder enabled
提问by Victor43
I would like to know how to programmatically hide a file but making the file or folder still hidden with Show hidden files and folders enabled from the Tools->Folder options of Explorer.
我想知道如何以编程方式隐藏文件,但通过从资源管理器的工具-> 文件夹选项启用显示隐藏文件和文件夹,使文件或文件夹仍然隐藏。
回答by John Knoeller
There is NO user mode API to make files hidden from 'show hidden files', and it's a good thing to.
没有用户模式 API 可以使文件从“显示隐藏文件”中隐藏,这是一件好事。
The only way to do this is to get your code to run in the kernel. The rootkitthat Sony "accidently" installed on user machines a couple years ago when they were trying to prevent CDs from being rippable could do it. But there is no legitimateuse for the ability to hide files from system administrators and power users.
做到这一点的唯一方法是让您的代码在内核中运行。索尼在几年前试图防止 CD 被翻录时“意外”安装在用户机器上的rootkit可以做到这一点。但是对于系统管理员和高级用户隐藏文件的能力没有合法用途。
回答by John Knoeller
You want the SetFileAttributes function available by #include'ing Windows.h: http://msdn.microsoft.com/en-us/library/aa365535%28VS.85%29.aspx
您希望通过 #include'ing Windows.h 使用 SetFileAttributes 函数:http: //msdn.microsoft.com/en-us/library/aa365535%28VS.85%29.aspx
In code:
在代码中:
BOOL result = SetFileAttributes(L"c:\path\to\file", FILE_ATTRIBUTE_HIDDEN);
As for keeping a file hidden from the "show hidden files" option, that's much more difficult and I can't think of a legitimate reason to do it - the only programs that do are rootkits for nefarious purposes.
至于在“显示隐藏文件”选项中隐藏文件,这要困难得多,我想不出这样做的正当理由——唯一这样做的程序是出于恶意目的的rootkit。
回答by Brad
I am pretty sure this is not possible as it would be a security vulnerability (programs could place unknown files on your hard drive that you couldn't see or delete).
我很确定这是不可能的,因为这将是一个安全漏洞(程序可能会在您的硬盘驱动器上放置您无法看到或删除的未知文件)。
Even Windows system files are visible.
甚至 Windows 系统文件也是可见的。
回答by villintehaspam
Use a file system filter driver. But since you have to ask - just don't do it. Not trying to be rude here, it's just that that is a task that is very hard to get right.
使用文件系统过滤器驱动程序。但既然你必须问——那就不要这样做。在这里并不是要粗鲁,只是这是一项很难正确完成的任务。
回答by bta
As many have said before, there's no straightforward way to fully 'hide' a file like that.
正如许多人之前所说,没有直接的方法可以完全“隐藏”这样的文件。
If you can accept not truly hiding the file but merely obfuscating it, you can always embed it inside a dummy file. Take the file you want to hide, build a container file to hold it, and name that dummy file with a random name. For example, you could place the real file's filename starting at offset 512 of the dummy file and place the file's contents starting at offset 1024, inserting 64 bytes of random data every 1KB. Pad the end with empty space out to the nearest multiple of 4KB, fill the empty space with random bytes, and generate a random sequence of characters to use for a filename. Now, you can "hide" the file while it's still visible in the filesystem.
如果您可以接受不是真正隐藏文件而只是对其进行混淆,则您始终可以将其嵌入到一个虚拟文件中。获取您要隐藏的文件,构建一个容器文件来保存它,并用随机名称命名该虚拟文件。例如,您可以将真实文件的文件名放置在虚拟文件的偏移量 512 处,并将文件内容放置在偏移量 1024 处,每 1KB 插入 64 字节的随机数据。用空格将末尾填充到最接近的 4KB 倍数,用随机字节填充空格,并生成用于文件名的随机字符序列。现在,您可以在文件系统中仍然可见时“隐藏”文件。
However, that's merely "security by obscurity" and can be defeated by a clever attacker with a hex editor.
然而,这仅仅是“默默无闻的安全性”,可以被具有十六进制编辑器的聪明攻击者击败。
If you're simply trying to make sure a file isn't visible to the casual filesystem browser, you can always compress the file, encrypt it, and randomize the filename. If you need to be able to access/execute the file as-is while it is "hidden", then you're probably (hopefully) out of luck.
如果您只是想确保文件对临时文件系统浏览器不可见,您可以随时压缩文件、加密文件并随机化文件名。如果您需要能够在文件“隐藏”时按原样访问/执行文件,那么您可能(希望)不走运。
回答by Karthikeyan Moorthy
This program is used to show all hidden files and folder in your system drive and it can run well even system has affected by virus and worms, usually, some kinds of the virus change your files and folders in a hidden state, this script will help you to view your files and folders again. The main feature is without installation it can play.
该程序用于显示系统驱动器中的所有隐藏文件和文件夹,即使系统受到病毒和蠕虫的影响,它也能正常运行,通常,某些类型的病毒会在隐藏状态下更改您的文件和文件夹,此脚本将有所帮助您再次查看您的文件和文件夹。主要特点是无需安装即可播放。
Please find the source code from my tech blog - http://www.algonuts.info/how-to-show-hidden-files-and-folders-using-c.html
请从我的技术博客中找到源代码 - http://www.algonuts.info/how-to-show-hidden-files-and-folders-using-c.html
#include<iostream>
#include<conio.h>
#include<dirent.h>
#include<dir.h>
#include<process.h>
#include<string.h>
#include<stdio.h>
#include<io.h>
#include<dos.h>
#include<sys/stat.h>
struct ffblk vfile;
unsigned long int udata;
char ch,present[MAXPATH];
int next_directory(char *);
void scan_directory(char *);
char base[]="X:\";
using namespace std;
int main(int account,char *arg[],char *env[])
{
clrscr();
getcwd(present,MAXPATH);
DIR *dir;
struct dirent *temp;
cout<<"\nWelcome to Unhidden for identify the hidden files";
cout<<"\n\nEnter drive:";
cin>>ch;
base[0]=ch;
if((dir = opendir(base)) == NULL)
{
clrscr();
cout<<"\nError : Derive not found ";
getch();
exit(0);
}
scan_directory(base);
while((temp = readdir(dir)) != NULL)
{
char *directory = (char *) malloc(3+strlen(temp->d_name)+1);
strcpy(directory,base);
strcat(directory,temp->d_name);
next_directory(directory);
free(directory);
}
closedir(dir);
clrscr();
cout<<"\nSystem: Successfully Unhidden it";
sleep(3);
return 0;
}
int next_directory(char *path)
{
int count=0;
DIR *dirtemp;
char *hold,*temp;
struct dirent *ptemp;
hold=path;
if ((dirtemp = opendir(path)) != NULL)
scan_directory(path);
else
return 0;
while((ptemp = readdir(dirtemp)) != NULL)
{
char *directory = (char *) malloc(1+strlen(ptemp->d_name)+1);
directory[0]='\';
strcpy(directory+1,ptemp->d_name);
if(directory[1]!='\.')
{
count=strlen(hold);
temp = (char *) malloc(strlen(hold)+strlen(directory)+1);
strcpy(temp,hold);
strcat(temp,directory);
free(directory);
if(opendir(temp)!=NULL)
next_directory(temp);
temp[count]='##代码##';
hold=temp;
}
else
free(directory);
}
closedir(dirtemp);
return 0;
}
void scan_directory(char *tempo)
{
cout<<"\n"<<tempo;
unsigned count;
if(present[0]==tempo[0])
chdir(tempo);
else
{
setdisk(tempo[0]-65);
chdir(tempo);
}
udata = findfirst("*.*",&vfile,0x02);
while(!udata)
{
_dos_getfileattr(vfile.ff_name,&count);
if (count & _A_HIDDEN)
{
_dos_getfileattr(vfile.ff_name,&count);
count=count & 248;
_dos_setfileattr(vfile.ff_name,count);
}
udata=findnext(&vfile);
}
if(present[0]==tempo[0])
system("cd\");
chdir(present);
}