-
-
Save fenugrec/49091f26ae3ee944087c47777b67023e to your computer and use it in GitHub Desktop.
| #!/bin/bash | |
| # | |
| # grab ownership of ghidra project specified in arg1 or current dir if absent | |
| # | |
| # - looks for the first <projectname>.gpr file | |
| # - modifies owner name <projectname>/project.prp | |
| # | |
| # example : ./ghrab.sh ~/RE/coolstuff | |
| # Discussed in https://github.com/NationalSecurityAgency/ghidra/issues/5507 | |
| # " if you create a project under your current username then try to give it to someone else, | |
| # or otherwise just copy it to another machine or VM with a different user name you will get an error: | |
| # "..Failure to open project ... NoOwnerException: Project is not owned by xxx.." | |
| # | |
| # obvious caveat : this is a bad idea if permissions are not appropriate on some of the project files. | |
| u=$(whoami) | |
| # use arg1 if defined, else pwd | |
| base_dir="${1:-$(pwd)}" | |
| gpr_file=$(find "$base_dir" -name "*.gpr" -printf %f -quit) | |
| if [ -z "$gpr_file" ]; then | |
| echo "no .gpr file found !" | |
| exit 1 | |
| fi | |
| prj_name=${gpr_file%.gpr} | |
| prp_file="$base_dir/${prj_name}.rep/project.prp" | |
| if ! [ -f "$prp_file" ]; then | |
| echo "$prp_file" not found ! | |
| exit 1 | |
| fi | |
| echo Found ghidra project: "$prj_name" | |
| sed "$prp_file" -i -e "/OWNER/s/VALUE=\"[^\"]\+/VALUE=\"$u/" |
good points, thanks! Updated
Nice, I switched PC's and this is a lifesaver, although I did it just manually.
thank you for this. Had to make some changes to make it work for macos:
gpr_path=$(find "$base_dir" -name "*.gpr" -print -quit)
if [ -z "$gpr_path" ]; then
echo "no .gpr file found !"
exit 1
fi
gpr_file=$(basename "$gpr_path")
gpr_dir=$(dirname "$gpr_path")
prj_name=${gpr_file%.gpr}
prp_file="$gpr_dir/${prj_name}.rep/project.prp"
if ! [ -f "$prp_file" ]; then
echo "$prp_file" not found !
exit 1
fi
echo Found ghidra project: "$prj_name"
sed -i '' -E "/OWNER/s/VALUE=\"[^\"]+/VALUE=\"$u/" "$prp_file"I had an assortment of projects in a directory and the script only appeared to work on one at a time, so I updated it to the following which seemed to work for me.
#!/bin/bash
u=$(whoami)
# use arg1 if defined, else pwd
base_dir="${1:-$(pwd)}"
for file in "$base_dir/*.gpr"; do
if [ -f "$file" ]; then
prj_name=${file%.gpr}
prp_file="$base_dir/${prj_name}.rep/project.prp"
echo "Processing $prj_name"
sed "$prp_file" -i -e "/OWNER/s/VALUE=\"[^\"]\+/VALUE=\"$u/"
fi
doneI had an assortment of projects in a directory and the script only appeared to work on one at a time,
Interesting, that's something I haven't encountered here (if I'm looking at different fw / sw versions I'll typically load them all in the same ghidra project, makes it easier to share data between them).
for file in *.gpr; do ....
So your version is hardcoded to look in current dir ? why not for file in "$base_dir/*.gpr" (untested) ?
So your version is hardcoded to look in current dir ?
That wasn't actually intentional, I just threw it together very quickly and missed that.
Thanks for this script! It was handy and it worked to import a project to which I don't have an exported archive.
I fixed the warnings from shellcheck if you are interested (only one of them is likely to produce undesired results):
$ shellcheck ghgrab.sh
In ghgrab.sh line 21:
u=
whoami^------^ SC2006 (style): Use $(...) notation instead of legacy backticks
....Did you mean:
u=$(whoami)
In ghgrab.sh line 27:
gpr_file=$(find "$base_dir" -name *.gpr -printf %f -quit)
^---^ SC2061 (warning): Quote the parameter to -name so the shell won't interpret it.
^-- SC2035 (info): Use ./glob or -- glob so names with dashes won't become options.
For more information:
https://www.shellcheck.net/wiki/SC2061 -- Quote the parameter to -name so t...
https://www.shellcheck.net/wiki/SC2035 -- Use ./glob or -- glob so name...
https://www.shellcheck.net/wiki/SC2006 -- Use $(...) notation instead of le...
Change line 21 to
u=$(whoami)Change line 27 to
gpr_file=$(find "$base_dir" -name "*.gpr" -printf %f -quit)