Skip to content

Instantly share code, notes, and snippets.

@s1037989
Created July 24, 2023 23:06
Show Gist options
  • Save s1037989/2817a552b453b8a2ad6aaed246ad5191 to your computer and use it in GitHub Desktop.
Save s1037989/2817a552b453b8a2ad6aaed246ad5191 to your computer and use it in GitHub Desktop.
Brute force FIPS HMAC Key extract from binaries
#!/bin/bash
# This allows modifying a library with, e.g., patchelf and updating the FIPS-required .libXXX.hmac
# to ensure that the library can be loaded on a FIPS-enabled system.
# $ extract-hmackey libcrypto.so.1.1 '^[A-Za-z0-9 ]+$' '.\{16,64\}'
# orboDeJITITejsirpADONivirpUkvarP
# success
# $ extract-hmackey libcrypto.so.1.1 '^a$' '.\{16,64\}'
# fail
# $ extract-hmackey libcrypto.so.1.1 '^[A-Za-z0-9 ]+$' '.\{32\}'
# orboDeJITITejsirpADONivirpUkvarP
# success
# Set the lib to extract the FIPS HMAC Key
export LIB="$1"
# Set the regex to reduce the list of strings to try
export REGEX="$2"
# Set the length range to reduce the list of strings to try
export LEN="$3"
# Run thru the list of reduced-set strings and check it against the known .hmac
strings $LIB | sort -ru | grep -P "$REGEX" | grep -x "$LEN" | HMAC=$(<.$LIB.hmac) perl -Mojo -MDigest::SHA=hmac_sha256_hex -E 'BEGIN{$|=1;$main::lib=f($ENV{LIB})->slurp}END{exit !$main::exit} @_=<STDIN>; my $c=0; foreach (@_) { $c++; chomp; printf "%s/%s\r", $c, $#_; my $hmac = hmac_sha256_hex($main::lib, $_); printf "\r%-64s\n", $_ and $main::exit=1 and exit if $hmac eq $ENV{HMAC} }' && echo success || echo fail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment