Bypass Windows permission restrictions on program files folder
I have made a small ap开发者_开发知识库plication in C. It does some file manipulation, basically searches and changes some files names. But if it is used in Win Vista/7 in the program files folder the program says permission denied. Is there anyway to bypass this?
Also, can Java bypass this?
There wouldn't be much point to file permissions if they could be easily bypassed.
You could bypass them if you wrote a kernel driver that accessed disk blocks directly, but then you need to be an Administrator to install kernel drivers anyway.
At the end of the day, the only way to "bypass" file permissions is to run your program as Administrator.
Don't bypass, use "Application Data" directory.
Program Files is intended to store files that do not change during run of application. Files stored there should be changed only during installation/update.
Files to be changed during application run (profiles, settings etc.) should be stored "Application Data" directory in users folder.
To obtain the path use SHGetFolderPath function passing:
CSIDL_APPDATA
- to obtain current user "Application Data" directory
CSIDL_COMMON_APPDATA
- to obtain all users "Application Data" directory
(and eventually) CSIDL_LOCAL_APPDATA
- to obtain current user "Local Application Data" directory which is intended to store informations specific only to local machine that cannot be moved to other machines, in opposition to "Application Data" (also called "Roaming Application Data") where files can be freely moved from machine to machine (i.e. you can preserve them during OS reinstallation).
If your application do really need to alter Program Files directory then asking the user for permission is that what you should do. You can give your application special manifest so it'll try to obtain administrator rights every time it's run. You can also obtain privileges on-the-fly.
If you are writing specific application that hardly interferes with the OS, then you can create a service (daemon). Every time the service is started it will obtain privileges given during service registration. For more information see "DLLs, Processes, and Threads -> Services" on MSDN
// EDIT
You can also easily crate a service in .NET (C# is most suitable). Service application can communicate with client application written in other language. But to register a service you will need administrator rights. With this kind of application you must consider all security issues.
Is there anyway to bypass this?
You should not do that. Even if there is a way, then it is a security hole, and will be fixed eventually.
If your software is running from non-admin account, it should behave properly, and shouldn't try to mess system folders (renaming file in program files is exactly that - potentially damaging the system). That's the whole point of permissions and separation between user and admin - program running from user account should not be able to corrupt or destroy system files, unless administrator permits that.
So if you can't rename files due to permissions, report "permission denied" to user. Let the user handle situation. Silently bypassing permissions (even if it is possible) is extremely dangerous - eventually some not-very-bright user will kill the system using your software AND blame YOU for his problems.
As many said before, you shouldn't try to bypass restrictions imposed by the Operating System.
If your application needs to change files that only the Administrator can change, your program requires Administrator privileges. Now, if you want to consider this, there are some useful APIs to elevate an application's privileges.
In Vista/Win7/Server2k8, for instance, you can use the UAC. I'm not citing the specific APIs here because there are different ways to elevate or gain privileges through the UAC. An important thing to note is that UAC relies on the user interaction for granting this, and this is a positive thing :-)
Hopefully, this will guide you, and others as well, to the right direction.
If you are asking for yourself because you just want to run this app there are many things you can do. First, you can turn off UAC which as we all agree is a bad idea. But no-one has listed your other options:
- sign on to your machine as Administrator. Not as a user in the Administrators group, but as Administrator. Everything you launch will be elevated.
- open an elevated command prompt and consent once to doing that. Now everything you launch from there will be elevated without a dialog.
- use Scheduled Tasks to run the app under certain specific circumstances (every Tuesday at 10, or every time you log on) and check the box to have it run elevated. You will need to consent to UAC once to set this up, then never again.
Just completely turn off UAC. I have mine turned off, and I have no security issues whatsoever since I am quite careful and don't execute a variety of programs or even download anything, executable.
Off-topic'ish answer but I need somewhere to save this so that I can find it again later.
If you need to work with a specific folder somewhere like 'Program Files' but don't want the UAC prompt every time, one workaround is to take ownership of the folder:
- Copy/paste the following into a new text file:
takeown /F %1
icacls %1 /grant %username%:F /t /q
Save it as 'letmewrite.bat' or whatever.
Get a couple of Explorer windows open so that you can see both the folder you want access to and 'letmewrite.bat'.
Drag the folder you want access to onto 'letmewrite.bat'
精彩评论