Beispiel #1
0
        public List <String> GetDomainList()
        {
            int           entriesread = 0, totalentries = 0;
            List <String> NetworkList = new List <String>();

            try
            {
                do
                {
                    // Buffer to store the available servers
                    // Filled by the NetServerEnum function
                    IntPtr buf = new IntPtr();

                    NetServerAPIWrapper.SERVER_INFO_101 server;
                    int returnResult = NetServerAPIWrapper.NetServerEnum(null, 101, out buf, -1,
                                                                         ref entriesread, ref totalentries,
                                                                         NetServerAPIWrapper.SV_101_TYPES.SV_TYPE_DOMAIN_ENUM, null, IntPtr.Zero);

                    // if the function returned any data, fill the tree view
                    if (returnResult == NetServerAPIWrapper.ERROR_SUCCESS ||
                        returnResult == NetServerAPIWrapper.ERROR_MORE_DATA ||
                        entriesread > 0)
                    {
                        IntPtr ptr = buf;

                        for (int domainIndex = 0; domainIndex < entriesread; domainIndex++)
                        {
                            // cast pointer to a SERVER_INFO_101 structure
                            server = (NetServerAPIWrapper.SERVER_INFO_101)Marshal.PtrToStructure(ptr, typeof(NetServerAPIWrapper.SERVER_INFO_101));

                            //Cast the pointer to a ulong so this addition will work on 32-bit or 64-bit systems.
                            ptr = (IntPtr)((ulong)ptr + (ulong)Marshal.SizeOf(server));



                            NetworkList.Add(server.sv101_name);
                        }
                    }

                    // free the buffer
                    NetAPICommon.NetApiBufferFree(buf);
                }while
                (
                    entriesread < totalentries &&
                    entriesread != 0
                );
            }

            catch (Exception ex)
            {
                AMTLogger.WriteToLog(ex.Message, MethodBase.GetCurrentMethod().Name, 0, AMTLogger.LogInfo.Error);
            }

            return(NetworkList);
        }
Beispiel #2
0
        public List <BaseShareInfo> EnumNetShares(string ServerName)
        {
            List <BaseShareInfo> ShareInfos = new List <BaseShareInfo>();
            int           entriesread       = 0;
            int           totalentries      = 0;
            int           resume_handle     = 0;
            int           nStructSize       = Marshal.SizeOf(typeof(NetShareWrapper.SHARE_INFO_503));
            IntPtr        bufPtr            = IntPtr.Zero;
            StringBuilder server            = new StringBuilder(ServerName);

            try
            {
                int ret = NetShareWrapper.NetShareEnum(server, 503, ref bufPtr, ErrorConstants.MAX_PREFERRED_LENGTH, ref entriesread, ref totalentries, ref resume_handle);

                if (ret == ErrorConstants.ERROR_ACCESS_DENIED)
                {
                    NetAPIConnection.EstablishConnectionWithPrompt(ServerName, String.Empty, String.Empty, ref ret);

                    if (ret == ErrorConstants.NERR_Success)
                    {
                        ret = NetShareWrapper.NetShareEnum(server, 503, ref bufPtr, ErrorConstants.MAX_PREFERRED_LENGTH, ref entriesread, ref totalentries, ref resume_handle);
                    }
                }

                if (ret == ErrorConstants.NERR_Success)
                {
                    IntPtr currentPtr = bufPtr;
                    for (int i = 0; i < entriesread; i++)
                    {
                        NetShareWrapper.SHARE_INFO_503 shi1 = (NetShareWrapper.SHARE_INFO_503)Marshal.PtrToStructure(currentPtr, typeof(NetShareWrapper.SHARE_INFO_503));

                        ShareInfos.Add(new BaseShareInfo()
                        {
                            ServerName = ServerName,
                            ShareName  = shi1.shi503_netname,
                            SharePath  = @"\\" + ServerName + @"\" + shi1.shi503_netname,
                            DomainName = NetAPIConnection.GetDomainName(ServerName)
                        });

                        currentPtr = (IntPtr)((ulong)currentPtr + (ulong)Marshal.SizeOf(shi1));
                    }
                    NetAPICommon.NetApiBufferFree(bufPtr);
                }
            }
            catch (Exception ex)
            {
                AMTLogger.WriteToLog(ex.Message, MethodBase.GetCurrentMethod().Name, 0, AMTLogger.LogInfo.Error);
            }
            return(ShareInfos);
        }
Beispiel #3
0
        public static String GetDomainName(String ServerName)
        {
            IntPtr domainPtr = new IntPtr();
            int    result    = 0;

            String DomaiName = String.Empty;

            try
            {
                result = NetWkstaGetInfo(ServerName, 100, out domainPtr);

                if (result == ErrorConstants.ERROR_ACCESS_DENIED)
                {
                    EstablishConnectionWithPrompt(ServerName, String.Empty, String.Empty, ref result);

                    if (result == ErrorConstants.NERR_Success)
                    {
                        result = NetWkstaGetInfo(ServerName, 100, out domainPtr);
                    }
                }

                if (result == ErrorConstants.NERR_Success)
                {
                    WKSTA_INFO_100 wksta_info = (WKSTA_INFO_100)Marshal.PtrToStructure(domainPtr, typeof(WKSTA_INFO_100));

                    if (!string.IsNullOrEmpty(wksta_info.lan_group))
                    {
                        DomaiName = wksta_info.lan_group;
                    }
                }



                NetAPICommon.NetApiBufferFree(domainPtr);
            }
            catch (Exception ex)
            {
                AMTLogger.WriteToLog(ex.Message, MethodBase.GetCurrentMethod().Name, 0, AMTLogger.LogInfo.Error);
            }
            return(DomaiName);
        }
Beispiel #4
0
        public List <DomainInfo> GetNetworkList()
        {
            int entriesread = 0, totalentries = 0;
            List <DomainInfo> NetworkList = new List <DomainInfo>();

            try
            {
                do
                {
                    // Buffer to store the available servers
                    // Filled by the NetServerEnum function
                    IntPtr buf = new IntPtr();

                    NetServerAPIWrapper.SERVER_INFO_101 server;
                    int returnResult = NetServerAPIWrapper.NetServerEnum(null, 101, out buf, -1,
                                                                         ref entriesread, ref totalentries,
                                                                         NetServerAPIWrapper.SV_101_TYPES.SV_TYPE_DOMAIN_ENUM, null, IntPtr.Zero);

                    // if the function returned any data, fill the tree view
                    if (returnResult == NetServerAPIWrapper.ERROR_SUCCESS ||
                        returnResult == NetServerAPIWrapper.ERROR_MORE_DATA ||
                        entriesread > 0)
                    {
                        IntPtr ptr = buf;

                        for (int domainIndex = 0; domainIndex < entriesread; domainIndex++)
                        {
                            // cast pointer to a SERVER_INFO_101 structure
                            server = (NetServerAPIWrapper.SERVER_INFO_101)Marshal.PtrToStructure(ptr, typeof(NetServerAPIWrapper.SERVER_INFO_101));

                            //Cast the pointer to a ulong so this addition will work on 32-bit or 64-bit systems.
                            ptr = (IntPtr)((ulong)ptr + (ulong)Marshal.SizeOf(server));



                            NetworkList.Add(new DomainInfo()
                            {
                                DomainName = server.sv101_name, Severs = GetServerList(server.sv101_name)
                            });
                        }
                    }

                    // free the buffer
                    NetAPICommon.NetApiBufferFree(buf);
                }while
                (
                    entriesread < totalentries &&
                    entriesread != 0
                );


                if (NetworkList.Count == 0)
                {
                    foreach (var testItem in new List <String>()
                    {
                        "D1", "D2", "D"
                    })
                    {
                        foreach (var serve in new List <String>()
                        {
                            "Dummy1", "Dummy2", "Dummy3", "Dummy4"
                        })
                        {
                            NetworkList.Add(new DomainInfo()
                            {
                                DomainName = testItem, Severs = new List <ServerInfo>()
                                {
                                    new ServerInfo {
                                        ServerName = serve, ServerType = 26, MajorVersion = 6, MinorVersion = 5, PlatformId = 6
                                    }
                                }
                            });
                        }
                    }

                    //NetworkList.Add(new DomainInfo() { DomainName = "localHost", Severs = new List<ServerInfo>() { new ServerInfo { ServerName = Environment.MachineName, ServerType = 26, MajorVersion = 6, MinorVersion = 5, PlatformId = 6 } } });
                }
            }

            catch (Exception ex)
            {
                AMTLogger.WriteToLog(ex.Message, MethodBase.GetCurrentMethod().Name, 0, AMTLogger.LogInfo.Error);
            }

            return(NetworkList);
        }
Beispiel #5
0
        public List <ServerInfo> GetServerList(String DomainName)
        {
            List <ServerInfo> serverItems = new List <ServerInfo>();
            int entriesread = 0, totalentries = 0;

            try
            {
                do
                {
                    // Buffer to store the available servers
                    // Filled by the NetServerEnum function
                    IntPtr buf = new IntPtr();
                    //NetServerAPIWrapper.SV_101_TYPES.SV_TYPE_WORKSTATION | NetServerAPIWrapper.SV_101_TYPES.SV_TYPE_SERVER
                    NetServerAPIWrapper.SERVER_INFO_101 server;
                    int returnResult = NetServerAPIWrapper.NetServerEnum(null, 101, out buf, -1,
                                                                         ref entriesread, ref totalentries,

                                                                         NetServerAPIWrapper.SV_101_TYPES.SV_TYPE_WORKSTATION |
                                                                         NetServerAPIWrapper.SV_101_TYPES.SV_TYPE_DOMAIN_CTRL | NetServerAPIWrapper.SV_101_TYPES.SV_TYPE_DOMAIN_BAKCTRL


                                                                         , DomainName, IntPtr.Zero);

                    // if the function returned any data, fill the tree view
                    if (returnResult == NetServerAPIWrapper.ERROR_SUCCESS ||
                        returnResult == NetServerAPIWrapper.ERROR_MORE_DATA ||
                        entriesread > 0)
                    {
                        IntPtr ptr = buf;

                        for (int domainIndex = 0; domainIndex < entriesread; domainIndex++)
                        {
                            // cast pointer to a SERVER_INFO_101 structure
                            server = (NetServerAPIWrapper.SERVER_INFO_101)Marshal.PtrToStructure(ptr, typeof(NetServerAPIWrapper.SERVER_INFO_101));

                            //Cast the pointer to a ulong so this addition will work on 32-bit or 64-bit systems.
                            ptr = (IntPtr)((ulong)ptr + (ulong)Marshal.SizeOf(server));



                            serverItems.Add(new ServerInfo()
                            {
                                ServerName   = server.sv101_name,
                                PlatformId   = server.sv101_platform_id,
                                ServerType   = server.sv101_type,
                                MinorVersion = server.sv101_version_minor,
                                MajorVersion = server.sv101_version_major,
                                Comment      = server.sv101_comment
                            }


                                            );
                        }
                    }

                    // free the buffer
                    NetAPICommon.NetApiBufferFree(buf);
                }while
                (
                    entriesread < totalentries &&
                    entriesread != 0
                );



                if (serverItems.Count == 0)
                {
                    foreach (var testItem in new List <String>()
                    {
                        "Dummy1", "Dummy2", "Dummy3", "Dummy4"
                    })
                    {
                        serverItems.Add(new ServerInfo()
                        {
                            ServerName = DomainName + testItem
                        });
                    }
                }
            }



            catch (Exception ex)
            {
                AMTLogger.WriteToLog(ex.Message, MethodBase.GetCurrentMethod().Name, 0, AMTLogger.LogInfo.Error);
            }

            return(serverItems);
        }
