2 # Get-HardenFlags - Checks hardening flags on the binaries.
4 # Copyright 2015 Graham Bloice <graham.bloice@trihedral.com>
6 # Wireshark - Network traffic analyzer
7 # By Gerald Combs <gerald@wireshark.org>
8 # Copyright 1998 Gerald Combs
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # as published by the Free Software Foundation; either version 2
13 # of the License, or (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 # Get-HardenFlags does:
27 # call the dumpbin utility to get the binary header flags
28 # on all the binaries in the distribution, and then filters
29 # for the NXCOMPAT and DYNAMICBASE flags.
31 # This script will probably fail for the forseeable future.
33 # Many of our third-party libraries are compiled using MinGW-w64. Its version
34 # of `ld` doesn't enable the dynamicbase, nxcompat, or high-entropy-va flags
35 # by default. When you *do* pass --dynamicbase it strips the relocation
36 # section of the executable:
38 # https://sourceware.org/bugzilla/show_bug.cgi?id=19011
40 # As a result, none of the distributions that produce Windows applications
41 # and libraries have any sort of hardening flags enabled:
43 # http://mingw-w64.org/doku.php/download
48 Checks the NXCOMPAT and DYNAMICBASE flags on all the binaries.
51 This script downloads and extracts third-party libraries required to compile
55 Specifies the directory where the binaries may be found.
58 -BinaryDir Directory containing the binaries to be checked.
61 Any binary that doesn't have the flags is written to the error stream
64 C:\PS> .\tools\Get-HardenFlags.ps1 -BinaryDir run\RelWithDebInfo
68 [Parameter(Mandatory=$true, Position=0)]
73 # Create a list of 3rd party binaries that are not hardened
80 "gspawn-win32-helper-console.exe",
81 "gspawn-win32-helper.exe",
87 "libfontconfig-1.dll",
89 "libgcc_s_sjlj-1.dll",
91 "libgdk-win32-2.0-0.dll",
92 "libgdk_pixbuf-2.0-0.dll",
96 "libgmodule-2.0-0.dll",
99 "libgobject-2.0-0.dll",
100 "libgpg-error-0.dll",
101 "libgtk-win32-2.0-0.dll",
103 "libhogweed-2-4.dll",
110 "libpango-1.0-0.dll",
111 "libpangocairo-1.0-0.dll",
112 "libpangoft2-1.0-0.dll",
113 "libpangowin32-1.0-0.dll",
119 # The x64 ones that are different
121 "gspawn-win64-helper-console.exe",
122 "gspawn-win64-helper.exe",
125 "libgcc_s_seh-1.dll",
126 "libgpg-error6-0.dll",
128 # Unfortunately the nsis uninstaller is not hardened.
132 # CD into the bindir, allows Resolve-Path to work in relative mode.
133 Push-Location $BinaryDir
134 [Console]::Error.WriteLine("Checking in $BinaryDir for unhardened binaries:")
136 # Retrieve the list of binaries. -Filter is quicker than -Include, but can only handle one item
137 $Binaries = Get-ChildItem -Path $BinaryDir -Recurse -Include *.exe,*.dll
139 # Number of "soft" binaries found
142 # Iterate over the list
143 $Binaries | ForEach-Object {
146 $flags = dumpbin $_ /HEADERS;
148 # Check for the required flags
149 $match = $flags | Select-String -Pattern "NX compatible", "Dynamic base"
150 if ($match.Count -ne 2) {
152 # Write-Error outputs error records, we simply want the filename
153 [Console]::Error.WriteLine((Resolve-Path $_ -Relative))
155 # Don't count files that won't ever be OK
156 if ($SoftBins -notcontains (Split-Path $_ -Leaf)) {