Problem
I need to automate the auditing of my SQL Servers for blank sa passwords. What's the best way to do this? Are there any options in PowerShell? Check out this tip to learn more.
Solution
With the use of PowerShell, this becomes a pretty easy thing to do. Using a trick from a previous tip, we can pull a list of SQL Servers from a text file and then test each one to see if we can log in via sa using no password. Below is an example of the text file. Note that each SQL Server instance is listed on a separate line:
First, let's define the loop where we get the SQL Servers from the text file:
What we'll need to do is attempt to establish a connection to each SQL Server using a connection string with the username of sa and a blank password. We can test to see if the connection is successful using try...catch. If the connection is successful, that's a problem. That means we were able to login as sa with a blank password. In that case, we need to raise an alert that the SQL Server is vulnerable. On the other hand, if the connection fails, then we know attempting to logon with a blank sa password was unsuccessful. So if we put all this together, we're left with:
Below is an example of the output. Here we see that the sql2008 named instance has been flagged as having a blank sa password.
If you're automating it, however, you probably don't want the output to go to the screen. The easiest way to change the script is to add another variable, $auditReport, which has the path to the audit file where we will write our results and to pipe our output string to that file instead of using Write-Host. In that case we'll use the Out-File with the -append option like so:
Source Collected from MSSQLTIPS.COM
No comments :
Post a Comment