| 1 |
#!/bin/sh |
| 2 |
# Report the hardening characterists of a set of binaries. |
| 3 |
# Copyright (C) 2009, 2010 Kees Cook <[email protected]> |
| 4 |
# License: GPLv2 or newer |
| 5 |
|
| 6 |
skip_pie=no |
| 7 |
skip_stackprotector=no |
| 8 |
skip_fortify=no |
| 9 |
skip_relro=no |
| 10 |
skip_bindnow=no |
| 11 |
quiet=no |
| 12 |
while getopts psfrbq opt |
| 13 |
do |
| 14 |
case "$opt" in |
| 15 |
p) skip_pie=yes ;; |
| 16 |
s) skip_stackprotector=yes ;; |
| 17 |
f) skip_fortify=yes ;; |
| 18 |
r) skip_relro=yes ;; |
| 19 |
b) skip_bindnow=yes ;; |
| 20 |
q) quiet=yes ;; |
| 21 |
[?]) |
| 22 |
echo >&2 "Usage: $0 [-p] [-s] [-f] [-r] [-b] file ..." |
| 23 |
echo >&2 " -p Do not require PIE binary" |
| 24 |
echo >&2 " -s Do not require stack protector" |
| 25 |
echo >&2 " -f Do not require fortify source" |
| 26 |
echo >&2 " -r Do not require RELRO markings" |
| 27 |
echo >&2 " -b Do not require BIND_NOW markings" |
| 28 |
echo >&2 " -q Only report failures" |
| 29 |
exit 1 ;; |
| 30 |
esac |
| 31 |
done |
| 32 |
shift $(( $OPTIND-1 )) |
| 33 |
|
| 34 |
overall=0 |
| 35 |
|
| 36 |
rc=0 |
| 37 |
report="" |
| 38 |
|
| 39 |
good () { |
| 40 |
if [ "$quiet" != "yes" ]; then |
| 41 |
report="$report |
| 42 |
$1" |
| 43 |
fi |
| 44 |
} |
| 45 |
|
| 46 |
bad () { |
| 47 |
report="$report |
| 48 |
$1" |
| 49 |
rc=1 |
| 50 |
} |
| 51 |
|
| 52 |
for file in "$@" |
| 53 |
do |
| 54 |
rc=0 |
| 55 |
report="$file:" |
| 56 |
|
| 57 |
PROG_REPORT=$(LANG=C readelf -lW "$file") |
| 58 |
if [ -z "$PROG_REPORT" ]; then rc=1; continue; fi |
| 59 |
DYN_REPORT=$(LANG=C readelf -dW "$file" 2>/dev/null) |
| 60 |
RELOC_REPORT=$(LANG=C readelf -sW "$file" 2>/dev/null | \ |
| 61 |
egrep ' FUNC .* UND ' | \ |
| 62 |
sed -re 's/ \([0-9]+\)$//g; s/.* //g; s/@.*//g;') |
| 63 |
|
| 64 |
# PIE |
| 65 |
# First, verify this is an executable, not a library. This seems to be |
| 66 |
# best seen by checking for the PHDR program header. |
| 67 |
name=" Position Independent Executable" |
| 68 |
if echo "$PROG_REPORT" | awk '{print $1}' | grep -q '^PHDR$'; then |
| 69 |
if echo "$PROG_REPORT" | grep -q '^Elf file type is DYN '; then |
| 70 |
good "$name: yes" |
| 71 |
else |
| 72 |
msg="$name: no, normal executable!" |
| 73 |
if [ "$skip_pie" = "yes" ]; then |
| 74 |
good "$msg (ignored)" |
| 75 |
else |
| 76 |
bad "$msg" |
| 77 |
fi |
| 78 |
fi |
| 79 |
else |
| 80 |
if echo "$PROG_REPORT" | grep -q '^Elf file type is DYN '; then |
| 81 |
good "$name: no, regular shared library (ignored)" |
| 82 |
else |
| 83 |
bad "$name: not a known ELF type!?" |
| 84 |
fi |
| 85 |
fi |
| 86 |
|
| 87 |
# Stack-protected |
| 88 |
name=" Stack protected" |
| 89 |
if echo "$RELOC_REPORT" | grep -q '^__stack_chk_fail$'; then |
| 90 |
good "$name: yes" |
| 91 |
else |
| 92 |
msg="$name: no, not found!" |
| 93 |
if [ "$skip_stackprotector" = "yes" ]; then |
| 94 |
good "$msg (ignored)" |
| 95 |
else |
| 96 |
bad "$msg" |
| 97 |
fi |
| 98 |
fi |
| 99 |
|
| 100 |
if echo "$RELOC_REPORT" | grep -q '^__stack_chk_fail$'; then |
| 101 |
good "$name: yes" |
| 102 |
else |
| 103 |
### for OpenBSD |
| 104 |
if echo "$RELOC_REPORT" | grep -q '^__stack_smash_handler$'; then |
| 105 |
good "$name: yes" |
| 106 |
else |
| 107 |
msg="$name: no, not found!" |
| 108 |
if [ "$skip_stackprotector" = "yes" ]; then |
| 109 |
good "$msg (ignored)" |
| 110 |
else |
| 111 |
bad "$msg" |
| 112 |
fi |
| 113 |
fi |
| 114 |
fi |
| 115 |
|
| 116 |
# Fortified |
| 117 |
name=" Fortify Source functions" |
| 118 |
if echo "$RELOC_REPORT" | grep -q '^__.*_chk$'; then |
| 119 |
good "$name: yes" |
| 120 |
else |
| 121 |
msg="$name: no, not found!" |
| 122 |
if [ "$skip_fortify" = "yes" ]; then |
| 123 |
good "$msg (ignored)" |
| 124 |
else |
| 125 |
bad "$msg" |
| 126 |
fi |
| 127 |
fi |
| 128 |
|
| 129 |
# Format |
| 130 |
# unfortunately, I haven't thought of a way to test for this after |
| 131 |
# compilation. What it really needs is a lintian-like check that |
| 132 |
# reviews the build logs and looks for the warnings, or that the |
| 133 |
# argument is changed to use -Werror,format-security to stop the build. |
| 134 |
|
| 135 |
# RELRO |
| 136 |
name=" Read-only relocations" |
| 137 |
if echo "$PROG_REPORT" | awk '{print $1}' | grep -q '^GNU_RELRO$'; then |
| 138 |
good "$name: yes" |
| 139 |
else |
| 140 |
msg="$name: no, not found!" |
| 141 |
if [ "$skip_relro" = "yes" ]; then |
| 142 |
good "$msg (ignored)" |
| 143 |
else |
| 144 |
bad "$msg" |
| 145 |
fi |
| 146 |
fi |
| 147 |
|
| 148 |
# BIND_NOW |
| 149 |
name=" Immediate binding" |
| 150 |
if echo "$DYN_REPORT" | awk '{print $2}' | grep -q '^(BIND_NOW)$'; then |
| 151 |
good "$name: yes" |
| 152 |
else |
| 153 |
msg="$name: no, not found!" |
| 154 |
if [ "$skip_bindnow" = "yes" ]; then |
| 155 |
good "$msg (ignored)" |
| 156 |
else |
| 157 |
bad "$msg" |
| 158 |
fi |
| 159 |
fi |
| 160 |
|
| 161 |
if [ "$quiet" != "yes" ] || [ $rc -ne 0 ]; then |
| 162 |
echo "$report" |
| 163 |
fi |
| 164 |
|
| 165 |
if [ $rc -ne 0 ]; then |
| 166 |
overall=$rc |
| 167 |
fi |
| 168 |
done |
| 169 |
|
| 170 |
exit $overall |
| 171 |
|
| 172 |
:<<=cut |
| 173 |
|
| 174 |
=pod |
| 175 |
|
| 176 |
=head1 NAME |
| 177 |
|
| 178 |
hardening-check - check binaries for security hardening features |
| 179 |
|
| 180 |
=head1 SYNOPSIS |
| 181 |
|
| 182 |
Examine a given set of ELF binaries and check for several security hardening |
| 183 |
features, failing if they are not all found. |
| 184 |
|
| 185 |
=head1 DESCRIPTION |
| 186 |
|
| 187 |
This utility checks a given list of ELF binaries for several security |
| 188 |
hardening features that can be compiled into an executable. These |
| 189 |
features are: |
| 190 |
|
| 191 |
=over 8 |
| 192 |
|
| 193 |
=item B<Position Independent Executable> |
| 194 |
|
| 195 |
This indicates that the executable was built in such a way (PIE) that |
| 196 |
the "text" section of the program can be relocated in memory. To take |
| 197 |
full advantage of this feature, the executing kernel must support text |
| 198 |
Address Space Layout Randomization (ASLR). |
| 199 |
|
| 200 |
=item B<Stack Protected> |
| 201 |
|
| 202 |
This indicates that the executable was compiled with the L<gcc(1)> |
| 203 |
option B<-fstack-protector>. The program will be resistant to have its |
| 204 |
stack overflowed. |
| 205 |
|
| 206 |
=item B<Fortify Source functions> |
| 207 |
|
| 208 |
This indicates that the executable was compiled with |
| 209 |
B<-D_FORTIFY_SOURCE=2> and B<-O2> or higher. This causes certain unsafe |
| 210 |
glibc functions with their safer counterparts (e.g. strncpy instead |
| 211 |
of strcpy). |
| 212 |
|
| 213 |
=item B<Read-only relocations> |
| 214 |
|
| 215 |
This indicates that the executable was build with B<-Wl,-z,relro> to |
| 216 |
have ELF markings (RELRO) that ask the runtime linker to mark any |
| 217 |
regions of the relocation table as "read-only" if they were resolved |
| 218 |
before execution begins. This reduces the possible areas of memory in |
| 219 |
a program that can be used by an attacker that performs a successful |
| 220 |
memory corruption exploit. |
| 221 |
|
| 222 |
=item B<Immediate binding> |
| 223 |
|
| 224 |
This indicates that the executable was built with B<-Wl,-z,now> to have |
| 225 |
ELF markings (BIND_NOW) that ask the runtime linker to resolve all |
| 226 |
relocations before starting program execution. When combined with RELRO |
| 227 |
above, this further reduces the regions of memory available to memory |
| 228 |
corruption attacks. |
| 229 |
|
| 230 |
=back |
| 231 |
|
| 232 |
=head1 OPTIONS |
| 233 |
|
| 234 |
=over 8 |
| 235 |
|
| 236 |
=item B<-p> |
| 237 |
|
| 238 |
No not require that the checked binaries be built as PIE. |
| 239 |
|
| 240 |
=item B<-s> |
| 241 |
|
| 242 |
No not require that the checked binaries be built with the stack protector. |
| 243 |
|
| 244 |
=item B<-f> |
| 245 |
|
| 246 |
No not require that the checked binaries be built with Fority Source. |
| 247 |
|
| 248 |
=item B<-r> |
| 249 |
|
| 250 |
No not require that the checked binaries be built with RELRO. |
| 251 |
|
| 252 |
=item B<-b> |
| 253 |
|
| 254 |
No not require that the checked binaries be built with BIND_NOW. |
| 255 |
|
| 256 |
=item B<-q> |
| 257 |
|
| 258 |
Only report failures. |
| 259 |
|
| 260 |
=back |
| 261 |
|
| 262 |
=head1 RETURN VALUE |
| 263 |
|
| 264 |
When all checked binaries have all checkable hardening features detected, |
| 265 |
this program will finish with an exit code of 0. If any check fails, the |
| 266 |
exit code with be 1. Individual checks can be disabled via command line |
| 267 |
options. |
| 268 |
|
| 269 |
=head1 AUTHOR |
| 270 |
|
| 271 |
Kees Cook <[email protected]> |
| 272 |
|
| 273 |
=head1 COPYRIGHT AND LICENSE |
| 274 |
|
| 275 |
Copyright 2009 Kees Cook <[email protected]>. |
| 276 |
|
| 277 |
This program is free software; you can redistribute it and/or modify it |
| 278 |
under the terms of the GNU General Public License as published by the |
| 279 |
Free Software Foundation; version 2 or later. |
| 280 |
|
| 281 |
=head1 SEE ALSO |
| 282 |
|
| 283 |
L<gcc(1)>, L<hardening-wrapper(1)> |
| 284 |
|
| 285 |
=cut |