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) as below...
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);
Any sample code(C#.NET-Core Web app) to find out Endpoint Statistics ( loop through open ports in NSG and list them in details) and Dangerous Endpoints (loop through open ports in NSG and identify ports like 3389/22).
Pls, advice on above.
Thanks
If you mean list all the ports in NSG -> Inbound security rules, like screenshot below:
Then you can use the code like below:
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**");
}
}
Thanks @ivan Yang,, for response and help...
Below is working code, i modified urs code as per my
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
}
Now we can check(Dangerous Endpoints) in tcp ports as open ports in NSG and identify ports like 3389/22 or *..
Many Thanks,
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.