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 # SPDX-License-Identifier: GPL-2.0-or-later
14 # Get-HardenFlags does:
15 # call the dumpbin utility to get the binary header flags
16 # on all the binaries in the distribution, and then filters
17 # for the NXCOMPAT and DYNAMICBASE flags.
19 # This script will probably fail for the foreseeable future.
21 # Many of our third-party libraries are compiled using MinGW-w64. Its version
22 # of `ld` doesn't enable the dynamicbase, nxcompat, or high-entropy-va flags
23 # by default. When you *do* pass --dynamicbase it strips the relocation
24 # section of the executable:
26 # https://sourceware.org/bugzilla/show_bug.cgi?id=19011
28 # As a result, none of the distributions that produce Windows applications
29 # and libraries have any sort of hardening flags enabled:
31 # https://mingw-w64.org/doku.php/download
36 Checks the NXCOMPAT and DYNAMICBASE flags on all the binaries
.
39 This script downloads and extracts third-party libraries required to compile
43 Specifies the directory
where the binaries may be found
.
46 -BinaryDir Directory containing the binaries to be checked
.
49 Any binary that doesn
't have the flags is written to the error stream
52 C:\PS> .\tools\Get-HardenFlags.ps1 -BinaryDir run\RelWithDebInfo
56 [Parameter(Mandatory=$true, Position=0)]
61 # Create a list of 3rd party binaries that are not hardened
73 "libfontconfig-1.dll",
75 "libgcc_s_sjlj-1.dll",
77 "libgdk-win32-2.0-0.dll",
78 "libgdk_pixbuf-2.0-0.dll",
81 "libgmodule-2.0-0.dll",
84 "libgobject-2.0-0.dll",
86 "libgtk-win32-2.0-0.dll",
97 "libpangocairo-1.0-0.dll",
98 "libpangoft2-1.0-0.dll",
99 "libpangowin32-1.0-0.dll",
105 # The x64 ones that are different
109 "libgcc_s_seh-1.dll",
110 "libgpg-error6-0.dll",
112 # Unfortunately the nsis uninstaller is not hardened.
116 # CD into the bindir, allows Resolve-Path to work in relative mode.
117 Push-Location $BinaryDir
118 [Console]::Error.WriteLine("Checking in $BinaryDir for unhardened binaries:")
120 # Retrieve the list of binaries. -Filter is quicker than -Include, but can only handle one item
121 $Binaries = Get-ChildItem -Path $BinaryDir -Recurse -Include *.exe,*.dll
123 # Number of "soft" binaries found
126 # Iterate over the list
127 $Binaries | ForEach-Object {
130 $flags = dumpbin $_ /HEADERS;
132 # Check for the required flags
133 $match = $flags | Select-String -Pattern "NX compatible", "Dynamic base"
134 if ($match.Count -ne 2) {
136 # Write-Error outputs error records, we simply want the filename
137 [Console]::Error.WriteLine((Resolve-Path $_ -Relative))
139 # Don't count files that won
't ever be OK
140 if ($SoftBins -notcontains (Split-Path $_ -Leaf)) {