Beispiel #6
0
        public List <ShareInfo> EnumNetSharesWithPermission(string ServerName)
        {
            List <ShareInfo> ShareInfos = new List <ShareInfo>();
            int           entriesread   = 0;
            int           totalentries  = 0;
            int           resume_handle = 0;
            int           nStructSize   = Marshal.SizeOf(typeof(NetShareWrapper.SHARE_INFO_503));
            IntPtr        bufPtr        = IntPtr.Zero;
            StringBuilder server        = new StringBuilder(ServerName);

            try
            {
                int ret = NetShareWrapper.NetShareEnum(server, 503, ref bufPtr, ErrorConstants.MAX_PREFERRED_LENGTH, ref entriesread, ref totalentries, ref resume_handle);

                if (ret == ErrorConstants.ERROR_ACCESS_DENIED)
                {
                    NetAPIConnection.EstablishConnectionWithPrompt(ServerName, String.Empty, String.Empty, ref ret);

                    if (ret == ErrorConstants.NERR_Success)
                    {
                        ret = NetShareWrapper.NetShareEnum(server, 503, ref bufPtr, ErrorConstants.MAX_PREFERRED_LENGTH, ref entriesread, ref totalentries, ref resume_handle);
                    }
                }

                if (ret == ErrorConstants.NERR_Success)
                {
                    IntPtr currentPtr = bufPtr;
                    for (int i = 0; i < entriesread; i++)
                    {
                        List <SharePermission> permissionItem = null;

                        NetShareWrapper.SHARE_INFO_503 shi1 = (NetShareWrapper.SHARE_INFO_503)Marshal.PtrToStructure(currentPtr, typeof(NetShareWrapper.SHARE_INFO_503));



                        if (shi1.shi503_security_descriptor != null && shi1.shi503_security_descriptor != IntPtr.Zero)
                        {
                            permissionItem = GetSharePermission(currentPtr);
                        }



                        ShareInfos.Add(new ShareInfo()
                        {
                            CurrentUses        = shi1.shi503_current_uses,
                            Maxuses            = shi1.shi503_max_uses,
                            Name               = shi1.shi503_netname,
                            Path               = shi1.shi503_path,
                            Permissions        = shi1.shi503_permissions,
                            Remark             = shi1.shi503_remark,
                            Reserved           = shi1.shi503_reserved,
                            SecurityDescriptor = shi1.shi503_security_descriptor,
                            ServerName         = ServerName,
                            Type               = (int)shi1.shi503_type,

                            SharePermissions = permissionItem != null ? permissionItem : new List <SharePermission>()
                        });

                        currentPtr = (IntPtr)((ulong)currentPtr + (ulong)Marshal.SizeOf(shi1));
                    }
                    NetAPICommon.NetApiBufferFree(bufPtr);
                }
                //else
                //{
                //    // ShareInfos.Add(new SHARE_INFO_1  NetShareWrapper.SHARE_INFO_503("ERROR=" + ret.ToString(), 10, string.Empty));

                //}
            }
            catch (Exception ex)
            {
                AMTLogger.WriteToLog(ex.Message, MethodBase.GetCurrentMethod().Name, 0, AMTLogger.LogInfo.Error);
            }
            return(ShareInfos);
        }
