我正在使用https://www.nuget.org/packages/Microsoft.Azure.Management.Fluent用于通过编程方式(C#.NET-Core Web 应用程序)获取 Azure 中的资源,并尝试通过提供服务主体(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**");
}
}
关于c# - 如何使用 Azure Mgmt SDK Fluent 获取端点统计信息和危险端点列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63259631/