I even created a cute lil logo for it
Recently, our team had a discussion about security in #dbatools-dev and I realized I should probably highlight why PowerShell and dbatools are ideal for every organization, including security-minded organizations.
If you’re questioned about PowerShell or dbatools, here are some handy facts to help prove we can help make your administration more, not less, secure.
So how is using PowerShell more secure than GUI administration? This shocking video from a few years back can help explain. In it, Microsoft Security MVP Marcus Murray highlights just how easy it is to steal a token and impersonate an admin who is logged in via the GUI.
JPG -> IIS -> cmd/.NET -> file system -> SQL Server -> token theft from GUI -> domain controller ->
Remote PowerShell sessions do not create tokens, and consequently do not subject us to the threat of token theft and privilege escalation.
I remember when I heard the term PowerShell Remoting and saw some book chapters on it, I was like “aw man, another thing I gotta learn?” but it’s actually pretty straight forward.
Windows PowerShell supports remote computing by using various technologies, including WMI, SQL WMI, RPC, SSH & WS-Management. Some commands require no remote configuration, including:
Remoting refers to commands that use WS-Management. And it’s Microsoftās recommended method of managing Windows. Why?
The PowerShell team’s mantra is Secure by Design and you can see it in action with this remoting implementation.
The PowerShell team is very serious about security and their lead security architect, Lee Holmes, is well-respected in the InfoSec community. He and Jeffrey Snover even did a keynote at DerbyCon where they talked about PowerShell security in-depth.
Yet we’ll sometimes see things like this:
Click to Tweet So why do anti-virus (AV) companies highlight the obvious fact that suspicious files uploaded to a suspicious-file-checker will probably be suspicious?
Lee Holmes addresses this very issue in the keynote:
AV, they love to talk about ransomware attacks that use PowerShell, and the reason they do this is because they literally canāt keep up with the plain old stuff like exes. But they also do this because we as a community encourage it. We blog it, we tweet about it, we talk about it. And the next thing you hear is that thereās a PowerShell problem. They donāt seem to ask how to solve their C++ problem or their darn x64 problem.
He then went on to say
When security researchers leverage PowerShell, it is always in a post-compromise context. They have compromised a machine through some other avenue (phishing, SQL injection, etc.), and ultimately have the choice of any number of tools on the system. They were able to accomplish their goals before PowerShell, and will be able to accomplish their goals without PowerShell.
Pretty convincing argument there, Lee! Thank you
Looking for more? Check out PowerShell, Remoting, and Security by Microsoft MVP Don Jones.
This tweet speaks volumes.
āIām a red teamer so I try and stay as far away from PowerShell as I canā @h4wkst3r while discussing SharPersist tool release at #DerbyCon https://t.co/wY67IlrlFJ
This should make @Lee_Holmes proud pic.twitter.com/eKlE5DSz4D
— Christopher Glyer (@cglyer) September 7, 2019
dbatools is an open source project so all of our coding and discussions happen out in the open. Whether it be on GitHub or Slack. We even created a channel called #dbatools-github just to watch GitHub spam, which includes all Pull Requests, Issues and commits to master & development.
Open source is generally considered more secure, but as the SQLShack article “SQL Server security considerations with open source tools” highlights, there are some concerns to address.
What data pro doesn’t love integrity? #DBCCCHECK4EVER
Thanks to a donation from dbatools contributors, Data Masterminds, dbatools is legit, code-signed, Enterprise software.
Code signing requires a code signing certificate. Obtaining a globally recognized code signing certificate isn’t easy and took me about a month. A ton of paperwork & multiple proofs of identity were required, but I did it because I wanted dbatools to be as professional as possible.
What are code signing certs? DigiCert says:
Code Signing Certificates are used by software developers to digitally sign apps, drivers, and software programs as a way for end-users to verify that the code they receive has not been altered or compromised by a third party. They include your signature, your companyās name, and if desired, a timestamp.
Our signatures do include a timestamp, and I personally sign every release that goes into the PowerShell Gallery and chocolately. Rob Sewell and I are the only two people with access to the certificate. And while Rob can also potentially sign dbatools, he uses the certificate solely to sign our sister project, dbachecks.
Going back to that article on SQLShack.com: they talk about diffusion of responsibility. Basically, the human impulse to say “another developer is looking, so I don’t have to.”
Only six of us can merge code into the dbatools master branch, and only six of us ever have. How did we determine this number? We executed the following command and threw the results into Excel:
git log --graph --abbrev-commit --decorate --first-parent master --merges --pretty=format:%h-%aN
Five of the six people (Me, Shawn, Fred, Rob, Simone and Stuart) who can currently merge code intro dev/master are current/former MVPs or Microsoft employees. Our primary C# library developer, Friedrich Weinmann, is a former MVP and current Security PFE at Microsoft. Friedrich approves all C# code. The other dbatools code is mostly approved by me and fellow MVPs Shawn Melton and Stuart Moore.
We are all known by Microsoft and have visible community profiles. You’ll notice that I’m the primary merger, but if you watch our repo, you’ll also notice Shawn spends a good deal of time evaluating and testing code, even after I’ve merged it and after it’s been tested by appveyor & Pester.
dbatools also has a decently manageable code base. Here are some stats about our code, courtesy of Simone who used cloc to produce this pretty chart.
Note that over 35% of our PowerShell code is comments or comment-based help
Our dbatools C# library code can be found in our GitHub repo, AND! You can even set it to compile each time you import dbatools using the $dbatools_alwaysbuildlibrary
variable instead of relying on our included dll.
$dbatools_alwaysbuildlibrary = $true Import-Module dbatools
The module itself handles the compile so that’s all you have to do.
Note that $dbatools_alwaysbuildlibrary
is not supported by our PowerShell Gallery or chocolatey releases. If you need to compile the DLL, you’ll need to either clone our repo or download the zip directly from GitHub.
Our external libraries and programs come primarily from Microsoft. But like SQL Server Management Studio, we also use community DLLs.
Our XESmartTarget support comes from Data Platform MVP & Formula 1 DBA Gianluca Sartori and nuget.
We also obtained permission to include bcp.exe and sqlcmd.exe, and that came directly from my own installation of SQL Server Management Studio.
While it’d be foolish to make a guaranteed promise that our library is , we do try our best and no malware has ever been detected in my repo root or in our PowerShell Gallery and chocolatey packages.
Thanks to Paul Broadwith, dbatools is now available in the chocolatey repository, and chocolatey takes package integrity very seriously.
Every version of every package submitted must pass through a rigorous moderation review process before they become publicly available (includes checks for quality, consistency, installation, and validations against VirusTotal).
You can find out more about this review process at chocolatey.org.
You can also find us on the PowerShell Gallery which performs an “antivirus scan by using System Center Endpoint Protection”
According to Microsoft, all modules have to meet a minimum quality standard, which includes being free of malware and viruses.
Amazingly enough, Microsoft also uses dbatools (and 1999 me is totally tripping out). So while Microsoft does not officially endorse us, they appear to trust that we’re a project with integrity.
David Peter Hansen, SQL Server PFE, has an awesome blog post where he details the tools he uses. dbatools is all up in the mix Another PFE and former Data Platform MVP, Ryan J. Adams, actually suggests using dbatools to sync logins for Availability Groups.
And I just noticed he even made a video about dbatools!
Also, SQL PFE Patrick Keisler wrote a post on blogs.msdn.microsoft.com which doesn’t use dbatools directly, but does use some of the shared code base that I wrote.
Word has it, we’re also used in the banking industry, the airline industry, the super fancy speedcar industry, the insurance industry, the medical industry and more.
There’s also some low-hanging fruit that we address, like using ScriptAnalyzer to ensure we follow security best practices. We also update our website’s backend regularly and each of our sites use HTTPS.
Microsoft itself has written extensively about PowerShell security.
Who’s afraid of PowerShell security? by former Microsoft PFE Ashley McGlone was an instant classic because it was effective and succinct. This article made so many great points, including:
The improvements in WMF 5.0 (or WMF 4.0 with KB3000850) make PowerShell the worst tool of choice for a hacker when you enable script block logging and system-wide transcription. Hackers will leave fingerprints everywhere, unlike popular CMD utilities. For this reason, PowerShell should be the only tool you allow for remote administration. These features allow you to answer the classic questions who, what, when, where, and how for activities on your servers.
PowerShell Security at Enterprise Customers by former Microsoft PFE David das Neves is another highly referenced article. I consider this the definitive, in-depth article about PowerShell security.
This post is practically a book, and even making an outline would be too long. If you need to know anything about PowerShell security, this post is a great reference. Bravo, David!
A Comparison of Shell and Scripting Language Security by PowerShell’s Security Architect Lee Holmes is another ace.
My favorite part was this easy-to-understand visual bit that shows why PowerShell is mega-secure
I hope this addresses everything your security department needs. If not, post a comment or email me at [email protected] and we’ll see what we can do.
- Chrissy
]]>Right, so the first thing you need to know is: I’m not Chrissy. My name’s Drew, and I’m here tell you about an exciting new release of dbatools: schwifty!
In this release we’ve added three new commands to help you manage SPNs for SQL Server Kerberos authentication. If you have a lot of linked servers in your environment, then this is probably a welcome addition to the toolset for you.
These commands leverage SQL Server instance info from commands that already existed in the dbatools code base and active directory PowerShell support to determine required SPNs for all instances on a given computer and whether they’re set or not. If they aren’t, we have commands to not only set the SPNs for you, but also enable constrained delegation to accounts for the SPNs that were just added.
Before these functions were added to dbatools, you had a couple options when it came to checking/adding SPNs:
Microsoft Kerberos Configuration Manager for SQL Server. Microsoft does have a tool to help you manage your SPNs for SQL Server too. It’s called Kerberos Configuration Manager for SQL Server and it’s awesome because not only does it tell you what SPNs need set, but it checks to see if they are there. If they aren’t, it’ll try and fix them for you, or generate a .cmd file that you can run on your own. It’s a neat tool that essentially generates SPNs based on best practice straight from Microsoft. While the auto fixing and script generation are neat,Ā the program can be a little slow. Also: up until very recently (2017 Jan 31), it wasn’t compatible with SQL Server 2016. It also doesn’t set delegation for your accounts, either. Less yuck, but still: yuck!
Now we have four new commands to help manage SPNs – Get-DbaSpn, Test-DbaSpn, Set-DbaSpn, and Remove-DbaSpn. Our goal with the new SPN commands was to make them fast, and make them at least as useful as the GUI tool provided by Microsoft, but to allow multi-server administration and automation of setting correct SPNs and enabling constrained delegation. And we’ve done it! These commands are based on guidance from MSDN about setting SQL Server Service Principal names which you can read more about here.
Oh and for the performance minded among you, we’ve built these functions to be super fast; faster than the traditional setspn.exe and Configuration Manager options by quite a bit. While the GUI tool took up to 51 seconds per server, our testing showed the ability to return required SPN info for 9 servers in 9 seconds. That’s 1 server per second, granted, we don’t test for SSAS or SSRS at this time, but we do have it planned, and it’ll still be quick.
Take a look below for a detailed description of the new commands in this release. You can use these new functions by grabbing our latest release (0.8.709).
A couple things to note when using these functions:
Returns a list of any service principal names (SPNs) set for a given server or active directory account name
Returns of listing of “required” SPNs for a given computer’s instance of SQL Server. The cmdlet will discover all instances of SQL Server on a given computer name. For each instance found, the cmdlet will generate a list of required SPNs based on active TCP/IP ports. The cmdlet will also warn you if an instance is using dynamic ports. Each returned SPN object will also specify if the SPN is currently set or not.
You can even check your entire estate in just one line. Here, you can see us testing several instances in our lab.
Or use Test to help easily Set all required SPNs
Connects to Active Directory and sets a given SPN to a given account. Will also set constrained delegation to the account to the recently added SPN.
Both Set and Remove fully support -WhatIf
Connects to Active Directory and removes a given SPN to a given account. Will also remove the associated constrained delegation.
The following screenshot shows a way to easily remove SPNS of decommissioned servers.
We also have a few other commands that can help you deal with SPNs.
Test-DbaConnectionAuthScheme
This command returns the transport protocol and authentication scheme of the connection. This is useful to determine if your connection is using Kerberos. By default, the ConnectName, ServerName, Transport and AuthScheme of the current connection will be returned.
These commands are just the start of our support for SPNs. Right now, they’re just focused on SQL-related services on your computers. In the future, we’re going to extend these scripts to look at ALL services on your computers and tie them back to service accounts and SPNs. This includes SSAS, SSIS, and SSRS SPN support. We’re also going to keep looking at the commands and adding support for anything we didn’t think of when we released, which is why it’s important that you give us feedback (more on that below).
Thanks for reading, and I hope you enjoy these new commands! If you have any issues or questions about their use, feel free to drop by our Slack channel, #dbatools, and let us know. We’d love to hear from you!
- Drew
]]>