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.
Related
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?
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.
(How) can I enumerate programmatically Users in Well Known groups (SidTypeWellKnownGroup) ?
Ex: "Autenticated Users" or "Domain Users"
You can probably do this in WMI, but I'm not that familiar with it; I prefer using the plain old C APIs anyway. You can use the Network Management functions to do this. Given the group name, you can use NetGroupGetUsers to get all the members of a group. To get the group name you can use CreateWellKnownSid (or AllocateAndInitializeSid) and LookupAccountSid.
Using my c++ program how can I find out what group the current user running my program belongs to? So my program need to figure out a couple of things :
The current username of the user
The group the user belongs to
How can do the above 2 using c++ on a RedHat / Linux machine?
With getuid(2) and getgid(2). See credentials(7) for more information.
Use getpwuid(3) and getgrgid(3) for the names.
You can find some of the information via getgid() (real GID) and getegid() (effective GID). For the other auxilliary groups, you need to use getgroups().
In practice, the real and effective GID are normally the same, but it is the effective GID that is used when creating a file. Usually, the group list returned by getgroups() includes the real group - though it is not clear that it actually has to do so.
You use getuid(2) and getgid(2) to get the numeric user and group ids, then use getpwuid(3) and getgrgid(3) to look up those ids in the user/group databases and turn them into text names.
Would someone be able to point me to the C++ API's that I can use to display a list of processes and the user name in Windows?
My current code uses the CreateToolhelp32Snapshot Function which shows all the processes running for all users, but I do not know what API's to use to retreieve the user name so I can filter it by user. I do not need to use CreateToolhelp32Snapshot, and I have seen other methods to retrieve the process list, but none seem to get me the user name that is running the process.
Thanks for any help.
I know that using GetTokenInformation with TokenUser gets you the SID, and a quick serach reveals that LookupAccountSid should get you the corresponding account. Havent't tried that last one myself though.
Well this link appears to have helpful code for you. Apply the code contained there in to the Token handle you get from calling OpenProcessToken on the handle you get from Process32First/Next.