Skip to content

Instantly share code, notes, and snippets.

@menushka
Last active April 23, 2021 11:58
Show Gist options
  • Save menushka/4521b06c0792bb77109d47efcf0e5f6a to your computer and use it in GitHub Desktop.
Save menushka/4521b06c0792bb77109d47efcf0e5f6a to your computer and use it in GitHub Desktop.
Convert Objective C header file to Logos tweak file with all methods hooked and logged with option to log return value also
#!/bin/sh
getStringFormat() {
SED_REPALCE[0]='s/HBLogDebug(id)/HBLogDebug(@"Returns: %@", r)/g; '
SED_REPALCE[1]='s/HBLogDebug(int)/HBLogDebug(@"Returns: %d", r)/g; '
SED_REPALCE[2]='s/HBLogDebug(unsigned int)/HBLogDebug(@"Returns: %u", r)/g; '
SED_REPALCE[3]='s/HBLogDebug(double)/HBLogDebug(@"Returns: %f", r)/g; '
SED_REPALCE[4]='s/HBLogDebug(unsigned char)/HBLogDebug(@"Returns: %c", r)/g; '
SED_REPALCE[5]='s/HBLogDebug(unichar)/HBLogDebug(@"Returns: %C", r)/g; '
SED_REPALCE[6]='s/HBLogDebug(long long)/HBLogDebug(@"Returns: %lld", r)/g; '
SED_REPALCE[7]='s/HBLogDebug(BOOL)/HBLogDebug(@"Returns: %@", r ? @"YES" : @"NO")/g; '
SED_REPALCE[8]='s/HBLogDebug(void)/HBLogDebug(@"Returns: void")/g; '
SED_REPALCE[9]='s/HBLogDebug(Class)/HBLogDebug(@"Returns: %@", r)/g'
echo "${SED_REPALCE[*]}"
}
SED_REPALCE_STRING=`getStringFormat`
LOG_RETURN_VALUES=false
while [ "$1" != "" ]; do
if [ ${1:0:1} == '-' ]; then
case $1 in
-f | -file)
HEADER_FILE="$2"
;;
-r | -returnValues)
LOG_RETURN_VALUES=true
;;
*)
;;
esac
fi
shift
done
if [ "$HEADER_FILE" = "" ]; then
echo "Please supply link to header file ex. 'hookAllMethods -file path/to/headerFile.h'"
exit 0
fi
headerFileText=`cat $HEADER_FILE`
interfaceLineNumber=`echo "$headerFileText" | grep -n @interface | cut -f1 -d:`
interfaceLineNumber=$((interfaceLineNumber - 1))
headerFileText=`echo "$headerFileText" | sed -e "1,${interfaceLineNumber}d"`
firstMethodLineNumber=`echo "$headerFileText" | grep -m 1 -n ^[-+] | cut -f1 -d:`
firstMethodLineNumber=$((firstMethodLineNumber - 1))
headerFileText=`echo "$headerFileText" | sed -e "2,${firstMethodLineNumber}d"`
headerFileText=`echo "$headerFileText" | sed -e 's/@interface \([a-zA-Z0-9_]*\).*/%hook \1/g'`
headerFileText=`echo "$headerFileText" | sed -e 's/@end/%end/g'`
headerFileText=`echo "$headerFileText" | sed -e 's/([a-zA-Z0-9_]* \*)/(id)/g'`
headerFileText=`echo "$headerFileText" | sed -e 's/(id<[a-zA-Z0-9_]*>)/(id)/g'`
if $LOG_RETURN_VALUES; then
headerFileText=`echo "$headerFileText" | sed -e "s/\([-+](void).*\);/\1{void}/g"`
headerFileText=`echo "$headerFileText" | sed -e "s/\([-+](\([a-zA-Z0-9 ]*\)).*\);/\1{\2}/g"`
headerFileText=`echo "$headerFileText" | sed -e "s/{void}/{ %log; HBLogDebug(@\"Returns: void\"); return %orig; }/g"`
headerFileText=`echo "$headerFileText" | sed -e "s/{\([a-zA-Z0-9 ]*\)}/{ \1 r = %orig; %log; HBLogDebug(\1); return r; }/g"`
headerFileText=`echo "$headerFileText" | sed -e "$SED_REPALCE_STRING"`
else
headerFileText=`echo "$headerFileText" | sed -e "s/\([-+](\([a-zA-Z0-9 ]*\)).*\);/\1{ %log; return %orig; }/g"`
fi
echo "$headerFileText"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment