Checking Open Ports In Azure

 

Checking Open Ports In Azure

 

Let’s be honest here, data security is really important to me. Some people probably think that I go to extremes to ensure that my data as well as my customers data is secured. With that, sometime ago, I wrote a blog post on utilizing a VPN server that I built on an Azure Virtual Machine to help facilitate a secure connection whenever I am away from my home network. Using a VPN server is a great way to ensure security and peace of mind.

Turns out that the server got hacked. And it was my fault.

See, if you leave certain ports open to the outside world like I did, you are just asking for hackers to attack you. And that’s exactly what happened. When I first created the virtual machine, I mistakenly left port 3389, which is used to make remote desktop protocol (RDP) connections, open and available to the world. There wasn’t any rule in place limiting those connections to a certain IP address or ranges so it was a free for all for hackers. By the way, hackers will routinely scan IP addresses looking for open ports so it was only natural that eventually one would find me.

Ironically, the hackers wanted bitcoin funds to release the virtual machine back to me. I quickly laughed at them and promptly deleted the entire machine off of Azure. Once the VM was removed, I was able to get a new virtual machine stood up, along with the VPN software re-installed, and available for connections within about 30 minutes. I did make sure to apply certain Network Security Group (NSG) rules to help prevent this from happening again.

Azure Advisor

This was a great learning lesson for me, however it got me thinking about network security in Azure and ways to prevent this. One way to do this is to utilize the Azure Advisor. The Advisor offers up security recommendations (as well as recommendations in other categories) that will help further harden your Azure foot print. These recommendations can range from NSG rules, enabling auditing, or installing endpoint protection solutions that will actively monitor for viruses and malware. Here’s an example of what a report looks like,

Checking Open Ports In Azure 

As you can see, you can walk through all of the recommendations and decide on whether or not to implement them. Keep in mind that some of the recommendations might incur additional costs against the respective subscriptions so make sure to evaluate them carefully.

Powershell

In conjunction with Azure Advisor, I also decided to utilize Powershell. I thought that having a script that I could easily run on an ad-hoc basis would help me to identify any open ports that I might have. Later I’ll automate this in some fashion, probably via Runbooks, but for now just having a script is handy enough for me.

The script is fairly basic in that it will loop through all of the subscriptions that are associated with your login, look for any NSG’s in which the source address is “*” (which this means basically the world) and the Access method is set to “Allow”. This, of course, is adjustable for your needs so feel free to play around with the settings. The script will also output the NSG’s which satisfy those requirements to a file and then subsequently open the file.

Let’s walk through it!!

First, I’m going to set a path to the local file in which to export the output. After that I need to authenticate to my Azure subscription. If you are already connected and authenticated in the context of Powershell, the script will check for that and skip the login process accordingly.

  1. $path = "c:\temp\openports.txt"  
  2. #log into Azure Account if needed  
  3. IF (-NOT (get-azcontext)){  
  4.    Login-AzAccount  

Once we are authenticated and logged in, we can then start to loop through things. First we’ll loop through every subscription and within every subscription, check every NSG for open ports. If the NSG has an open port, it will output the results into the file we specified in the step above.

  1. #get all of the subs  
  2. $subs = get-azsubscription  
  3. #loop through the subscriptions  
  4. foreach($sub in $subs){  
  5. set-azcontext -SubscriptionID $sub.ID  
  6. $nsgs = Get-AzNetworkSecurityGroup  
  7. foreach($nsg in $nsgs){  
  8.    $nsg | get-aznetworksecurityruleconfig | where-object -FilterScript {$_.sourceaddressprefix -eq "*" -and $_.Access -eq "Allow"} | out-file -FilePath $path -Append  
  9.    }  
  10. }  
At this time, the file has been populated. We can now just open the file with the Invoke-Item cmdlet which will open the text file into Notepad.
  1. #open the file  
  2. invoke-item $path  

Here is the entire script put together,

  1. #############################################################################  
  2. # MIT License  
  3. #  
  4. # Copyright (c) 2017-Present John Morehouse  
  5. #  
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy  
  7. # of this software and associated documentation files (the "Software"), to deal  
  8. # in the Software without restriction, including without limitation the rights  
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
  10. # copies of the Software, and to permit persons to whom the Software is  
  11. # furnished to do so, subject to the following conditions:  
  12. # The above copyright notice and this permission notice shall be included in all  
  13. # copies or substantial portions of the Software.  
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE  
  20. # SOFTWARE.  
  21. #############################################################################  
  22.   
  23. $path = "c:\temp\openports.txt"  
  24. #log into Azure Account if needed  
  25. IF (-NOT (get-azcontext)){  
  26.    Login-AzAccount  
  27. }  
  28. #get all of the subs  
  29. $subs = get-azsubscription  
  30. foreach($sub in $subs){  
  31.    set-azcontext -SubscriptionID $sub.ID  
  32.    $nsgs = Get-AzNetworkSecurityGroup  
  33.    foreach($nsg in $nsgs){  
  34.    $nsg | get-aznetworksecurityruleconfig | where-object -FilterScript {$_.sourceaddressprefix -eq "*" -and $_.Access -eq "Allow"} | out-   file -FilePath $path -Append  
  35.    }  
  36. }  
  37. #open the file  
  38. invoke-item $path  
You can also find the script in Github located here.
 

Summary

As a data professional, security should and is always on my mind. Understanding the risks of using technology will help you find methods to prevent unauthorized access. In this case, the hacker only really caused me about 30 minutes of time to rebuild my server, however they taught me a great lesson in security. Even in your development environments, make sure to review your security rules so that this doesn’t happen to you.


Denny Cherry & Associates Consulting
Expert Consultants From HA to DR to up-time to SQL virtualization to scalability.