windows 如何防止文件在windows文件系统中被复制或剪切?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2290149/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:57:56  来源:igfitidea点击:

How to prevent a file being copied or cut in windows file system?

windowsfilesystemscopy-paste

提问by

I want that an exe file can't be copied or cut from the Windows file system to paste somewhere.

我希望不能从 Windows 文件系统中复制或剪切 exe 文件以粘贴到某处。

The exe is made in C#. which must have to be in only one PC.

exe 是用 C# 编写的。必须仅在一台 PC 中。

I have worked with FileSystemWatcher, NSIS, Clipboard. but for all I need to detect whether that file is being copied.

我曾与FileSystemWatcherNSISClipboard一起工作。但无论如何,我需要检测该文件是否正在被复制。

I also have seen 'Prevent'(http www free-download-blog.com disable-cut-paste-copy-delete-rename-functions-using-prevent ), but I need to prevent only that particular exefrom being copied or cut.

我也看过“防止”(http www free-download-blog.com disable-cut-paste-copy-delete-rename-functions-using-prevent ),但我只需要防止复制或剪切那个特定的exe.

Any pointer or idea will help.

任何指针或想法都会有所帮助。

回答by GSerg

If you need the exe to be executable, you need to permit loading it into memory. As soon as you do, anyone can read it to memory using ReadFile and then write to an arbitrary location using WriteFile. No shell-detectable copying involved.

如果您需要 exe 可执行,则需要允许将其加载到内存中。只要您这样做,任何人都可以使用 ReadFile 将其读取到内存中,然后使用 WriteFile 将其写入任意位置。不涉及外壳可检测的复制。

A good reading: Raymond's post and its comments on preventing copying.

很好的阅读:雷蒙德的帖子及其关于防止复制的评论

回答by Scott

As others have suggested you won't be able to disable the copy/cut behaviour so easily.
An alternative would be to disable the execution of the copied versions.
In your executable you could check many things like :

正如其他人所建议的那样,您将无法如此轻松地禁用复制/剪切行为。
另一种方法是禁用复制版本的执行
在您的可执行文件中,您可以检查许多内容,例如:

  • The path of the present executable is explicitly your_path
  • The name of the machine and user is the one you authorise
  • 当前可执行文件的路径明确是 your_path
  • 机器名和用户名是你授权的

You could even prevent the file of being executed more than once using Windows register entries (if already 1 don't launch). It won't be perfect since any experimented user could tweak that out, assuming they are seeking for that. But depending on your users profile it might be sufficient.

您甚至可以使用 Windows 注册条目(如果已经 1 不启动)防止文件被多次执行。它不会是完美的,因为任何经过实验的用户都可以对其进行调整,假设他们正在寻求这一点。但根据您的用户个人资料,这可能就足够了。

回答by Scott

Well, this is a hard problem. Even if you get explorer.exe to disable cut&paste, what prevents a user from using the command window? Or writing their own exe to do it? Booting up in linux and reading it?

嗯,这是一个难题。即使您让 explorer.exe 禁用剪切和粘贴,是什么阻止用户使用命令窗口?还是自己写exe来做?在 linux 中启动并阅读它?

Still, you have a few options (there will be more, most likely) which you could try:

不过,您还有一些选择(很可能会有更多)您可以尝试:

  • Use the right permissions: Set the permissions such that the users who you don't want to cut&paste cannot read the file.

  • Write a device driver which can hook onto the filesystem calls and do that for you.

  • Encrypt the file.

  • 使用正确的权限:设置权限,使您不想剪切和粘贴的用户无法读取文件。

  • 编写一个设备驱动程序,它可以挂接到文件系统调用上并为您完成。

  • 加密文件。

And some hacky options like:

还有一些hacky选项,例如:

  • Use the APPINIT_DLLS regkey to put your own dll to be loaded into each process ( I am not sure if this will work with console process though). Then on your dll load, do IAT hooking to replace the kernel32.dll file calls.

  • Replace kernel32.dll with your own version. Might have to do some messing around with the PE format etc.

  • 使用 APPINIT_DLLS regkey 将您自己的 dll 加载到每个进程中(我不确定这是否适用于控制台进程)。然后在加载 dll 时,执行 IAT 挂钩以替换 kernel32.dll 文件调用。

  • 将 kernel32.dll 替换为您自己的版本。可能需要对 PE 格式等进行一些处理。

There are no guarantees though. If for instance, you expect them to be able to execute it, but not copy it, you are probably stuck.

但是没有任何保证。例如,如果您希望他们能够执行它,但不能复制它,那么您可能被卡住了。

回答by MartW

Any local admin will be able to undo anything you do to prevent copying. I can pretty much guarantee the program on that page you mention relies on a service or background process to prevent copy-and-paste, and therefore is easily circumventable. If your users are in a closed environment where none of them are admins and they have very limited rights to their PCs, then you have a chance.

任何本地管理员都可以撤消您为防止复制所做的任何操作。我几乎可以保证您提到的那个页面上的程序依赖于服务或后台进程来防止复制和粘贴,因此很容易规避。如果您的用户处于封闭环境中,其中没有人是管理员,并且他们对自己的 PC 的权限非常有限,那么您就有机会了。

回答by Lonelywolf

if you could completly block explorer from copying or moving files, then all u need is a 3rd party software for copying files (but make sure it can filter file extensions) for example Copy Handler

如果您可以完全阻止资源管理器复制或移动文件,那么您只需要一个用于复制文件的 3rd 方软件(但请确保它可以过滤文件扩展名),例如 Copy Handler

回答by Ryan Fernandes

Set up an ENVIRONMENT variable in your machine

在您的机器中设置环境变量

In your code add a check if (ENVIRONMENT Variable=='Same as defined') //Execute code else //Suspend execution

在您的代码中添加一个检查 if (ENVIRONMENT Variable=='Same as defined') //Execute code else //Suspend execution