Beispiel #7
0
        private List <SharePermission> GetSharePermission(IntPtr securityDescrPointer)
        {
            List <SharePermission> sharePermissionList = new List <SharePermission>();
            int Error = 0;

            try
            {
                NetShareWrapper.SHARE_INFO_503 shareInfo = (NetShareWrapper.SHARE_INFO_503)Marshal.PtrToStructure(securityDescrPointer, typeof(NetShareWrapper.SHARE_INFO_503));

                bool   bDaclPresent;
                bool   bDaclDefaulted;
                IntPtr pAcl = new IntPtr();// IntPtr.Zero;
                NetAPICommon.GetSecurityDescriptorDacl(shareInfo.shi503_security_descriptor, out bDaclPresent, ref pAcl, out bDaclDefaulted);
                if (bDaclPresent)
                {
                    NetAPICommon.ACL_SIZE_INFORMATION AclSize = new NetAPICommon.ACL_SIZE_INFORMATION();
                    NetAPICommon.GetAclInformation(pAcl, ref AclSize, (uint)Marshal.SizeOf(typeof(NetAPICommon.ACL_SIZE_INFORMATION)), NetAPICommon.ACL_INFORMATION_CLASS.AclSizeInformation);
                    for (int i = 0; i < AclSize.AceCount; i++)
                    {
                        SharePermission sharePermission = new SharePermission();
                        IntPtr          pAce;
                        Error = NetAPICommon.GetAce(pAcl, i, out pAce);

                        NetAPICommon.ACCESS_ALLOWED_ACE ace = (NetAPICommon.ACCESS_ALLOWED_ACE)Marshal.PtrToStructure(pAce, typeof(NetAPICommon.ACCESS_ALLOWED_ACE));

                        IntPtr iter = (IntPtr)((long)pAce + (long)Marshal.OffsetOf(typeof(NetAPICommon.ACCESS_ALLOWED_ACE), "SidStart"));


                        byte[] bSID = null;
                        int    size = (int)NetAPICommon.GetLengthSid(iter);
                        bSID = new byte[size];
                        Marshal.Copy(iter, bSID, 0, size);
                        IntPtr ptrSid;
                        NetAPICommon.ConvertSidToStringSid(bSID, out ptrSid);
                        string strSID = Marshal.PtrToStringAuto(ptrSid);

                        //Console.WriteLine("The details of ACE number {0} are ", i + 1);

                        StringBuilder             UserName                = new StringBuilder();
                        uint                      cchName                 = (uint)UserName.Capacity;
                        StringBuilder             referencedDomainName    = new StringBuilder();
                        uint                      cchReferencedDomainName = (uint)referencedDomainName.Capacity;
                        NetAPICommon.SID_NAME_USE sidUse;

                        NetAPICommon.LookupAccountSid(null, bSID, UserName, ref cchName, referencedDomainName, ref cchReferencedDomainName, out sidUse);

                        //Console.WriteLine("Trustee Name " + UserName);
                        //Console.WriteLine("Domain Name " + referencedDomainName);


                        sharePermission.UserName   = UserName.ToString();
                        sharePermission.DomainName = referencedDomainName.ToString();


                        if ((ace.Mask & 0x1F01FF) == 0x1F01FF)
                        {
                            sharePermission.Permission = "Full Control";
                            //Console.WriteLine("Permission Full Control");
                        }
                        else if ((ace.Mask & 0x01BF) == 0x01BF)
                        {
                            sharePermission.Permission = "Read and Modify";
                            // Console.WriteLine("Permission READ and CHANGE");
                        }
                        else if ((ace.Mask & 0x1200A9) == 0x1200A9)
                        {
                            sharePermission.Permission = "Read";
                            //   Console.WriteLine("Permission READ only");
                        }

                        sharePermission.AcessType = ace.Header.AceType == 0 ? "Allow" : "Deny";
                        sharePermission.SID       = strSID;

                        //   Console.WriteLine("SID {0} \nHeader AceType {1} \nAccess Mask {2} \nHeader AceFlag {3}", strSID, ace.Header.AceType.ToString(), ace.Mask.ToString(), ace.Header.AceFlags.ToString());
                        // Console.WriteLine("\n");

                        sharePermissionList.Add(sharePermission);
                    }


                    NetAPICommon.NetApiBufferFree(pAcl);
                }
            }



            catch (Exception ex)
            {
                AMTLogger.WriteToLog(ex.Message, MethodBase.GetCurrentMethod().Name, 0, AMTLogger.LogInfo.Error);
            }
            return(sharePermissionList);
        }