I am writing some C++ code for the Windows(XP/7) platform to check the permission associated to a file. I want to verify that the file I am reading cannot be written by accounts with non elevated privileges. This is what I am currently doing:
I get the DACL associated with the file calling GetNamedSecurityInfo
I call CreateWellKnownSid with well known Sids, such as WinAuthenticatedUserSid (the Users group)
I call BuildTrusteeWithSid to build a TRUSTEE with the previous SID
I call GetEffectiveRightsFromAcl with the previously created trustee to get the effective acl
I check that the ACL does not contain the Write Flag set.
This code is working perfectly for the Users group. What about the other groups such as Everyone, Guests, or other specific users which may have specific write access on the file? I would like to find a solution where I don't need to enumerate all possible SID and check against all of them. Is there a SID I could use such as "anything but not admin"?
Regards,
Ant
I'd take a slightly different approach:
Get all ACE's via GetExplicitEntriesFromAcl, for the file and all its parents
Select the GRANT_ACCESS and SET_ACCESS ACE's
Get the list of trustees from the selected ACE's
For each of the trustees, perform an access check. GetEffectiveRightsFromAcl is probably the easiest solution here.
You'll want to think specifically what you want to do with the OWNER. He can alter the rights at any time.
Related
Is anyone aware of an easy (easier) way to obtain or create some 'mainstream' SECURITY_DESCRIPTOR ?
In my case i just need something like an "Everyone can do anything to this" for a CreateMutex call (if I pass NULL(meaning default) a non-elevated user cannot obtain the mutex created by an elevated one). This is sort of the same thing you see when you look at the security tab of a file - and it can be quite verbose.
MSDN has an example of how to create one from scratch:
https://learn.microsoft.com/da-dk/windows/desktop/SecAuthZ/creating-a-security-descriptor-for-a-new-object-in-c--
As you can see, it is very verbose (4-5 steps needed). I guess Im hoping for a higher level function that makes this trivial or something like query the system for the "Everyone" group and copy from that, etc.
I have a collection of SID values and need to display if they are in a given group. I know I can do that with CheckTokenMembership, if I had a token handle. How do I get a token handle from an SID in Windows?
Use LookupAccountSid to get the account for the SID
Use NetUserGetGroups to get all groups this account is a member of. EDIT Note from eryksun: Check target group's SID first. If it's SidTypeGroup, call NetUserGetGroups. If it's SidTypeAlias, call NetUserGetLocalGroups (AKA aliases, such as "BUILTIN\Administrators")
Use LookupAccountName to get the SIDs of the group.
Check if account SID (step 1) is member of the SID collection (step 3). Use EqualSid function.
You find appropriate old code one the old win32.mvps.org pages. Sadly the page is gone. But Wayback machine helps. Here a link to the old fksec samples
You find an easy to use sid class there. It also provides sid::MemberOf and sid::Members functions. The code isn't my style but it works.
There is setgroups(size_t size, const gid_t *list) method in C which would allow me to set the supplementary group ids of the calling process.
Question 1:
But in order run this we need to have proper rights so the only way I got to run this program without getting Operation not permitted error is to run it using root. But does that mean using this method we could only set the groups of root??
Question 2:
On the other hand what I intend to do is given a particular user and a list of gid's I want to set the given gid's as the supplementary groups to given user. Is there a method defined for this in C/C++?
Thanks in advance
Technically under Linux you need the CAP_SETGID capability, which need not be root (but normally is).
Note that this works with the effective gid, not the actual gid. Note also the actual / effective gid of the calling process is not necessarily the same as the actual / effective uid. You should have a look at the manpages for setuid, seteuid, setgid and setegid for more details, but one scenario would be for a SUID program to setegid back to an unprivileged group then use setgroups.
I have a BHO which on the first run is gathering activation information and storing this in the registry.
(I think) due to IE's permission's I am only able to store this in the registry branch
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\InternetRegistry\REGISTRY\USER\S-0-0-00-000000000-000000000-000000000-0000\Software\MyBHO\MyKey
Where S-0-0-00-000000000-000000000-000000000-0000 is a unique key for each user.
Which is fine using RegCreateKey() with "Software\MyBHO\MyKey". It's all created and running lovely. It determines where in space to store the Key with no problems.
The Problem:
When I carry out an uninstall I want to remove this key and as this is run outside of IE I have no way to determine where that key is / what the user string is.
Options I have in mind:
Option 1 (Ideal)
Find out this user string first to then build a new path for the key I wish to remove and remove it. How?
Option 2
At the point of activation store the path to the key in another registry value that can be accessed. Then read, and delete both (Which seems a bit backwards and probably wont work due to the access restrictions of the BHO on the registry (Thus it being written there in the first place))
Do you know if there is any way to find this User key or even how to find the parent dir.
Edit Upon continued research I've found that the thing I'm referring to as "user key" is the current Users "SID". Maybe this will yield me better results.
Call GetUserName to get the user name, and LookupAccountName to get his SID.
ConvertSidToStringSid is a useful utility function to format a SID as a S-1-5-32-00000000-00000000-00000000-00000000-0000 string
If you really want to write per-user data to the registry, use IEGetWriteableHKCU().
In general there is no good way to remove per-user data at uninstall. For example, what if you install as user A and the uninstall as user B? Are you going to go find all of them and delete them? Just leave the turds behind.
Alternatively you could consider using a different data store. Do you really need the registry? Can you store this data in a file? What about Web Storage?
Does anyone have a smal example of how to programmatically, in c/c++, load a users registry hive? I would loike to load a hive set some values and close the hive.
Thanks in advance for any help.
Tony
You can use RegLoadKey() and RegUnLoadKey(). You can build the paths to the user hives (NTUSER.DAT) via the HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList. However, it's generally not a good idea to use these functions willy-nilly. If the user tries to logon while you have his profile loaded, he will be unable to load his profile and will get a temporary default profile.
Documentation says you should pass predefined key HKEY_CURRENT_USER as first argument of RegOpenKeyEx function.
You can also enumerate HKEY_CURRENT_USER passing it directly to RegQueryInfoKey.
I haven't got a specific example, but the Windows API calls you need would be:
RegOpenKeyEx() to load the registry
key
RegSetValueEx() / RegGetValue() [and sister
functions] to get/set registry values
RegCloseKey() to close the
registry.
There's some example code behind this link on codersource.net ... although I can't vouch for how complete or correct it is. Review against the MSDN :-)