Mikey
Aspiring Old Fart, EE, computer & networking geek
Would we need to fill the entire space back there?
No. Ideally, you would find where the pipe(s) are rattling against each other or the framing. The separate them a little bit with a small shim. Then squirt the foam in the gap, which will fill the gap and prevent the shim from shifting around or falling out.
Verdeboy raises a good point. Pipes don't rattle just for the hell of it. Usually the rattle is caused by a flow of water suddenly stopping, and 99% of the time (unverified statistic) it's caused by a leaky toilet. When the water drops enough to open the fill valve, water flows to fill the tank, but when the float rises and shuts off the flow, the water stops suddenly. The momentum of water moving throughout the system, and suddenly stopping, moves pipes and can make a noise.
An easy way to see if you've got a leaky toilet: put some food coloring in the tank and come by a while later and see if there's any color in the bowl. If there is, you've got a leak. Turn off the valve at the base of the toilet and see if your mysterious noises go away. If so, repairing the toilet is a lot easier than masking the noise, and will save you $$ on your water bill. You could also simulate the problem by pushing the float down to start water flowing, then release it so the water stops suddenly. See if that causes the noise.