Skip to content

Instantly share code, notes, and snippets.

@Max-E
Last active August 11, 2019 21:01
Show Gist options
  • Save Max-E/4a9c4c3f982d5ab952c0d89e9def1066 to your computer and use it in GitHub Desktop.
Save Max-E/4a9c4c3f982d5ab952c0d89e9def1066 to your computer and use it in GitHub Desktop.
Generate Linux executables with missing dependencies
#! /bin/bash
# USAGE:
# generate_exe_with_unmet_dependencies.sh OUTPUT_EXECUTABLE_NAME NUMBER_OF_DEPS
# Generates an executable with the specified file name, which will dynamically
# link against the specified number of libraries, These libraries will have
# randomly generated names. They'll be removed once the main executable is
# complete and should always be missing on anyone else's system; therefore the
# generated executable is guaranteed to always fail due to missing dynamic
# libraries.
client_program="$1"
client_source=$(mktemp -p /tmp client.XXXXXXXXXXXXXXXXX.c)
rm -f $client_source
touch $client_source
remove_files=$client_source
LDFLAGS="-L$(dirname "$client_program")"
for i in $(seq $2); do
func_name=x$(uuidgen | tr -d -)
library_path=$(mktemp -p /tmp library.XXXXXXXXXXXXXXXXX.so)
library_source=$library_path.c
echo "void $func_name (void) {}" > $library_source
gcc -shared -o $library_path -fPIC $library_source
LDFLAGS="$LDFLAGS -l$(echo $library_path | sed 's,.*/,,; s/^lib//; s/\.so$//')"
echo "void $func_name (void);" >> $client_source
rm $library_source
remove_files="$remove_files $library_path"
done
echo "int main(int argc, char *argv[]) {return 0;}" >> $client_source
gcc -o "$client_program" $LDFLAGS -no-pie $client_source
rm -f $remove_files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment