Skip to content

Instantly share code, notes, and snippets.

@hc0d3r
Created December 28, 2016 22:30
Show Gist options
  • Save hc0d3r/6eb1047a6ee053b9a74fd22407fd8ce4 to your computer and use it in GitHub Desktop.
Save hc0d3r/6eb1047a6ee053b9a74fd22407fd8ce4 to your computer and use it in GitHub Desktop.
#!/bin/bash
green="\e[1;32m";
yellow="\e[1;33m";
reset="\e[0m";
get_search_path(){
OLD_IFS="${IFS}"
IFS=''
paths=()
while read line;do
if [[ "$line" =~ \ (/.+) ]];then
paths+=("${BASH_REMATCH[1]}")
fi
done < <(echo | cpp -Wp,-v - 2>&1)
IFS="${OLD_IFS}"
echo "${paths[@]}"
}
if [ $# -ne 1 ];then
printf "\n[*] search for c struct definition\n"
printf "[+] example: $0 [dirent]\n\n";
exit 0
fi
struct_name=$1
search_paths=($(get_search_path))
printf "[+] starting search...\n"
for((i=$((${#search_paths[@]}-1)); i>=0; i--));do
pathname=${search_paths[i]}
while read archive;do
include_file=${archive:${#pathname}+1}
struct_members=$(printf "#include <$include_file>\nstruct $struct_name x;" | cpp - 2>/dev/null | grep -Pzo '(?s)struct\s+'$struct_name'\s*{([^}]+)}')
if [ -n "$struct_members" ];then
printf "[+] struct found in: ${yellow}$archive${reset}\n\n"
printf "${green}${struct_members}${reset}\n\n"
fi
done < <(find $pathname -type f -exec grep -HPlz '(?s)struct '$struct_name'[\n ]*{([^}]+)}' {} \;)
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment