I have the same issue, rattling head liner. Like you, another owner used a type of foam, wedging it between the plastic bracing just at the back of the moon roof. Seems to have stopped the noise 100% without dealer repairs.
Looks like I have to try this today. Since I have to make a trip to Home Depot, I need to buy some foam and fix this.
2014 Mustang GT custom ordered to replace the GT500: Sterling Gray, NAV, Recaros, Brembos, Glass Roof, back up camera, 6 speed auto, loaded
2012 Focus Titanium - 18" wheels with handling package
2009 GT500 Shelby (700 rwhp) - sold