I am fighting the same hack. What is happening is code is being run from your cgi-bin directory. You have to find the cgi-file and then find out which template file it is in.
For example, this is how I found mine:
find . -name \*.php -exec grep -l “cgi-bin\/wp-head” {} \;
I am still unsure how they hacked the code at this point though.