簡體   English   中英

如何使用 Azure Mgmt SDK fluent 獲取端點統計信息和危險端點列表

[英]How to get list of Endpoint Statistics and Dangerous Endpoints by using of Azure Mgmt SDK fluent

I am using https://www.nuget.org/packages/Microsoft.Azure.Management.Fluent for getting resources in Azure with programmatically(C#.NET-Core Web app) and tried to get resources information by providing service principals(CS ) 如下...

 string subscriptionId = "xxx";
            string clientId = "xxx";
            string tenantId = "xxx";
            string clientSecret = "xxx";

            AzureCredentials cred = new AzureCredentialsFactory()
                .FromServicePrincipal(
                clientId,
                clientSecret,
                tenantId,
                AzureEnvironment.AzureGlobalCloud
                );

            var azure = Azure.Configure()
                             .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                             .Authenticate(cred)
                             .WithSubscription(subscriptionId);

任何示例代碼(C#.NET-Core Web 應用程序),以找出端點統計信息(循環通過 NSG 中的開放端口並詳細列出它們)和危險端點(循環通過 NSG 中的開放端口並識別端口,如 3389/22)。

請在上面提供建議。

謝謝

如果您的意思是列出 NSG 中的所有端口 -> 入站安全規則,如下圖所示:

在此處輸入圖像描述

然后你可以使用如下代碼:

        foreach (var nsg in azure.NetworkSecurityGroups.List())
        {
            
            var rules = nsg.SecurityRules;

            foreach (var r in rules)
            {
                Console.WriteLine($"*** the NSG: {r.Value.Name} ***");

                if (r.Value.DestinationPortRange != null)
                {
                    //after you get the port, you can apply your logic here.
                    Console.WriteLine(r.Value.DestinationPortRange);
                }

                if (r.Value.DestinationPortRanges != null)
                {
                    foreach (var port in r.Value.DestinationPortRanges)
                    {
                        //after you get the port, you can apply your logic here.
                        Console.WriteLine(port);
                    }
                }
                Console.WriteLine("**end**");
            }
          }

感謝@ivan Yang,您的回復和幫助...

下面是工作代碼,我根據我的修改了 urs 代碼

 var ntwrrkDetails = new List<EndTcpPorts>();  

   EndTcpPorts objEndTcpPorts; // cls object

  foreach (var nsg in azure.NetworkSecurityGroups.List())
                {
                    objEndTcpPorts = new EndTcpPorts();
                    objEndTcpPorts.ResourceGroup = nsg.ResourceGroupName.ToString();

                    try
                    {
                        var rules = nsg.SecurityRules;
                        foreach (var r in rules)
                        {
                            try
                            {
                                objEndTcpPorts.NSGName = r.Value.Name.ToString();
                            }
                            catch (Exception)
                            {
                                objEndTcpPorts.NSGName = "";
                            }
                            if (r.Value.DestinationPortRanges != null)
                            {
                                try
                                {
                                    //get ports
                                    objEndTcpPorts.TcpPorts = r.Value.DestinationPortRange.ToString(); //((Microsoft.Azure.Management.ResourceManager.Fluent.Core.IndexableWrapper<Microsoft.Azure.Management.Network.Fluent.Models.SecurityRuleInner>)r.Value).Inner.Protocol.Value.ToString();
                                }
                                catch (Exception)
                                {

                                    objEndTcpPorts.TcpPorts = "";
                                }
                            }

                        }
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                    
                    ntwrrkDetails.Add(objEndTcpPorts); // add to list
                }

現在我們可以將 tcp 端口中的(危險端點)檢查為 NSG 中的開放端口,並識別 3389/22 或 *..

非常感謝,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM