83c36e509a33918a4dcb71a0de59793d203e7d7d
[metze/wireshark/wip.git] / tools / Get-HardenFlags.ps1
1 #
2 # Get-HardenFlags - Checks hardening flags on the binaries.
3 #
4 # Copyright 2015 Graham Bloice <graham.bloice@trihedral.com>
5 #
6 # Wireshark - Network traffic analyzer
7 # By Gerald Combs <gerald@wireshark.org>
8 # Copyright 1998 Gerald Combs
9 #
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.
14 #
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.
19 #
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.
23
24 #requires -version 2
25
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.
30
31 # This script will probably fail for the forseeable future.
32 #
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:
37 #
38 #   https://sourceware.org/bugzilla/show_bug.cgi?id=19011
39 #
40 # As a result, none of the distributions that produce Windows applications
41 # and libraries have any sort of hardening flags enabled:
42 #
43 #   http://mingw-w64.org/doku.php/download
44 #
45
46 <#
47 .SYNOPSIS
48 Checks the NXCOMPAT and DYNAMICBASE flags on all the binaries.
49
50 .DESCRIPTION
51 This script downloads and extracts third-party libraries required to compile
52 Wireshark.
53
54 .PARAMETER BinaryDir
55 Specifies the directory where the binaries may be found.
56
57 .INPUTS
58 -BinaryDir Directory containing the binaries to be checked.
59
60 .OUTPUTS
61 Any binary that doesn't have the flags is written to the error stream
62
63 .EXAMPLE
64 C:\PS> .\tools\Get-HardenFlags.ps1 -BinaryDir run\RelWithDebInfo
65 #>
66
67 Param(
68     [Parameter(Mandatory=$true, Position=0)]
69     [String]
70     $BinaryDir
71 )
72
73 # Create a list of 3rd party binaries that are not hardened
74 $SoftBins = (
75     "libpixmap.dll",
76     "libwimp.dll",
77     "libgail.dll",
78     "airpcap.dll",
79     "comerr32.dll",
80     "gspawn-win32-helper-console.exe",
81     "gspawn-win32-helper.exe",
82     "k5sprt32.dll",
83     "krb5_32.dll",
84     "libatk-1.0-0.dll",
85     "libcairo-2.dll",
86     "libffi-6.dll",
87     "libfontconfig-1.dll",
88     "libfreetype-6.dll",
89     "libgcc_s_sjlj-1.dll",
90     "libgcrypt-20.dll",
91     "libgdk-win32-2.0-0.dll",
92     "libgdk_pixbuf-2.0-0.dll",
93     "libGeoIP-1.dll",
94     "libgio-2.0-0.dll",
95     "libglib-2.0-0.dll",
96     "libgmodule-2.0-0.dll",
97     "libgmp-10.dll",
98     "libgnutls-28.dll",
99     "libgobject-2.0-0.dll",
100     "libgpg-error-0.dll",
101     "libgtk-win32-2.0-0.dll",
102     "libharfbuzz-0.dll",
103     "libhogweed-2-4.dll",
104     "libintl-8.dll",
105     "libjasper-1.dll",
106     "libjpeg-8.dll",
107     "liblzma-5.dll",
108     "libnettle-4-6.dll",
109     "libp11-kit-0.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",
114     "libpixman-1-0.dll",
115     "libpng15-15.dll",
116     "libtasn1-6.dll",
117     "libtiff-5.dll",
118     "libxml2-2.dll",
119 # The x64 ones that are different
120     "comerr64.dll",
121     "gspawn-win64-helper-console.exe",
122     "gspawn-win64-helper.exe",
123     "k5sprt64.dll",
124     "krb5_64.dll",
125     "libgcc_s_seh-1.dll",
126     "libgpg-error6-0.dll",
127     "libpng16-16.dll",
128 # Unfortunately the nsis uninstaller is not hardened.
129     "uninstall.exe"
130 )
131
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:")
135
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
138
139 # Number of "soft" binaries found
140 $Count = 0;
141
142 # Iterate over the list
143 $Binaries | ForEach-Object {
144
145     # Get the flags
146     $flags = dumpbin $_ /HEADERS;
147
148     # Check for the required flags
149     $match = $flags | Select-String -Pattern "NX compatible", "Dynamic base"
150     if ($match.Count -ne 2) {
151
152         # Write-Error outputs error records, we simply want the filename
153         [Console]::Error.WriteLine((Resolve-Path $_ -Relative))
154
155         # Don't count files that won't ever be OK
156         if ($SoftBins -notcontains (Split-Path $_ -Leaf)) {
157             $Count++
158         }
159     }
160 }
161
162 exit $Count