Exemple #1
0
        // https://stackoverflow.com/questions/3567063/get-a-list-of-all-unc-shared-folders-on-a-local-network-server
        // v2: https://stackoverflow.com/questions/6227892/reading-share-permissions-in-c-sharp
        public static List <Dictionary <string, string> > GetNetworkShares(string pcname)
        {
            List <Dictionary <string, string> > results = new List <Dictionary <string, string> >();

            try
            {
                ManagementClass manClass = new ManagementClass(@"\\" + pcname + @"\root\cimv2:Win32_Share"); //get shares

                foreach (ManagementObject objShare in manClass.GetInstances())
                {
                    int    current_perm = 0;
                    string perm_str     = "";

                    try
                    {
                        //get the access values you have
                        ManagementBaseObject result = objShare.InvokeMethod("GetAccessMask", null, null);

                        //value meanings: http://msdn.microsoft.com/en-us/library/aa390438(v=vs.85).aspx
                        current_perm = Convert.ToInt32(result.Properties["ReturnValue"].Value);
                        perm_str     = MyUtils.PermInt2Str(current_perm);
                    }
                    catch (ManagementException me)
                    {
                        perm_str = ""; //no permissions are set on the share
                    }

                    Dictionary <string, string> share = new Dictionary <string, string> {
                    };
                    share["Name"]        = String.Format("{0}", objShare.Properties["Name"].Value);
                    share["Path"]        = String.Format("{0}", objShare.Properties["Path"].Value);
                    share["Permissions"] = perm_str;
                    results.Add(share);
                }
            }
            catch (Exception ex)
            {
                Beaprint.GrayPrint(String.Format("  [X] Exception: {0}", ex.Message));
            }
            return(results);
        }
Exemple #2
0
        public static Dictionary <string, string> GetModifiableServices(Dictionary <string, string> SIDs)
        {
            Dictionary <string, string> results = new Dictionary <string, string>();

            ServiceController[] scServices;
            scServices = ServiceController.GetServices();

            var GetServiceHandle = typeof(System.ServiceProcess.ServiceController).GetMethod("GetServiceHandle", BindingFlags.Instance | BindingFlags.NonPublic);

            object[] readRights = { 0x00020000 };

            foreach (ServiceController sc in scServices)
            {
                try
                {
                    IntPtr handle = (IntPtr)GetServiceHandle.Invoke(sc, readRights);
                    ServiceControllerStatus status = sc.Status;
                    byte[] psd = new byte[0];
                    uint   bufSizeNeeded;
                    bool   ok = QueryServiceObjectSecurity(handle, SecurityInfos.DiscretionaryAcl, psd, 0, out bufSizeNeeded);
                    if (!ok)
                    {
                        int err = Marshal.GetLastWin32Error();
                        if (err == 122 || err == 0)
                        { // ERROR_INSUFFICIENT_BUFFER
                          // expected; now we know bufsize
                            psd = new byte[bufSizeNeeded];
                            ok  = QueryServiceObjectSecurity(handle, SecurityInfos.DiscretionaryAcl, psd, bufSizeNeeded, out bufSizeNeeded);
                        }
                        else
                        {
                            //throw new ApplicationException("error calling QueryServiceObjectSecurity() to get DACL for " + _name + ": error code=" + err);
                            continue;
                        }
                    }
                    if (!ok)
                    {
                        //throw new ApplicationException("error calling QueryServiceObjectSecurity(2) to get DACL for " + _name + ": error code=" + Marshal.GetLastWin32Error());
                        continue;
                    }

                    // get security descriptor via raw into DACL form so ACE ordering checks are done for us.
                    RawSecurityDescriptor rsd = new RawSecurityDescriptor(psd, 0);
                    RawAcl           racl     = rsd.DiscretionaryAcl;
                    DiscretionaryAcl dacl     = new DiscretionaryAcl(false, false, racl);

                    string permissions = "";

                    foreach (System.Security.AccessControl.CommonAce ace in dacl)
                    {
                        if (SIDs.ContainsKey(ace.SecurityIdentifier.ToString()))
                        {
                            int serviceRights = ace.AccessMask;

                            string current_perm_str = MyUtils.PermInt2Str(serviceRights, true);
                            if (!String.IsNullOrEmpty(current_perm_str))
                            {
                                permissions += current_perm_str;
                            }
                        }
                    }

                    if (!String.IsNullOrEmpty(permissions))
                    {
                        results.Add(sc.ServiceName, permissions);
                    }
                }
                catch (Exception ex)
                {
                    //Beaprint.GrayPrint(String.Format("  [X] Exception: {0}", ex.Message));
                }
            }
            return(results);
        }